-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathInputfieldPageName.module
More file actions
244 lines (213 loc) · 6.77 KB
/
InputfieldPageName.module
File metadata and controls
244 lines (213 loc) · 6.77 KB
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
<?php
/**
* An Inputfield for handling ProcessWire "name" fields
*
*/
class InputfieldPageName extends InputfieldName implements ConfigurableModule {
public static function getModuleInfo() {
return array(
'title' => 'Page Name',
'version' => 104,
'summary' => 'Text input validated as a ProcessWire Page name field',
'permanent' => true,
);
}
public static $defaultReplacements = array(
'æ' => 'ae',
'å' => 'a',
'ä' => 'a',
'ß' => 'ss',
'ö' => 'o',
'ő' => 'o',
'ü' => 'u',
'ű' => 'u',
'đ' => 'dj',
'ж' => 'zh',
'х' => 'kh',
'ц' => 'tc',
'ч' => 'ch',
'ш' => 'sh',
'щ' => 'shch',
'ю' => 'iu',
'я' => 'ia',
':' => '-',
',' => '-',
'à' => 'a',
'á' => 'a',
'â' => 'a',
'è' => 'e',
'é' => 'e',
'ë' => 'e',
'ê' => 'e',
'ě' => 'e',
'ì' => 'i',
'í' => 'i',
'ï' => 'i',
'î' => 'i',
'õ' => 'o',
'ò' => 'o',
'ó' => 'o',
'ô' => 'o',
'ø' => 'o',
'ù' => 'u',
'ú' => 'u',
'û' => 'u',
'ů' => 'u',
'ñ' => 'n',
'ç' => 'c',
'č' => 'c',
'ć' => 'c',
'ď' => 'd',
'ĺ' => 'l',
'ľ' => 'l',
'ń' => 'n',
'ň' => 'n',
'ŕ' => 'r',
'ř' => 'r',
'š' => 's',
'ť' => 't',
'ý' => 'y',
'ž' => 'z',
'а' => 'a',
'б' => 'b',
'в' => 'v',
'г' => 'g',
'д' => 'd',
'е' => 'e',
'ё' => 'e',
'з' => 'z',
'и' => 'i',
'й' => 'i',
'к' => 'k',
'л' => 'l',
'м' => 'm',
'н' => 'n',
'о' => 'o',
'п' => 'p',
'р' => 'r',
'с' => 's',
'т' => 't',
'у' => 'u',
'ф' => 'f',
'ы' => 'y',
'э' => 'e',
'ę' => 'e',
'ą' => 'a',
'ś' => 's',
'ł' => 'l',
'ż' => 'z',
'ź' => 'z',
);
public function init() {
parent::init();
$this->label = $this->_('Name'); // Field label for 'Name'
$this->set('parentPage', null);
$this->set('sanitizeMethod', 'pageName');
$this->set('languageSupportLabel', '');
$this->set('slashUrls', 1); // whether a trailing slash should be shown in the URL preview
// optional checkbox associated with the input, for use with language support
$this->set('checkboxName', ''); // leave blank to disable
$this->set('checkboxLabel', '');
$this->set('checkboxValue', '');
$this->set('checkboxChecked', false);
$this->description = $this->_("Any combination of letters (a-z), numbers (0-9), dashes or underscores (no spaces)."); // Field description describing what characters are allowed
}
public function ___render() {
$hasLanguagePageNames = $this->wire('modules')->isInstalled('LanguageSupportPageNames');
$url = '';
$out = '';
$box = '';
$user = $this->wire('user');
if($this->parentPage) {
$url = $this->parentPage->path;
if($hasLanguagePageNames && $this->parentPage->id == $this->wire('config')->rootPageID) {
if($user->language->isDefault()) {
$parentName = $this->parentPage->name;
if(!trim($url, '/') && $parentName) $url = "/$parentName";
}
}
}
if($this->languageSupportLabel) {
if($this->checkboxName) {
$checked = $this->checkboxChecked ? " checked='checked'" : '';
$name = wire('sanitizer')->entities($this->checkboxName);
$value = wire('sanitizer')->entities($this->checkboxValue);
$label = wire('sanitizer')->entities($this->checkboxLabel);
$box = "<label class='checkbox detail'><input type='checkbox' name='$name' value='$value'$checked /> $label</label>";
}
$label = wire('sanitizer')->entities($this->languageSupportLabel);
$id = wire('sanitizer')->entities($this->attr('id'));
$out .= "<div class='LanguageSupport'>" .
"<label for='$id' class='LanguageSupportLabel detail'>" .
"$label</label>";
}
$value = $this->attr('value');
$link = '';
$slashUrls = (int) $this->slashUrls;
if(strlen($value) && $hasLanguagePageNames) {
$link = $this->wire('config')->urls->root . trim($url, '/') . "/$value" . ($slashUrls ? '/' : '');
$link = "<a href='$link'>";
}
$p = "\n<p id='{$this->id}_path' data-slashUrls='$slashUrls' class='InputfieldPageNameURL'>";
if($link) $p .= $link;
$p .= rtrim($url, '/') . "/<strong></strong>";
if($link) $p .= "</a>";
$p .= "</p>";
$out .= $p;
$out .= parent::___render();
if($this->languageSupportLabel) $out .= $box . "</div>";
// make the replacements part of the JS config
$replacements = empty($this->replacements) ? self::$defaultReplacements : $this->replacements;
$this->config->js($this->className(), array('replacements' => $replacements));
return $out;
}
static public function replacementStringToArray($str) {
$r = preg_split('/[\r\n]+/', $str);
$a = array();
foreach($r as $key => $value) {
if(!strpos($value, '=')) continue;
list($k, $v) = explode('=', $value);
$a[trim($k)] = trim($v);
}
return $a;
}
static public function replacementArrayToString(array $a) {
$str = '';
foreach($a as $k => $v) $str .= "$k=$v\n";
return rtrim($str);
}
static public function getModuleConfigInputfields(array $data) {
$modules = wire('modules');
$modules->addHookBefore('saveModuleConfigData', null, 'InputfieldPageName_saveModuleConfigData');
$name = 'replacements';
if(empty($data[$name])) $data[$name] = self::$defaultReplacements;
if(is_array($data[$name])) {
// data already in right save format, but need it to be a string for editing
if(empty($replacements)) $replacements = self::$defaultReplacements;
$replacements = self::replacementArrayToString($data[$name]);
} else {
// data is a string so they must have just saved, but we want to save the array version instead
$replacements = $data[$name];
$data[$name] = self::replacementStringToArray($replacements);
}
$fields = new InputfieldWrapper();
$field = $modules->get("InputfieldTextarea");
$field->attr('name', $name);
$field->attr('value', $replacements);
$field->attr('rows', 15);
$field->label = __('Character replacements');
$field->description = __('Enter the replacements that will occur when a user is entering characters into a page name field. Enter one replacement per line in key=value format. Meaning, on each new line, enter the character(s) you want to replace followed by an equals sign "=" and the ascii character(s) you want to replace with.'); // Character replacements description
$field->notes = __('The replacement value for each must be one or more of: a-z, 0-9, dash, underscore or period.'); // Character replacements notes
$fields->append($field);
return $fields;
}
}
function InputfieldPageName_saveModuleConfigData(HookEvent $event) {
$arguments = $event->arguments;
if($arguments[0] != 'InputfieldPageName') return;
$data = $arguments[1];
$name = 'replacements';
if(!is_array($data[$name])) $data[$name] = InputfieldPageName::replacementStringToArray($data[$name]);
$arguments[1] = $data;
$event->arguments = $arguments;
}