Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
basketpropertiesbase.php
1<?php
2namespace Bitrix\Sale;
3
5use Bitrix\Main\Entity\DeleteResult;
10
11Loc::loadMessages(__FILE__);
12
18{
20 protected $basketItem;
21
25 protected function getEntityParent()
26 {
27 return $this->getBasketItem();
28 }
29
34 {
35 $this->basketItem = $basketItem;
36 }
37
41 public function getBasketItem()
42 {
43 return $this->basketItem;
44 }
45
51 private static function createBasketPropertiesCollectionObject()
52 {
53 $registry = Registry::getInstance(static::getRegistryType());
54 $basketPropertiesCollectionClassName = $registry->getBasketPropertiesCollectionClassName();
55
56 return new $basketPropertiesCollectionClassName();
57 }
58
68 public static function load(BasketItemBase $basketItem)
69 {
70 $basketPropertyCollection = static::createBasketPropertiesCollectionObject();
71 $basketPropertyCollection->setBasketItem($basketItem);
72 $basketItem->setPropertyCollection($basketPropertyCollection);
73
74 if ($basketItem->getId() > 0)
75 {
76 $registry = Registry::getInstance(static::getRegistryType());
77
79 $basketPropertyItemClass = $registry->getBasketPropertyItemClassName();
80
81 $propertyList = $basketPropertyItemClass::loadForBasketItem($basketItem->getId());
83 foreach ($propertyList as $property)
84 {
85 $property->setCollection($basketPropertyCollection);
86 $basketPropertyCollection->addItem($property);
87 }
88 }
89
90 return $basketItem->getPropertyCollection();
91 }
92
98 protected static function getBasketIdList(BasketItemCollection $basket)
99 {
100 $resultList = array();
101
103 foreach ($basket as $basketItem)
104 {
105 if ($basketItem->getId() > 0)
106 {
107 $resultList[] = $basketItem->getId();
108 }
109 }
110
111 return $resultList;
112 }
113
122 public static function loadByCollection(BasketItemCollection $collection)
123 {
124 $propertyList = [];
125
126 $basketIdList = static::getBasketIdList($collection);
127
128 if (!empty($basketIdList))
129 {
130 $registry = Registry::getInstance(static::getRegistryType());
131
133 $basketPropertyItemClass = $registry->getBasketPropertyItemClassName();
134
135 $propertyList = $basketPropertyItemClass::loadForBasket($basketIdList);
136 }
137
139 foreach ($collection as $basketItem)
140 {
141 if ($basketItem->isExistPropertyCollection())
142 {
143 continue;
144 }
145
146 $basketPropertyCollection = static::createBasketPropertiesCollectionObject();
147 $basketPropertyCollection->setBasketItem($basketItem);
148
149 if (isset($propertyList[$basketItem->getId()]))
150 {
152 foreach ($propertyList[$basketItem->getId()] as $property)
153 {
154 $property->setCollection($basketPropertyCollection);
155 $basketPropertyCollection->addItem($property);
156 }
157 }
158
159 $basketItem->setPropertyCollection($basketPropertyCollection);
160 }
161 }
162
170 public function createItem()
171 {
172 $registry = Registry::getInstance(static::getRegistryType());
173
175 $basketPropertyItemClassName = $registry->getBasketPropertyItemClassName();
176
177 $basketPropertyItem = $basketPropertyItemClassName::create($this);
178 $this->addItem($basketPropertyItem);
179
180 return $basketPropertyItem;
181 }
182
187 private function getPropertyCode(BasketPropertyItemBase $property)
188 {
189 return $property->getField('NAME')."|".$property->getField("CODE");
190 }
191
202 public function redefine(array $properties)
203 {
204 $indexList = array();
205
207 foreach($this->collection as $propertyItem)
208 {
209 $code = $this->getPropertyCode($propertyItem);
210 $indexList[$code] = $propertyItem->getId();
211 }
212
213 foreach ($properties as $value)
214 {
215 if (!is_array($value) || empty($value))
216 {
217 continue;
218 }
219
220 if (isset($value['ID']) && intval($value['ID']) > 0)
221 {
222 $propertyItem = $this->getItemById($value['ID']);
223 }
224 else
225 {
226 $propertyItem = $this->getPropertyItemByValue($value);
227 }
228
229 if (!$propertyItem)
230 {
231 $propertyItem = $this->createItem();
232 }
233 else
234 {
235 $code = $this->getPropertyCode($propertyItem);
236 if (isset($indexList[$code]))
237 {
238 unset($indexList[$code]);
239 }
240 }
241
242 $availableFields = $propertyItem::getAvailableFields();
243
244 $fields = array();
245 foreach ($value as $k => $v)
246 {
247 if (isset($availableFields[$k]))
248 {
249 $fields[$k] = $v;
250 }
251 }
252
253 $propertyItem->setFields($fields);
254 }
255
256 if (!empty($indexList))
257 {
258 foreach($indexList as $code => $id)
259 {
260 if ($id > 0)
261 {
263 if ($propertyItem = $this->getItemById($id))
264 {
265 if ($propertyItem->getField('CODE') == "CATALOG.XML_ID"
266 || $propertyItem->getField('CODE') == "PRODUCT.XML_ID"
267 )
268 {
269 continue;
270 }
271
272 $propertyItem->delete();
273 }
274 }
275 else
276 {
278 foreach ($this->collection as $propertyItem)
279 {
280 if ($propertyItem->getField('CODE') == "CATALOG.XML_ID"
281 || $propertyItem->getField('CODE') == "PRODUCT.XML_ID"
282 )
283 {
284 continue;
285 }
286
287 $propertyCode = $this->getPropertyCode($propertyItem);
288 if ($propertyCode == $code)
289 {
290 $propertyItem->delete();
291 }
292 }
293 }
294 }
295 }
296 }
297
304 public function save()
305 {
306 $result = new Sale\Result();
307
308 $itemsFromDb = [];
309
310 $isItemDeleted = $this->isAnyItemDeleted();
311 if ($isItemDeleted)
312 {
313 $basketItem = $this->getBasketItem();
314 $itemsFromDbList = static::getList(
315 [
316 "select" => ["ID"],
317 "filter" => ["BASKET_ID" => ($basketItem) ? $basketItem->getId() : 0]
318 ]
319 );
320
321 while ($itemsFromDbItem = $itemsFromDbList->fetch())
322 {
323 $itemsFromDb[$itemsFromDbItem["ID"]] = true;
324 }
325 }
326
328 foreach ($this->collection as $basketProperty)
329 {
330 $r = $basketProperty->save();
331 if (!$r->isSuccess())
332 {
333 $result->addErrors($r->getErrors());
334 }
335
336 unset($itemsFromDb[$basketProperty->getId()]);
337 }
338
339 foreach ($itemsFromDb as $basketPropertyId => $value)
340 {
341 static::delete($basketPropertyId);
342 }
343
344 if ($isItemDeleted)
345 {
346 $this->setAnyItemDeleted(false);
347 }
348
349 return $result;
350 }
351
356 public function isPropertyAlreadyExists(array $values)
357 {
358 if (!($propertyValues = $this->getPropertyValues()))
359 {
360 return false;
361 }
362
363 $requestValues = array();
364 foreach ($values as $value)
365 {
366 if (!($propertyValue = static::bringingPropertyValue($value)))
367 {
368 continue;
369 }
370
371 $requestValues[$propertyValue['CODE']] = $propertyValue["VALUE"];
372 }
373
374 if (count($requestValues) !== count($propertyValues))
375 {
376 return false;
377 }
378 else
379 {
380 foreach($requestValues as $key => $val)
381 {
382 if (!array_key_exists($key, $propertyValues) || (array_key_exists($key, $propertyValues) && $propertyValues[$key]['VALUE'] != $val))
383 {
384 return false;
385 }
386 }
387 }
388
389 return true;
390 }
391
396 public function getPropertyItemByValue(array $value)
397 {
398 if (!($propertyValue = static::bringingPropertyValue($value)))
399 {
400 return false;
401 }
402
404 foreach ($this->collection as $propertyItem)
405 {
406 $propertyItemValues = $propertyItem->getFieldValues();
407
408 if (!($propertyItemValue = static::bringingPropertyValue($propertyItemValues)))
409 {
410 continue;
411 }
412
413
414 if ($propertyItemValue['CODE'] == $propertyValue['CODE'])
415 {
416 return $propertyItem;
417 }
418 }
419
420 return false;
421 }
422
426 public function getPropertyValues()
427 {
428 $result = array();
429
431 foreach($this->collection as $property)
432 {
433 $value = $property->getFieldValues();
434 $propertyValue = static::bringingPropertyValue($value);
435 if (!$propertyValue)
436 {
437 continue;
438 }
439
440 $result[$propertyValue['CODE']] = $propertyValue;
441 }
442
443 return $result;
444 }
445
446
451 private static function bringingPropertyValue(array $value)
452 {
453 $result = array();
454 if (array_key_exists('VALUE', $value))
455 {
456 $propID = '';
457 if (array_key_exists('CODE', $value) && (string)$value["CODE"] !== '')
458 {
459 $propID = $value["CODE"];
460 }
461 elseif (array_key_exists('NAME', $value) && (string)$value["NAME"] !== '')
462 {
463 $propID = $value["NAME"];
464 }
465
466 $propID = (string)$propID;
467 if ($propID !== '')
468 {
469 $result = array(
470 'CODE' => $propID,
471 'VALUE' => $value['VALUE'],
472 'NAME' => $value['NAME'] ?? null,
473 'SORT' => $value['SORT'] ?? null,
474 'ID' => $value['ID'] ?? null,
475 );
476 }
477 }
478
479 return $result;
480 }
481
495 public function createClone(\SplObjectStorage $cloneEntity)
496 {
498 $basketPropertiesCollectionClone = parent::createClone($cloneEntity);
499
501 if ($basketItem = $this->basketItem)
502 {
503 if (!$cloneEntity->contains($basketItem))
504 {
505 $cloneEntity[$basketItem] = $basketItem->createClone($cloneEntity);
506 }
507
508 if ($cloneEntity->contains($basketItem))
509 {
510 $basketPropertiesCollectionClone->basketItem = $cloneEntity[$basketItem];
511 }
512 }
513
514 return $basketPropertiesCollectionClone;
515 }
516
521 public function verify()
522 {
523 $result = new Result();
524
526 foreach ($this->collection as $basketPropertyItem)
527 {
528 $r = $basketPropertyItem->verify();
529 if (!$r->isSuccess())
530 {
531 $result->addErrors($r->getErrors());
532 }
533 }
534 return $result;
535 }
536
543 public static function getList(array $parameters = array())
544 {
545 throw new NotImplementedException();
546 }
547
554 protected static function delete($primary)
555 {
556 throw new NotImplementedException();
557 }
558
571 public function setProperty(array $values)
572 {
573 $this->redefine($values);
574 }
575}
static loadMessages($file)
Definition loc.php:64
static getList(array $parameters=array())
static getInstance($type)
Definition registry.php:183