Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
integertype.php
1<?php
2
4
7use CUserTypeManager;
8
9Loc::loadMessages(__FILE__);
10
15class IntegerType extends BaseType
16{
17 public const
18 USER_TYPE_ID = 'integer',
19 RENDER_COMPONENT = 'bitrix:main.field.integer';
20
24 public static function getDescription(): array
25 {
26 return [
27 'DESCRIPTION' => GetMessage('USER_TYPE_INTEGER_DESCRIPTION'),
28 'BASE_TYPE' => CUserTypeManager::BASE_TYPE_INT
29 ];
30 }
31
35 public static function getDbColumnType(): string
36 {
37 $connection = \Bitrix\Main\Application::getConnection();
38 $helper = $connection->getSqlHelper();
39 return $helper->getColumnTypeByField(new \Bitrix\Main\ORM\Fields\IntegerField('x'));
40 }
41
46 public static function prepareSettings(array $userField): array
47 {
48 $size = (int)($userField['SETTINGS']['SIZE'] ?? 0);
49 $min = (int)($userField['SETTINGS']['MIN_VALUE'] ?? 0);
50 $max = (int)($userField['SETTINGS']['MAX_VALUE'] ?? 0);
51 $default = ($userField['SETTINGS']['DEFAULT_VALUE'] ?? '') !== ''
52 ? (int)$userField['SETTINGS']['DEFAULT_VALUE']
53 : null
54 ;
55
56 return [
57 'SIZE' => ($size <= 1 ? 20 : ($size > 255 ? 225 : $size)),
58 'MIN_VALUE' => $min,
59 'MAX_VALUE' => $max,
60 'DEFAULT_VALUE' => $default
61 ];
62 }
63
69 public static function getFilterData(?array $userField, array $additionalSettings): array
70 {
71 return [
72 'id' => $additionalSettings['ID'],
73 'name' => $additionalSettings['NAME'],
74 'type' => 'number',
75 'filterable' => ''
76 ];
77 }
78
84 public static function checkFields(array $userField, $value): array
85 {
86 $fieldName = HtmlFilter::encode(
87 $userField['EDIT_FORM_LABEL'] <> ''
88 ? $userField['EDIT_FORM_LABEL'] : $userField['FIELD_NAME']
89 );
90
91 $msg = [];
92 if(
93 $value !== ''
94 && $userField['SETTINGS']['MIN_VALUE'] != 0
95 && (int)$value < $userField['SETTINGS']['MIN_VALUE']
96 )
97 {
98 $msg[] = [
99 'id' => $userField['FIELD_NAME'],
100 'text' => Loc::getMessage('USER_TYPE_INTEGER_MIN_VALUE_ERROR',
101 [
102 '#FIELD_NAME#' => $fieldName,
103 '#MIN_VALUE#' => $userField['SETTINGS']['MIN_VALUE']
104 ]
105 ),
106 ];
107 }
108 if(
109 $value !== ''
110 && $userField['SETTINGS']['MAX_VALUE'] != 0
111 && (int)$value > $userField['SETTINGS']['MAX_VALUE']
112 )
113 {
114 $msg[] = [
115 'id' => $userField['FIELD_NAME'],
116 'text' => Loc::getMessage('USER_TYPE_INTEGER_MAX_VALUE_ERROR',
117 [
118 '#FIELD_NAME#' => $fieldName,
119 '#MAX_VALUE#' => $userField['SETTINGS']['MAX_VALUE']
120 ]
121 ),
122 ];
123 }
124 if(
125 $value != ''
126 && !preg_match('/^[-+]?\d+$/', $value)
127 )
128 {
129 $msg[] = [
130 'id' => $userField['FIELD_NAME'],
131 'text' => Loc::getMessage('USER_TYPE_INTEGER_TYPE_ERROR',
132 [
133 '#FIELD_NAME#' => $fieldName
134 ]
135 ),
136 ];
137 }
138
139 return $msg;
140 }
141
146 public static function onSearchIndex(array $userField): ?string
147 {
148 if(is_array($userField['VALUE']))
149 {
150 $result = implode('\r\n', $userField['VALUE']);
151 }
152 else
153 {
154 $result = $userField['VALUE'];
155 }
156 return $result;
157 }
158}
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)