Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
enumtype.php
1<?php
2
4
7use CDBResult;
8use CUserFieldEnum;
9use CUserTypeManager;
11
12Loc::loadMessages(__FILE__);
13
18class EnumType extends BaseType
19{
20 public const USER_TYPE_ID = 'enumeration';
21 public const RENDER_COMPONENT = 'bitrix:main.field.enum';
22 public const DISPLAY_LIST = 'LIST';
23 public const DISPLAY_CHECKBOX = 'CHECKBOX';
24 public const DISPLAY_UI = 'UI';
25 public const DISPLAY_DIALOG = 'DIALOG';
26
30 public static function getDescription(): array
31 {
32 return [
33 'DESCRIPTION' => Loc::getMessage('USER_TYPE_ENUM_DESCRIPTION'),
34 'BASE_TYPE' => CUserTypeManager::BASE_TYPE_ENUM,
35 ];
36 }
37
38 public static function renderField(array $userField, ?array $additionalParameters = []): string
39 {
40 static::getEnumList($userField, $additionalParameters);
41 return parent::renderField($userField, $additionalParameters);
42 }
43
44 public static function renderView(array $userField, ?array $additionalParameters = []): string
45 {
46 static::getEnumList($userField, $additionalParameters);
47 return parent::renderView($userField, $additionalParameters);
48 }
49
50 public static function renderEdit(array $userField, ?array $additionalParameters = []): string
51 {
52 static::getEnumList($userField, $additionalParameters);
53 return parent::renderEdit($userField, $additionalParameters);
54 }
55
56 public static function renderEditForm(array $userField, ?array $additionalParameters): string
57 {
58 $enum = call_user_func([$userField['USER_TYPE']['CLASS_NAME'], 'getlist'], $userField);
59 if (!$enum)
60 {
61 return '';
62 }
63 $items = [];
64 while ($item = $enum->GetNext())
65 {
66 $items[$item['ID']] = $item;
67 }
68 $additionalParameters['items'] = $items;
69
70 return parent::renderEditForm($userField, $additionalParameters);
71 }
72
73 public static function renderFilter(array $userField, ?array $additionalParameters): string
74 {
75 $enum = call_user_func([$userField['USER_TYPE']['CLASS_NAME'], 'getlist'], $userField);
76 if (!$enum)
77 {
78 return '';
79 }
80 $items = [];
81 while ($item = $enum->GetNext())
82 {
83 $items[$item['ID']] = $item['VALUE'];
84 }
85 $additionalParameters['items'] = $items;
86 return parent::renderFilter($userField, $additionalParameters);
87 }
88
89 public static function renderAdminListView(array $userField, ?array $additionalParameters): string
90 {
91 static $cache = [];
92
93 $emptyCaption = '&nbsp;';
94
95 $value = (int)($additionalParameters['VALUE'] ?? 0);
96 $fieldId = (int)$userField['ID'];
97
98 if (!isset($cache[$fieldId][$value]))
99 {
100 $enum = call_user_func([$userField['USER_TYPE']['CLASS_NAME'], 'getlist'], $userField);
101 if (!$enum)
102 {
103 $additionalParameters['VALUE'] = $emptyCaption;
104 return parent::renderAdminListView($userField, $additionalParameters);
105 }
106 while ($item = $enum->GetNext())
107 {
108 $cache[$fieldId][(int)$item['ID']] = $item['VALUE'];
109 }
110 }
111 if (!isset($cache[$fieldId][$value]))
112 {
113 $cache[$fieldId][$value] = $emptyCaption;
114 }
115
116 $additionalParameters['VALUE'] = $cache[$fieldId][$value];
117 return parent::renderAdminListView($userField, $additionalParameters);
118 }
119
120 public static function renderAdminListEdit(array $userField, ?array $additionalParameters): string
121 {
122 $enum = call_user_func([$userField['USER_TYPE']['CLASS_NAME'], 'getlist'], $userField);
123 $values = [];
124 if ($enum)
125 {
126 while ($item = $enum->GetNext())
127 {
128 $values[$item['ID']] = $item['VALUE'];
129 }
130 }
131 $additionalParameters['enumItems'] = $values;
132
133 return parent::renderAdminListEdit($userField, $additionalParameters);
134 }
135
139 public static function getDbColumnType(): string
140 {
141 $connection = \Bitrix\Main\Application::getConnection();
142 $helper = $connection->getSqlHelper();
143 return $helper->getColumnTypeByField(new \Bitrix\Main\ORM\Fields\IntegerField('x'));
144 }
145
150 public static function prepareSettings(array $userField): array
151 {
152 $height = (int)($userField['SETTINGS']['LIST_HEIGHT'] ?? 0);
153 $display = $userField['SETTINGS']['DISPLAY'] ?? '';
154 $caption_no_value = trim($userField['SETTINGS']['CAPTION_NO_VALUE'] ?? '');
155 $show_no_value = (isset($userField['SETTINGS']['SHOW_NO_VALUE']) && $userField['SETTINGS']['SHOW_NO_VALUE'] === 'N' ? 'N' : 'Y');
156
157 $displays = [
161 ];
162
163 if (!in_array($display, $displays, true))
164 {
165 $display = self::DISPLAY_LIST;
166 }
167
168 return [
169 'DISPLAY' => $display,
170 'LIST_HEIGHT' => ($height < 1 ? 1 : $height),
171 'CAPTION_NO_VALUE' => $caption_no_value, // no default value - only in output
172 'SHOW_NO_VALUE' => $show_no_value, // no default value - only in output
173 ];
174 }
175
181 public static function checkFields(array $userField, $value): array
182 {
183 return [];
184 }
185
190 public static function onSearchIndex(array $userField): ?string
191 {
192 $res = '';
193
194 if (is_array($userField['VALUE']))
195 {
196 $val = $userField['VALUE'];
197 }
198 else
199 {
200 $val = [$userField['VALUE']];
201 }
202
203 $val = array_filter($val, 'strlen');
204 if (!empty($val))
205 {
206 $ob = new CUserFieldEnum();
207 $rs = $ob->GetList([], [
208 'USER_FIELD_ID' => $userField['ID'],
209 'ID' => $val,
210 ]);
211
212 while ($ar = $rs->Fetch())
213 {
214 $res .= $ar['VALUE'] . '\r\n';
215 }
216 }
217
218 return $res;
219 }
220
226 public static function getFilterData(array $userField, array $additionalParameters): array
227 {
228 $enum = call_user_func([$userField['USER_TYPE']['CLASS_NAME'], 'getlist'], $userField);
229 $items = [];
230 if ($enum)
231 {
232 while ($item = $enum->GetNext())
233 {
234 $items[$item['ID']] = $item['VALUE'];
235 }
236 }
237 return [
238 'id' => $additionalParameters['ID'],
239 'name' => $additionalParameters['NAME'],
240 'type' => 'list',
241 'items' => $items,
242 'params' => ['multiple' => 'Y'],
243 'filterable' => '',
244 ];
245 }
246
251 public static function getList(array $userField)
252 {
253 $userFieldEnum = new CUserFieldEnum();
254 return $userFieldEnum->GetList([], ['USER_FIELD_ID' => $userField['ID'] ?? null]);
255 }
256
261 public static function getEnumList(array &$userField, array $additionalParameters = []): void
262 {
263 $showNoValue = (
264 (!isset($userField['MANDATORY']) || $userField['MANDATORY'] !== 'Y')
265 ||
266 (!isset($userField['SETTINGS']['SHOW_NO_VALUE']) || $userField['SETTINGS']['SHOW_NO_VALUE'] !== 'N')
267 ||
268 (
269 isset($additionalParameters['SHOW_NO_VALUE'])
270 &&
271 $additionalParameters['SHOW_NO_VALUE'] === true
272 )
273 );
274
275 if (
276 $showNoValue
277 &&
278 (
279 (!isset($userField['SETTINGS']['DISPLAY']) || $userField['SETTINGS']['DISPLAY'] !== 'CHECKBOX')
280 ||
281 (!isset($userField['MULTIPLE']) || $userField['MULTIPLE'] !== 'Y')
282 )
283 )
284 {
285 $enum = [null => static::getEmptyCaption($userField)];
286 $userField['USER_TYPE']['FIELDS'] = $enum;
287 $userField['USER_TYPE']['~FIELDS'] = $enum;
288 }
289
290 $enumList = static::getList($userField);
291
292 while ($item = $enumList->Fetch())
293 {
294 $userField['USER_TYPE']['FIELDS'][$item['ID']] = HtmlFilter::encode($item['VALUE']);
295 $userField['USER_TYPE']['~FIELDS'][$item['ID']] = $item['VALUE'];
296 }
297 }
298
304 public static function getEmptyCaption(array $userField): string
305 {
306 $message = ($userField['SETTINGS']['CAPTION_NO_VALUE'] ?? '');
307 return (
308 $message !== ''
309 ? HtmlFilter::encode($userField['SETTINGS']['CAPTION_NO_VALUE'])
310 : Loc::getMessage('USER_TYPE_ENUM_NO_VALUE')
311 );
312 }
313
319 public static function getListMultiple(array $userFields)
320 {
321 $ids = [];
322 foreach ($userFields as $field)
323 {
324 $ids[] = $field['ID'];
325 }
326 $userFieldEnum = new CUserFieldEnum();
327 return $userFieldEnum->GetList(
328 ['USER_FIELD_ID' => 'ASC', 'SORT' => 'ASC', 'ID' => 'ASC'],
329 ['USER_FIELD_ID' => $ids]
330 );
331 }
332
338 public static function getGroupActionData(array $userField, ?array $additionalParameters): array
339 {
340 $result = [];
341 $enum = call_user_func([$userField['USER_TYPE']['CLASS_NAME'], 'getlist'], $userField);
342 if (!$enum)
343 {
344 return $result;
345 }
346
347 while ($item = $enum->GetNext())
348 {
349 $result[] = ['NAME' => $item['VALUE'], 'VALUE' => $item['ID']];
350 }
351
352 return $result;
353 }
354
360 public static function getAdminListEditHtmlMulty(array $userField, ?array $additionalParameters): string
361 {
362 return static::renderAdminListEdit($userField, $additionalParameters);
363 }
364
365 public static function getDefaultValue(array $userField, array $additionalParameters = [])
366 {
367 if (!isset($userField['ENUM']))
368 {
369 $userField['ENUM'] = [];
370 $enumValuesManager = new CUserFieldEnum();
371 $dbRes = $enumValuesManager->getList(
372 [],
373 [
374 'USER_FIELD_ID' => $userField['ID'],
375 'DEF' => 'Y',
376 ]
377 );
378
379 while ($enumValue = $dbRes->fetch())
380 {
381 $userField['ENUM'][] = [
382 'ID' => $enumValue['ID'],
383 'VALUE' => $enumValue['VALUE'],
384 'DEF' => $enumValue['DEF'],
385 'SORT' => $enumValue['SORT'],
386 'XML_ID' => $enumValue['XML_ID'],
387 ];
388 }
389 }
390
391 $userField['ENTITY_VALUE_ID'] = 0;
392 return static::getFieldValue($userField, $additionalParameters);
393 }
394
395 public static function onBeforeSave($userField, $value)
396 {
397 return ($userField['MULTIPLE'] !== 'Y' && is_array($value)) ? array_shift($value) : $value;
398 }
399
400 public static function getFieldValue(array $userField, array $additionalParameters = [])
401 {
402 $bVarsFromForm = ($additionalParameters['bVarsFromForm'] ?? false);
403 if (!$bVarsFromForm && !isset($additionalParameters['VALUE']))
404 {
405 if (
406 isset($userField['ENTITY_VALUE_ID'], $userField['ENUM'])
407 && $userField['ENTITY_VALUE_ID'] <= 0
408 )
409 {
410 $value = ($userField['MULTIPLE'] === 'Y' ? [] : null);
411 foreach ($userField['ENUM'] as $enum)
412 {
413 if ($enum['DEF'] === 'Y')
414 {
415 if ($userField['MULTIPLE'] === 'Y')
416 {
417 $value[] = $enum['ID'];
418 }
419 else
420 {
421 $value = $enum['ID'];
422 break;
423 }
424 }
425 }
426 }
427 else
428 {
429 $value = $userField['VALUE'] ?? null;
430 }
431 }
432 elseif (isset($additionalParameters['VALUE']))
433 {
434 $value = $additionalParameters['VALUE'];
435 }
436 else
437 {
438 $value = Context::getCurrent()->getRequest()->get($userField['FIELD_NAME']);
439 }
440
441 return $value;
442 }
443}
static getCurrent()
Definition context.php:241
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static encode($string, $flags=ENT_COMPAT, $doubleEncode=true)
static renderAdminListEdit(array $userField, ?array $additionalParameters)
Definition enumtype.php:120
static renderEdit(array $userField, ?array $additionalParameters=[])
Definition enumtype.php:50
static getDefaultValue(array $userField, array $additionalParameters=[])
Definition enumtype.php:365
static getFilterData(array $userField, array $additionalParameters)
Definition enumtype.php:226
static renderEditForm(array $userField, ?array $additionalParameters)
Definition enumtype.php:56
static getListMultiple(array $userFields)
Definition enumtype.php:319
static getEnumList(array &$userField, array $additionalParameters=[])
Definition enumtype.php:261
static getList(array $userField)
Definition enumtype.php:251
static renderField(array $userField, ?array $additionalParameters=[])
Definition enumtype.php:38
static onBeforeSave($userField, $value)
Definition enumtype.php:395
static renderView(array $userField, ?array $additionalParameters=[])
Definition enumtype.php:44
static onSearchIndex(array $userField)
Definition enumtype.php:190
static getFieldValue(array $userField, array $additionalParameters=[])
Definition enumtype.php:400
static checkFields(array $userField, $value)
Definition enumtype.php:181
static renderFilter(array $userField, ?array $additionalParameters)
Definition enumtype.php:73
static getEmptyCaption(array $userField)
Definition enumtype.php:304
static prepareSettings(array $userField)
Definition enumtype.php:150
static renderAdminListView(array $userField, ?array $additionalParameters)
Definition enumtype.php:89
static getGroupActionData(array $userField, ?array $additionalParameters)
Definition enumtype.php:338
static getAdminListEditHtmlMulty(array $userField, ?array $additionalParameters)
Definition enumtype.php:360