-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
list.php
executable file
·374 lines (306 loc) · 12.4 KB
/
list.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
370
371
372
373
374
<?php
class fieldList extends cmsFormField {
public $title = LANG_PARSER_LIST;
public $sql = 'int NULL DEFAULT NULL';
public $filter_type = 'int';
public $filter_hint = LANG_PARSER_LIST_FILTER_HINT;
public $var_type = 'string';
public $native_tag = false;
public $dynamic_list = false;
public $show_empty_value = true;
public $disable_array_key_rules = false;
public function __clone() {
if (isset($this->generator) || $this->getOption('list_where') === 'table') {
$this->items = null;
}
}
public function getOptions() {
return [
new fieldCheckbox('as_radio_btn', [
'title' => LANG_PARSER_LIST_AS_RADIO_BTN,
'default' => false
]),
new fieldCheckbox('filter_multiple', [
'title' => LANG_PARSER_LIST_FILTER_MULTI,
'default' => false
]),
new fieldCheckbox('filter_multiple_checkbox', [
'title' => LANG_PARSER_LIST_FILTER_MULTICH,
'default' => false,
'visible_depend' => ['options:filter_multiple' => ['show' => ['1']]]
]),
new fieldCheckbox('is_autolink', [
'title' => LANG_PARSER_LIST_IS_AUTOLINK,
'hint' => LANG_PARSER_LIST_IS_AUTOLINK_FILTER,
'default' => false,
'extended_option' => true
]),
new fieldCheckbox('show_empty_value', [
'title' => LANG_PARSER_LIST_ADD_EMPTY,
'default' => true
]),
new fieldList('list_where', [
'title' => LANG_PARSER_LIST_WHERE,
'items' => [
'' => LANG_PARSER_LIST_WHERE_PRE,
'table' => LANG_PARSER_LIST_WHERE_TBL
]
]),
new fieldString('list_table', [
'title' => LANG_TABLE,
'visible_depend' => ['options:list_where' => ['show' => ['table']]]
]),
new fieldString('list_where_cond', [
'title' => LANG_PARSER_LIST_COND,
'patterns_hint' => ['patterns' => [
'eq' => '=',
'gt' => '>',
'lt' => '<',
'ge' => '≥',
'le' => '≤',
'nn' => LANG_FILTER_NOT_NULL,
'ni' => LANG_FILTER_IS_NULL,
'lk' => LANG_FILTER_LIKE,
'ln' => LANG_FILTER_NOT_LIKE,
'lb' => LANG_FILTER_LIKE_BEGIN,
'lf' => LANG_FILTER_LIKE_END,
'dy' => LANG_FILTER_DATE_YOUNGER,
'do' => LANG_FILTER_DATE_OLDER
], 'wrap_symbols' => ['"','"']],
'hint' => LANG_PARSER_LIST_COND_HINT,
'visible_depend' => ['options:list_where' => ['show' => ['table']]]
]),
new fieldString('list_order', [
'title' => LANG_SORTING,
'hint' => LANG_PARSER_LIST_ORDER,
'visible_depend' => ['options:list_where' => ['show' => ['table']]]
]),
new fieldString('list_where_id', [
'title' => LANG_PARSER_LIST_WHERE_ID,
'visible_depend' => ['options:list_where' => ['show' => ['table']]]
]),
new fieldString('list_where_title', [
'title' => LANG_PARSER_LIST_WHERE_TITLE,
'visible_depend' => ['options:list_where' => ['show' => ['table']]]
]),
new fieldList('list_sorting', [
'title' => LANG_SORTING,
'default' => 'keys',
'items' => [
'' => LANG_SORTING_BYORDER,
'keys' => LANG_PARSER_LIST_SORT_BY_KEYS,
'values' => LANG_PARSER_LIST_SORT_BY_VALUES
],
'visible_depend' => ['options:list_where' => ['show' => ['']]]
])
];
}
public function setOptions($options) {
parent::setOptions($options);
if (array_key_exists('show_empty_value', $this->options)) {
$this->show_empty_value = $this->options['show_empty_value'];
}
}
public function getFilterInput($value) {
if (!$this->show_filter_input_title) {
$this->title = false;
}
if (empty($this->options['filter_multiple'])) {
$this->setProperty('show_empty_value', true);
return parent::getFilterInput($value);
} else {
$value = is_array($value) ? $value : [];
$this->setProperty('as_radio_btn', false);
$this->setOption('as_radio_btn', false);
if (!empty($this->options['filter_multiple_checkbox'])) {
$this->setProperty('is_multiple', true);
// Если уже сформирорвали список, удаляем первое пустое
if($this->show_empty_value && isset($this->items)){
unset($this->items['']);
}
$this->setProperty('show_empty_value', false);
} else {
$this->setProperty('is_chosen_multiple', true);
}
return parent::getFilterInput($value);
}
}
public function getRules() {
if ($this->disable_array_key_rules) {
return $this->rules;
}
if (!$this->dynamic_list) {
$this->rules[] = ['array_key', $this->getListItems()];
} else {
$this->rules[] = ['array_key_dynamic'];
}
return $this->rules;
}
public function getStringValue($value) {
if (is_empty_value($value)) {
return '';
}
$items = $this->getListItems();
$item = [];
if (!is_array($value)) {
$value = [$value];
}
foreach ($value as $val) {
if (!is_array($val) && isset($items[$val])) {
$item[] = $items[$val];
}
}
return implode(', ', $item);
}
public function parse($value) {
if (is_empty_value($value)) {
return '';
}
$items = $this->getListItems();
$item = '';
if (isset($items[$value])) {
$item = $items[$value];
}
if ($this->getOption('is_autolink')) {
return '<a class="list_autolink ' . $this->item['ctype_name'] . '_list_autolink" href="' . href_to($this->item['ctype_name']) . '?' . $this->name . '=' . urlencode($value) . '">' . html($item, false) . '</a>';
}
return html($item, false);
}
public function getListValuesItems() {
$items = [];
if (isset($this->value_items)) {
$items = $this->value_items;
} else if (isset($this->values_generator)) {
$generator = $this->values_generator;
$items = $generator($this->item);
}
return $items;
}
public function getDefaultVarType($is_filter = false) {
if ($this->context === 'filter') {
$is_filter = true;
}
if ($is_filter && $this->getOption('filter_multiple')) {
$this->var_type = 'array';
}
if ($this->getProperty('is_multiple')) {
$this->var_type = 'array';
}
if ($this->getProperty('is_chosen_multiple')) {
$this->var_type = 'array';
}
return parent::getDefaultVarType($is_filter);
}
public function applyFilter($model, $value) {
if (!is_array($value)) {
return $model->filterEqual($this->name, $value);
} else {
return $model->filterIn($this->name, $value);
}
}
public function getInput($value) {
if ($this->getDefaultVarType() === 'array' && $value && !is_array($value)) {
$value = cmsModel::yamlToArray($value);
}
if (is_array($value) && $value) {
foreach ($value as $k => $v) {
if (!is_array($v) && is_numeric($v)) {
$value[$k] = (int) $v;
}
}
}
$as_radio_btn = $this->getProperty('as_radio_btn')?:$this->getOption('as_radio_btn');
$this->data['dom_attr'] = $this->getProperty('attributes') ?: [];
$this->data['dom_attr']['id'] = $this->id;
$this->data['items'] = $this->getListItems();
$this->data['is_multiple'] = $this->getProperty('is_multiple');
$this->data['multiple_select_deselect'] = $this->getProperty('multiple_select_deselect');
$this->data['is_chosen_multiple'] = $this->getProperty('is_chosen_multiple');
$this->data['is_tree'] = $this->getProperty('is_tree');
$this->data['parent'] = $this->getProperty('parent');
$this->data['is_ns_value_items'] = false;
$this->data['select_hint_if_empty'] = $this->getProperty('select_hint_if_empty') ?: LANG_SELECT;
$this->data['select_hintmp_if_empty'] = $this->getProperty('select_hintmp_if_empty') ?: LANG_SELECT_MULTIPLE;
if ($this->dynamic_list) {
$this->data['value_items'] = $this->getListValuesItems();
$first_value_item = reset($this->data['value_items']);
$this->data['is_ns_value_items'] = is_array($first_value_item);
$this->class = 'list_dynamic';
if (!$value) {
$value = new stdClass();
}
if (!isset($this->multiple_keys)) {
$this->multiple_keys = new stdClass();
}
} elseif($as_radio_btn) {
$this->class = 'list_radio';
}
return parent::getInput($value);
}
public function validate_array_key_dynamic($value) {
if (empty($value)) {
return true;
}
if (!is_array($value)) {
return ERR_VALIDATE_INVALID;
}
$items = [
// Еще может быть ячейка field_value, в ней обычный input
'field' => $this->getListItems(),
'field_select' => $this->getListValuesItems()
];
// Если разбиты по группам
// избавляемся от вложенности
if ($items['field_select']) {
$first_value_item = reset($items['field_select']);
if (is_array($first_value_item)) {
$field_select = [];
foreach ($items['field_select'] as $fskey => $fsvalue) {
foreach ($fsvalue as $fsv_key => $fsv_value) {
$field_select[$fsv_key] = [$fsv_value];
}
}
$items['field_select'] = $field_select;
}
}
if (isset($this->multiple_keys)) {
foreach ($value as $val) {
if (!is_array($val)) {
return ERR_VALIDATE_INVALID;
}
foreach ($this->multiple_keys as $name => $type) {
if (!array_key_exists($name, $val)) {
return ERR_VALIDATE_INVALID;
}
// Не пустой список
if (!empty($items[$type])) {
if (!isset($items[$type][$val[$name]])) {
return ERR_VALIDATE_INVALID;
}
}
}
}
return true;
} else {
foreach ($value as $k => $val) {
if (!$k) {
if (!isset($items['field'][0]) && !isset($items['field'][''])) {
return ERR_VALIDATE_INVALID;
}
} else {
if (!isset($items['field'][$k])) {
return ERR_VALIDATE_INVALID;
}
}
if (is_array($val)) {
return ERR_VALIDATE_INVALID;
}
if (!isset($items['field_select'][$val])) {
return ERR_VALIDATE_INVALID;
}
}
return true;
}
return ERR_VALIDATE_INVALID;
}
}