Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
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'] ?? null);
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 $currency = trim((string)$currency);
174
175 return $value . static::DB_SEPARATOR . $currency;
176 }
177
182 public static function unFormatFromDb(?string $value): array
183 {
184 if ($value === null || $value === '')
185 {
186 return [
187 '',
188 '',
189 ];
190 }
191
192 $result = explode(static::DB_SEPARATOR, $value);
193 if (count($result) === 1)
194 {
195 $result[] = '';
196 }
197
198 return $result;
199 }
200
201 private static function checkValueFormat(array $userField, $value): bool
202 {
203 if ($value === '' || $value === null)
204 {
205 return true;
206 }
207
208 $isStrictFormat = self::isStrictFormat($userField);
209
210 if (!$isStrictFormat)
211 {
212 if (
213 is_int($value)
214 || is_float($value)
215 )
216 {
217 $value = (string)$value;
218 }
219 }
220
221 if (!is_string($value))
222 {
223 return false;
224 }
225
226 $format = $isStrictFormat
227 ? self::STRICT_FIELD_FORMAT
228 : self::LIGHT_FIELD_FORMAT
229 ;
230 $prepared = [];
231 if (!preg_match($format, $value, $prepared))
232 {
233 return false;
234 }
235
236 return true;
237 }
238
239 private static function isStrictFormat(array $userField): bool
240 {
241 return ($userField['SETTINGS']['STRICT_FORMAT'] ?? 'N') === 'Y';
242 }
243}
static onBeforeSave(array $userField, $value)
static formatToDb(string $value, ?string $currency)
static checkFields(array $userField, $value)
Definition moneytype.php:39
static prepareSettings(array $userField)
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)