Bitrix-D7 22.6
 
Загрузка...
Поиск...
Не найдено
moneytype.php
1<?php
2
4
9use CUserTypeManager;
10
11Loc::loadLanguageFile(__FILE__);
12
13class MoneyType extends BaseType
14{
15 public const
16 USER_TYPE_ID = 'money',
18 RENDER_COMPONENT = 'bitrix:currency.field.money';
19
20 private const STRICT_FIELD_FORMAT = '/^\-?[0-9]+\.?[0-9]*\|[A-Z]{3}$/';
21 private const LIGHT_FIELD_FORMAT = '/^\-?[0-9]+\.?[0-9]*(\|[A-Z]{3})?$/';
22
23 public static function getDescription(): array
24 {
25 return [
26 'DESCRIPTION' => Loc::getMessage('USER_TYPE_MONEY_DESCRIPTION'),
27 'BASE_TYPE' => CUserTypeManager::BASE_TYPE_STRING,
28 ];
29 }
30
34 public static function getDbColumnType(): string
35 {
36 return 'varchar(200)';
37 }
38
39 public static function checkFields(array $userField, $value): array
40 {
41 $fieldName = HtmlFilter::encode(
42 $userField['EDIT_FORM_LABEL'] ?: $userField['FIELD_NAME']
43 );
44
45 $result = [];
46
47 if ($userField['MULTIPLE'] === 'N')
48 {
49 if (!self::checkValueFormat($userField, $value))
50 {
51 $result[] = [
52 'id' => $userField['FIELD_NAME'],
53 'text' => Loc::getMessage('USER_TYPE_MONEY_ERR_BAD_SINGLE_FORMAT',
54 [
55 '#FIELD_NAME#' => $fieldName,
56 ]
57 ),
58 ];
59 }
60 }
61 else
62 {
63 if (is_array($value))
64 {
65 foreach ($value as $row)
66 {
67 if (
68 !is_string($row)
69 || !self::checkValueFormat($userField, $row)
70 )
71 {
72 $result[] = [
73 'id' => $userField['FIELD_NAME'],
74 'text' => Loc::getMessage('USER_TYPE_MONEY_ERR_BAD_ROW_FORMAT',
75 [
76 '#FIELD_NAME#' => $fieldName,
77 ]
78 ),
79 ];
80 break;
81 }
82 }
83 }
84 else
85 {
86 if (!self::checkValueFormat($userField, $value))
87 {
88 $result[] = [
89 'id' => $userField['FIELD_NAME'],
90 'text' => Loc::getMessage('USER_TYPE_MONEY_ERR_BAD_SINGLE_FORMAT',
91 [
92 '#FIELD_NAME#' => $fieldName,
93 ]
94 ),
95 ];
96 }
97 }
98 }
99
100 return $result;
101 }
102
108 public static function onBeforeSave(array $userField, $value): string
109 {
110 if ($value === '' || $value === null)
111 {
112 return '';
113 }
114
115 if (!self::checkValueFormat($userField, $value))
116 {
117 return '';
118 }
119
120 [$value, $currency] = static::unFormatFromDb($value);
121
122 if ($value !== '')
123 {
124 if (!$currency)
125 {
126 if (self::isStrictFormat($userField))
127 {
128 return '';
129 }
130 $currency = CurrencyManager::getBaseCurrency();
131 }
132
133 return static::formatToDB($value, $currency);
134 }
135
136 return '';
137 }
138
143 public static function prepareSettings(array $userField): array
144 {
145 [$value, $currency] = static::unFormatFromDb($userField['SETTINGS']['DEFAULT_VALUE']);
146 if ($value !== '')
147 {
148 if($currency === '')
149 {
150 $currency = CurrencyManager::getBaseCurrency();
151 }
152 $value = static::formatToDB($value, $currency);
153 }
154
155 return [
156 'DEFAULT_VALUE' => $value,
157 ];
158 }
159
165 public static function formatToDb(string $value, ?string $currency): string
166 {
167 if($value === '')
168 {
169 return '';
170 }
171
172 $value = (string)((float)$value);
173
174 return $value . static::DB_SEPARATOR . trim($currency);
175 }
176
181 public static function unFormatFromDb(?string $value): array
182 {
183 if ($value === null)
184 {
185 return [
186 '',
187 '',
188 ];
189 }
190
191 return explode(static::DB_SEPARATOR, $value);
192 }
193
194 private static function checkValueFormat(array $userField, $value): bool
195 {
196 if ($value === '' || $value === null)
197 {
198 return true;
199 }
200
201 $isStrictFormat = self::isStrictFormat($userField);
202
203 if (!$isStrictFormat)
204 {
205 if (
206 is_int($value)
207 || is_float($value)
208 )
209 {
210 $value = (string)$value;
211 }
212 }
213
214 if (!is_string($value))
215 {
216 return false;
217 }
218
219 $format = $isStrictFormat
220 ? self::STRICT_FIELD_FORMAT
221 : self::LIGHT_FIELD_FORMAT
222 ;
223 $prepared = [];
224 if (!preg_match($format, $value, $prepared))
225 {
226 return false;
227 }
228
229 return true;
230 }
231
232 private static function isStrictFormat(array $userField): bool
233 {
234 return ($userField['SETTINGS']['STRICT_FORMAT'] ?? 'N') === 'Y';
235 }
236}
static onBeforeSave(array $userField, $value)
Definition: moneytype.php:108
static formatToDb(string $value, ?string $currency)
Definition: moneytype.php:165
static unFormatFromDb(?string $value)
Definition: moneytype.php:181
static checkFields(array $userField, $value)
Definition: moneytype.php:39
static prepareSettings(array $userField)
Definition: moneytype.php:143
static loadLanguageFile($file, $language=null, $normalize=true)
Definition: loc.php:224
static getMessage($code, $replace=null, $language=null)
Definition: loc.php:29
static encode($string, $flags=ENT_COMPAT, $doubleEncode=true)
Definition: htmlfilter.php:12