Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
booleantype.php
1<?php
2
4
7use CUserTypeManager;
8
9Loc::loadMessages(__FILE__);
10
15class BooleanType extends BaseType
16{
17 public const
18 USER_TYPE_ID = 'boolean',
19 RENDER_COMPONENT = 'bitrix:main.field.boolean';
20
21 public const
22 DISPLAY_DROPDOWN = 'DROPDOWN',
23 DISPLAY_RADIO = 'RADIO',
24 DISPLAY_CHECKBOX = 'CHECKBOX';
25
29 public static function getDescription(): array
30 {
31 return [
32 'DESCRIPTION' => Loc::getMessage('USER_TYPE_BOOL_DESCRIPTION'),
33 'BASE_TYPE' => CUserTypeManager::BASE_TYPE_INT,
34 ];
35 }
36
40 public static function getDbColumnType(): string
41 {
42 $connection = \Bitrix\Main\Application::getConnection();
43 $helper = $connection->getSqlHelper();
44 return $helper->getColumnTypeByField(new \Bitrix\Main\ORM\Fields\IntegerField('x'));
45 }
46
51 public static function prepareSettings(array $userField): array
52 {
53 $label = ($userField['SETTINGS']['LABEL'] ?? ['', '']);
54
55 if($label[0] === Loc::getMessage('MAIN_NO'))
56 {
57 $label[0] = '';
58 }
59 if($label[1] === Loc::getMessage('MAIN_YES'))
60 {
61 $label[1] = '';
62 }
63
64 $labelCheckbox = ($userField['SETTINGS']['LABEL_CHECKBOX'] ?? '');
65 if($labelCheckbox === Loc::getMessage('MAIN_YES'))
66 {
67 $labelCheckbox = '';
68 }
69
70
71 $def = (int)($userField['SETTINGS']['DEFAULT_VALUE'] ?? 0);
72 if($def !== 1)
73 {
74 $def = 0;
75 }
76
77 $disp = ($userField['SETTINGS']['DISPLAY'] ?? '');
78 if($disp !== 'CHECKBOX' && $disp !== 'RADIO' && $disp !== 'DROPDOWN')
79 {
80 $disp = 'CHECKBOX';
81 }
82
83 return [
84 'DEFAULT_VALUE' => $def,
85 'DISPLAY' => $disp,
86 'LABEL' => [
87 $label[0], $label[1]
88 ],
89 'LABEL_CHECKBOX' => $labelCheckbox,
90 ];
91 }
92
97 public static function getLabels(array $userField): array
98 {
99 $label = [Loc::getMessage('MAIN_NO'), GetMessage('MAIN_YES')];
100 if(isset($userField['SETTINGS']['LABEL']) && is_array($userField['SETTINGS']['LABEL']))
101 {
102 foreach($label as $key => $value)
103 {
104 if($userField['SETTINGS']['LABEL'][$key] <> '')
105 {
106 $label[$key] = $userField['SETTINGS']['LABEL'][$key];
107 }
108 }
109 }
110
111 return $label;
112 }
113
119 public static function getFilterData(array $userField, array $additionalParameters): array
120 {
121 return [
122 'id' => $additionalParameters['ID'],
123 'name' => $additionalParameters['NAME'],
124 'type' => 'list',
125 'items' => [
126 'Y' => Loc::getMessage('MAIN_YES'),
127 'N' => Loc::getMessage('MAIN_NO')
128 ],
129 'filterable' => ''
130 ];
131 }
132
138 public static function onBeforeSave($userField, $value): int
139 {
140 $result = 0;
141 if($value)
142 {
143 $result = 1;
144 }
145 return $result;
146 }
147
152 final public static function getAllDisplays(): array
153 {
154 $reflection = new \ReflectionClass(__CLASS__);
155 $constants = $reflection->getConstants();
156 $result = [];
157 foreach($constants as $name => $value)
158 {
159 if(mb_strpos($name, 'DISPLAY_') === 0)
160 {
161 $result[$name] = $value;
162 }
163 }
164 return $result;
165 }
166
167 public static function getAdminListViewHtmlMulty(array $userField, ?array $additionalParameters): string
168 {
169 return parent::renderAdminListView($userField, $additionalParameters);
170 }
171
172 public static function isMandatorySupported(): bool
173 {
174 return false;
175 }
176
177 public static function isMultiplicitySupported(): bool
178 {
179 return false;
180 }
181
182 public static function checkFields(array $userField, $value): array
183 {
184 return [];
185 }
186
187 public static function getEntityField($fieldName, $fieldParameters)
188 {
189 $fieldParameters['values'] = [0, 1];
190
191 $field = (new BooleanField($fieldName, $fieldParameters))
192 ->configureNullable();
193
194 return $field;
195 }
196}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getAdminListViewHtmlMulty(array $userField, ?array $additionalParameters)
static getLabels(array $userField)
static getFilterData(array $userField, array $additionalParameters)
static onBeforeSave($userField, $value)
static checkFields(array $userField, $value)
static getEntityField($fieldName, $fieldParameters)
static prepareSettings(array $userField)