/
field.textarea.php
executable file
·369 lines (295 loc) · 11 KB
/
field.textarea.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
/**
* @package toolkit
*/
require_once FACE . '/interface.exportablefield.php';
require_once FACE . '/interface.importablefield.php';
/**
* A simple Textarea field that essentially maps to HTML's `<textarea/>`.
*/
Class fieldTextarea extends Field implements ExportableField, ImportableField {
public function __construct(){
parent::__construct();
$this->_name = __('Textarea');
$this->_required = true;
// Set default
$this->set('show_column', 'no');
$this->set('required', 'no');
}
/*-------------------------------------------------------------------------
Definition:
-------------------------------------------------------------------------*/
public function canFilter(){
return true;
}
/*-------------------------------------------------------------------------
Setup:
-------------------------------------------------------------------------*/
public function createTable(){
return Symphony::Database()->query("
CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . $this->get('id') . "` (
`id` int(11) unsigned NOT NULL auto_increment,
`entry_id` int(11) unsigned NOT NULL,
`value` MEDIUMTEXT,
`value_formatted` MEDIUMTEXT,
PRIMARY KEY (`id`),
UNIQUE KEY `entry_id` (`entry_id`),
FULLTEXT KEY `value` (`value`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
");
}
/*-------------------------------------------------------------------------
Utilities:
-------------------------------------------------------------------------*/
protected function __applyFormatting($data, $validate=false, &$errors=NULL){
$result = '';
if($this->get('formatter')) {
$formatter = TextformatterManager::create($this->get('formatter'));
$result = $formatter->run($data);
}
if($validate === true) {
include_once(TOOLKIT . '/class.xsltprocess.php');
if(!General::validateXML($result, $errors, false, new XsltProcess)){
$result = html_entity_decode($result, ENT_QUOTES, 'UTF-8');
$result = $this->__replaceAmpersands($result);
if(!General::validateXML($result, $errors, false, new XsltProcess)){
return false;
}
}
}
return $result;
}
private function __replaceAmpersands($value) {
return preg_replace('/&(?!(#[0-9]+|#x[0-9a-f]+|amp|lt|gt);)/i', '&', trim($value));
}
/*-------------------------------------------------------------------------
Settings:
-------------------------------------------------------------------------*/
public function findDefaults(array &$settings){
if(!isset($settings['size'])) $settings['size'] = 15;
}
public function displaySettingsPanel(XMLElement &$wrapper, $errors = null) {
parent::displaySettingsPanel($wrapper, $errors);
// Textarea Size
$label = Widget::Label(__('Number of default rows'));
$label->setAttribute('class', 'column');
$input = Widget::Input('fields['.$this->get('sortorder').'][size]', (string)$this->get('size'));
$label->appendChild($input);
$div = new XMLElement('div');
$div->setAttribute('class', 'two columns');
$div->appendChild($this->buildFormatterSelect($this->get('formatter'), 'fields['.$this->get('sortorder').'][formatter]', __('Text Formatter')));
$div->appendChild($label);
$wrapper->appendChild($div);
$div = new XMLElement('div', NULL, array('class' => 'two columns'));
$this->appendRequiredCheckbox($div);
$this->appendShowColumnCheckbox($div);
$wrapper->appendChild($div);
}
public function commit(){
if(!parent::commit()) return false;
$id = $this->get('id');
if($id === false) return false;
$fields = array();
if($this->get('formatter') != 'none') $fields['formatter'] = $this->get('formatter');
$fields['size'] = $this->get('size');
return FieldManager::saveSettings($id, $fields);
}
/*-------------------------------------------------------------------------
Publish:
-------------------------------------------------------------------------*/
public function displayPublishPanel(XMLElement &$wrapper, $data = null, $flagWithError = null, $fieldnamePrefix = null, $fieldnamePostfix = null, $entry_id = null){
$label = Widget::Label($this->get('label'));
if($this->get('required') != 'yes') $label->appendChild(new XMLElement('i', __('Optional')));
$value = isset($data['value']) ? $data['value'] : null;
$textarea = Widget::Textarea('fields'.$fieldnamePrefix.'['.$this->get('element_name').']'.$fieldnamePostfix, (int)$this->get('size'), 50, (strlen($value) != 0 ? General::sanitize($value) : null));
if($this->get('formatter') != 'none') $textarea->setAttribute('class', $this->get('formatter'));
/**
* Allows developers modify the textarea before it is rendered in the publish forms
*
* @delegate ModifyTextareaFieldPublishWidget
* @param string $context
* '/backend/'
* @param Field $field
* @param Widget $label
* @param Widget $textarea
*/
Symphony::ExtensionManager()->notifyMembers('ModifyTextareaFieldPublishWidget', '/backend/', array(
'field' => &$this,
'label' => &$label,
'textarea' => &$textarea
));
$label->appendChild($textarea);
if($flagWithError != NULL) $wrapper->appendChild(Widget::Error($label, $flagWithError));
else $wrapper->appendChild($label);
}
public function checkPostFieldData($data, &$message, $entry_id=NULL){
$message = NULL;
if($this->get('required') == 'yes' && strlen($data) == 0){
$message = __('‘%s’ is a required field.', array($this->get('label')));
return self::__MISSING_FIELDS__;
}
if($this->__applyFormatting($data, true, $errors) === false){
$message = __('‘%s’ contains invalid XML.', array($this->get('label'))) . ' ' . __('The following error was returned:') . ' <code>' . $errors[0]['message'] . '</code>';
return self::__INVALID_FIELDS__;
}
return self::__OK__;
}
public function processRawFieldData($data, &$status, &$message=null, $simulate = false, $entry_id = null) {
$status = self::__OK__;
$result = array(
'value' => $data
);
$result['value_formatted'] = $this->__applyFormatting($data, true, $errors);
if($result['value_formatted'] === false){
// Run the formatter again, but this time do not validate. We will sanitize the output
$result['value_formatted'] = General::sanitize($this->__applyFormatting($data));
}
return $result;
}
/*-------------------------------------------------------------------------
Output:
-------------------------------------------------------------------------*/
public function fetchIncludableElements() {
if ($this->get('formatter')) {
return array(
$this->get('element_name') . ': formatted',
$this->get('element_name') . ': unformatted'
);
}
return array(
$this->get('element_name')
);
}
public function appendFormattedElement(XMLElement &$wrapper, $data, $encode = false, $mode = null, $entry_id = null) {
$attributes = array();
if (!is_null($mode)) $attributes['mode'] = $mode;
if ($mode == 'formatted') {
if ($this->get('formatter') && isset($data['value_formatted'])) {
$value = $data['value_formatted'];
}
else {
$value = $this->__replaceAmpersands($data['value']);
}
$wrapper->appendChild(
new XMLElement(
$this->get('element_name'),
($encode ? General::sanitize($value) : $value),
$attributes
)
);
}
else if ($mode == null || $mode == 'unformatted') {
$wrapper->appendChild(
new XMLElement(
$this->get('element_name'),
sprintf('<![CDATA[%s]]>', str_replace(']]>',']]]]><![CDATA[>',$data['value'])),
$attributes
)
);
}
}
/*-------------------------------------------------------------------------
Import:
-------------------------------------------------------------------------*/
public function getImportModes() {
return array(
'getValue' => ImportableField::STRING_VALUE,
'getPostdata' => ImportableField::ARRAY_VALUE
);
}
public function prepareImportValue($data, $mode, $entry_id = null) {
$message = $status = null;
$modes = (object)$this->getImportModes();
if($mode === $modes->getValue) {
return $data;
}
else if($mode === $modes->getPostdata) {
return $this->processRawFieldData($data, $status, $message, true, $entry_id);
}
return null;
}
/*-------------------------------------------------------------------------
Export:
-------------------------------------------------------------------------*/
/**
* Return a list of supported export modes for use with `prepareExportValue`.
*
* @return array
*/
public function getExportModes() {
return array(
'getHandle' => ExportableField::HANDLE,
'getFormatted' => ExportableField::FORMATTED,
'getUnformatted' => ExportableField::UNFORMATTED,
'getPostdata' => ExportableField::POSTDATA
);
}
/**
* Give the field some data and ask it to return a value using one of many
* possible modes.
*
* @param mixed $data
* @param integer $mode
* @param integer $entry_id
* @return string|null
*/
public function prepareExportValue($data, $mode, $entry_id = null) {
$modes = (object)$this->getExportModes();
// Export handles:
if ($mode === $modes->getHandle) {
if (isset($data['handle'])) {
return $data['handle'];
}
else if (isset($data['value'])) {
return General::createHandle($data['value']);
}
}
// Export unformatted:
else if ($mode === $modes->getUnformatted || $mode === $modes->getPostdata) {
return isset($data['value'])
? $data['value']
: null;
}
// Export formatted:
else if ($mode === $modes->getFormatted) {
if (isset($data['value_formatted'])) {
return $data['value_formatted'];
}
else if (isset($data['value'])) {
return General::sanitize($data['value']);
}
}
return null;
}
/*-------------------------------------------------------------------------
Filtering:
-------------------------------------------------------------------------*/
public function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation = false) {
$field_id = $this->get('id');
if (self::isFilterRegex($data[0])) {
$this->buildRegexSQL($data[0], array('value'), $joins, $where);
}
else {
if (is_array($data)) $data = $data[0];
$this->_key++;
$data = $this->cleanValue($data);
$joins .= "
LEFT JOIN
`tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key}
ON (e.id = t{$field_id}_{$this->_key}.entry_id)
";
$where .= "
AND MATCH (t{$field_id}_{$this->_key}.value) AGAINST ('{$data}' IN BOOLEAN MODE)
";
}
return true;
}
/*-------------------------------------------------------------------------
Events:
-------------------------------------------------------------------------*/
public function getExampleFormMarkup(){
$label = Widget::Label($this->get('label'));
$label->appendChild(Widget::Textarea('fields['.$this->get('element_name').']', (int)$this->get('size'), 50));
return $label;
}
}