1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
currency_lang.php
См. документацию.
1<?php
2
7
10
12{
23
24 protected static array $arSeparators = [
30 ];
31
32 protected static array $arDefaultValues = [
33 'FORMAT_STRING' => '#',
35 'THOUSANDS_SEP' => ' ',
36 'DECIMALS' => 2,
38 'HIDE_ZERO' => 'N',
39 ];
40
41 protected static array $arCurrencyFormat = [];
42
43 protected static int $useHideZero = 0;
44
45 private static string $region;
46
47 public static function enableUseHideZero(): void
48 {
49 if (defined('ADMIN_SECTION') && ADMIN_SECTION === true)
50 {
51 return;
52 }
53 self::$useHideZero++;
54 }
55
56 public static function disableUseHideZero(): void
57 {
58 if (defined('ADMIN_SECTION') && ADMIN_SECTION === true)
59 {
60 return;
61 }
62 self::$useHideZero--;
63 }
64
65 public static function isAllowUseHideZero(): bool
66 {
67 return (!(defined('ADMIN_SECTION') && ADMIN_SECTION === true) && self::$useHideZero >= 0);
68 }
69
70 public static function checkFields($action, &$fields, $currency = '', $language = '', $getErrors = false)
71 {
72 global $DB, $USER, $APPLICATION;
73
74 $getErrors = ($getErrors === true);
75 $action = mb_strtoupper($action);
76 if ($action != 'ADD' && $action != 'UPDATE')
77 return false;
78 if (!is_array($fields))
79 return false;
80 if ($action == 'ADD')
81 {
82 if (isset($fields['CURRENCY']))
83 $currency = $fields['CURRENCY'];
84 if (isset($fields['LID']))
85 $language = $fields['LID'];
86 }
88 $language = Currency\CurrencyManager::checkLanguage($language);
89 if ($currency === false || $language === false)
90 return false;
91
92 $errorMessages = array();
93
94 $clearFields = array(
95 '~CURRENCY',
96 '~LID',
97 'TIMESTAMP_X',
98 'DATE_CREATE',
99 '~DATE_CREATE',
100 '~MODIFIED_BY',
101 '~CREATED_BY',
102 '~FORMAT_STRING',
103 '~FULL_NAME',
104 '~DEC_POINT',
105 '~THOUSANDS_SEP',
106 '~DECIMALS',
107 '~THOUSANDS_VARIANT',
108 '~HIDE_ZERO'
109 );
110 if ($action == 'UPDATE')
111 {
112 $clearFields[] = 'CREATED_BY';
113 $clearFields[] = 'CURRENCY';
114 $clearFields[] = 'LID';
115 }
116 $fields = array_filter($fields, 'CCurrencyLang::clearFields');
117 foreach ($clearFields as $fieldName)
118 {
119 if (isset($fields[$fieldName]))
120 unset($fields[$fieldName]);
121 }
122 unset($fieldName, $clearFields);
123
124 if ($action == 'ADD')
125 {
126 $defaultValues = self::$arDefaultValues;
127 unset($defaultValues['FORMAT_STRING']);
128
129 $fields = array_merge($defaultValues, $fields);
130 unset($defaultValues);
131
132 if (empty($fields['FORMAT_STRING']))
133 {
134 $errorMessages[] = array(
135 'id' => 'FORMAT_STRING', 'text' => Loc::getMessage('BT_CUR_LANG_ERR_FORMAT_STRING_IS_EMPTY', array('#LANG#' => $language))
136 );
137 }
138
139 if (empty($errorMessages))
140 {
141 $fields['CURRENCY'] = $currency;
142 $fields['LID'] = $language;
143 }
144 }
145
146 if (empty($errorMessages))
147 {
148 if (isset($fields['FORMAT_STRING']) && empty($fields['FORMAT_STRING']))
149 {
150 $errorMessages[] = array(
151 'id' => 'FORMAT_STRING', 'text' => Loc::getMessage('BT_CUR_LANG_ERR_FORMAT_STRING_IS_EMPTY', array('#LANG#' => $language))
152 );
153 }
154 if (isset($fields['DECIMALS']))
155 {
156 $fields['DECIMALS'] = (int)$fields['DECIMALS'];
157 if ($fields['DECIMALS'] < 0)
158 $fields['DECIMALS'] = self::$arDefaultValues['DECIMALS'];
159 }
160 $validateCustomSeparator = false;
161 if (isset($fields['THOUSANDS_VARIANT']))
162 {
163 if (empty($fields['THOUSANDS_VARIANT']) || !isset(self::$arSeparators[$fields['THOUSANDS_VARIANT']]))
164 {
165 $fields['THOUSANDS_VARIANT'] = false;
166 $validateCustomSeparator = true;
167 }
168 else
169 {
170 $fields['THOUSANDS_SEP'] = self::$arSeparators[$fields['THOUSANDS_VARIANT']];
171 }
172 }
173 else
174 {
175 if (isset($fields['THOUSANDS_SEP']))
176 $validateCustomSeparator = true;
177 }
178
179 if ($validateCustomSeparator)
180 {
181 if (!isset($fields['THOUSANDS_SEP']) || $fields['THOUSANDS_SEP'] == '')
182 {
183 $errorMessages[] = array(
184 'id' => 'THOUSANDS_SEP',
185 'text' => Loc::getMessage(
186 'BT_CUR_LANG_ERR_THOUSANDS_SEP_IS_EMPTY',
187 array('#LANG#' => $language)
188 )
189 );
190 }
191 else
192 {
193 if (!preg_match('/^&(#[x]?[0-9a-zA-Z]+|[a-zA-Z]+);$/', $fields['THOUSANDS_SEP']))
194 {
195 $errorMessages[] = array(
196 'id' => 'THOUSANDS_SEP',
197 'text' => Loc::getMessage(
198 'BT_CUR_LANG_ERR_THOUSANDS_SEP_IS_NOT_VALID',
199 array('#LANG#' => $language)
200 )
201 );
202 }
203 }
204 }
205 unset($validateCustomSeparator);
206
207 if (isset($fields['HIDE_ZERO']))
208 $fields['HIDE_ZERO'] = ($fields['HIDE_ZERO'] == 'Y' ? 'Y' : 'N');
209 }
210 $intUserID = 0;
211 $boolUserExist = CCurrency::isUserExists();
212 if ($boolUserExist)
213 $intUserID = (int)$USER->GetID();
214 $strDateFunction = $DB->GetNowFunction();
215 $fields['~TIMESTAMP_X'] = $strDateFunction;
216 if ($boolUserExist)
217 {
218 if (!isset($fields['MODIFIED_BY']))
219 $fields['MODIFIED_BY'] = $intUserID;
220 $fields['MODIFIED_BY'] = (int)$fields['MODIFIED_BY'];
221 if ($fields['MODIFIED_BY'] <= 0)
222 $fields['MODIFIED_BY'] = $intUserID;
223 }
224 if ($action == 'ADD')
225 {
226 $fields['~DATE_CREATE'] = $strDateFunction;
227 if ($boolUserExist)
228 {
229 if (!isset($arFields['CREATED_BY']))
230 $fields['CREATED_BY'] = $intUserID;
231 $fields['CREATED_BY'] = (int)$fields['CREATED_BY'];
232 if ($fields['CREATED_BY'] <= 0)
233 $fields['CREATED_BY'] = $intUserID;
234 }
235 }
236
237 if (empty($errorMessages))
238 {
239 if ($action == 'ADD')
240 {
241 if (!empty($fields['THOUSANDS_VARIANT']) && isset(self::$arSeparators[$fields['THOUSANDS_VARIANT']]))
242 {
243 if ($fields['DEC_POINT'] == self::$arSeparators[$fields['THOUSANDS_VARIANT']])
244 {
245 $errorMessages[] = array(
246 'id' => 'DEC_POINT',
247 'text' => Loc::getMessage(
248 'BT_CUR_LANG_ERR_DEC_POINT_EQUAL_THOUSANDS_SEP',
249 array('#LANG#' => $language)
250 )
251 );
252 }
253 }
254 }
255 else
256 {
257 if (
258 isset($fields['DEC_POINT'])
259 || (isset($fields['THOUSANDS_VARIANT']) && isset(self::$arSeparators[$fields['THOUSANDS_VARIANT']]))
260 )
261 {
262 $copyFields = $fields;
263 $needFields = [];
264 if (!isset($copyFields['DEC_POINT']))
265 $needFields[] = 'DEC_POINT';
266 if (!isset($copyFields['THOUSANDS_VARIANT']))
267 $needFields[] = 'THOUSANDS_VARIANT';
268
269 if (!empty($needFields))
270 {
272 'select' => $needFields,
273 'filter' => ['=CURRENCY' => $currency, '=LID' => $language]
274 ])->fetch();
275 if (!empty($row))
276 {
277 $copyFields = array_merge($copyFields, $row);
278 $needFields = [];
279 }
280 unset($row);
281 }
282 if (
283 empty($needFields)
284 && (!empty($copyFields['THOUSANDS_VARIANT']) && isset(self::$arSeparators[$copyFields['THOUSANDS_VARIANT']]))
285 && ($copyFields['DEC_POINT'] == self::$arSeparators[$copyFields['THOUSANDS_VARIANT']])
286 )
287 {
288 $errorMessages[] = array(
289 'id' => 'DEC_POINT',
290 'text' => Loc::getMessage(
291 'BT_CUR_LANG_ERR_DEC_POINT_EQUAL_THOUSANDS_SEP',
292 array('#LANG#' => $language)
293 )
294 );
295 }
296 unset($needFields, $copyFields);
297 }
298 }
299 }
300
301 if (!empty($errorMessages))
302 {
303 if ($getErrors)
304 return $errorMessages;
305
306 $obError = new CAdminException($errorMessages);
307 $APPLICATION->ResetException();
308 $APPLICATION->ThrowException($obError);
309 return false;
310 }
311 return true;
312 }
313
314 public static function Add($arFields)
315 {
316 global $DB;
317
318 if (!self::checkFields('ADD', $arFields))
319 return false;
320
321 $arInsert = $DB->PrepareInsert("b_catalog_currency_lang", $arFields);
322
323 $strSql = "insert into b_catalog_currency_lang(".$arInsert[0].") values(".$arInsert[1].")";
324 $DB->Query($strSql);
325
328
329 return true;
330 }
331
332 public static function Update($currency, $lang, $arFields)
333 {
334 global $DB;
335
338 if ($currency === false || $lang === false)
339 return false;
340
341 if (!self::checkFields('UPDATE', $arFields, $currency, $lang))
342 return false;
343
344 $strUpdate = $DB->PrepareUpdate("b_catalog_currency_lang", $arFields);
345 if (!empty($strUpdate))
346 {
347 $strSql = "update b_catalog_currency_lang set ".$strUpdate." where CURRENCY = '".$DB->ForSql($currency)."' and LID='".$DB->ForSql($lang)."'";
348 $DB->Query($strSql);
349
352 }
353
354 return true;
355 }
356
357 public static function Delete($currency, $lang)
358 {
359 global $DB;
360
363 if ($currency === false || $lang === false)
364 return false;
365
368
369 $strSql = "delete from b_catalog_currency_lang where CURRENCY = '".$DB->ForSql($currency)."' and LID = '".$DB->ForSql($lang)."'";
370 $DB->Query($strSql);
371
372 return true;
373 }
374
375 public static function GetByID($currency, $lang)
376 {
377 global $DB;
378
381 if ($currency === false || $lang === false)
382 return false;
383
384 $strSql = "select * from b_catalog_currency_lang where CURRENCY = '".$DB->ForSql($currency)."' and LID = '".$DB->ForSql($lang)."'";
385 $db_res = $DB->Query($strSql);
386
387 if ($res = $db_res->Fetch())
388 return $res;
389
390 return false;
391 }
392
393 public static function GetCurrencyFormat($currency, $lang = LANGUAGE_ID)
394 {
396 global $stackCacheManager;
397
398 if (defined("CURRENCY_SKIP_CACHE") && CURRENCY_SKIP_CACHE)
399 {
400 $arCurrencyLang = CCurrencyLang::GetByID($currency, $lang);
401 }
402 else
403 {
404 $cacheTime = CURRENCY_CACHE_DEFAULT_TIME;
405 if (defined("CURRENCY_CACHE_TIME"))
406 $cacheTime = (int)CURRENCY_CACHE_TIME;
407
408 $strCacheKey = $currency."_".$lang;
409
410 $stackCacheManager->SetLength("currency_currency_lang", 20);
411 $stackCacheManager->SetTTL("currency_currency_lang", $cacheTime);
412 if ($stackCacheManager->Exist("currency_currency_lang", $strCacheKey))
413 {
414 $arCurrencyLang = $stackCacheManager->Get("currency_currency_lang", $strCacheKey);
415 }
416 else
417 {
418 $arCurrencyLang = CCurrencyLang::GetByID($currency, $lang);
419 $stackCacheManager->Set("currency_currency_lang", $strCacheKey, $arCurrencyLang);
420 }
421 }
422
423 return $arCurrencyLang;
424 }
425
426 public static function GetList($by = 'lang', $order = 'asc', $currency = '')
427 {
428 global $DB;
429
430 $strSql = "select CURL.* from b_catalog_currency_lang CURL ";
431
432 if ('' != $currency)
433 $strSql .= "where CURL.CURRENCY = '".$DB->ForSql($currency, 3)."' ";
434
435 if (strtolower($by) == "currency") $strSqlOrder = " order by CURL.CURRENCY ";
436 elseif (strtolower($by) == "name") $strSqlOrder = " order by CURL.FULL_NAME ";
437 else
438 {
439 $strSqlOrder = " order BY CURL.LID ";
440 }
441
442 if ($order == "desc")
443 $strSqlOrder .= " desc ";
444
445 $strSql .= $strSqlOrder;
446
447 return $DB->Query($strSql);
448 }
449
450 public static function GetDefaultValues(): array
451 {
452 return self::$arDefaultValues;
453 }
454
455 public static function GetSeparators(): array
456 {
457 return self::$arSeparators;
458 }
459
460 public static function GetSeparatorTypes($boolFull = false): array
461 {
462 $boolFull = (true == $boolFull);
463 if ($boolFull)
464 {
465 return [
466 Currency\CurrencyClassifier::SEPARATOR_EMPTY => Loc::getMessage('BT_CUR_LANG_SEP_VARIANT_EMPTY'),
467 Currency\CurrencyClassifier::SEPARATOR_DOT => Loc::getMessage('BT_CUR_LANG_SEP_VARIANT_DOT'),
468 Currency\CurrencyClassifier::SEPARATOR_COMMA => Loc::getMessage('BT_CUR_LANG_SEP_VARIANT_COMMA'),
469 Currency\CurrencyClassifier::SEPARATOR_SPACE => Loc::getMessage('BT_CUR_LANG_SEP_VARIANT_SPACE'),
470 Currency\CurrencyClassifier::SEPARATOR_NBSPACE => Loc::getMessage('BT_CUR_LANG_SEP_VARIANT_NBSPACE'),
471 ];
472 }
473 return [
479 ];
480 }
481
482 public static function GetFormatTemplates(): array
483 {
485 $templates = [];
486 $templates[] = [
487 'TEXT' => '$1.234,10',
488 'FORMAT' => '$#',
490 'THOUSANDS_VARIANT' => Currency\CurrencyClassifier::SEPARATOR_DOT,
491 'DECIMALS' => '2',
492 ];
493 $templates[] = [
494 'TEXT' => '$1 234,10',
495 'FORMAT' => '$#',
498 'DECIMALS' => '2',
499 ];
500 $templates[] = [
501 'TEXT' => '1.234,10 USD',
502 'FORMAT' => '# USD',
504 'THOUSANDS_VARIANT' => Currency\CurrencyClassifier::SEPARATOR_DOT,
505 'DECIMALS' => '2',
506 ];
507 $templates[] = [
508 'TEXT' => '1 234,10 USD',
509 'FORMAT' => '# USD',
512 'DECIMALS' => '2',
513 ];
514 $templates[] = [
515 'TEXT' => '&euro;2.345,20',
516 'FORMAT' => '&euro;#',
518 'THOUSANDS_VARIANT' => Currency\CurrencyClassifier::SEPARATOR_DOT,
519 'DECIMALS' => '2',
520 ];
521 $templates[] = [
522 'TEXT' => '&euro;2 345,20',
523 'FORMAT' => '&euro;#',
526 'DECIMALS' => '2',
527 ];
528 $templates[] = [
529 'TEXT' => '2.345,20 EUR',
530 'FORMAT' => '# EUR',
532 'THOUSANDS_VARIANT' => Currency\CurrencyClassifier::SEPARATOR_DOT,
533 'DECIMALS' => '2',
534 ];
535 $templates[] = [
536 'TEXT' => '2 345,20 EUR',
537 'FORMAT' => '# EUR',
540 'DECIMALS' => '2',
541 ];
542
543 if (in_array('RUB', $installCurrencies))
544 {
545 $rubTitle = Loc::getMessage('BT_CUR_LANG_CURRENCY_RUBLE');
546 $templates[] = [
547 'TEXT' => '3.456,70 '.$rubTitle,
548 'FORMAT' => '# '.$rubTitle,
550 'THOUSANDS_VARIANT' => Currency\CurrencyClassifier::SEPARATOR_DOT,
551 'DECIMALS' => '2',
552 ];
553 $templates[] = [
554 'TEXT' => '3 456,70 '.$rubTitle,
555 'FORMAT' => '# '.$rubTitle,
558 'DECIMALS' => '2',
559 ];
560 }
561
562 return $templates;
563 }
564
565 public static function GetFormatDescription($currency)
566 {
567 $safeFormat = (
568 Main\Context::getCurrent()->getRequest()->isAdminSection()
570 );
571 $currency = (string)$currency;
572
573 if (!isset(self::$arCurrencyFormat[$currency]))
574 {
576 if ($arCurFormat === false)
577 {
578 $arCurFormat = self::$arDefaultValues;
579 $arCurFormat['FULL_NAME'] = $currency;
580 }
581 else
582 {
583 if (!isset($arCurFormat['DECIMALS']))
584 {
585 $arCurFormat['DECIMALS'] = self::$arDefaultValues['DECIMALS'];
586 }
587 $arCurFormat['DECIMALS'] = (int)$arCurFormat['DECIMALS'];
588 if (!isset($arCurFormat['DEC_POINT']))
589 {
590 $arCurFormat['DEC_POINT'] = self::$arDefaultValues['DEC_POINT'];
591 }
592 if (
593 !empty($arCurFormat['THOUSANDS_VARIANT'])
594 && isset(self::$arSeparators[$arCurFormat['THOUSANDS_VARIANT']])
595 )
596 {
597 $arCurFormat['THOUSANDS_SEP'] = self::$arSeparators[$arCurFormat['THOUSANDS_VARIANT']];
598 }
599 elseif (!isset($arCurFormat['THOUSANDS_SEP']))
600 {
601 $arCurFormat['THOUSANDS_SEP'] = self::$arDefaultValues['THOUSANDS_SEP'];
602 }
603 if (!isset($arCurFormat['FORMAT_STRING']))
604 {
605 $arCurFormat['FORMAT_STRING'] = self::$arDefaultValues['FORMAT_STRING'];
606 }
607
608 $sanitizer = new \CBXSanitizer();
609 $sanitizer->setLevel(\CBXSanitizer::SECURE_LEVEL_LOW);
610 $sanitizer->ApplyDoubleEncode(false);
611 $arCurFormat["FORMAT_STRING"] = $sanitizer->SanitizeHtml($arCurFormat["FORMAT_STRING"]);
612 unset($sanitizer);
613
614 if ($safeFormat)
615 {
616 $arCurFormat["FORMAT_STRING"] = strip_tags(preg_replace(
617 '#<script[^>]*?>.*?</script[^>]*?>#is',
618 '',
619 $arCurFormat["FORMAT_STRING"]
620 ));
621 }
622 if (empty($arCurFormat['HIDE_ZERO']))
623 {
624 $arCurFormat['HIDE_ZERO'] = self::$arDefaultValues['HIDE_ZERO'];
625 }
626 }
627
628 $arCurFormat['TEMPLATE'] = [
629 'SINGLE' => $arCurFormat['FORMAT_STRING'],
630 'PARTS' => [
631 0 => $arCurFormat['FORMAT_STRING'],
632 ],
633 'VALUE_INDEX' => 0,
634 ];
635 $parts = static::explodeFormatTemplate($arCurFormat['FORMAT_STRING']);
636 if (!empty($parts))
637 {
638 $arCurFormat['TEMPLATE']['PARTS'] = $parts;
639 $arCurFormat['TEMPLATE']['VALUE_INDEX'] = (int)array_search('#', $parts);
640 }
641 unset($parts);
642
643 $arCurFormat['CURRENCY'] = $currency;
644
645 self::$arCurrencyFormat[$currency] = $arCurFormat;
646 }
647 else
648 {
649 $arCurFormat = self::$arCurrencyFormat[$currency];
650 }
651
652 return $arCurFormat;
653 }
654
655 public static function CurrencyFormat($price, $currency, $useTemplate = true)
656 {
657 static $eventExists = null;
658
659 $useTemplate = (bool)$useTemplate;
660 if ($useTemplate)
661 {
662 if ($eventExists === true || $eventExists === null)
663 {
664 foreach (GetModuleEvents('currency', 'CurrencyFormat', true) as $arEvent)
665 {
666 $eventExists = true;
667 $result = ExecuteModuleEventEx($arEvent, array($price, $currency));
668 if ((string)$result !== '')
669 {
670 return $result;
671 }
672 }
673 if ($eventExists === null)
674 {
675 $eventExists = false;
676 }
677 }
678 }
679
680 if (!isset($price) || $price === '')
681 {
682 return '';
683 }
684
686 if ($currency === false)
687 {
688 return '';
689 }
690
691 $format = self::$arCurrencyFormat[$currency] ?? self::GetFormatDescription($currency);
692
693 return static::formatValue($price, $format, $useTemplate);
694 }
695
696 public static function formatValue($value, array $format, $useTemplate = true): string
697 {
698 if (!isset(self::$region))
699 {
700 self::$region = Main\Application::getInstance()->getLicense()->getRegion();
701 }
702
703 $format['DECIMALS'] = (int)($format['DECIMALS'] ?? self::$arDefaultValues['DECIMALS']);
704 $format['HIDE_ZERO'] ??= self::$arDefaultValues['HIDE_ZERO'];
705 $format['DEC_POINT'] = (string)($format['DEC_POINT'] ?? self::$arDefaultValues['DEC_POINT']);
706 $format['THOUSANDS_SEP'] = (string)($format['THOUSANDS_SEP'] ?? self::$arDefaultValues['THOUSANDS_SEP']);
707 $format['FORMAT_STRING'] = (string)($format['FORMAT_STRING'] ?? self::$arDefaultValues['FORMAT_STRING']);
708 $format['CURRENCY'] = $format['CURRENCY'] ?? null;
709
710 if (is_string($value))
711 {
712 $value = str_replace(',', '.', $value);
713 }
714
715 if (
716 $format['CURRENCY'] === 'INR'
717 && (self::$region === 'hi' || self::$region === 'in')
718 )
719 {
720 if (self::useSimpleFormat($value, $format['DECIMALS']))
721 {
722 $value = round((float)$value, $format['DECIMALS']);
723 }
724 $result = self::extendedInrFormatValue((string)$value, $format);
725 }
726 else
727 {
728 if (self::useSimpleFormat($value, $format['DECIMALS']))
729 {
730 $result = self::simpleFormatValue((float)$value, $format);
731 }
732 else
733 {
734 $result = self::extendedFormatValue((string)$value, $format);
735 }
736 }
737
738 return (
739 $useTemplate
740 ? static::applyTemplate($result, $format['FORMAT_STRING'])
741 : $result
742 );
743 }
744
745 public static function formatEditValue(int|float|string|null $value, array $format): string
746 {
747 $format['THOUSANDS_VARIANT'] = (string)($format['THOUSANDS_VARIANT'] ?? self::$arDefaultValues['THOUSANDS_VARIANT']);
748 $format['THOUSANDS_SEP'] = (string)($format['THOUSANDS_SEP'] ?? self::$arDefaultValues['THOUSANDS_SEP']);
749 if (
750 $format['THOUSANDS_VARIANT'] === Currency\CurrencyClassifier::SEPARATOR_NBSPACE
751 || $format['THOUSANDS_SEP'] === self::$arSeparators[Currency\CurrencyClassifier::SEPARATOR_NBSPACE]
752 )
753 {
754 $format['THOUSANDS_VARIANT'] = Currency\CurrencyClassifier::SEPARATOR_SPACE;
755 $format['THOUSANDS_SEP'] = self::$arSeparators[Currency\CurrencyClassifier::SEPARATOR_SPACE];
756 }
757
758 return static::formatValue($value, $format, false);
759 }
760
761 public static function applyTemplate($value, $template): string
762 {
763 return (string)preg_replace('/(^|[^&])#/', '${1}'.$value, (string)$template);
764 }
765
770 public static function checkLanguage($language): bool|string
771 {
773 }
774
775 public static function isExistCurrencyLanguage($currency, $language): bool
776 {
777 global $DB;
779 $language = Currency\CurrencyManager::checkLanguage($language);
780 if ($currency === false || $language === false)
781 {
782 return false;
783 }
784 $query = "select LID from b_catalog_currency_lang where CURRENCY = '".$DB->ForSql($currency)."' and LID = '".$DB->ForSql($language)."'";
785 $searchIterator = $DB->Query($query);
786 $result = $searchIterator->Fetch();
787 unset($searchIterator);
788
789 return !empty($result);
790 }
791
792 public static function getParsedCurrencyFormat(string $currency): array
793 {
794 $result = self::$arCurrencyFormat[$currency] ?? self::GetFormatDescription($currency);
795
796 return $result['TEMPLATE']['PARTS'];
797 }
798
799 protected static function explodeFormatTemplate(string $template): ?array
800 {
801 $result = preg_split('/(?<!&)(#)/', $template, -1, PREG_SPLIT_DELIM_CAPTURE);
802 if (!is_array($result))
803 {
804 return null;
805 }
806 $resultCount = count($result);
807 if ($resultCount > 1)
808 {
809 $needSlice = false;
810 $offset = 0;
811 $count = $resultCount;
812 if ($result[0] == '')
813 {
814 $needSlice = true;
815 $offset = 1;
816 $count--;
817 }
818 if ($result[$resultCount-1] == '')
819 {
820 $needSlice = true;
821 $count--;
822 }
823 if ($needSlice)
824 {
825 $result = array_slice($result, $offset, $count);
826 }
827 unset($count, $offset, $needSlice);
828 }
829 unset($resultCount);
830
831 return $result;
832 }
833
834 public static function getPriceControl(string $control, string $currency): string
835 {
836 if ($control === '')
837 {
838 return '';
839 }
841 {
842 return $control;
843 }
844 $format = static::getParsedCurrencyFormat($currency);
845 if (empty($format))
846 {
847 return $control;
848 }
849 $index = array_search('#', $format);
850 if ($index === false)
851 {
852 return $control;
853 }
854 $format[$index] = $control;
855
856 return implode('', $format);
857 }
858
859 protected static function clearFields($value): bool
860 {
861 return ($value !== null);
862 }
863
864 public static function getUnFormattedValue(string $formattedValue, string $currency, string $lang = LANGUAGE_ID): string
865 {
866 $format = static::GetCurrencyFormat($currency, $lang);
867
868 return static::unFormatValue($formattedValue, (string)$format['THOUSANDS_SEP'], (string)$format['DEC_POINT']);
869 }
870
871 protected static function unFormatValue(string $formattedValue, string $thousandsSeparator, string $decPoint): string
872 {
873 $result = $formattedValue;
874
875 if($thousandsSeparator !== '')
876 {
877 $result = str_replace($thousandsSeparator, '', $result);
878 }
879
880 if($decPoint !== '.' && $decPoint !== '')
881 {
882 $result = str_replace($decPoint, '.', $result);
883 }
884
885 return $result;
886 }
887
888 private static function useSimpleFormat(string|float|int|null $value, int $decimals): bool
889 {
890 static $floatPrecision = null;
891 if ($floatPrecision === null)
892 {
893 $floatPrecision = (int)ini_get('precision');
894 }
895 if (is_int($value) || is_float($value))
896 {
897 return true;
898 }
899 if ($value === '' || $value === null || $value === '0')
900 {
901 return true;
902 }
903
904 if ($value !== (string)((float)$value))
905 {
906 return false;
907 }
908
909 $parsedValue = Currency\Helpers\Editor::parseValue($value);
910 if ($parsedValue === null)
911 {
912 return true;
913 }
914
915 $flatValue = ($parsedValue[2] === '' ? '0' : $parsedValue[2]);
916 $parsedValue[3] ??= '';
917 if ($parsedValue[3] !== '' && $parsedValue[3] !== '.')
918 {
919 $fraction = str_pad($parsedValue[3], $decimals + 1, '0', STR_PAD_RIGHT);
920 if ($fraction !== '.')
921 {
922 $flatValue .= $fraction;
923 }
924 }
925
926 // empirical condition based on the precision of floating point values
927 return (strlen($flatValue) + 2) < $floatPrecision;
928 }
929
930 private static function simpleFormatValue(float $value, array $format): string
931 {
932 $decimals = $format['DECIMALS'];
933 if (static::isAllowUseHideZero() && $format['HIDE_ZERO'] === 'Y')
934 {
935 if (round($value, $format['DECIMALS']) === round($value, 0))
936 {
937 $decimals = 0;
938 }
939 }
940
941 return number_format($value, $decimals, $format['DEC_POINT'], $format['THOUSANDS_SEP']);
942 }
943
944 private static function extendedFormatValue(string $value, array $format): string
945 {
946 $triadSep = $format['THOUSANDS_SEP'];
947
948 $value = str_replace(',', '.', $value);
949 $parcedValue = explode('.', $value, 2);
950 $wholePart = $parcedValue[0] ?? '';
951 $fraction = $parcedValue[1] ?? '';
952 unset($parcedValue);
953 $result = '';
954 if ($wholePart[0] === '-')
955 {
956 $result = '-';
957 $wholePart = substr($wholePart, 1);
958 }
959
960 $normalizedFraction = self::normalizeFraction($fraction, $format);
961 $fraction = $normalizedFraction['FRACTION'];
962 if ($normalizedFraction['FIX_UP'] !== '')
963 {
964 $wholePart = self::roundWholePart($wholePart, $normalizedFraction['FIX_UP']);
965 }
966 unset($normalizedFraction['FIX_UP']);
967
968 $leadLen = strlen($wholePart) % 3;
969 if ($leadLen === 0)
970 {
971 $leadLen = 3; //take a first triad
972 }
973
974 $lead = substr($wholePart, 0, $leadLen);
975 $triads = substr($wholePart, $leadLen);
976
977 $result .=
978 $triads !== ''
979 ? $lead . preg_replace('/(\\d{3})/', $triadSep.'\\1', $triads)
980 : ($lead !== '' ? $lead : '0')
981 ;
982
983 if ($fraction !== '')
984 {
985 $result .= $format['DEC_POINT'] . $fraction;
986 }
987
988 return $result;
989 }
990
991 private static function extendedInrFormatValue(string $value, array $format): string
992 {
993 $blockSep = $format['THOUSANDS_SEP'];
994
995 $value = str_replace(',', '.', $value);
996 $parcedValue = explode('.', $value, 2);
997 $wholePart = $parcedValue[0] ?? '';
998 $fraction = $parcedValue[1] ?? '';
999 unset($parcedValue);
1000 $result = '';
1001 if ($wholePart[0] === '-')
1002 {
1003 $result = '-';
1004 $wholePart = substr($wholePart, 1);
1005 }
1006
1007 $normalizedFraction = self::normalizeFraction($fraction, $format);
1008 $fraction = $normalizedFraction['FRACTION'];
1009 if ($normalizedFraction['FIX_UP'] !== '')
1010 {
1011 $wholePart = self::roundWholePart($wholePart, $normalizedFraction['FIX_UP']);
1012 }
1013 unset($normalizedFraction['FIX_UP']);
1014
1015 if (strlen($wholePart) <= 3)
1016 {
1017 $result .= $wholePart;
1018 }
1019 else
1020 {
1021 $rightTriad = substr($wholePart, -3);
1022 $wholePart = substr($wholePart, 0, -3);
1023
1024 $leadLen = strlen($wholePart) % 2;
1025 if ($leadLen === 0)
1026 {
1027 $leadLen = 2; //take a first block
1028 }
1029
1030 $lead = substr($wholePart, 0, $leadLen);
1031 $blocks = substr($wholePart, $leadLen);
1032
1033 $result .=
1034 $blocks !== ''
1035 ? $lead . preg_replace('/(\\d{2})/', $blockSep.'\\1', $blocks)
1036 : ($lead !== '' ? $lead : '')
1037 ;
1038
1039 $result .= $blockSep . $rightTriad;
1040 }
1041
1042 if ($fraction !== '')
1043 {
1044 $result .= $format['DEC_POINT'] . $fraction;
1045 }
1046
1047 return $result;
1048 }
1049
1050 private static function normalizeFraction(string $fraction, array $format): ?array
1051 {
1052 $decimals = $format['DECIMALS'];
1053 if (static::isAllowUseHideZero() && $format['HIDE_ZERO'] === 'Y')
1054 {
1055 if ($fraction === '')
1056 {
1057 $decimals = 0;
1058 }
1059 else
1060 {
1061 $prepared = [];
1062 if (preg_match('/^0+$/', $fraction, $prepared))
1063 {
1064 $decimals = 0;
1065 }
1066 unset($prepared);
1067 }
1068 }
1069
1070 $result = [
1071 'FRACTION' => '',
1072 'FIX_UP' => '',
1073 ];
1074
1075 if ($fraction === '')
1076 {
1077 $result['FRACTION'] = $decimals > 0 ? str_repeat('0', $decimals) : '';
1078
1079 return $result;
1080 }
1081
1082 $fractionLength = strlen($fraction);
1083 if ($fractionLength > $decimals)
1084 {
1085 $carry = 0;
1086 for ($i = $fractionLength - 1; $i >=$decimals; $i--)
1087 {
1088 $value = (int)$fraction[$i] + $carry;
1089 if ($value >= 5)
1090 {
1091 $carry = 1;
1092 }
1093 }
1094
1095 $roundFraction = '';
1096 for ($i = $decimals - 1; $i >= 0; $i--)
1097 {
1098 $value = (int)$fraction[$i] + $carry;
1099 $carry = 0;
1100
1101 if ($value > 9)
1102 {
1103 $carry = 1;
1104 $value -= 10;
1105 }
1106
1107 $roundFraction = $value . $roundFraction;
1108 }
1109 $result['FRACTION'] = $roundFraction;
1110 if ($carry === 1)
1111 {
1112 $result['FIX_UP'] = '1';
1113 }
1114 }
1115 elseif ($fractionLength < $decimals)
1116 {
1117 $result['FRACTION'] = str_pad($fraction, $decimals, '0', STR_PAD_RIGHT);
1118 }
1119 else
1120 {
1121 $result['FRACTION'] = $fraction;
1122 }
1123
1124 return $result;
1125 }
1126
1127 private static function roundWholePart(string $wholePart, string $fixUp): string
1128 {
1129 $length = strlen($wholePart);
1130 $carry = 0;
1131
1132 $value = (int)$wholePart[$length - 1] + (int)$fixUp;
1133 if ($value > 9)
1134 {
1135 $carry = 1;
1136 $value -= 10;
1137 }
1138 $result = $value;
1139
1140 for ($i = $length - 2; $i >= 0; $i--)
1141 {
1142 $value = (int)$wholePart[$i] + $carry;
1143 $carry = 0;
1144
1145 if ($value > 9)
1146 {
1147 $carry = 1;
1148 $value -= 10;
1149 }
1150
1152 }
1153
1154 if ($carry)
1155 {
1156 $result = '1' . $result;
1157 }
1158
1159 return $result;
1160 }
1161}
$count
Определения admin_tab.php:4
$db_res
Определения options_user_settings.php:8
global $APPLICATION
Определения include.php:80
static clearCurrencyCache($language='')
Определения currencymanager.php:416
static checkLanguage($language)
Определения currencymanager.php:47
static getInstalledCurrencies()
Определения currencymanager.php:280
static checkCurrencyID($currency)
Определения currencymanager.php:35
static getInstance()
Определения application.php:98
static isModuleInstalled($moduleName)
Определения modulemanager.php:125
static getList(array $parameters=array())
Определения datamanager.php:431
static isUserExists()
Определения currency.php:673
const SECURE_LEVEL_LOW
Определения sanitizer.php:30
Определения currency_lang.php:12
static array $arSeparators
Определения currency_lang.php:24
const SEP_DOT
Определения currency_lang.php:16
static checkFields($action, &$fields, $currency='', $language='', $getErrors=false)
Определения currency_lang.php:70
const SEP_SPACE
Определения currency_lang.php:20
static Update($currency, $lang, $arFields)
Определения currency_lang.php:332
const SEP_NBSPACE
Определения currency_lang.php:22
static array $arDefaultValues
Определения currency_lang.php:32
static unFormatValue(string $formattedValue, string $thousandsSeparator, string $decPoint)
Определения currency_lang.php:871
static formatEditValue(int|float|string|null $value, array $format)
Определения currency_lang.php:745
static Add($arFields)
Определения currency_lang.php:314
const SEP_COMMA
Определения currency_lang.php:18
static applyTemplate($value, $template)
Определения currency_lang.php:761
static explodeFormatTemplate(string $template)
Определения currency_lang.php:799
static GetSeparators()
Определения currency_lang.php:455
static GetSeparatorTypes($boolFull=false)
Определения currency_lang.php:460
static int $useHideZero
Определения currency_lang.php:43
static GetFormatTemplates()
Определения currency_lang.php:482
static getUnFormattedValue(string $formattedValue, string $currency, string $lang=LANGUAGE_ID)
Определения currency_lang.php:864
static GetByID($currency, $lang)
Определения currency_lang.php:375
static isExistCurrencyLanguage($currency, $language)
Определения currency_lang.php:775
static Delete($currency, $lang)
Определения currency_lang.php:357
static isAllowUseHideZero()
Определения currency_lang.php:65
static formatValue($value, array $format, $useTemplate=true)
Определения currency_lang.php:696
static GetFormatDescription($currency)
Определения currency_lang.php:565
static getPriceControl(string $control, string $currency)
Определения currency_lang.php:834
static checkLanguage($language)
Определения currency_lang.php:770
static CurrencyFormat($price, $currency, $useTemplate=true)
Определения currency_lang.php:655
static array $arCurrencyFormat
Определения currency_lang.php:41
static GetCurrencyFormat($currency, $lang=LANGUAGE_ID)
Определения currency_lang.php:393
static GetList($by='lang', $order='asc', $currency='')
Определения currency_lang.php:426
static enableUseHideZero()
Определения currency_lang.php:47
static GetDefaultValues()
Определения currency_lang.php:450
const SEP_EMPTY
Определения currency_lang.php:14
static clearFields($value)
Определения currency_lang.php:859
static disableUseHideZero()
Определения currency_lang.php:56
static getParsedCurrencyFormat(string $currency)
Определения currency_lang.php:792
const CURRENCY_CACHE_DEFAULT_TIME
Определения include.php:47
$arFields
Определения dblapprove.php:5
$template
Определения file_edit.php:49
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
$query
Определения get_search.php:11
$control
Определения iblock_catalog_edit.php:61
$defaultValues
Определения iblock_catalog_edit.php:124
global $DB
Определения cron_frame.php:29
global $USER
Определения csv_new_run.php:40
if(!defined('SITE_ID')) $lang
Определения include.php:91
ExecuteModuleEventEx($arEvent, $arParams=[])
Определения tools.php:5214
GetModuleEvents($MODULE_ID, $MESSAGE_ID, $bReturnArray=false)
Определения tools.php:5177
$value
Определения Param.php:39
$order
Определения payment.php:8
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
const ADMIN_SECTION
Определения rss.php:2
$i
Определения factura.php:643
</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
$arCurFormat
Определения template.php:265
$action
Определения file_dialog.php:21
$fields
Определения yandex_run.php:501