Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
BaseIblockElementEntity.php
1<?php
2
3namespace Bitrix\Catalog\v2;
4
17
27{
29 protected $iblockInfo;
38
39 public function __construct(
41 RepositoryContract $repository,
44 )
45 {
46 parent::__construct($repository);
47 $this->iblockInfo = $iblockInfo;
48 $this->propertyRepository = $propertyRepository;
49 $this->imageRepository = $imageRepository;
50 }
51
55 public function getIblockInfo(): IblockInfo
56 {
57 return $this->iblockInfo;
58 }
59
64 {
65 if ($this->propertyCollection === null)
66 {
67 // ToDo make lazy load like sku collection with iterator callback?
69 }
70
72 }
73
78 {
79 return $this->propertyRepository->getCollectionByParent($this);
80 }
81
89 {
90 $propertyCollection->setParent($this);
91
92 $this->propertyCollection = $propertyCollection;
93
94 return $this;
95 }
96
102 protected function unsetPropertyCollection(): self
103 {
104 if ($this->propertyCollection !== null)
105 {
106 $this->propertyCollection->setParent(null);
107 $this->propertyCollection = null;
108 }
109
110 return $this;
111 }
112
117 {
118 if ($this->imageCollection === null)
119 {
121 }
122
124 }
125
130 {
131 return $this->getImageCollection();
132 }
133
138 {
139 return $this->imageRepository->getCollectionByParent($this);
140 }
141
149 {
150 $imageCollection->setParent($this);
151
152 $this->imageCollection = $imageCollection;
153
154 return $this;
155 }
156
162 protected function unsetImageCollection(): self
163 {
164 if ($this->imageCollection !== null)
165 {
166 $this->imageCollection->setParent(null);
167 $this->imageCollection = null;
168 }
169
170 return $this;
171 }
172
173 public function getIblockId(): ?int
174 {
175 return (int)$this->getField('IBLOCK_ID') ?: null;
176 }
177
178 public function setIblockId(int $iblockId): BaseEntity
179 {
180 return $this->setField('IBLOCK_ID', $iblockId);
181 }
182
183 public function setField(string $name, $value): BaseEntity
184 {
185 if ($name === 'IBLOCK_ID')
186 {
187 $iblockId = $this->getIblockId();
188
189 // ToDo make immutable field type in type caster
190 if ($iblockId !== null && $iblockId != $value)
191 {
192 throw new NotSupportedException('Iblock id field has been already initialized.');
193 }
194 }
195 elseif ($name === 'DETAIL_PICTURE' || $name === 'PREVIEW_PICTURE')
196 {
198 $image =
199 $name === 'DETAIL_PICTURE'
200 ? $imageCollection->getDetailImage()
201 : $imageCollection->getPreviewImage();
202
203 if (is_numeric($value))
204 {
205 $value = \CFile::MakeFileArray($value);
206 }
207
208 if (is_array($value))
209 {
210 $image->setFileStructure($value);
211 }
212
213 return $this;
214 }
215
216 return parent::setField($name, $value);
217 }
218
219 // ToDo make tests coverage for TYPEs
220 public function setType(int $type): BaseEntity
221 {
222 return $this->setField('TYPE', $type);
223 }
224
225 public function getType(): int
226 {
227 return (int)$this->getField('TYPE');
228 }
229
230 public function isSimple(): bool
231 {
232 $type = $this->getType();
233
234 return (
236 || $type === ProductTable::TYPE_SERVICE
238 );
239 }
240
241 public function allowConvertToSku(): bool
242 {
243 $type = $this->getType();
244
245 return (
248 );
249 }
250
251 public function setActive(bool $active): BaseEntity
252 {
253 return $this->setField('ACTIVE', $active ? 'Y' : 'N');
254 }
255
256 public function isActive(): bool
257 {
258 return $this->getField('ACTIVE') === 'Y';
259 }
260
261 public function setName($name): BaseEntity
262 {
263 return $this->setField('NAME', $name);
264 }
265
266 public function getName()
267 {
268 return $this->getField('NAME');
269 }
270
271 public function hasName(): bool
272 {
273 return $this->getName() !== null && $this->getName() !== '';
274 }
275
276 public function getDetailUrl(): string
277 {
278 return (string)$this->getField('DETAIL_PAGE_URL');
279 }
280
281 public function saveInternal(): Result
282 {
283 $entityChanged = $this->isChanged();
284 if ($entityChanged && !$this->hasChangedFields())
285 {
286 $this->setField('MODIFIED_BY', CurrentUser::get()->getId());
287 }
288
289 $propertyCollectionChanged = $this->propertyCollection && $this->propertyCollection->isChanged();
290 $imageCollectionChanged = $this->imageCollection && $this->imageCollection->isChanged();
291
292 $result = parent::saveInternal();
293
294 if ($result->isSuccess())
295 {
296 if ($entityChanged)
297 {
298 \CIBlock::clearIblockTagCache($this->getIblockId());
299 }
300
301 // ToDo reload if at least one file property changed?
302 if ($propertyCollectionChanged)
303 {
304 // hack to re-initialize saved ids from database after files saving
306 }
307
308 if ($imageCollectionChanged)
309 {
310 // hack to re-initialize saved ids from database after files saving
311 $this->unsetImageCollection();
312 }
313 }
314
315 return $result;
316 }
317
318 protected function getFieldsMap(): array
319 {
320 return [
321 'ID' => MapTypeCaster::NULLABLE_INT,
322 'IBLOCK_ID' => MapTypeCaster::INT,
323 'NAME' => MapTypeCaster::NULLABLE_STRING,
324 'CODE' => MapTypeCaster::NULLABLE_STRING,
325 'XML_ID' => MapTypeCaster::NULLABLE_STRING,
326 'TIMESTAMP_X' => MapTypeCaster::DATETIME,
327 'MODIFIED_BY' => MapTypeCaster::NULLABLE_INT,
328 'DATE_CREATE' => MapTypeCaster::DATETIME,
329 'CREATED_BY' => MapTypeCaster::NULLABLE_INT,
330 'IBLOCK_SECTION_ID' => MapTypeCaster::NULLABLE_INT,
331 'ACTIVE' => MapTypeCaster::Y_OR_N,
332 'ACTIVE_FROM' => MapTypeCaster::DATETIME,
333 'ACTIVE_TO' => MapTypeCaster::DATETIME,
334 'SORT' => MapTypeCaster::NULLABLE_INT,
335 'PREVIEW_TEXT' => MapTypeCaster::NULLABLE_STRING,
336 'PREVIEW_TEXT_TYPE' => MapTypeCaster::NULLABLE_STRING,
337 'DETAIL_TEXT' => MapTypeCaster::NULLABLE_STRING,
338 'DETAIL_TEXT_TYPE' => MapTypeCaster::NULLABLE_STRING,
339
340 'PREVIEW_PICTURE' => static function ($value) {
341 return is_numeric($value) ? (int)$value : $value;
342 },
343 'DETAIL_PICTURE' => static function ($value) {
344 return is_numeric($value) ? (int)$value : $value;
345 },
346
347 // ToDo make immutable
348 'DETAIL_PAGE_URL' => MapTypeCaster::NOTHING,
349
350 'QUANTITY' => MapTypeCaster::NULLABLE_FLOAT,
351 'WEIGHT' => MapTypeCaster::NULLABLE_FLOAT,
352 'VAT_ID' => MapTypeCaster::NULLABLE_INT,
353 'VAT_INCLUDED' => MapTypeCaster::Y_OR_N,
354 'PURCHASING_PRICE' => MapTypeCaster::NULLABLE_FLOAT,
355 'PURCHASING_CURRENCY' => MapTypeCaster::NULLABLE_STRING,
356 'BARCODE_MULTI' => MapTypeCaster::Y_OR_N,
357 'QUANTITY_RESERVED' => MapTypeCaster::NULLABLE_FLOAT,
358 'WIDTH' => MapTypeCaster::NULLABLE_FLOAT,
359 'LENGTH' => MapTypeCaster::NULLABLE_FLOAT,
360 'HEIGHT' => MapTypeCaster::NULLABLE_FLOAT,
361 'MEASURE' => MapTypeCaster::NULLABLE_INT,
362 'TYPE' => MapTypeCaster::NULLABLE_INT,
363 'AVAILABLE' => MapTypeCaster::Y_OR_N,
364 'BUNDLE' => MapTypeCaster::Y_OR_N,
365
366 'QUANTITY_TRACE' => MapTypeCaster::Y_OR_N_OR_D,
367 'CAN_BUY_ZERO' => MapTypeCaster::Y_OR_N_OR_D,
368 'SUBSCRIBE' => MapTypeCaster::Y_OR_N_OR_D,
369
370 // TODO: change this horror
371 'UF_PRODUCT_GROUP' => MapTypeCaster::NULLABLE_INT,
372 'UF_PRODUCT_MAPPING' => MapTypeCaster::NULLABLE_MULTI_INT,
373 ];
374 }
375}
setImageCollection(ImageCollection $imageCollection)
setPropertyCollection(PropertyCollection $propertyCollection)
__construct(IblockInfo $iblockInfo, RepositoryContract $repository, PropertyRepositoryContract $propertyRepository, ImageRepositoryContract $imageRepository)