1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
basketpropertiesbase.php
См. документацию.
1<?php
2
3namespace Bitrix\Sale;
4
5use Bitrix\Main;
6use Bitrix\Main\NotImplementedException;
7use Bitrix\Main\ObjectNotFoundException;
8use Bitrix\Sale;
9use Bitrix\Main\Localization\Loc;
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
63 public static function load(BasketItemBase $basketItem)
64 {
65 $basketPropertyCollection = static::createBasketPropertiesCollectionObject();
66 $basketPropertyCollection->setBasketItem($basketItem);
67 $basketItem->setPropertyCollection($basketPropertyCollection);
68
69 if ($basketItem->getId() > 0)
70 {
71 $registry = Registry::getInstance(static::getRegistryType());
72
74 $basketPropertyItemClass = $registry->getBasketPropertyItemClassName();
75
76 $propertyList = $basketPropertyItemClass::loadForBasketItem($basketItem->getId());
78 foreach ($propertyList as $property)
79 {
80 $property->setCollection($basketPropertyCollection);
81 $basketPropertyCollection->bindItem($property);
82 }
83 }
84
85 return $basketItem->getPropertyCollection();
86 }
87
93 protected static function getBasketIdList(BasketItemCollection $basket)
94 {
95 $resultList = array();
96
98 foreach ($basket as $basketItem)
99 {
100 if ($basketItem->getId() > 0)
101 {
102 $resultList[] = $basketItem->getId();
103 }
104 }
105
106 return $resultList;
107 }
108
117 public static function loadByCollection(BasketItemCollection $collection)
118 {
119 $propertyList = [];
120
121 $basketIdList = static::getBasketIdList($collection);
122
123 if (!empty($basketIdList))
124 {
125 $registry = Registry::getInstance(static::getRegistryType());
126
128 $basketPropertyItemClass = $registry->getBasketPropertyItemClassName();
129
130 $propertyList = $basketPropertyItemClass::loadForBasket($basketIdList);
131 }
132
134 foreach ($collection as $basketItem)
135 {
136 if ($basketItem->isExistPropertyCollection())
137 {
138 continue;
139 }
140
141 $basketPropertyCollection = static::createBasketPropertiesCollectionObject();
142 $basketPropertyCollection->setBasketItem($basketItem);
143
144 if (isset($propertyList[$basketItem->getId()]))
145 {
147 foreach ($propertyList[$basketItem->getId()] as $property)
148 {
149 $property->setCollection($basketPropertyCollection);
150 $basketPropertyCollection->addItem($property);
151 }
152 }
153
154 $basketItem->setPropertyCollection($basketPropertyCollection);
155 }
156 }
157
165 public function createItem()
166 {
167 $registry = Registry::getInstance(static::getRegistryType());
168
170 $basketPropertyItemClassName = $registry->getBasketPropertyItemClassName();
171
172 $basketPropertyItem = $basketPropertyItemClassName::create($this);
173 $this->addItem($basketPropertyItem);
174
175 return $basketPropertyItem;
176 }
177
182 private function getPropertyCode(BasketPropertyItemBase $property)
183 {
184 return $property->getField('NAME')."|".$property->getField("CODE");
185 }
186
197 public function redefine(array $properties)
198 {
199 $indexList = array();
200
202 foreach($this->collection as $propertyItem)
203 {
204 $code = $this->getPropertyCode($propertyItem);
205 $indexList[$code] = $propertyItem->getId();
206 }
207
208 foreach ($properties as $value)
209 {
210 if (!is_array($value) || empty($value))
211 {
212 continue;
213 }
214
215 if (isset($value['ID']) && intval($value['ID']) > 0)
216 {
217 $propertyItem = $this->getItemById($value['ID']);
218 }
219 else
220 {
221 $propertyItem = $this->getPropertyItemByValue($value);
222 }
223
224 if (!$propertyItem)
225 {
226 $propertyItem = $this->createItem();
227 }
228 else
229 {
230 $code = $this->getPropertyCode($propertyItem);
231 if (isset($indexList[$code]))
232 {
233 unset($indexList[$code]);
234 }
235 }
236
237 $availableFields = $propertyItem::getAvailableFields();
238
239 $fields = array();
240 foreach ($value as $k => $v)
241 {
242 if (isset($availableFields[$k]))
243 {
244 $fields[$k] = $v;
245 }
246 }
247
248 $propertyItem->setFields($fields);
249 }
250
251 if (!empty($indexList))
252 {
253 foreach($indexList as $code => $id)
254 {
255 if ($id > 0)
256 {
258 if ($propertyItem = $this->getItemById($id))
259 {
260 if ($propertyItem->getField('CODE') == "CATALOG.XML_ID"
261 || $propertyItem->getField('CODE') == "PRODUCT.XML_ID"
262 )
263 {
264 continue;
265 }
266
267 $propertyItem->delete();
268 }
269 }
270 else
271 {
273 foreach ($this->collection as $propertyItem)
274 {
275 if ($propertyItem->getField('CODE') == "CATALOG.XML_ID"
276 || $propertyItem->getField('CODE') == "PRODUCT.XML_ID"
277 )
278 {
279 continue;
280 }
281
282 $propertyCode = $this->getPropertyCode($propertyItem);
283 if ($propertyCode == $code)
284 {
285 $propertyItem->delete();
286 }
287 }
288 }
289 }
290 }
291 }
292
299 public function save()
300 {
301 $result = new Sale\Result();
302
303 $itemsFromDb = [];
304
305 $isItemDeleted = $this->isAnyItemDeleted();
306 if ($isItemDeleted)
307 {
308 $basketItem = $this->getBasketItem();
309 $itemsFromDbList = static::getList(
310 [
311 "select" => ["ID"],
312 "filter" => ["BASKET_ID" => ($basketItem) ? $basketItem->getId() : 0]
313 ]
314 );
315
316 while ($itemsFromDbItem = $itemsFromDbList->fetch())
317 {
318 $itemsFromDb[$itemsFromDbItem["ID"]] = true;
319 }
320 }
321
323 foreach ($this->collection as $basketProperty)
324 {
325 $r = $basketProperty->save();
326 if (!$r->isSuccess())
327 {
328 $result->addErrors($r->getErrors());
329 }
330
331 unset($itemsFromDb[$basketProperty->getId()]);
332 }
333
334 foreach ($itemsFromDb as $basketPropertyId => $value)
335 {
336 static::delete($basketPropertyId);
337 }
338
339 if ($isItemDeleted)
340 {
341 $this->setAnyItemDeleted(false);
342 }
343
344 return $result;
345 }
346
351 public function isPropertyAlreadyExists(array $values)
352 {
353 if (!($propertyValues = $this->getPropertyValues()))
354 {
355 return false;
356 }
357
358 $requestValues = array();
359 foreach ($values as $value)
360 {
361 if (!($propertyValue = static::bringingPropertyValue($value)))
362 {
363 continue;
364 }
365
366 $requestValues[$propertyValue['CODE']] = $propertyValue["VALUE"];
367 }
368
369 if (count($requestValues) !== count($propertyValues))
370 {
371 return false;
372 }
373 else
374 {
375 foreach($requestValues as $key => $val)
376 {
377 if (!array_key_exists($key, $propertyValues) || (array_key_exists($key, $propertyValues) && $propertyValues[$key]['VALUE'] != $val))
378 {
379 return false;
380 }
381 }
382 }
383
384 return true;
385 }
386
391 public function getPropertyItemByValue(array $value)
392 {
393 if (!($propertyValue = static::bringingPropertyValue($value)))
394 {
395 return false;
396 }
397
399 foreach ($this->collection as $propertyItem)
400 {
401 $propertyItemValues = $propertyItem->getFieldValues();
402
403 if (!($propertyItemValue = static::bringingPropertyValue($propertyItemValues)))
404 {
405 continue;
406 }
407
408
409 if ($propertyItemValue['CODE'] == $propertyValue['CODE'])
410 {
411 return $propertyItem;
412 }
413 }
414
415 return false;
416 }
417
421 public function getPropertyValues()
422 {
423 $result = array();
424
426 foreach($this->collection as $property)
427 {
428 $value = $property->getFieldValues();
429 $propertyValue = static::bringingPropertyValue($value);
430 if (!$propertyValue)
431 {
432 continue;
433 }
434
436 }
437
438 return $result;
439 }
440
441
446 private static function bringingPropertyValue(array $value)
447 {
448 $result = array();
449 if (array_key_exists('VALUE', $value))
450 {
451 $propID = '';
452 if (array_key_exists('CODE', $value) && (string)$value["CODE"] !== '')
453 {
454 $propID = $value["CODE"];
455 }
456 elseif (array_key_exists('NAME', $value) && (string)$value["NAME"] !== '')
457 {
458 $propID = $value["NAME"];
459 }
460
461 $propID = (string)$propID;
462 if ($propID !== '')
463 {
464 $result = array(
465 'CODE' => $propID,
466 'VALUE' => $value['VALUE'],
467 'NAME' => $value['NAME'] ?? null,
468 'SORT' => $value['SORT'] ?? null,
469 'ID' => $value['ID'] ?? null,
470 );
471 }
472 }
473
474 return $result;
475 }
476
490 public function createClone(\SplObjectStorage $cloneEntity)
491 {
493 $basketPropertiesCollectionClone = parent::createClone($cloneEntity);
494
496 if ($basketItem = $this->basketItem)
497 {
498 if (!$cloneEntity->contains($basketItem))
499 {
500 $cloneEntity[$basketItem] = $basketItem->createClone($cloneEntity);
501 }
502
503 if ($cloneEntity->contains($basketItem))
504 {
505 $basketPropertiesCollectionClone->basketItem = $cloneEntity[$basketItem];
506 }
507 }
508
509 return $basketPropertiesCollectionClone;
510 }
511
516 public function verify()
517 {
518 $result = new Result();
519
521 foreach ($this->collection as $basketPropertyItem)
522 {
523 $r = $basketPropertyItem->verify();
524 if (!$r->isSuccess())
525 {
526 $result->addErrors($r->getErrors());
527 }
528 }
529 return $result;
530 }
531
538 public static function getList(array $parameters = array())
539 {
540 throw new NotImplementedException();
541 }
542
549 protected static function delete($primary)
550 {
551 throw new NotImplementedException();
552 }
553
566 public function setProperty(array $values)
567 {
568 $this->redefine($values);
569 }
570}
static getList(array $parameters=array())
Определения basketpropertiesbase.php:538
setBasketItem(BasketItemBase $basketItem)
Определения basketpropertiesbase.php:33
static getInstance($type)
Определения registry.php:183
</td ></tr ></table ></td ></tr ><?endif?><? $propertyIndex=0;foreach( $arGlobalProperties as $propertyCode=> $propertyValue
Определения file_new.php:729
</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
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
$value
Определения Param.php:39
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
$val
Определения options.php:1793
$k
Определения template_pdf.php:567
$fields
Определения yandex_run.php:501