Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
BaseProduct.php
1<?php
2
4
21
31{
32 private const EVENT_PREFIX = 'Bitrix\Catalog\Product\Entity::';
33
37 protected $skuRepository;
38
42 protected $skuCollection;
43
44 public function __construct(
46 ProductRepositoryContract $productRepository,
51 )
52 {
53 parent::__construct($iblockInfo, $productRepository, $propertyRepository, $imageRepository);
54 $this->sectionRepository = $sectionRepository;
55 $this->skuRepository = $skuRepository;
56
57 $this->setIblockId($this->iblockInfo->getProductIblockId());
58 $this->setType($this->iblockInfo->canHaveSku() ? ProductTable::TYPE_SKU : ProductTable::TYPE_PRODUCT);
59
60 if (SystemField\ProductMapping::isAllowed())
61 {
62 $userField = SystemField\ProductMapping::load();
63 if (!empty($userField))
64 {
65 $value = (!empty($userField['SETTINGS']['DEFAULT_VALUE']) && is_array($userField['SETTINGS']['DEFAULT_VALUE'])
66 ? $userField['SETTINGS']['DEFAULT_VALUE']
67 : null
68 );
69 if ($value === null)
70 {
72 $className = SystemField\ProductMapping::getTypeId();
73
74 $list = $className::getIdByXmlId(
75 $userField['SETTINGS']['HLBLOCK_ID'],
76 [SystemField\ProductMapping::MAP_LANDING]
77 );
78 if (isset($list[SystemField\ProductMapping::MAP_LANDING]))
79 {
80 $value = [
81 $list[SystemField\ProductMapping::MAP_LANDING],
82 ];
83 }
84 }
85 if ($value !== null)
86 {
87 $this->setField($userField['FIELD_NAME'], $value);
88 }
89 }
90 }
91 }
92
97 {
98 if ($this->sectionCollection === null)
99 {
100 // ToDo make lazy load like sku collection with iterator callback?
102 }
103
105 }
106
111 {
112 return $this->sectionRepository->getCollectionByProduct($this);
113 }
114
122 {
123 $sectionCollection->setParent($this);
124
125 $this->sectionCollection = $sectionCollection;
126
127 return $this;
128 }
129
134 {
135 if ($this->skuCollection === null)
136 {
137 $this->setSkuCollection(
138 $this->skuRepository->getCollectionByProduct($this)
139 );
140 }
141
143 }
144
149 {
150 if ($this->skuCollection === null)
151 {
152 $this->setSkuCollection(
153 $this->skuRepository->loadEagerCollectionByProduct($this)
154 );
155 }
156
158 }
159
167 {
168 $skuCollection->setParent($this);
169
170 $this->skuCollection = $skuCollection;
171
172 return $this;
173 }
174
175 public function saveInternal(): Result
176 {
177 $isNew = $this->isNew();
178
179 if ($this->getType() === ProductTable::TYPE_EMPTY_SKU)
180 {
182 }
183
184 $result = parent::saveInternal();
185 if ($result->isSuccess())
186 {
187 if ($isNew)
188 {
189 $eventId = self::EVENT_PREFIX . ORM\Data\DataManager::EVENT_ON_AFTER_ADD;
190 }
191 else
192 {
193 $eventId = self::EVENT_PREFIX . ORM\Data\DataManager::EVENT_ON_AFTER_UPDATE;
194 }
195
196 $this->sendOnAfterEvents($eventId);
197 }
198
199 return $result;
200 }
201
202 public function delete(): Result
203 {
204 $result = $this->deleteInternal();
205 if ($result->isSuccess())
206 {
207 $this->sendOnAfterEvents(self::EVENT_PREFIX . ORM\Data\DataManager::EVENT_ON_AFTER_DELETE);
208 }
209
210 return $result;
211 }
212
213 private function sendOnAfterEvents(string $eventId): void
214 {
215 $eventData = [
216 'id' => $this->getId(),
217 ];
218
219 switch ($eventId)
220 {
221 case self::EVENT_PREFIX . ORM\Data\DataManager::EVENT_ON_AFTER_ADD:
222 case self::EVENT_PREFIX . ORM\Data\DataManager::EVENT_ON_AFTER_UPDATE:
223 $eventData['fields'] = $this->getFields();
224 $type = $this->getType();
225 if (
226 $type !== ProductTable::TYPE_SKU
228 )
229 {
231 $item = $this->getSkuCollection()->getFirst();
232 if ($item !== null)
233 {
234 $eventData['fields']['PRICES'] = $item->getPriceCollection()->toArray();
235 }
236 }
237 break;
238 }
239
240 $event = new Event('catalog', $eventId, $eventData);
241 $event->send();
242 }
243
244 public function setField(string $name, $value): BaseEntity
245 {
246 if ($name === 'NAME')
247 {
248 $productName = $this->getName();
249
250 foreach ($this->getSkuCollection() as $sku)
251 {
252 if ($sku->getName() === $productName)
253 {
254 $sku->setName($value);
255 }
256 }
257 }
258
259 return parent::setField($name, $value);
260 }
261}
__construct(IblockInfo $iblockInfo, RepositoryContract $repository, PropertyRepositoryContract $propertyRepository, ImageRepositoryContract $imageRepository)
setSkuCollection(SkuCollection $skuCollection)
setSectionCollection(SectionCollection $sectionCollection)