Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
fieldadapter.php
1<?php
2
4
6
8{
9 public const STRING = 'string';
10 public const TEXTAREA = 'textarea';
11 public const NUMBER = 'number';
12 public const DATE = 'date';
13 public const LIST = 'list';
14 public const DEST_SELECTOR = 'dest_selector';
15 public const ENTITY_SELECTOR = 'entity_selector';
16 public const CUSTOM = 'custom';
17 public const CUSTOM_ENTITY = 'custom_entity';
18 public const CUSTOM_DATE = 'custom_date';
19 public const CHECKBOX = 'checkbox';
20
26 public static function adapt(array $sourceField, $filterId = ''): array
27 {
28 $sourceField = static::normalize($sourceField);
29 switch ($sourceField['type'])
30 {
31 case self::LIST:
32 $items = [];
33
34 if (isset($sourceField['items']) && !empty($sourceField['items']) && is_array($sourceField['items']))
35 {
36 foreach ($sourceField['items'] as $selectItemValue => $selectItem)
37 {
38 if (is_array($selectItem))
39 {
40 $selectItem['VALUE'] = (string)$selectItemValue;
41 $listItem = $selectItem;
42 }
43 else
44 {
45 $listItem = [
46 'NAME' => $selectItem,
47 'VALUE' => (string)$selectItemValue,
48 ];
49 }
50
51 $items[] = $listItem;
52 }
53 }
54
55 if ($sourceField['params']['multiple'])
56 {
57 $field = Field::multiSelect(
58 $sourceField['id'],
59 $items,
60 [],
61 $sourceField['name'],
62 $sourceField['placeholder']
63 );
64 }
65 else
66 {
67 if (empty($items))
68 {
69 $items[] = [
70 'NAME' => Loc::getMessage('MAIN_UI_FILTER__NOT_SET'),
71 'VALUE' => '',
72 ];
73 }
74 elseif (empty($items[0]['VALUE']) && empty($items[0]['NAME']))
75 {
76 $items[0]['NAME'] = Loc::getMessage('MAIN_UI_FILTER__NOT_SET');
77 }
78 elseif (!empty($items[0]['VALUE']) && !empty($items[0]['NAME']))
79 {
80 array_unshift(
81 $items,
82 [
83 'NAME' => Loc::getMessage('MAIN_UI_FILTER__NOT_SET'),
84 'VALUE' => '',
85 ]
86 );
87 }
88
89 $field = Field::select(
90 $sourceField['id'],
91 $items,
92 [],
93 $sourceField['name'],
94 $sourceField['placeholder']
95 );
96 }
97
98 break;
99
100 case self::DATE:
101 $field = Field::date(
102 $sourceField['id'],
104 [],
105 $sourceField['name'],
106 $sourceField['placeholder'],
107 ($sourceField['time'] ?? false),
108 ($sourceField['exclude'] ?? []),
109 ($sourceField['include'] ?? []),
110 ($sourceField['allow_years_switcher'] ?? false),
111 ($sourceField['messages'] ?? [])
112 );
113 break;
114
115 case self::NUMBER:
116 $field = Field::number(
117 $sourceField['id'],
119 [],
120 $sourceField['name'],
121 $sourceField['placeholder'],
122 ($sourceField['exclude'] ?? []),
123 ($sourceField['include'] ?? []),
124 ($sourceField['messages'] ?? [])
125 );
126
127 break;
128
129 case self::CUSTOM:
130 $field = Field::custom(
131 $sourceField['id'],
132 $sourceField['value'],
133 $sourceField['name'],
134 $sourceField['placeholder'],
135 ($sourceField['style'] ?? false)
136 );
137 break;
138
140 $field = Field::customEntity(
141 $sourceField['id'],
142 $sourceField['name'],
143 $sourceField['placeholder'],
144 $sourceField['params']['multiple']
145 );
146 break;
147
148 case self::CHECKBOX:
149 $values = isset($sourceField['valueType']) && $sourceField['valueType'] === 'numeric'
150 ? [
151 '1',
152 '0',
153 ]
154 : [
155 'Y',
156 'N',
157 ]
158 ;
159
160 $items = [
161 [
162 'NAME' => Loc::getMessage('MAIN_UI_FILTER__NOT_SET'),
163 'VALUE' => '',
164 ],
165 [
166 'NAME' => Loc::getMessage('MAIN_UI_FILTER__YES'),
167 'VALUE' => $values[0],
168 ],
169 [
170 'NAME' => Loc::getMessage('MAIN_UI_FILTER__NO'),
171 'VALUE' => $values[1],
172 ]
173 ];
174
175 $field = Field::select(
176 $sourceField['id'],
177 $items,
178 $items[0],
179 $sourceField['name'],
180 $sourceField['placeholder']
181 );
182
183 break;
184
186 $field = Field::customDate($sourceField);
187 break;
188
190 $field = Field::destSelector(
191 $sourceField['id'],
192 $sourceField['name'],
193 $sourceField['placeholder'],
194 $sourceField['params']['multiple'],
195 $sourceField['params'],
196 ($sourceField['lightweight'] ?? false),
197 $filterId
198 );
199 break;
200
202 $field = Field::entitySelector(
203 (string)($sourceField['id'] ?? ''),
204 (string)($sourceField['name'] ?? ''),
205 (string)($sourceField['placeholder'] ?? ''),
206 (isset($sourceField['params']) && is_array($sourceField['params'])) ? $sourceField['params'] : [],
207 (string)$filterId
208 );
209 break;
210
211 case self::TEXTAREA:
212 $field = Field::textarea(
213 $sourceField['id'],
214 '',
215 $sourceField['name'],
216 $sourceField['placeholder']
217 );
218 break;
219
220 case self::STRING:
221 default:
222 $field = Field::string(
223 $sourceField['id'],
224 '',
225 $sourceField['name'],
226 $sourceField['placeholder']
227 );
228 break;
229 }
230
231 if (!empty($sourceField['html']))
232 {
233 $field['HTML'] = $sourceField['html'];
234 }
235 if (!empty($sourceField['additionalFilter']))
236 {
237 $field['ADDITIONAL_FILTER_ALLOWED'] = $sourceField['additionalFilter'];
238 }
239
240 if (isset($sourceField['sectionId']) && $sourceField['sectionId'] !== '')
241 {
242 $field['SECTION_ID'] = $sourceField['sectionId'];
243 }
244 if (!empty($sourceField['icon']))
245 {
246 $field['ICON'] = $sourceField['icon'];
247 }
248
249 return $field;
250 }
251
256 public static function normalize(array $sourceField): array
257 {
258 if (!isset($sourceField['type']))
259 {
260 $sourceField['type'] = self::STRING;
261 }
262 if (!isset($sourceField['placeholder']))
263 {
264 $sourceField['placeholder'] = '';
265 }
266 if (!isset($sourceField['params']) || !is_array($sourceField['params']))
267 {
268 $sourceField['params'] = [];
269 }
270 if (!isset($sourceField['params']['multiple']))
271 {
272 $sourceField['params']['multiple'] = false;
273 }
274 else
275 {
276 $sourceField['params']['multiple'] = (
277 $sourceField['params']['multiple'] === 'Y'
278 || $sourceField['params']['multiple'] === true
279 );
280 }
281
282 return $sourceField;
283 }
284}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static normalize(array $sourceField)
static adapt(array $sourceField, $filterId='')
static select($name, $items, Array $defaultValue=array(), $label="", $placeholder="")
Definition field.php:413
static date( $name, $type=DateType::NONE, $values=[], $label="", $placeholder="", $enableTime=false, $exclude=[], $include=[], $allowYearsSwithcer=false, $messages=[])
Definition field.php:127
static multiSelect($name, $items, $defaultValues=array(), $label="", $placeholder="")
Definition field.php:445
static customEntity($name, $label="", $placeholder="", $multiple=false)
Definition field.php:94
static destSelector($name, $label="", $placeholder="", $multiple=false, $params=array(), $lightweight=false, $filterName='')
Definition field.php:631
static custom($name, $value, $label="", $placeholder="", $style=false)
Definition field.php:70
static textarea($name, $defaultValue="", $label="", $placeholder="")
Definition field.php:46
static number( $name, $type=NumberType::SINGLE, $values=[], $label="", $placeholder="", $exclude=[], $include=[], $messages=[])
Definition field.php:332
static entitySelector(string $name, string $label='', string $placeholder='', array $params=[], string $filterName='')
Definition field.php:725
static string($name, $defaultValue="", $label="", $placeholder="")
Definition field.php:25
static customDate($options=array())
Definition field.php:462