/
field.input.php
321 lines (249 loc) · 8.95 KB
/
field.input.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
<?php
/**
* @package toolkit
*/
/**
* A simple Input field that essentially maps to HTML's `<input type='text'/>`.
*/
require_once(TOOLKIT . '/class.xsltprocess.php');
Class fieldInput extends Field {
public function __construct(&$parent){
parent::__construct($parent);
$this->_name = __('Text Input');
$this->_required = true;
$this->set('required', 'no');
}
/*-------------------------------------------------------------------------
Definition:
-------------------------------------------------------------------------*/
public function canFilter(){
return true;
}
public function canImport(){
return true;
}
public function canPrePopulate(){
return true;
}
public function isSortable(){
return true;
}
public function allowDatasourceOutputGrouping(){
return true;
}
public function allowDatasourceParamOutput(){
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,
`handle` varchar(255) default NULL,
`value` varchar(255) default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `entry_id` (`entry_id`),
KEY `handle` (`handle`),
KEY `value` (`value`)
) ENGINE=MyISAM;
");
}
/*-------------------------------------------------------------------------
Utilities:
-------------------------------------------------------------------------*/
private function __applyValidationRules($data){
$rule = $this->get('validator');
return ($rule ? General::validateString($data, $rule) : true);
}
private function __replaceAmpersands($value) {
return preg_replace('/&(?!(#[0-9]+|#x[0-9a-f]+|amp|lt|gt);)/i', '&', trim($value));
}
/*-------------------------------------------------------------------------
Settings:
-------------------------------------------------------------------------*/
public function setFromPOST($postdata){
parent::setFromPOST($postdata);
if($this->get('validator') == '') $this->remove('validator');
}
public function displaySettingsPanel(&$wrapper, $errors = null) {
parent::displaySettingsPanel($wrapper, $errors);
$this->buildValidationSelect($wrapper, $this->get('validator'), 'fields['.$this->get('sortorder').'][validator]');
$div = new XMLElement('div', NULL, array('class' => 'compact'));
$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();
$fields['field_id'] = $id;
$fields['validator'] = ($fields['validator'] == 'custom' ? NULL : $this->get('validator'));
Symphony::Database()->query("DELETE FROM `tbl_fields_".$this->handle()."` WHERE `field_id` = '$id' LIMIT 1");
return Symphony::Database()->insert($fields, 'tbl_fields_' . $this->handle());
}
/*-------------------------------------------------------------------------
Publish:
-------------------------------------------------------------------------*/
public function displayPublishPanel(&$wrapper, $data=NULL, $flagWithError=NULL, $fieldnamePrefix=NULL, $fieldnamePostfix=NULL, $entry_id = null){
$value = General::sanitize($data['value']);
$label = Widget::Label($this->get('label'));
if($this->get('required') != 'yes') $label->appendChild(new XMLElement('i', __('Optional')));
$label->appendChild(Widget::Input('fields'.$fieldnamePrefix.'['.$this->get('element_name').']'.$fieldnamePostfix, (strlen($value) != 0 ? $value : NULL)));
if($flagWithError != NULL) $wrapper->appendChild(Widget::wrapFormElementWithError($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->__applyValidationRules($data)){
$message = __("'%s' contains invalid data. Please check the contents.", array($this->get('label')));
return self::__INVALID_FIELDS__;
}
return self::__OK__;
}
public function processRawFieldData($data, &$status, $simulate = false, $entry_id = null) {
$status = self::__OK__;
if (strlen(trim($data)) == 0) return array();
$result = array(
'value' => $data
);
$result['handle'] = Lang::createHandle($result['value']);
return $result;
}
/*-------------------------------------------------------------------------
Output:
-------------------------------------------------------------------------*/
public function appendFormattedElement(&$wrapper, $data, $encode=false){
$value = $data['value'];
if($encode === true){
$value = General::sanitize($value);
}
else{
include_once(TOOLKIT . '/class.xsltprocess.php');
if(!General::validateXML($data['value'], $errors, false, new XsltProcess)){
$value = html_entity_decode($data['value'], ENT_QUOTES, 'UTF-8');
$value = $this->__replaceAmpersands($value);
if(!General::validateXML($value, $errors, false, new XsltProcess)){
$value = General::sanitize($data['value']);
}
}
}
$wrapper->appendChild(
new XMLElement(
$this->get('element_name'), $value, array('handle' => $data['handle'])
)
);
}
/*-------------------------------------------------------------------------
Filtering:
-------------------------------------------------------------------------*/
public function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation = false) {
$field_id = $this->get('id');
if (self::isFilterRegex($data[0])) {
$this->_key++;
if (preg_match('/^regexp:/i', $data[0])) {
$pattern = preg_replace('/^regexp:\s*/i', null, $this->cleanValue($data[0]));
$regex = 'REGEXP';
} else {
$pattern = preg_replace('/^not-?regexp:\s*/i', null, $this->cleanValue($data[0]));
$regex = 'NOT REGEXP';
}
if(strlen($pattern) == 0) return;
$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 (
t{$field_id}_{$this->_key}.value {$regex} '{$pattern}'
OR t{$field_id}_{$this->_key}.handle {$regex} '{$pattern}'
)
";
} elseif ($andOperation) {
foreach ($data as $value) {
$this->_key++;
$value = $this->cleanValue($value);
$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 (
t{$field_id}_{$this->_key}.value = '{$value}'
OR t{$field_id}_{$this->_key}.handle = '{$value}'
)
";
}
} else {
if (!is_array($data)) $data = array($data);
foreach ($data as &$value) {
$value = $this->cleanValue($value);
}
$this->_key++;
$data = implode("', '", $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 (
t{$field_id}_{$this->_key}.value IN ('{$data}')
OR t{$field_id}_{$this->_key}.handle IN ('{$data}')
)
";
}
return true;
}
/*-------------------------------------------------------------------------
Sorting:
-------------------------------------------------------------------------*/
public function buildSortingSQL(&$joins, &$where, &$sort, $order='ASC'){
if(in_array(strtolower($order), array('random', 'rand'))) {
$sort = 'ORDER BY RAND()';
}
else {
$sort = sprintf(
'ORDER BY (
SELECT %s
FROM tbl_entries_data_%d AS `ed`
WHERE entry_id = e.id
) %s',
'`ed`.value',
$this->get('id'),
$order
);
}
}
/*-------------------------------------------------------------------------
Grouping:
-------------------------------------------------------------------------*/
public function groupRecords($records){
if(!is_array($records) || empty($records)) return;
$groups = array($this->get('element_name') => array());
foreach($records as $r){
$data = $r->getData($this->get('id'));
$value = General::sanitize($data['value']);
$handle = Lang::createHandle($value);
if(!isset($groups[$this->get('element_name')][$handle])){
$groups[$this->get('element_name')][$handle] = array(
'attr' => array('handle' => $handle, 'value' => $value),
'records' => array(),
'groups' => array()
);
}
$groups[$this->get('element_name')][$handle]['records'][] = $r;
}
return $groups;
}
}