Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
doubletype.php
1<?php
2
4
7use CUserTypeManager;
8
9Loc::loadMessages(__FILE__);
10
15class DoubleType extends BaseType
16{
17 public const
18 USER_TYPE_ID = 'double',
19 RENDER_COMPONENT = 'bitrix:main.field.double';
20
24 public static function getDescription(): array
25 {
26 return [
27 'DESCRIPTION' => GetMessage('USER_TYPE_DOUBLE_DESCRIPTION'),
28 'BASE_TYPE' => CUserTypeManager::BASE_TYPE_DOUBLE,
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\FloatField('x'));
40 }
41
46 public static function prepareSettings(array $userField): array
47 {
48 $prec = (int)$userField['SETTINGS']['PRECISION'];
49 $size = (int)$userField['SETTINGS']['SIZE'];
50 $min = (double)$userField['SETTINGS']['MIN_VALUE'];
51 $max = (double)$userField['SETTINGS']['MAX_VALUE'];
52
53 return [
54 'PRECISION' => ($prec < 0 ? 0 : ($prec > 12 ? 12 : $prec)),
55 'SIZE' => ($size <= 1 ? 20 : ($size > 255 ? 225 : $size)),
56 'MIN_VALUE' => $min,
57 'MAX_VALUE' => $max,
58 'DEFAULT_VALUE' => $userField['SETTINGS']['DEFAULT_VALUE'] <> ''
59 ? (double)$userField['SETTINGS']['DEFAULT_VALUE']
60 : null
61 ,
62 ];
63 }
64
70 public static function getFilterData(?array $userField, array $additionalSettings): array
71 {
72 return [
73 'id' => $additionalSettings['ID'],
74 'name' => $additionalSettings['NAME'],
75 'type' => 'number',
76 'filterable' => ''
77 ];
78 }
79
85 public static function checkFields(array $userField, $value): array
86 {
87 $msg = [];
88
89 $value = str_replace([',', ' '], ['.', ''], $value);
90
91 $fieldName = HtmlFilter::encode(
92 $userField['EDIT_FORM_LABEL'] <> ''
93 ? $userField['EDIT_FORM_LABEL'] : $userField['FIELD_NAME']
94 );
95
96 if(
97 mb_strlen($value)
98 && $userField['SETTINGS']['MIN_VALUE'] != 0
99 && (double)$value < $userField['SETTINGS']['MIN_VALUE']
100 )
101 {
102 $msg[] = [
103 'id' => $userField['FIELD_NAME'],
104 'text' => Loc::getMessage(
105 'USER_TYPE_DOUBLE_MIN_VALUE_ERROR',
106 [
107 '#FIELD_NAME#' => $fieldName,
108 '#MIN_VALUE#' => $userField['SETTINGS']['MIN_VALUE']
109 ]
110 )
111 ];
112 }
113 if(
114 mb_strlen($value)
115 && $userField['SETTINGS']['MAX_VALUE'] != 0
116 && (double)$value > $userField['SETTINGS']['MAX_VALUE']
117 )
118 {
119 $msg[] = [
120 'id' => $userField['FIELD_NAME'],
121 'text' => Loc::getMessage(
122 'USER_TYPE_DOUBLE_MAX_VALUE_ERROR',
123 [
124 '#FIELD_NAME#' => $fieldName,
125 '#MAX_VALUE#' => $userField['SETTINGS']['MAX_VALUE']
126 ]
127 ),
128 ];
129 }
130 if(
131 $value != ''
132 && !preg_match('/^[-+]?\d*[.,]?\d+?$/', $value)
133 )
134 {
135 $msg[] = [
136 'id' => $userField['FIELD_NAME'],
137 'text' => Loc::getMessage('USER_TYPE_DOUBLE_TYPE_ERROR',
138 [
139 '#FIELD_NAME#' => $fieldName
140 ]
141 ),
142 ];
143 }
144 return $msg;
145 }
146
151 public static function onSearchIndex(array $userField): ?string
152 {
153 if(is_array($userField['VALUE']))
154 {
155 $result = implode('\r\n', $userField['VALUE']);
156 }
157 else
158 {
159 $result = $userField['VALUE'];
160 }
161 return $result;
162 }
163
169 public static function onBeforeSave(array $userField, $value)
170 {
171 $value = str_replace([',', ' '], ['.', ''], $value);
172 if($value <> '')
173 {
174 return (string) round((double)$value, $userField['SETTINGS']['PRECISION']);
175 }
176 return null;
177 }
178}
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 onBeforeSave(array $userField, $value)
static onSearchIndex(array $userField)
static checkFields(array $userField, $value)
static prepareSettings(array $userField)