1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
BaseIblockElementEntity.php
См. документацию.
1<?php
2
3namespace Bitrix\Catalog\v2;
4
5use Bitrix\Catalog\ProductTable;
6use Bitrix\Catalog\v2\Event\Event;
7use Bitrix\Catalog\v2\Fields\TypeCasters\MapTypeCaster;
8use Bitrix\Catalog\v2\Iblock\IblockInfo;
9use Bitrix\Catalog\v2\Image\HasImageCollection;
10use Bitrix\Catalog\v2\Image\ImageCollection;
11use Bitrix\Catalog\v2\Image\ImageRepositoryContract;
12use Bitrix\Catalog\v2\Property\HasPropertyCollection;
13use Bitrix\Catalog\v2\Property\PropertyCollection;
14use Bitrix\Catalog\v2\Property\PropertyRepositoryContract;
15use Bitrix\Main\Engine\CurrentUser;
16use Bitrix\Main\NotSupportedException;
17use Bitrix\Main\Result;
18
28{
30 protected $iblockInfo;
39
40 public function __construct(
42 RepositoryContract $repository,
45 )
46 {
47 parent::__construct($repository);
48 $this->iblockInfo = $iblockInfo;
49 $this->propertyRepository = $propertyRepository;
50 $this->imageRepository = $imageRepository;
51 }
52
56 public function getIblockInfo(): IblockInfo
57 {
58 return $this->iblockInfo;
59 }
60
65 {
66 if ($this->propertyCollection === null)
67 {
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 (
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 if (!$this->isNew() && $entityChanged)
316 {
317 Event::send(
318 Event::ENTITY_PRODUCT,
319 Event::METHOD_UPDATE,
320 Event::STAGE_AFTER,
321 [
322 'id' => $this->getId(),
323 ],
324 );
325 }
326
327 return $result;
328 }
329
330 protected function getFieldsMap(): array
331 {
332 return [
333 'ID' => MapTypeCaster::NULLABLE_INT,
334 'IBLOCK_ID' => MapTypeCaster::INT,
335 'NAME' => MapTypeCaster::NULLABLE_STRING,
336 'CODE' => MapTypeCaster::NULLABLE_STRING,
337 'XML_ID' => MapTypeCaster::NULLABLE_STRING,
338 'TIMESTAMP_X' => MapTypeCaster::DATETIME,
339 'MODIFIED_BY' => MapTypeCaster::NULLABLE_INT,
340 'DATE_CREATE' => MapTypeCaster::DATETIME,
341 'CREATED_BY' => MapTypeCaster::NULLABLE_INT,
342 'IBLOCK_SECTION_ID' => MapTypeCaster::NULLABLE_INT,
343 'ACTIVE' => MapTypeCaster::Y_OR_N,
344 'ACTIVE_FROM' => MapTypeCaster::DATETIME,
345 'ACTIVE_TO' => MapTypeCaster::DATETIME,
346 'SORT' => MapTypeCaster::NULLABLE_INT,
347 'PREVIEW_TEXT' => MapTypeCaster::NULLABLE_STRING,
348 'PREVIEW_TEXT_TYPE' => MapTypeCaster::NULLABLE_STRING,
349 'DETAIL_TEXT' => MapTypeCaster::NULLABLE_STRING,
350 'DETAIL_TEXT_TYPE' => MapTypeCaster::NULLABLE_STRING,
351
352 'PREVIEW_PICTURE' => static function ($value) {
353 return is_numeric($value) ? (int)$value : $value;
354 },
355 'DETAIL_PICTURE' => static function ($value) {
356 return is_numeric($value) ? (int)$value : $value;
357 },
358
359 // ToDo make immutable
360 'DETAIL_PAGE_URL' => MapTypeCaster::NOTHING,
361
362 'QUANTITY' => MapTypeCaster::NULLABLE_FLOAT,
363 'WEIGHT' => MapTypeCaster::NULLABLE_FLOAT,
364 'VAT_ID' => MapTypeCaster::NULLABLE_INT,
365 'VAT_INCLUDED' => MapTypeCaster::Y_OR_N,
366 'PURCHASING_PRICE' => MapTypeCaster::NULLABLE_FLOAT,
367 'PURCHASING_CURRENCY' => MapTypeCaster::NULLABLE_STRING,
368 'BARCODE_MULTI' => MapTypeCaster::Y_OR_N,
369 'QUANTITY_RESERVED' => MapTypeCaster::NULLABLE_FLOAT,
370 'WIDTH' => MapTypeCaster::NULLABLE_FLOAT,
371 'LENGTH' => MapTypeCaster::NULLABLE_FLOAT,
372 'HEIGHT' => MapTypeCaster::NULLABLE_FLOAT,
373 'MEASURE' => MapTypeCaster::NULLABLE_INT,
374 'TYPE' => MapTypeCaster::NULLABLE_INT,
375 'AVAILABLE' => MapTypeCaster::Y_OR_N,
376 'BUNDLE' => MapTypeCaster::Y_OR_N,
377
378 'QUANTITY_TRACE' => MapTypeCaster::Y_OR_N_OR_D,
379 'CAN_BUY_ZERO' => MapTypeCaster::Y_OR_N_OR_D,
380 'SUBSCRIBE' => MapTypeCaster::Y_OR_N_OR_D,
381
382 // TODO: change this horror
383 'UF_PRODUCT_GROUP' => MapTypeCaster::NULLABLE_INT,
384 'UF_PRODUCT_MAPPING' => MapTypeCaster::NULLABLE_MULTI_INT,
385 ];
386 }
387}
$type
Определения options.php:106
const TYPE_EMPTY_SKU
Определения product.php:75
const TYPE_SERVICE
Определения product.php:76
const TYPE_PRODUCT
Определения product.php:70
getField(string $name)
Определения BaseEntity.php:119
setImageCollection(ImageCollection $imageCollection)
Определения BaseIblockElementEntity.php:148
setPropertyCollection(PropertyCollection $propertyCollection)
Определения BaseIblockElementEntity.php:88
__construct(IblockInfo $iblockInfo, RepositoryContract $repository, PropertyRepositoryContract $propertyRepository, ImageRepositoryContract $imageRepository)
Определения BaseIblockElementEntity.php:40
</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
$name
Определения menu_edit.php:35
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393