1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
iblockmoneyproperty.php
См. документацию.
1<?php
2
4
10
11class IblockMoneyProperty
12{
13 public const USER_TYPE = 'Money';
14 public const SEPARATOR = '|';
15
21 public static function getUserTypeDescription()
22 {
23 $className = get_called_class();
24 return [
25 'PROPERTY_TYPE' => 'S',
26 'USER_TYPE' => self::USER_TYPE,
27 'DESCRIPTION' => Loc::getMessage('CIMP_INPUT_DESCRIPTION'),
28 'GetPublicEditHTML' => [$className, 'getPublicEditHTML'],
29 'GetPublicViewHTML' => [$className, 'getPublicViewHTML'],
30 'GetPropertyFieldHtml' => [$className, 'getPropertyFieldHtml'],
31 'GetAdminListViewHTML' => [$className, 'getAdminListViewHTML'],
32 'GetUIEntityEditorProperty' => [$className, 'GetUIEntityEditorProperty'],
33 'getFormattedValue' => [$className, 'getSeparatedValues'],
34 'CheckFields' => [$className, 'checkFields'],
35 'GetLength' => [$className, 'getLength'],
36 'ConvertToDB' => [$className, 'convertToDB'],
37 'ConvertFromDB' => [$className, 'convertFromDB'],
38 'AddFilterFields' => [$className, 'addFilterFields'],
39 ];
40 }
41
50 public static function getPublicEditHTML($property, $value, $controlSettings)
51 {
52 return self::getPropertyFieldHtml($property, $value, $controlSettings);
53 }
54
63 public static function getPublicViewHTML($property, $value, $controlSettings)
64 {
65 return self::getAdminListViewHTML($property, $value, $controlSettings);
66 }
67
76 public static function getPropertyFieldHtml($property, $value, $controlSettings)
77 {
78 $randString = mb_strtolower(\Bitrix\Main\Security\Random::getString(6));
79
80 $explode = (is_string($value['VALUE']) ? explode(self::SEPARATOR, $value['VALUE']) : []);
81 $currentValue = (isset($explode[0]) && $explode[0] !== '' ? $explode[0] : '');
82 $currentCurrency = ($explode[1] ?? '');
83
84 $html = '<input type="text" style="width: auto;" value="'.htmlspecialcharsbx($currentValue).
85 '" id="input-'.$randString.'">';
86 $html .= '<input type="hidden" id="hidden-'.$randString.'" name="'.
87 htmlspecialcharsbx($controlSettings['VALUE']).'" value="'.htmlspecialcharsbx($value["VALUE"]).'">';
88 $listCurrency = self::getListCurrency();
89 if($listCurrency)
90 {
91 if (($property['MULTIPLE'] ?? null) === 'Y')
92 {
93 $html .= '<input type="hidden" data-id="'.$randString.'">';
94 }
95 $html .= '<select id="selector-'.$randString.'" style="width: auto; margin: 0 5px;">';
96 foreach($listCurrency as $currency)
97 {
98 $selected = ($currentCurrency == $currency['CURRENCY'] ||
99 (!$currentCurrency && $currency['BASE'] == 'Y')) ? 'selected' : '';
100 $html .= '<option '.$selected.' value="'.$currency['CURRENCY'].'">'.
101 htmlspecialcharsbx($currency['NAME']).'</option>';
102 }
103 $html .= '</select>';
104 $html .= '<span id="error-'.$randString.'" style="color: red;"></span>';
105 $html .= self::getJsHandlerSelector($randString, $listCurrency);
106 }
107
108 return $html;
109 }
110
119 public static function getAdminListViewHTML($property, $value, $controlSettings)
120 {
121 $explode = is_string($value['VALUE']) ? explode(self::SEPARATOR, $value['VALUE']) : [];
122 $currentValue = (isset($explode[0]) && $explode[0] !== '' ? $explode[0] : '');
123 $currentCurrency = $explode[1] ?? '';
124
125 if (!$currentCurrency)
126 return is_numeric($currentValue) ? $currentValue : '';
127
128 if (CurrencyManager::isCurrencyExist($currentCurrency))
129 {
130 if(!empty($controlSettings['MODE']))
131 {
132 switch($controlSettings['MODE'])
133 {
134 case 'CSV_EXPORT':
135 return $value['VALUE'];
136 case 'ELEMENT_TEMPLATE':
137 case 'SIMPLE_TEXT':
138 return $currentValue;
139 }
140 }
141
142 list($currentValue, $currentCurrency, $decimalsValue) = array_values(self::getSeparatedValues($value['VALUE']));
143 $currentValue = $currentValue.'.'.$decimalsValue;
144
145 return \CCurrencyLang::CurrencyFormat($currentValue, $currentCurrency, true);
146 }
147
148 return '';
149 }
150
158 public static function checkFields($property, $value)
159 {
160 $result = [];
161 if(empty($value['VALUE'])) return $result;
162 $explode = (is_string($value['VALUE']) ? explode(self::SEPARATOR, $value['VALUE']) : []);
163 $currentValue = (isset($explode[0]) && $explode[0] !== '' ? $explode[0] : '');
164 $currentCurrency = ($explode[1] ?? '');
165
166 if(!$currentCurrency)
167 return is_numeric($currentValue) ? $result : array(Loc::getMessage('CIMP_FORMAT_ERROR'));
168
169 if(CurrencyManager::isCurrencyExist($currentCurrency))
170 {
171 $format = \CCurrencyLang::GetFormatDescription($currentCurrency);
172 $decPoint = $format['DEC_POINT'];
173 $thousandsSep = $format['THOUSANDS_SEP'];
174 $decimals = $format['DECIMALS'];
175 $regExp = '/^\d{1,3}(('.$thousandsSep.'){0,1}\d{3})*(\\'.$decPoint.'\d{1,'.$decimals.'})?$/';
176 if($thousandsSep && $decPoint)
177 {
178 if ($decimals > 0)
179 {
180 $regExp = '/^\d{1,3}(('.$thousandsSep.'){0,1}\d{3})*(\\'.$decPoint.'\d{1,'.$decimals.'})?$/';
181 }
182 else
183 {
184 $regExp = '/^\d{1,3}(('.$thousandsSep.'){0,1}\d{3})*$/';
185 }
186 }
187 elseif($thousandsSep && !$decPoint)
188 {
189 $regExp = '/^\d{1,3}(('.$thousandsSep.'){0,1}\d{3})*$/';
190 }
191 elseif(!$thousandsSep && $decPoint)
192 {
193 if ($decimals > 0)
194 {
195 $regExp = '/^[0-9]*(\\'.$decPoint.'\d{1,'.$decimals.'})?$/';
196 }
197 else
198 {
199 $regExp = '/^[0-9]*$/';
200 }
201 }
202 elseif(!$thousandsSep && !$decPoint)
203 {
204 $regExp = '/^[0-9]*$/';
205 }
206 if(!preg_match($regExp, $currentValue))
207 {
208 $result[] = Loc::getMessage('CIMP_FORMAT_ERROR');
209 }
210 }
211 else
212 {
213 $result[] = Loc::getMessage('CIMP_FORMAT_ERROR');
214 }
215
216 return $result;
217 }
218
226 public static function getLength($property, $value)
227 {
228 return mb_strlen(trim($value['VALUE'], "\n\r\t"));
229 }
230
238 public static function convertToDB($property, $value)
239 {
240 return $value;
241 }
242
250 public static function convertFromDB($property, $value)
251 {
252 return $value;
253 }
254
255 public static function getSeparatedValues($value)
256 {
257 $explode = is_string($value) ? explode(self::SEPARATOR, $value) : [];
258 $currentValue = (isset($explode[0]) && $explode[0] !== '' ? $explode[0] : '');
259 $currentCurrency = $explode[1] ?? '';
260 $format = \CCurrencyLang::GetFormatDescription($currentCurrency);
261 $explode = explode($format['DEC_POINT'], $currentValue);
262 $currentValue = ($explode[0] !== '' ? $explode[0] : '');
263 $decimalsValue = $explode[1] ?? '';
264 return array(
265 'AMOUNT' => $currentValue,
266 'CURRENCY' => $currentCurrency,
267 'DECIMALS' => $decimalsValue
268 );
269 }
270
271 public static function getUnitedValue($amount, string $currency): string
272 {
273 return
274 ($amount === '')
275 ? ''
276 : $amount.self::SEPARATOR.$currency
277 ;
278 }
279
289 public static function addFilterFields($property, $controlSettings, &$filter, &$filtered)
290 {
291 $filtered = false;
292
293 if(isset($_REQUEST[$controlSettings['VALUE']]))
294 {
295 $value = $_REQUEST[$controlSettings['VALUE']];
296 }
297 elseif(isset($controlSettings["FILTER_ID"]))
298 {
299 $filterOption = new \Bitrix\Main\UI\Filter\Options($controlSettings["FILTER_ID"]);
300 $filterData = $filterOption->getFilter();
301 if(!empty($filterData[$controlSettings['VALUE']]))
302 $value = $filterData[$controlSettings['VALUE']];
303 }
304
305 if(!empty($value))
306 {
307 $explode = explode(self::SEPARATOR, $value);
308 if(empty($explode[1]))
309 {
310 $listCurrency = self::getListCurrency();
311 if($listCurrency)
312 {
313 $filter[$controlSettings['VALUE']] = array();
314 foreach($listCurrency as $currencyType => $currency)
315 {
316 $filter[$controlSettings['VALUE']][] = $value.self::SEPARATOR.$currencyType;
317 }
318 }
319 }
320 $filtered = true;
321 }
322 }
323
324 protected static function getListCurrency(): array
325 {
326 return Editor::getListCurrency();
327 }
328
329 protected static function getJsHandlerSelector($randString, array $listCurrency)
330 {
331 ob_start();
332 ?>
333 <script>
334 if (!window.BX && top.BX)
335 window.BX = top.BX;
336 BX.ready(function() {
337 'use strict';
338 if(!BX.HandlerMoneyField) {
339 BX.HandlerMoneyField = function(params) {
340 this.randomString = params.randomString;
341 this.listCurrency = params.listCurrency;
342 this.defaultSeparator = params.defaultSeparator;
343 setTimeout(BX.proxy(this.setDefaultParams, this), 300);
344 };
345 BX.HandlerMoneyField.prototype.setDefaultParams = function() {
346 this.input = BX('input-' + this.randomString);
347 this.hidden = BX('hidden-' + this.randomString);
348 this.selector = BX('selector-' + this.randomString);
349 this.error = BX('error-' + this.randomString);
350 this.init();
351 };
352 BX.HandlerMoneyField.prototype.init = function() {
353 if (!this.input || !this.selector || !this.hidden) return;
354 this.currentCurrency = this.selector.value;
355 this.currentFormat = null;
356 this.oldFormat = {
357 'currentCurrency': this.currentCurrency,
358 'decPoint': this.listCurrency[this.currentCurrency].DEC_POINT,
359 'thousandsSep': this.listCurrency[this.currentCurrency].SEPARATOR
360 };
361 this.availableKey = [8,9,13,36,37,39,38,40,46,18,17,16,188,190,86,65,112,113,114,115,116,117,118,
362 119,120,121,122,123,67,45,34,33,35];
363 this.availableSymbol = [];
364 this.changeCurrency();
365 BX.bind(this.selector, 'change', BX.proxy(this.changeCurrency, this));
366 BX.bind(this.input, 'keydown', BX.proxy(function(event) {this.onkeydown(event)}, this));
367 BX.bind(this.input, 'keyup', BX.proxy(this.onkeyup, this));
368 BX.bind(this.input, 'blur', BX.proxy(this.onblur, this));
369 BX.bind(this.input, 'focus', BX.proxy(this.onfocus, this));
370 BX.addCustomEvent(window, 'onAddNewRowBeforeInner', BX.proxy(function(htmlObject) {
371 var regExp = new RegExp('data-id=".+?"', 'g'), oldId;
372 var match = htmlObject.html.match(regExp);
373 if(match) match = match[0].match(/"([^"]*)"/i);
374 if(match) oldId = match[1];
375 if(this.randomString == oldId)
376 {
377 var newId = BX.util.getRandomString(6).toLowerCase();
378 htmlObject.html = htmlObject.html.replace(new RegExp(oldId, 'g'), newId);
379 }
380 }, this));
381 };
382 BX.HandlerMoneyField.prototype.changeCurrency = function() {
383 this.currentCurrency = (BX.proxy_context && BX.proxy_context.value) ?
384 BX.proxy_context.value : this.selector.value;
385 this.currentFormat = this.listCurrency[this.currentCurrency].FORMAT;
386 this.decPoint = this.listCurrency[this.currentCurrency].DEC_POINT;
387 this.thousandsSep = this.listCurrency[this.currentCurrency].SEPARATOR;
388 this.decimals = this.listCurrency[this.currentCurrency].DECIMALS;
389 if(this.realValue)
390 {
391 var regExp = this.oldFormat.thousandsSep ? '\\'+this.oldFormat.thousandsSep : '';
392 this.realValue = this.realValue.replace(new RegExp(regExp, 'g'),'');
393 this.input.value = this.oldFormat.decPoint ?
394 this.realValue.replace(this.oldFormat.decPoint,this.decPoint) : this.realValue;
395 }
396 this.oldFormat = {
397 'currentCurrency': this.currentCurrency,
398 'decPoint': this.decPoint,
399 'thousandsSep': this.thousandsSep
400 };
401 this.setRealValue();
402 this.setVisibleValue(true);
403 this.setHiddenValue();
404 var decimals = '';
405 for(var i = 1; i <= this.decimals; i++)
406 decimals += i;
407 this.availableSymbol.push(this.thousandsSep);
408 this.availableSymbol.push(this.decPoint);
409 this.regExp = '';
410 this.isDecimalsNull = (!parseInt(this.decimals));
411 if(this.thousandsSep && this.decPoint)
412 {
413 if (this.isDecimalsNull)
414 {
415 this.regExp = '^\\d{1,3}('+this.thousandsSep+'?\\d{3})*$';
416 this.exampleValue = '6'+this.thousandsSep+'456';
417 }
418 else
419 {
420 this.regExp = '^\\d{1,3}('+this.thousandsSep+
421 '?\\d{3})*(\\'+this.decPoint+'\\d{1,'+this.decimals+'})?$';
422 this.exampleValue = '6'+this.thousandsSep+'456'+this.decPoint+decimals;
423 }
424 }
425 else if(this.thousandsSep && !this.decPoint)
426 {
427 this.regExp = '^\\d{1,3}('+this.thousandsSep+'?\\d{3})*$';
428 this.exampleValue = '6'+this.thousandsSep+'456';
429 }
430 else if(!this.thousandsSep && this.decPoint)
431 {
432 if (this.isDecimalsNull)
433 {
434 this.regExp = '^[0-9]*$';
435 this.exampleValue = '6456';
436 }
437 else
438 {
439 this.regExp = '^[0-9]*(\\'+this.decPoint+'\\d{1,'+this.decimals+'})?$';
440 this.exampleValue = '6456'+this.decPoint+decimals;
441 }
442 }
443 else if(!this.thousandsSep && !this.decPoint)
444 {
445 this.regExp = '^[0-9]*$';
446 this.exampleValue = '6456';
447 }
448 this.checkFormatValue();
449 };
450 BX.HandlerMoneyField.prototype.onkeydown = function(event) {
451 this.setTextError();
452 if (!BX.util.in_array(event.key, this.availableSymbol)
453 && !BX.util.in_array(event.keyCode,this.availableKey)) {
454 if (isNaN(parseInt(event.key))) {
455 this.setTextError(BX.message('CIMP_INPUT_FORMAT_ERROR')
456 .replace('#example#', this.exampleValue));
457 return BX.PreventDefault(event);
458 }
459 }
460 };
461 BX.HandlerMoneyField.prototype.onkeyup = function() {
462 this.setRealValue();
463 this.setHiddenValue();
464 this.setVisibleValue(true);
465 };
466 BX.HandlerMoneyField.prototype.onblur = function() {
467 this.setRealValue();
468 this.setHiddenValue();
469 this.setVisibleValue(true);
470 this.setTextError();
471 this.checkFormatValue();
472 };
473 BX.HandlerMoneyField.prototype.onfocus = function() {
474 this.input.value = this.realValue;
475 };
476 BX.HandlerMoneyField.prototype.getTemplateValue = function() {
477 return this.currentFormat.replace(new RegExp('(^|[^&])#'), '$1' + this.realValue);
478 };
479 BX.HandlerMoneyField.prototype.checkFormatValue = function() {
480 if(!this.realValue) return;
481 var regExp = new RegExp(this.regExp);
482 if(!this.realValue.match(regExp))
483 {
484 this.setTextError(BX.message('CIMP_INPUT_FORMAT_ERROR')
485 .replace('#example#', this.exampleValue));
486 }
487 else
488 {
489 this.setTextError();
490 }
491 };
492 BX.HandlerMoneyField.prototype.setTextError = function(text) {
493 this.error.innerHTML = text ? text : '';
494 };
495 BX.HandlerMoneyField.prototype.setRealValue = function() {
496 this.realValue = this.getFormatValue();
497 };
498 BX.HandlerMoneyField.prototype.setVisibleValue = function(useTemplate) {
499 this.input.value = this.input.value ? !useTemplate ?
500 this.getTemplateValue() : this.getFormatValue(): '';
501 };
502 BX.HandlerMoneyField.prototype.setHiddenValue = function() {
503 if (this.input.value)
504 {
505 var regExp = this.thousandsSep ? '\\'+this.thousandsSep : '';
506 this.hidden.value = this.realValue.replace(new RegExp(regExp, 'g'), '') +
507 this.defaultSeparator + this.currentCurrency;
508 }
509 else
510 {
511 this.hidden.value = '';
512 }
513 };
514 BX.HandlerMoneyField.prototype.getFormatValue = function() {
515 var baseValue = this.input.value;
516 var valueLength = baseValue.length;
517 var formatValue = "";
518 var regExp;
519 if(this.thousandsSep == ',' || this.thousandsSep == '.')
520 regExp = new RegExp('['+this.decPoint+']');
521 else
522 regExp = new RegExp('['+this.decPoint+',.]');
523 var decPointPosition = baseValue.match(regExp);
524 decPointPosition = decPointPosition == null ? baseValue.length : decPointPosition.index;
525 var countDigit = 0;
526 for(var i = 0; i < baseValue.length; i++)
527 {
528 var symbolPosition = baseValue.length-1-i;
529 var symbol = baseValue.charAt(symbolPosition);
530 var isDigit = ('0123456789'.indexOf(symbol) >= 0);
531 if(isDigit) countDigit++;
532 if(symbolPosition == decPointPosition) countDigit = 0;
533 if(symbolPosition >= decPointPosition)
534 {
535 if(this.decPoint == '.' && symbol == ',')
536 symbol = this.decPoint;
537 if(this.decPoint == ',' && symbol == '.')
538 symbol = this.decPoint;
539 if(isDigit || (symbolPosition == decPointPosition && symbol == this.decPoint))
540 formatValue = symbol + formatValue;
541 else
542 if(valueLength > symbolPosition) valueLength--;
543 }
544 if(symbolPosition < decPointPosition)
545 {
546 if(isDigit)
547 formatValue = symbol+formatValue;
548 else
549 if(valueLength > symbolPosition) valueLength--;
550 if(isDigit && countDigit % 3 == 0 && countDigit !== 0 && symbolPosition !== 0)
551 {
552 formatValue = this.thousandsSep + formatValue;
553 if(valueLength >= symbolPosition) valueLength++;
554 }
555 }
556 }
557 if(this.decimals > 0)
558 {
559 decPointPosition = formatValue.match(new RegExp('['+this.decPoint+']'));
560 decPointPosition = decPointPosition == null ? formatValue.length : decPointPosition.index;
561 while(formatValue.length-1-decPointPosition > this.decimals)
562 {
563 if(valueLength >= formatValue.length-1) valueLength--;
564 formatValue = formatValue.substr(0, formatValue.length - 1);
565 }
566 }
567
568 return formatValue;
569 };
570 }
571 var handlerMoneyField = new BX.HandlerMoneyField({
572 randomString: '<?=$randString?>',
573 defaultSeparator: '<?=self::SEPARATOR?>',
574 listCurrency: <?=Json::encode($listCurrency)?>
575 });
576 BX.message({
577 CIMP_INPUT_FORMAT_ERROR: '<?=GetMessageJS('CIMP_INPUT_FORMAT_ERROR')?>'
578 });
579 });
580 </script>
581 <?php
582 $script = ob_get_contents();
583 ob_end_clean();
584 return $script;
585 }
586
587 public static function GetUIEntityEditorProperty($settings, $value)
588 {
589 if (method_exists(BaseForm::class, 'getAdditionalMoneyValues'))
590 {
591 return [
592 'type' => $settings['MULTIPLE'] === 'Y' ? 'multimoney' : 'money',
593 ];
594 }
595
596 return [
597 'type' => 'money',
598 ];
599 }
600}
Определения json.php:9
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
bx_acc_lim_group_list limitGroupList[] multiple<?=$group[ 'ID']?> ID selected margin top
Определения file_new.php:657
$_REQUEST["admin_mnu_menu_id"]
Определения get_menu.php:8
$result
Определения get_property_values.php:14
$filter
Определения iblock_catalog_list.php:54
htmlspecialcharsbx($string, $flags=ENT_COMPAT, $doubleEncode=true)
Определения tools.php:2701
$value
Определения Param.php:39
if(!function_exists("bx_hmac")) $amount
Определения payment.php:30
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$currency
Определения template.php:266