1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
state.php
См. документацию.
1<?php
2
3namespace Bitrix\Catalog\Config;
4
5use Bitrix\Main;
6use Bitrix\Main\Loader;
7use Bitrix\Main\ModuleManager;
8use Bitrix\Main\Localization\Loc;
9use Bitrix\Iblock;
10use Bitrix\Catalog;
11use Bitrix\Landing;
12use Bitrix\Crm;
13
20final class State
21{
22 private const EXTERNAL_CATALOG_OPTION = 'is_external_catalog';
23
25 private static $landingSections;
27 private static $iblockSections;
29 private static $fullIblockSections;
31 private static $elementCount;
33 private static $iblockList = [];
35 private static $crmIncluded;
36
42 public static function isUsedInventoryManagement(): bool
43 {
45 {
46 return false;
47 }
48
50 }
51
57 final public static function isEnabledInventoryManagement(): bool
58 {
59 return (Main\Config\Option::get('catalog', 'default_use_store_control') === 'Y');
60 }
61
62 final public static function isExternalCatalog(): bool
63 {
64 return Main\Config\Option::get('catalog', self::EXTERNAL_CATALOG_OPTION, 'N') === 'Y';
65 }
66
67 final public static function setIsExternalCatalog(bool $isEnabled): void
68 {
69 Main\Config\Option::set('catalog', self::EXTERNAL_CATALOG_OPTION, $isEnabled ? 'Y' : 'N');
70 }
71
77 public static function isShowedStoreReserve(): bool
78 {
79 if (!self::isUsedInventoryManagement())
80 {
81 return false;
82 }
83 if (
84 Main\Config\Option::get('catalog', 'enable_reservation') === 'Y'
85 && Main\Config\Option::get('catalog', 'show_store_reserve') === 'Y'
86 )
87 {
88 return true;
89 }
90 if (self::isCrmIncluded())
91 {
92 return (Main\Config\Option::get('crm', 'enable_order_deal_create') === 'Y');
93 }
94
95 return false;
96 }
97
103 public static function isExceededPriceTypeLimit(): bool
104 {
106 {
107 return false;
108 }
109
110 return Catalog\GroupTable::getCount([], ['ttl' => 86400]) > 1;
111 }
112
118 public static function isAllowedNewPriceType(): bool
119 {
121 {
122 return true;
123 }
124
125 return Catalog\GroupTable::getCount([], ['ttl' => 86400]) === 0;
126 }
127
133 public static function isExceededStoreLimit(): bool
134 {
136 {
137 return false;
138 }
139
140 return Catalog\StoreTable::getCount([], ['ttl' => 86400]) > 1;
141 }
142
148 public static function isAllowedNewStore(): bool
149 {
151 {
152 return true;
153 }
154
155 return Catalog\StoreTable::getCount([], ['ttl' => 86400]) === 0;
156 }
157
165 public static function getExceedingProductLimit(int $iblockId, ?int $sectionId = null): ?array
166 {
167 if ($iblockId <= 0)
168 {
169 return null;
170 }
171
172 if (!ModuleManager::isModuleInstalled('bitrix24'))
173 {
174 return null;
175 }
176
177 if ($iblockId !== self::getCrmCatalogId())
178 {
179 return null;
180 }
181
182 $result = self::checkIblockLimit($iblockId);
183 if ($result !== null && $sectionId !== null)
184 {
185 self::loadIblockSections($iblockId);
186 if (!isset(self::$fullIblockSections[$sectionId]))
187 {
188 $result = null;
189 }
190 }
191 if ($result === null)
192 {
193 $result = self::getCrmCatalogLimit($iblockId);
194 }
195
196 return $result;
197 }
198
204 public static function getCrmExceedingProductLimit(): ?array
205 {
206 $crmCatalogId = self::getCrmCatalogId();
207 if ($crmCatalogId > 0)
208 {
209 return self::getExceedingProductLimit($crmCatalogId);
210 }
211
212 return null;
213 }
214
215 public static function getProductLimitState(int $iblockId): ?array
216 {
217 if ($iblockId <= 0)
218 {
219 return null;
220 }
221
222 if (!ModuleManager::isModuleInstalled('bitrix24'))
223 {
224 return null;
225 }
226
227 if ($iblockId !== self::getCrmCatalogId())
228 {
229 return null;
230 }
231
232 $result = [];
234 $result[$variable] = [
235 'LIMIT_NAME' => $variable,
236 'LIMIT_VALUE' => Feature::getLandingProductLimit(),
237 'CURRENT_VALUE' => self::getElementCount($iblockId),
238 ];
239
240 $crmLimit = self::getCrmCatalogLimitState($iblockId);
241 if ($crmLimit !== null)
242 {
243 $result[$crmLimit['LIMIT_NAME']] = $crmLimit;
244 }
245
246 return $result;
247 }
248
255 public static function handlerBeforeIblockElementAdd(array &$fields): bool
256 {
257 if (!self::checkIblockId($fields))
258 {
259 return true;
260 }
261
262 $limit = self::checkIblockLimit((int)$fields['IBLOCK_ID']);
263 if (empty($limit))
264 {
265 return true;
266 }
267
268 if (!isset($fields['IBLOCK_SECTION']) || !is_array($fields['IBLOCK_SECTION']))
269 {
270 return true;
271 }
272 $sections = $fields['IBLOCK_SECTION'];
274 if (empty($sections))
275 {
276 return true;
277 }
278 self::loadIblockSections((int)$fields['IBLOCK_ID']);
279 $sections = array_intersect($sections, self::$fullIblockSections);
280 if (empty($sections))
281 {
282 return true;
283 }
284 unset($sections);
285
286 self::setProductLimitError($limit['MESSAGE']);
287 unset($limit);
288
289 return false;
290 }
291
298 public static function handlerAfterIblockElementAdd(array &$fields): void
299 {
300 if ($fields['RESULT'] === false)
301 return;
302
303 if (!self::checkIblockId($fields))
304 return;
305
306 $sections = $fields['IBLOCK_SECTION'] ?? null;
308 if (empty($sections))
309 return;
310 self::loadIblockSections((int)$fields['IBLOCK_ID']);
311 $sections = array_intersect($sections, self::$fullIblockSections);
312 if (empty($sections))
313 return;
314
315 self::$elementCount = null;
316 }
317
324 public static function handlerBeforeIblockElementUpdate(array &$fields): bool
325 {
326 if (!self::checkIblockId($fields))
327 {
328 return true;
329 }
330
331 $limit = self::checkIblockLimit((int)$fields['IBLOCK_ID']);
332 if (empty($limit))
333 {
334 return true;
335 }
336
337 if (!isset($fields['IBLOCK_SECTION']) || !is_array($fields['IBLOCK_SECTION']))
338 {
339 return true;
340 }
341 $sections = $fields['IBLOCK_SECTION'];
343 if (empty($sections))
344 {
345 return true;
346 }
347 self::loadIblockSections((int)$fields['IBLOCK_ID']);
348 $sections = array_intersect($sections, self::$fullIblockSections);
349 if (empty($sections))
350 {
351 return true;
352 }
353 unset($sections);
354
355 $notMove = false;
356 $iterator = Iblock\SectionElementTable::getList([
357 'select' => ['IBLOCK_SECTION_ID'],
358 'filter' => [
359 '=IBLOCK_ELEMENT_ID' => $fields['ID'],
360 '=ADDITIONAL_PROPERTY_ID' => null,
361 ],
362 ]);
363 while ($row = $iterator->fetch())
364 {
365 $row['ID'] = (int)$row['ID'];
366 if (isset(self::$fullIblockSections[$row['ID']]))
367 {
368 $notMove = true;
369 break;
370 }
371 }
372 unset($row, $iterator);
373 if ($notMove)
374 {
375 return true;
376 }
377
378 self::setProductLimitError($limit['MESSAGE']);
379 unset($limit);
380
381 return false;
382 }
383
390 public static function handlerAfterIblockElementUpdate(array &$fields): void
391 {
392 if ($fields['RESULT'] === false)
393 {
394 return;
395 }
396 if (!self::checkIblockId($fields))
397 {
398 return;
399 }
400 if (!array_key_exists('IBLOCK_SECTION', $fields))
401 {
402 return;
403 }
404
405 self::$elementCount = null;
406 }
407
414 public static function handlerAfterIblockElementDelete(array $fields): void
415 {
416 if (!self::checkIblockId($fields))
417 return;
418
419 self::$elementCount = null;
420 }
421
428 public static function handlerAfterIblockSectionAdd(array &$fields): void
429 {
430 if ($fields['RESULT'] === false)
431 return;
432 if (!self::checkIblockId($fields))
433 return;
434
435 self::$iblockSections = null;
436 self::$fullIblockSections = null;
437 }
438
445 public static function handlerBeforeIblockSectionUpdate(array &$fields): bool
446 {
447 if (!self::checkIblockId($fields))
448 {
449 return true;
450 }
451
452 $limit = self::getIblockLimit((int)$fields['IBLOCK_ID']);
453 if ($limit['LIMIT'] === 0)
454 {
455 return true;
456 }
457 if (!array_key_exists('IBLOCK_SECTION_ID', $fields))
458 {
459 return true;
460 }
461 $parentId = (int)$fields['IBLOCK_SECTION_ID'];
462 self::loadIblockSections((int)$fields['IBLOCK_ID']);
463 if (!isset(self::$fullIblockSections[$parentId]))
464 {
465 return true;
466 }
468 'select' => ['IBLOCK_SECTION_ID'],
469 'filter' => [
470 '=ID' => $fields['ID'],
471 '=IBLOCK_ID' => $fields['IBLOCK_ID'],
472 ],
473 ]);
474 $row = $iterator->fetch();
475 unset($iterator);
476 if (empty($row))
477 {
478 return true;
479 }
480 $oldParentId = (int)$row['IBLOCK_SECTION_ID'];
481 if (isset(self::$fullIblockSections[$oldParentId]))
482 {
483 return true;
484 }
485
486 $count = (int)\CIBlockElement::GetList(
487 [],
488 [
489 'IBLOCK_ID' => $fields['IBLOCK_ID'],
490 'SECTION_ID' => $fields['ID'],
491 'INCLUDE_SUBSECTIONS' => 'Y',
492 'CHECK_PERMISSIONS' => 'N',
493 ],
494 [],
495 false,
496 ['ID']
497 );
498 if ($count === 0)
499 {
500 return true;
501 }
502 $limit['COUNT'] += $count;
503 if ($limit['COUNT'] <= $limit['LIMIT'])
504 {
505 return true;
506 }
507
508 $limit['MESSAGE_ID'] = 'CATALOG_STATE_ERR_PRODUCT_IN_SECTION_LIMIT';
509
510 self::setProductLimitError(self::getProductLimitError($limit));
511 unset($limit);
512
513 return false;
514 }
515
522 public static function handlerAfterIblockSectionUpdate(array &$fields): void
523 {
524 if ($fields['RESULT'] === false)
525 {
526 return;
527 }
528 if (!self::checkIblockId($fields))
529 {
530 return;
531 }
532 if (!array_key_exists('IBLOCK_SECTION_ID', $fields))
533 {
534 return;
535 }
536
537 self::$iblockSections = null;
538 self::$fullIblockSections = null;
539 self::$elementCount = null;
540 }
541
549 {
550 if (!self::checkIblockId($fields))
551 return;
552
553 self::$iblockSections = null;
554 self::$fullIblockSections = null;
555 self::$elementCount = null;
556 }
557
562 private static function getElementCount(int $iblockId): int
563 {
564 if (self::$elementCount === null)
565 {
566 self::$elementCount = 0;
567
568 $iblockSectionIds = self::getIblockSections($iblockId);
569 if (!empty($iblockSectionIds))
570 {
571 $filter = [
572 'IBLOCK_ID' => $iblockId,
573 'SECTION_ID' => $iblockSectionIds,
574 'INCLUDE_SUBSECTIONS' => 'Y',
575 'CHECK_PERMISSIONS' => 'N',
576 ];
577 $filter = Catalog\Product\SystemField\ProductMapping::getExtendedFilterByArea(
578 $filter,
579 Catalog\Product\SystemField\ProductMapping::MAP_LANDING
580 );
581 self::$elementCount = (int)\CIBlockElement::GetList(
582 [],
583 $filter,
584 [],
585 false,
586 ['ID']
587 );
588 }
589 unset($iblockSectionIds);
590 }
591 return self::$elementCount;
592 }
593
598 private static function getIblockSections(int $iblockId): array
599 {
600 if (self::$iblockSections === null)
601 {
602 self::loadIblockSections($iblockId);
603 }
604 return self::$iblockSections;
605 }
606
611 private static function loadIblockSections(int $iblockId): void
612 {
613 if (self::$iblockSections === null)
614 {
615 self::$iblockSections = [];
616 self::$fullIblockSections = [];
617 $sections = self::getLandingSections();
618 if (!empty($sections))
619 {
620 $iterator = Iblock\SectionTable::getList([
621 'select' => [
622 'ID',
623 'LEFT_MARGIN',
624 'RIGHT_MARGIN',
625 ],
626 'filter' => [
627 '=IBLOCK_ID' => $iblockId,
628 '@ID' => $sections,
629 ]
630 ]);
631 while ($row = $iterator->fetch())
632 {
633 $row['ID'] = (int)$row['ID'];
634 self::$iblockSections[] = $row['ID'];
635 self::$fullIblockSections[$row['ID']] = $row['ID'];
636 $sublist = Iblock\SectionTable::getList([
637 'select' => ['ID'],
638 'filter' => [
639 '=IBLOCK_ID' => $iblockId,
640 '>LEFT_MARGIN' => $row['LEFT_MARGIN'],
641 '<RIGHT_MARGIN' => $row['RIGHT_MARGIN'],
642 ]
643 ]);
644 while ($sub = $sublist->fetch())
645 {
646 $sub['ID'] = (int)$sub['ID'];
647 self::$fullIblockSections[$sub['ID']] = $sub['ID'];
648 }
649 }
650 unset($sub, $sublist, $row, $iterator);
651 }
652 unset($sections);
653 }
654 }
655
661 private static function getLandingSections(): array
662 {
663 if (self::$landingSections === null)
664 {
665 self::$landingSections = [];
666
667 if (!Loader::includeModule('landing'))
668 {
669 return self::$landingSections;
670 }
671
672 $iterator = Landing\Internals\HookDataTable::getList([
673 'runtime' => [
674 new Main\ORM\Fields\Relations\Reference(
675 'TMP_LANDING_SITE',
676 'Bitrix\Landing\Internals\SiteTable',
677 ['=this.ENTITY_ID' => 'ref.ID']
678 )
679 ],
680 'select' => ['VALUE'],
681 'filter' => [
682 '=ENTITY_TYPE' => Landing\Hook::ENTITY_TYPE_SITE,
683 '=HOOK' => 'SETTINGS',
684 '=CODE' => 'SECTION_ID',
685 '=TMP_LANDING_SITE.DELETED' => 'N',
686 ],
687 'cache' => ['ttl' => 86400],
688 ]);
689 while ($row = $iterator->fetch())
690 {
691 $id = (int)$row['VALUE'];
692 if ($id <= 0)
693 {
694 continue;
695 }
696 self::$landingSections[$id] = $id;
697 }
698 unset($id, $row, $iterator);
699
700 if (!empty(self::$landingSections))
701 {
702 self::$landingSections = array_values(self::$landingSections);
703 }
704 }
705
706 return self::$landingSections;
707 }
708
714 private static function getCrmCatalogId(): ?int
715 {
716 $result = null;
717 if (self::isCrmIncluded())
718 {
719 $result = Crm\Product\Catalog::getDefaultId();
720 }
721
722 return $result;
723 }
724
729 private static function getCrmCatalogLimit(int $iblockId): ?array
730 {
731 if (!self::isCrmIncluded())
732 {
733 return null;
734 }
735
736 return Crm\Config\State::getExceedingProductLimit($iblockId);
737 }
738
739 private static function getCrmCatalogLimitState(int $iblockId): ?array
740 {
741 if (!self::isCrmIncluded())
742 {
743 return null;
744 }
745
746 if (!method_exists('\Bitrix\Crm\Config\State', 'getProductLimitState'))
747 {
748 return null;
749 }
750
751 return Crm\Config\State::getProductLimitState($iblockId);
752 }
753
759 private static function isCrmIncluded(): bool
760 {
761 if (self::$crmIncluded === null)
762 {
763 self::$crmIncluded = Loader::includeModule('crm');
764 }
765
766 return self::$crmIncluded;
767 }
768
775 private static function checkIblockId(array $fields): bool
776 {
777 if (!isset($fields['IBLOCK_ID']))
778 {
779 return false;
780 }
781 $iblockId = (int)$fields['IBLOCK_ID'];
782 if ($iblockId <= 0)
783 {
784 return false;
785 }
786 if (!isset(self::$iblockList[$iblockId]))
787 {
788 $result = true;
789 if (!ModuleManager::isModuleInstalled('bitrix24'))
790 {
791 $result = false;
792 }
793 if ($iblockId !== self::getCrmCatalogId())
794 {
795 $result = false;
796 }
797 self::$iblockList[$iblockId] = $result;
798 }
799
800 return self::$iblockList[$iblockId];
801 }
802
816 private static function checkIblockLimit(int $iblockId): ?array
817 {
818 $result = self::getIblockLimit($iblockId);
819 if (
820 $result['LIMIT'] === 0
821 || $result['COUNT'] < $result['LIMIT']
822 )
823 {
824 return null;
825 }
826 $result['MESSAGE'] = self::getProductLimitError($result);
827 unset($result['MESSAGE_ID']);
828 $result['HELP_MESSAGE'] = Feature::getProductLimitHelpLink();
829
830 return $result;
831 }
832
845 private static function getIblockLimit(int $iblockId): array
846 {
847 $result = [
848 'COUNT' => 0,
850 'MESSAGE_ID' => 'CATALOG_STATE_ERR_PRODUCT_LIMIT_1'
851 ];
852 if ($result['LIMIT'] === 0)
853 {
854 return $result;
855 }
856 $result['COUNT'] = self::getElementCount($iblockId);
857
858 return $result;
859 }
860
867 private static function getProductLimitError(array $limit): ?string
868 {
869 if (!isset($limit['COUNT']) || !isset($limit['LIMIT']) || !isset($limit['MESSAGE_ID']))
870 {
871 return null;
872 }
873
874 return Loc::getMessage(
875 $limit['MESSAGE_ID'],
876 [
877 '#COUNT#' => $limit['COUNT'],
878 '#LIMIT#' => $limit['LIMIT']
879 ]
880 );
881 }
882
889 private static function setProductLimitError(string $errorMessage): void
890 {
891 global $APPLICATION;
892
893 $error = new \CAdminException([
894 [
895 'text' => $errorMessage,
896 ]
897 ]);
898 $APPLICATION->ThrowException($error);
899 }
900
906 public static function isProductCardSliderEnabled(): bool
907 {
909 {
910 return false;
911 }
912
913 return Main\Config\Option::get('catalog', 'product_card_slider_enabled') === 'Y';
914 }
915
921 public static function isProductBatchMethodSelected(): bool
922 {
924 {
925 return false;
926 }
927
928 return \Bitrix\Catalog\Product\Store\CostPriceCalculator::getMethod() !== '';
929 }
930}
$count
Определения admin_tab.php:4
global $APPLICATION
Определения include.php:80
static getProductLimitHelpLink()
Определения feature.php:295
static getLandingProductLimit()
Определения feature.php:82
static isMultiStoresEnabled()
Определения feature.php:138
static checkInventoryManagementFeatureByCurrentMode()
Определения feature.php:153
static getLandingLimitVariable()
Определения feature.php:235
static isStoreBatchEnabled()
Определения feature.php:230
static isMultiPriceTypesEnabled()
Определения feature.php:118
static isCommonProductProcessingEnabled()
Определения feature.php:170
static isExceededStoreLimit()
Определения state.php:133
static handlerBeforeIblockSectionUpdate(array &$fields)
Определения state.php:445
static getProductLimitState(int $iblockId)
Определения state.php:215
static isExternalCatalog()
Определения state.php:62
static handlerAfterIblockElementUpdate(array &$fields)
Определения state.php:390
static handlerAfterIblockSectionDelete(array $fields)
Определения state.php:548
static isProductCardSliderEnabled()
Определения state.php:906
static isUsedInventoryManagement()
Определения state.php:42
static getCrmExceedingProductLimit()
Определения state.php:204
static handlerAfterIblockSectionUpdate(array &$fields)
Определения state.php:522
static isShowedStoreReserve()
Определения state.php:77
static handlerAfterIblockElementAdd(array &$fields)
Определения state.php:298
static handlerAfterIblockElementDelete(array $fields)
Определения state.php:414
static getExceedingProductLimit(int $iblockId, ?int $sectionId=null)
Определения state.php:165
static handlerBeforeIblockElementAdd(array &$fields)
Определения state.php:255
static isProductBatchMethodSelected()
Определения state.php:921
static isExceededPriceTypeLimit()
Определения state.php:103
static isEnabledInventoryManagement()
Определения state.php:57
static handlerBeforeIblockElementUpdate(array &$fields)
Определения state.php:324
static isAllowedNewPriceType()
Определения state.php:118
static setIsExternalCatalog(bool $isEnabled)
Определения state.php:67
static handlerAfterIblockSectionAdd(array &$fields)
Определения state.php:428
static isAllowedNewStore()
Определения state.php:148
static get($moduleId, $name, $default="", $siteId=false)
Определения option.php:30
static set($moduleId, $name, $value="", $siteId="")
Определения option.php:261
static getList(array $parameters=array())
Определения datamanager.php:431
static getCount($filter=array(), array $cache=array())
Определения datamanager.php:516
static normalizeArrayValuesByInt(&$map, $sorted=true)
Определения collection.php:150
</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
$iblockId
Определения iblock_catalog_edit.php:30
$filter
Определения iblock_catalog_list.php:54
while($arParentIBlockProperty=$dbParentIBlockProperty->Fetch()) $errorMessage
Определения basket.php:2
$error
Определения subscription_card_product.php:20
$iterator
Определения yandex_run.php:610
$fields
Определения yandex_run.php:501