1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
moneytype.php
См. документацию.
1<?php
2
4
10use CUserTypeManager;
11
12class MoneyType extends BaseType
13{
14 public const
15 USER_TYPE_ID = 'money',
17 RENDER_COMPONENT = 'bitrix:currency.field.money';
18
19 private const STRICT_FIELD_FORMAT = '/^\-?[0-9]+\.?[0-9]*\|[A-Z]{3}$/';
20 private const LIGHT_FIELD_FORMAT = '/^\-?[0-9]+\.?[0-9]*(\|[A-Z]{3})?$/';
21
22 public static function getDescription(): array
23 {
24 return [
25 'DESCRIPTION' => Loc::getMessage('USER_TYPE_MONEY_DESCRIPTION'),
26 'BASE_TYPE' => CUserTypeManager::BASE_TYPE_STRING,
27 ];
28 }
29
33 public static function getDbColumnType(): string
34 {
35 return 'varchar(200)';
36 }
37
38 public static function checkFields(array $userField, $value): array
39 {
40 $fieldName = HtmlFilter::encode(
41 $userField['EDIT_FORM_LABEL'] ?: $userField['FIELD_NAME']
42 );
43
44 $result = [];
45
46 if ($userField['MULTIPLE'] === 'N')
47 {
48 if (!self::checkValueFormat($userField, $value))
49 {
50 $result[] = [
51 'id' => $userField['FIELD_NAME'],
52 'text' => Loc::getMessage('USER_TYPE_MONEY_ERR_BAD_SINGLE_FORMAT',
53 [
54 '#FIELD_NAME#' => $fieldName,
55 ]
56 ),
57 ];
58 }
59 }
60 else
61 {
62 if (is_array($value))
63 {
64 foreach ($value as $row)
65 {
66 if (
67 !is_string($row)
68 || !self::checkValueFormat($userField, $row)
69 )
70 {
71 $result[] = [
72 'id' => $userField['FIELD_NAME'],
73 'text' => Loc::getMessage('USER_TYPE_MONEY_ERR_BAD_ROW_FORMAT',
74 [
75 '#FIELD_NAME#' => $fieldName,
76 ]
77 ),
78 ];
79 break;
80 }
81 }
82 }
83 else
84 {
85 if (!self::checkValueFormat($userField, $value))
86 {
87 $result[] = [
88 'id' => $userField['FIELD_NAME'],
89 'text' => Loc::getMessage('USER_TYPE_MONEY_ERR_BAD_SINGLE_FORMAT',
90 [
91 '#FIELD_NAME#' => $fieldName,
92 ]
93 ),
94 ];
95 }
96 }
97 }
98
99 return $result;
100 }
101
107 public static function onBeforeSave(array $userField, $value): string
108 {
109 if ($value === '' || $value === null)
110 {
111 return '';
112 }
113
114 if (!self::checkValueFormat($userField, $value))
115 {
116 return '';
117 }
118
119 [$value, $currency] = static::unFormatFromDb($value);
120
121 if ($value !== '')
122 {
123 if (!$currency)
124 {
125 if (self::isStrictFormat($userField))
126 {
127 return '';
128 }
129 $currency = CurrencyManager::getBaseCurrency();
130 }
131
132 return static::formatToDB($value, $currency);
133 }
134
135 return '';
136 }
137
144 public static function prepareSettings(array $userField): array
145 {
146 [$value, $currency] = static::unFormatFromDb($userField['SETTINGS']['DEFAULT_VALUE'] ?? null);
147 if ($value !== '')
148 {
149 if($currency === '')
150 {
151 $currency = CurrencyManager::getBaseCurrency();
152 }
153 $value = static::formatToDB($value, $currency);
154 }
155
156 return [
157 'DEFAULT_VALUE' => $value,
158 ];
159 }
160
168 public static function formatToDb(string $value, ?string $currency): string
169 {
170 $value = trim($value);
171 if ($value === '')
172 {
173 return '';
174 }
175
176 $value = (string)Editor::prepareValue($value);
177
178 $currency = trim((string)$currency);
179
180 return $value . static::DB_SEPARATOR . $currency;
181 }
182
189 public static function unFormatFromDb(?string $value): array
190 {
191 if ($value === null || $value === '')
192 {
193 return [
194 '',
195 '',
196 ];
197 }
198
199 $result = explode(static::DB_SEPARATOR, $value);
200 if (count($result) === 1)
201 {
202 $result[] = '';
203 }
204
205 return $result;
206 }
207
208 private static function checkValueFormat(array $userField, $value): bool
209 {
210 if ($value === '' || $value === null)
211 {
212 return true;
213 }
214
215 $isStrictFormat = self::isStrictFormat($userField);
216
217 if (!$isStrictFormat)
218 {
219 if (
220 is_int($value)
221 || is_float($value)
222 )
223 {
224 $value = (string)$value;
225 }
226 }
227
228 if (!is_string($value))
229 {
230 return false;
231 }
232
233 $format = $isStrictFormat
234 ? self::STRICT_FIELD_FORMAT
235 : self::LIGHT_FIELD_FORMAT
236 ;
237 $prepared = [];
238 if (!preg_match($format, $value, $prepared))
239 {
240 return false;
241 }
242
243 return true;
244 }
245
246 private static function isStrictFormat(array $userField): bool
247 {
248 return ($userField['SETTINGS']['STRICT_FORMAT'] ?? 'N') === 'Y';
249 }
250}
static onBeforeSave(array $userField, $value)
Определения moneytype.php:107
static formatToDb(string $value, ?string $currency)
Определения moneytype.php:168
static unFormatFromDb(?string $value)
Определения moneytype.php:189
static checkFields(array $userField, $value)
Определения moneytype.php:38
static prepareSettings(array $userField)
Определения moneytype.php:144
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$currency
Определения template.php:266