Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
stringtype.php
1<?php
2
4
7use CUserTypeManager;
8
9Loc::loadMessages(__FILE__);
10
15class StringType extends BaseType
16{
17 public const
18 USER_TYPE_ID = 'string',
19 RENDER_COMPONENT = 'bitrix:main.field.string';
20
21 public static function getDescription(): array
22 {
23 return [
24 'DESCRIPTION' => Loc::getMessage('USER_TYPE_STRING_DESCRIPTION'),
25 'BASE_TYPE' => CUserTypeManager::BASE_TYPE_STRING,
26 ];
27 }
28
39 public static function getDbColumnType(): string
40 {
41 $connection = \Bitrix\Main\Application::getConnection();
42 $helper = $connection->getSqlHelper();
43 return $helper->getColumnTypeByField(new \Bitrix\Main\ORM\Fields\TextField('x'));
44 }
45
55 public static function prepareSettings(array $userField): array
56 {
57 $size = (int)($userField['SETTINGS']['SIZE'] ?? 0);
58 $rows = (int)($userField['SETTINGS']['ROWS'] ?? 0);
59 $min = (int)($userField['SETTINGS']['MIN_LENGTH'] ?? 0);
60 $max = (int)($userField['SETTINGS']['MAX_LENGTH'] ?? 0);
61
62 $regExp = '';
63 if (
64 is_array($userField['SETTINGS'])
65 && !empty($userField['SETTINGS']['REGEXP'])
66 //Checking the correctness of the regular expression entered by the user
67 && @preg_match($userField['SETTINGS']['REGEXP'], null) !== false
68 )
69 {
70 $regExp = $userField['SETTINGS']['REGEXP'];
71 }
72
73 return [
74 'SIZE' => ($size <= 1 ? 20 : ($size > 255 ? 225 : $size)),
75 'ROWS' => ($rows <= 1 ? 1 : ($rows > 50 ? 50 : $rows)),
76 'REGEXP' => $regExp,
77 'MIN_LENGTH' => $min,
78 'MAX_LENGTH' => $max,
79 'DEFAULT_VALUE' => is_array($userField['SETTINGS']) ? $userField['SETTINGS']['DEFAULT_VALUE'] : '',
80 ];
81 }
82
88 public static function getFilterData(?array $userField, array $additionalSettings): array
89 {
90 return [
91 'id' => $additionalSettings['ID'],
92 'name' => $additionalSettings['NAME'],
93 'filterable' => ''
94 ];
95 }
96
105 public static function checkFields(array $userField, $value): array
106 {
107 $fieldName = HtmlFilter::encode(
108 $userField['EDIT_FORM_LABEL'] <> ''
109 ? $userField['EDIT_FORM_LABEL'] : $userField['FIELD_NAME']
110 );
111
112 $msg = [];
113 if($value != '' && mb_strlen($value) < $userField['SETTINGS']['MIN_LENGTH'])
114 {
115 $msg[] = [
116 'id' => $userField['FIELD_NAME'],
117 'text' => Loc::GetMessage('USER_TYPE_STRING_MIN_LEGTH_ERROR',
118 [
119 '#FIELD_NAME#' => $fieldName,
120 '#MIN_LENGTH#' => $userField['SETTINGS']['MIN_LENGTH']
121 ]
122 )
123 ];
124 }
125 if(
126 $userField['SETTINGS']['MAX_LENGTH'] > 0
127 && mb_strlen($value) > $userField['SETTINGS']['MAX_LENGTH']
128 )
129 {
130 $msg[] = [
131 'id' => $userField['FIELD_NAME'],
132 'text' => Loc::GetMessage('USER_TYPE_STRING_MAX_LEGTH_ERROR',
133 [
134 '#FIELD_NAME#' => $fieldName,
135 '#MAX_LENGTH#' => $userField['SETTINGS']['MAX_LENGTH']
136 ]
137 ),
138 ];
139 }
140 if(
141 !empty($userField['SETTINGS']['REGEXP'])
142 && (string) $value !== ''
143 && !preg_match($userField['SETTINGS']['REGEXP'], $value)
144 )
145 {
146 $msg[] = [
147 'id' => $userField['FIELD_NAME'],
148 'text' => ($userField['ERROR_MESSAGE'] != '' ?
149 $userField['ERROR_MESSAGE'] :
150 Loc::GetMessage('USER_TYPE_STRING_REGEXP_ERROR',
151 [
152 '#FIELD_NAME#' => $fieldName
153 ]
154 )
155 ),
156 ];
157 }
158 return $msg;
159 }
160
169 public static function onSearchIndex(array $userField): ?string
170 {
171 if(is_array($userField['VALUE']))
172 {
173 $result = implode('\r\n', $userField['VALUE']);
174 }
175 else
176 {
177 $result = $userField['VALUE'];
178 }
179
180 return $result;
181 }
182
183 //<editor-fold desc="Events and methods..." defaultstate="collapsed">
195 /*
196 public static function onBeforeGetPublicView($event)
197 {
198 $params = $event->getParameters();
199 $arUserField = &$params[0];
200 $arAdditionalParameters = &$params[1];
201 if ($arUserField['USER_TYPE_ID'] == 'string')
202 {
203 $arUserField['VIEW_COMPONENT_NAME'] = 'my:system.field.view';
204 $arUserField['VIEW_COMPONENT_TEMPLATE'] = 'string';
205 }
206 }
207 */
208
219 /*
220 public static function onGetPublicView($event)
221 {
222 $params = $event->getParameters();
223 $arUserField = $params[0];
224 $arAdditionalParameters = $params[1];
225 if ($arUserField['USER_TYPE_ID'] == 'string')
226 {
227 $html = 'demo string';
228 return new \Bitrix\Main\EventResult(\Bitrix\Main\EventResult::SUCCESS, $html);
229 }
230 }
231 */
232
243 /*
244 public static function onAfterGetPublicView($event)
245 {
246 $params = $event->getParameters();
247 $arUserField = $params[0];
248 $arAdditionalParameters = $params[1];
249 $html = &$params[2];
250 if ($arUserField['USER_TYPE_ID'] == 'string')
251 {
252 $html .= '!';
253 }
254 }
255 */
256
265 /* static function OnBeforeSave($arUserField, $value)
266 {
267 if(strlen($value)>0)
268 return ''.round(doubleval($value), $arUserField['SETTINGS']['PRECISION']);
269 }*/
270 //</editor-fold>
271}
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 getFilterData(?array $userField, array $additionalSettings)
static onSearchIndex(array $userField)
static checkFields(array $userField, $value)
static prepareSettings(array $userField)