-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTextareaCounter.module
217 lines (181 loc) · 7.06 KB
/
TextareaCounter.module
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
<?php
class TextareaCounter extends WireData implements Module
{
/**
* getModuleInfo is a module required by all modules to tell ProcessWire about them
*
* @return array
*
*/
public static function getModuleInfo()
{
return array(
'title' => 'Textarea Counter',
'version' => 103,
'summary' => 'Set limits for the count of characters and/or words in textarea inputs with direct feedback while typing.',
'href' => 'https://github.com/boundaryfunctions/TextareaCounter/',
'singular' => true,
'autoload' => true
);
}
public function init()
{
$this->addHookAfter('InputfieldTextarea::getConfigInputfields', $this, 'hookAddConfig');
$this->addHookBefore('InputfieldTextarea::render', $this, 'renderBeforeTextarea');
$this->addHookAfter('InputfieldTextarea::render', $this, 'renderTextarea');
$this->addHookAfter('InputfieldTextarea::processInput', $this, 'validateField');
}
public function hookAddConfig(HookEvent $event)
{
$field = $event->object;
// Don't add config for TinyMCE or CKEditor fields
if (!$field || $field == 'InputfieldTinyMCE' || $field == 'InputfieldCKEditor') {
return;
}
// Only add config for ProcessField process
if ($this->process != 'ProcessField') {
return;
}
// get inputfields from getConfigInputfields
$inputfields = $event->return;
// add maxchars setting
$f = $this->modules->get('InputfieldInteger');
$f->label = $this->_('Max. characters');
$f->description = $this->_('The maximum amount of characters that can be entered.');
$f->attr('name', 'maxchars');
$value = $this->fields->get($field->name)->maxchars;
$f->attr('value', $value ? $value : '');
$inputfields->append($f);
$f = $this->modules->get('InputfieldInteger');
$f->label = $this->_('Max. words');
$f->description = $this->_('The maximum amount of words that can be entered.');
$f->attr('name', 'maxwords');
$value = $this->fields->get($field->name)->maxwords;
$f->attr('value', $value ? $value : '');
$inputfields->append($f);
$f = $this->modules->get('InputfieldCheckbox');
$f->label = $this->_('Truncate text');
$f->description = $this->_('Reduce the inserted text to the Max length instead of revert to the old value.');
$f->attr('name', 'truncate');
$value = $this->fields->get($field->name)->truncate;
$f->attr('checked', ($value ? 'checked' : ''));
$inputfields->append($f);
}
public function renderBeforeTextarea(HookEvent $event)
{
$field = $event->object;
// Don't render for TinyMCE or CKEditor fields
if (!$field || $field == 'InputfieldTinyMCE' || $field == 'InputfieldCKEditor') {
return;
}
// Only for page edit process
if ($this->process != 'ProcessPageEdit') {
return;
}
// If the field is in a repeater, it will have the next text in the name.
$pos = strpos($field->name, "_repeater");
// We need only the name of the field to determine their attributes
if ($pos) {
$fieldName = substr($field->name, 0, $pos);
} else {
$fieldName = $field->name;
}
// get field to set attribute, will get rendered by the InputfieldTexarea
$inputfield = $this->fields->get($fieldName);
$types = array(
'chars',
'words'
);
foreach ($types as $type) {
$max = $inputfield->{'max' . $type};
if ($max && 0 < intval($max)) {
$field->attr('data-max' . $type, $inputfield->{'max' . $type});
}
}
}
public function renderTextarea(HookEvent $event)
{
$field = $event->object;
$value = $field->value;
// Don't render for TinyMCE or CKEditor fields
if (!$field || $field == 'InputfieldTinyMCE' || $field == 'InputfieldCKEditor') {
return;
}
// If the field is in a repeater, it will have the next text in the name.
$pos = strpos($field->name, "_repeater");
// add stuff only to admin pages edit screen
if ($this->process != 'ProcessPageEdit') {
return;
}
$labels = array(
'chars' => $this->_('Characters left'),
'words' => $this->_('Words left')
);
$append = array();
//We need only the name of the field to determine their attributes
if ($pos) {
$fieldName = substr($field->name, 0, $pos);
} else {
$fieldName = $field->name;
}
$truncate = $this->fields->get($fieldName)->truncate ? ' data-truncate="1"' : null;
foreach ($labels as $type => $label) {
$max = $this->fields->get($fieldName)->{'max' . $type};
if ($max && 0 < intval($max)) {
$left = $max - $this->{'count' . ucfirst($type)}($value);
$append[] = '<span class="counter' . ucfirst($type) . '"' . $truncate . '>'
. $label . ': '
. '<span class="counterVal">'
. $left
. '</span></span>';
}
}
if (0 < count($append)) {
// render counter spans after text field
$event->return .= implode(', ', $append);
// add scripts to admin page
$this->config->scripts->add($this->config->urls->TextareaCounter . 'TextareaCounter.js');
}
}
public function validateField(HookEvent $event)
{
$inputfield = $event->object;
if (!($inputfield instanceof InputfieldTextarea)) {
return;
}
$types = array(
'chars',
'words'
);
foreach ($types as $type) {
$configField = $inputfield->getConfigInputfields()->get('max' . $type);
if (!(is_object($configField) && $configField instanceof InputfieldInteger)) {
return;
}
// get the value for maximum length
$max = $configField->value;
// do nothing if empty
if (!($max && 0 < intval($max))) {
return;
}
// count chars/words of the inputfield
$inputfieldLength = $this->{'count' . ucfirst($type)}($inputfield->value);
if ($inputfieldLength > $max) {
// if too long, undo the change...
$inputfield->untrackChange('value');
// ...and show an error
$inputfield->error(sprintf($this->_("Too many %s"), $type));
} else {
$inputfield->value = trim($inputfield->value);
}
}
}
private function countChars($str)
{
return mb_strlen(trim($str));
}
private function countWords($str)
{
return count(preg_split('/\s+/', trim($str)));
}
}