-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataSetRenderer.module.php
272 lines (246 loc) · 8.32 KB
/
DataSetRenderer.module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php namespace ProcessWire;
// DEBUG disable file compiler for this file
// FileCompiler=0
/*
* Rendering module for data sets
*
* Provides rendering functions for the DataSet module.
*
* TODO: This needs to be completely rewritten.
*
* Copyright 2018 Tamas Meszaros <[email protected]>
* This file licensed under Mozilla Public License v2.0 http://mozilla.org/MPL/2.0/
*/
class DataSetRenderer extends WireData implements Module {
// letter substitutions in user input (what => with)
// the result is used in indices and selectors
public $letterSubstitutions = array(
'á' => 'a',
'é' => 'e',
'í' => 'i',
'ó' => 'o',
'ő' => 'ö',
'ú' => 'u',
'ű' => 'ü',
);
// default initial letters for navigation trees
public $initialLetters = array(
'a' => 'a',
'á' => 'á',
'b' => 'b',
'c' => 'c',
'cs' => 'cs',
'd' => 'd',
'dz' => 'dz',
'dzs' => 'dzs',
'e' => 'e',
'é' => 'é',
'f' => 'f',
'g' => 'g',
'gy' => 'gy',
'h' => 'h',
'i' => 'i',
'í' => 'í',
'j' => 'j',
'k' => 'k',
'l' => 'l',
'ly' => 'ly',
'm' => 'm',
'n' => 'n',
'ny' => 'ny',
'o' => 'o',
'ó' => 'ó',
'ö' => 'ö',
'ő' => 'ő',
'p' => 'p',
'q' => 'q',
'r' => 'r',
's' => 's',
'sz' => 'sz',
't' => 't',
'ty' => 'ty',
'u' => 'u',
'ú' => 'ú',
'ü' => 'ü',
'ű' => 'ű',
'v' => 'v',
'w' => 'w',
'x' => 'x',
'y' => 'y',
'z' => 'z',
'zs' => 'zs',
'-' => '-',
'*' => '*',
'$' => '$',
'+' => '+',
);
/***********************************************************************
* MODULE SETUP
**********************************************************************/
/**
* Called only when this module is installed
*
* Creates new custom database table for storing import configuration data.
*/
public function ___install() {
}
/**
* Called only when this module is uninstalled
*
* Drops database table created during installation.
*/
public function ___uninstall() {
}
/**
* Initialization
*
* This function attaches a hook for page save and decodes module options.
*/
public function init() {
}
/***********************************************************************
* RENDERING FUNCTIONS
**********************************************************************/
/**
* Render a navigation tree for datasets based on initial letters
*
* @param $dataSetPage data set page object
* @param $pattern array initial letters for the tree or string to prepend to letters in the default array
* @param $liClass additional attributes for <li> tags. If null <li> is omitted.
* @param $aClass additional attributes for <a> tags. If null <a> is omitted.
* @param $countHeadwords count the headwords matching the pattern (also skips empty sets)
* @returns html string to output
*/
public function renderLetterNav($dataSetPage, $pattern, $liClass=' class="nav-item"', $aClass=' class="nav-link"', $countHeadwords = false) {
$out = ''; $initial = '';
// always use the default language for listing headwords
// TODO multilanguage data sets are not supported atm
// $lang = $this->languages->get('default');
if (is_array($pattern)) {
// print out these letters
$letters = $pattern;
} else if (is_string($pattern) && mb_strlen($pattern)) {
// sanitizing pattern ($sanitizer->selectorValue() would not work well)
$pattern = str_replace('"', '', $pattern);
// assemble a set of letters for the menu
$letters = array(); $substr = '';
// add increasing number of starting letters of $pattern: 1 12 123 1234 ...
foreach (preg_split('//u', $pattern, -1, PREG_SPLIT_NO_EMPTY) as $letter) {
$substr .= $letter;
$index = mb_strtolower($substr);
if (isset($this->dictInitialLetters[$index])) {
// if the substring found in the default letters, get its qualified name
// this is the case with double and triple letters like sz, cs, dzs etc.
$letters[$index] = $this->dictInitialLetters[$index];
$initial = $index;
} else {
$letters[$index] = $substr;
}
}
// add possible letters after $pattern from the default letter set
foreach ($this->initialLetters as $u => $l) {
$index = mb_strtolower($pattern).$u;
$letters[$index] = $index;
}
} else { // empty or invalid pattern, use the default letter set
$letters = $this->initialLetters;
}
foreach ($letters as $u => $t) {
if (!is_null($liClass)) $out .= "<li$liClass>";
$url = urlencode($u);
$text = $t;
$selector = $u;
// for the active nav item add an extra span wrapper (TODO: this is a hack, avoid it)
if ($initial == $u) {
$text = '<span class="bg-primary text-white mx-2"> '.$text.' </span>';
}
// TODO always use the default language for querying headwords
$count = $this->pages->count('parent='.$dataSetPage.',title^="'.$selector.'"');
if ($count == 0) continue;
if ($countHeadwords) $text .= " ($count)";
$out .= "<a href='{$dataSetPage->url}?w={$url}'{$aClass}>{$text}</a>";
if (!is_null($liClass)) $out .= '</li>';
$out .= "\n";
}
return $out;
}
/**
* Display a navigation tree for dataSet items
*
* @param $dataSetPage dataSet page object
* @param $letters array initial letters for the tree
* @param $liClass additional attributes for <li> tags. If null <li> is omitted.
* @param $aClass additional attributes for <a> tags. If null <a> is omitted.
* @returns html string to output
*/
public function renderDataNav($dataSetPage, $selector='', $liClass=' class="nav-item"', $aClass=' class="nav-link"') {
$out = '';
// always use the default language for listing headwords
// TODO multilanguage dictionaries are not supported atm
// $lang = $this->user->language;
// $this->user->language = $this->languages->get('default');
$headwords = $dataSetPage->children($selector);
foreach ($headwords as $headword) {
if (!is_null($liClass)) $out .= "<li$liClass>";
if (is_null($aClass)) {
$out .= $t;
} else {
$out .= "<a href='{$headword->url}'{$aClass}>".str_replace('$', '|', $headword->title)."</a>";
}
if (!is_null($liClass)) $out .= '</li>';
$out .= "\n";
}
// restore the original language
// TODO enable this if multi-language support is implemented
// $this->user->language = $lang;
return $out;
}
/**
* Display a navigation tree for dataSet items
*
* @param $dataSetPage dataSet page object
* @param $letters array initial letters for the tree
* @param $liClass additional attributes for <li> tags. If null <li> is omitted.
* @param $aClass additional attributes for <a> tags. If null <a> is omitted.
* @returns html string to output
*/
public function renderDataList($dataSetPage, $selector='', $liClass=' class="nav-item"', $aClass=' class="nav-link"') {
$out = '';
if (!strlen($selector)) {
$selector = 'limit=30,sort=random';
}
$headwords = $dataSetPage->children($selector);
foreach ($headwords as $headword) {
if (!is_null($liClass)) $out .= "<li$liClass>";
if (is_null($aClass)) {
$out .= $t;
} else {
$out .= "<a href='{$headword->url}'{$aClass}>".str_replace('$', '|', $headword->title)."</a>";
}
if (!is_null($liClass)) $out .= '</li>';
$out .= "\n";
}
return $out;
}
/***********************************************************************
* UTILITY FUNCTIONS
**********************************************************************/
/**
* Sanitize user input
*
* @param $input string
* @param $substitute_accents substitute accented chars
* @returns string to use as selector or array index
*/
public function sanitizeInput($input, $substitute_accents = false) {
// TODO ?? mb_internal_encoding('UTF-8');
// TODO ?? html_entity_decode($input);
// filter out some illegal characters
$ret = mb_ereg_replace('["]', '', $input);
// replace letters with others
if ($substitute_accents) foreach ($this->letterSubstitutions as $what => $with)
$ret = mb_ereg_replace($what, $with, $ret);
// lower case
return mb_strtolower($ret);
}
}