Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Repository.php
1<?php
2
4
8
17final class Repository
18{
19 private ?string $detailUrlTemplate = null;
20
21 private bool $allowedDetailUrl = false;
22
23 public function loadProduct(int $productId): ?BaseProduct
24 {
25 $iblockId = (int)\CIBlockElement::GetIBlockByID($productId);
26 if (!$iblockId)
27 {
28 return null;
29 }
30
31 try
32 {
33 return $this->loadFromProductRepository($iblockId, $productId);
34 }
35 catch (\Bitrix\Main\SystemException $e)
36 {}
37
38 return null;
39 }
40
41 public function loadVariation(int $skuId): ?BaseSku
42 {
43 $iblockId = (int)\CIBlockElement::GetIBlockByID($skuId);
44 if (!$iblockId)
45 {
46 return null;
47 }
48
49 $iblockInfo = ServiceContainer::getIblockInfo($iblockId);
50 if (!$iblockInfo)
51 {
52 return null;
53 }
54
55 try
56 {
57 if ($iblockInfo->getProductIblockId() === $iblockId)
58 {
59 $product = $this->loadFromProductRepository($iblockId, $skuId);
60 if ($product)
61 {
62 return $product->getSkuCollection()->getFirst();
63 }
64 }
65 else
66 {
67 return $this->loadFromSkuRepository($iblockId, $skuId);
68 }
69 }
70 catch (\Bitrix\Main\SystemException $e)
71 {}
72
73 return null;
74 }
75
76 public function setAutoloadDetailUrl(bool $state): self
77 {
78 $this->allowedDetailUrl = $state;
79
80 return $this;
81 }
82
83 public function checkAutoloadDetailUrl(): bool
84 {
85 return $this->allowedDetailUrl;
86 }
87
88 public function setDetailUrlTemplate(?string $template): self
89 {
90 $this->detailUrlTemplate = $template;
91
92 $this->setAutoloadDetailUrl($template !== null);
93
94 return $this;
95 }
96
97 public function getDetailUrlTemplate(): ?string
98 {
99 return $this->detailUrlTemplate;
100 }
101
102 private function loadFromProductRepository(int $iblockId, int $productId): ?BaseProduct
103 {
104 static $repository = null;
105
106 if ($repository === null)
107 {
108 $repository = ServiceContainer::getProductRepository($iblockId);
109 if (!$repository)
110 {
111 return null;
112 }
113 }
114
115 $repository->setAutoloadDetailUrl($this->checkAutoloadDetailUrl());
116 $urlTemplate = $this->getDetailUrlTemplate();
117 if ($urlTemplate)
118 {
119 $repository->setDetailUrlTemplate($urlTemplate);
120 }
121
122 return $repository->getEntityById($productId);
123 }
124
125 private function loadFromSkuRepository(int $iblockId, int $skuId): ?BaseSku
126 {
127 static $repository = null;
128
129 if ($repository === null)
130 {
131 $repository = ServiceContainer::getSkuRepository($iblockId);
132 if (!$repository)
133 {
134 return null;
135 }
136 }
137
138 $repository->setAutoloadDetailUrl($this->checkAutoloadDetailUrl());
139 $urlTemplate = $this->getDetailUrlTemplate();
140 if ($urlTemplate)
141 {
142 $repository->setDetailUrlTemplate($urlTemplate);
143 }
144
145 return $repository->getEntityById($skuId);
146 }
147}
setDetailUrlTemplate(?string $template)