Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
entitypropertyvaluecollection.php
1<?php
2
3namespace Bitrix\Sale;
4
13
15{
16 abstract protected static function getOnValueDeletedEventName(): string;
17 abstract protected static function getOnBeforeValueDeletedEventName(): string;
21 abstract protected static function getPropertyClassName(): string;
22
26 abstract protected static function getEntityType(): string;
27
31 abstract protected static function getPropertyValueClassName(): string;
32
37 protected static function getAllItemsFromDb(int $entityId): array
38 {
39 return static::getList(
40 [
41 "filter" => [
42 '=ENTITY_TYPE' => static::getEntityType(),
43 '=ENTITY_ID' => $entityId
44 ],
45 "select" => ['ID', 'ORDER_PROPS_ID']
46 ]
47 )->fetchAll();
48 }
49
50 public function getEntityParentId(): int
51 {
52 return $this->getEntityParent()->getId();
53 }
54
59 public function getItemByOrderPropertyId($orderPropertyId)
60 {
62 foreach ($this->collection as $propertyValue)
63 {
64 if ($propertyValue->getField('ORDER_PROPS_ID') == $orderPropertyId)
65 {
66 return $propertyValue;
67 }
68 }
69
70 return null;
71 }
72
80 public function getArray(bool $refreshData = false)
81 {
82 $groups = $this->getGroups($refreshData);
83
84 $properties = [];
85
87 foreach ($this->collection as $propertyValue)
88 {
89 $p = $propertyValue->getProperty();
90
91 if (!isset($p["ID"]))
92 {
93 if ($propertyValue->getField("ORDER_PROPS_ID") > 0)
94 {
95 $p["ID"] = $propertyValue->getField('ORDER_PROPS_ID');
96 }
97 else
98 {
99 $p["ID"] = "n".$propertyValue->getInternalIndex();
100 }
101 }
102
103 $value = $propertyValue->getValue();
104
105 $value = $propertyValue->getValueId() ? $value : ($value ? $value : $p['DEFAULT_VALUE']);
106
107 $value = array_values(Manager::asMultiple($p, $value));
108
109 $p['VALUE'] = $value;
110
111 $properties[] = $p;
112 }
113
114 return ['groups' => $groups, 'properties' => $properties];
115 }
116
121 protected static function delete(array $value)
122 {
123 $result = new Result();
124
125 $r = static::deleteInternal($value['ID']);
126
127 if ($r->isSuccess())
128 {
129 $registry = Registry::getInstance(static::getRegistryType());
130
131 $propertyClass = static::getPropertyClassName();
133 $property = $propertyClass::getObjectById($value['ORDER_PROPS_ID']);
134 if ($property && isset($value['VALUE']))
135 {
136 $property->onValueDelete($value['VALUE']);
137 }
138 }
139 else
140 {
141 $result->addErrors($r->getErrors());
142 }
143
144 return $result;
145 }
146
150 public static function getRegistryType()
151 {
153 }
154
155 public function createItem(array $prop)
156 {
158 $propertyValueClass = static::getPropertyValueClassName();
159 $property = $propertyValueClass::create($this, $prop);
160 $this->addItem($property);
161
162 return $property;
163 }
164
169 public function getAttribute($name)
170 {
172 foreach ($this->collection as $item)
173 {
174 $property = $item->getPropertyObject();
175 if ($property->getField($name) === 'Y')
176 {
177 return $item;
178 }
179 }
180
181 return null;
182 }
183
188 public function getUserEmail()
189 {
190 return $this->getAttribute('IS_EMAIL');
191 }
192
197 public function getPayerName()
198 {
199 return $this->getAttribute('IS_PAYER');
200 }
201
205 public function getDeliveryLocation()
206 {
207 return $this->getAttribute('IS_LOCATION');
208 }
209
213 public function getTaxLocation()
214 {
215 return $this->getAttribute('IS_LOCATION4TAX');
216 }
217
221 public function getProfileName()
222 {
223 return $this->getAttribute('IS_PROFILE_NAME');
224 }
225
229 public function getDeliveryLocationZip()
230 {
231 return $this->getAttribute('IS_ZIP');
232 }
233
237 public function getPhone()
238 {
239 return $this->getAttribute('IS_PHONE');
240 }
241
245 public function getAddress()
246 {
247 return $this->getAttribute('IS_ADDRESS');
248 }
249
253 public function getAddressFrom()
254 {
255 return $this->getAttribute('IS_ADDRESS_FROM');
256 }
257
261 public function getAddressTo()
262 {
263 return $this->getAttribute('IS_ADDRESS_TO');
264 }
265
270 public function getGroups(bool $refreshData = false)
271 {
272 $result = [];
273
275 foreach ($this->collection as $propertyValue)
276 {
277 $property = $propertyValue->getPropertyObject();
278 $group = $property->getGroupInfo($refreshData);
279 if (!isset($result[$group['ID']]))
280 {
281 $result[$group['ID']] = $group;
282 }
283 }
284
285 return $result;
286 }
287
292 public function getPropertiesByGroupId($groupId)
293 {
294 $result = [];
295
296 $groups = $this->getGroups();
297
299 foreach ($this->collection as $propertyValue)
300 {
301 $property = $propertyValue->getPropertyObject();
302 if (!$property)
303 {
304 continue;
305 }
306
307 $propertyGroupId = (int)$property->getGroupId();
308 if (!isset($groups[$propertyGroupId]))
309 {
310 $propertyGroupId = 0;
311 }
312
313 if ($propertyGroupId === (int)$groupId)
314 {
315 $result[] = $propertyValue;
316 }
317 }
318
319 return $result;
320 }
321
326 public function getItemsByFilter(callable $filter)
327 {
328 $results = [];
329
331 foreach ($this->collection as $propertyValue)
332 {
333 if (!$filter($propertyValue))
334 {
335 continue;
336 }
337
338 $results[] = $propertyValue;
339 }
340
341 return $results;
342 }
343
347 public function verify()
348 {
349 $result = new Result();
350
352 foreach ($this->collection as $propertyValue)
353 {
354 $r = $propertyValue->verify();
355 if (!$r->isSuccess())
356 {
357 $result->addErrors($r->getErrors());
358 }
359 }
360
361 return $result;
362 }
363
367 public function save()
368 {
369 $result = new Result();
370
371 if (!$this->isChanged())
372 {
373 return $result;
374 }
375
376 $itemsFromDb = $this->getOriginalItemsValues();
377 foreach ($itemsFromDb as $k => $v)
378 {
379 if ($this->getItemById($k))
380 {
381 continue;
382 }
383
385
386 $r = static::delete($v);
387 if (!$r->isSuccess())
388 {
389 $result->addErrors($r->getErrors());
390 }
391
393 }
394
396 foreach ($this->collection as $property)
397 {
398 $r = $property->save();
399 if (!$r->isSuccess())
400 {
401 $result->addErrors($r->getErrors());
402 }
403 }
404
405 return $result;
406 }
407
413 protected function getOriginalItemsValues()
414 {
416 if (!$entity = $this->getEntityParent())
417 {
418 throw new ObjectNotFoundException('Entity not found');
419 }
420
421 $itemsFromDb = [];
422 if ($entity->getId() > 0)
423 {
424 $itemsFromDbList = static::getList(
425 [
426 "filter" => [
427 "=ENTITY_ID" => $entity->getId(),
428 "=ENTITY_TYPE" => static::getEntityType()
429 ],
430 "select" => [
431 "ID", "NAME", "CODE", "VALUE", "ORDER_PROPS_ID", "ENTITY_ID", "ENTITY_TYPE"
432 ]
433 ]
434 );
435
436 while ($itemsFromDbItem = $itemsFromDbList->fetch())
437 {
438 $itemsFromDb[$itemsFromDbItem["ID"]] = $itemsFromDbItem;
439 }
440 }
441
442 return $itemsFromDb;
443 }
444
448 protected function callEventOnSalePropertyValueDeleted($values)
449 {
450 $values['ENTITY_REGISTRY_TYPE'] = static::getRegistryType();
451
452 $event = new Event(
453 'sale',
454 static::getOnValueDeletedEventName(),
455 ['VALUES' => $values]
456 );
457
458 $event->send();
459 }
460
465 public function getItemsByOrderPropertyCode(string $propertyCode)
466 {
467 return $this->getItemsByFilter(
468 function ($propertyValue) use ($propertyCode)
469 {
470 return (
471 $propertyValue->getField('CODE') == $propertyCode
472 );
473 }
474 );
475 }
476
481 public function getItemByOrderPropertyCode(string $propertyCode)
482 {
483 $items = $this->getItemsByOrderPropertyCode($propertyCode);
484 return empty($items) ? null : current($items);
485 }
486
491 {
492 $values['ENTITY_REGISTRY_TYPE'] = static::getRegistryType();
493
494 $event = new Event(
495 'sale',
496 static::getOnBeforeValueDeletedEventName(),
497 ['VALUES' => $values]
498 );
499
500 $event->send();
501 }
502
506 protected static function deleteInternal($primary)
507 {
508 return OrderPropsValueTable::delete($primary);
509 }
510
515 public static function getList(array $parameters = [])
516 {
517 return OrderPropsValueTable::getList($parameters);
518 }
519
520 /*
521 * Refreshes related properties
522 */
523 public function refreshRelated(): void
524 {
526 $propertyValueClassName = static::getPropertyValueClassName();
527 $props = $propertyValueClassName::loadForEntity($this->getEntityParent());
528
530 foreach ($this->collection as $propertyValue)
531 {
532 if (!$propertyValue->needDeleteOnRefresh())
533 {
534 continue;
535 }
536
537 if ($propertyValue->getId() <= 0
538 && !isset($props[$propertyValue->getPropertyId()])
539 )
540 {
541 $propertyValue->delete();
542 }
543 }
544
546 foreach ($props as $propertyValue)
547 {
548 if (!$this->getItemByOrderPropertyId($propertyValue->getPropertyId()))
549 {
550 $propertyValue->setCollection($this);
551 $this->addItem($propertyValue);
552 }
553 }
554 }
555
561 public function setValuesFromPost($post, $files)
562 {
563 $post = File::getPostWithFiles($post, $files);
564
565 $result = new Result();
566
568 foreach ($this->collection as $property)
569 {
570 $r = $property->setValueFromPost($post);
571 if (!$r->isSuccess())
572 {
573 $result->addErrors($r->getErrors());
574 }
575 }
576
577 return $result;
578 }
579
586 public function checkErrors($fields, $files, $skipUtils = false)
587 {
588 $result = new Result();
589
590 $fields = File::getPostWithFiles($fields, $files);
591
593 foreach ($this->collection as $propertyValue)
594 {
595 if ($skipUtils && $propertyValue->isUtil())
596 {
597 continue;
598 }
599
600 if ($propertyValue->getField('ORDER_PROPS_ID') > 0)
601 {
602 $key = $propertyValue->getField('ORDER_PROPS_ID');
603 }
604 else
605 {
606 $key = "n".$propertyValue->getInternalIndex();
607 }
608
609 $value = isset($fields['PROPERTIES'][$key]) ? $fields['PROPERTIES'][$key] : null;
610
611 if (!isset($fields['PROPERTIES'][$key]))
612 {
613 $value = $propertyValue->getValue();
614 }
615
616 $r = $propertyValue->checkValue($key, $value);
617 if (!$r->isSuccess())
618 {
619 $result->addErrors($r->getErrors());
620 }
621 }
622
623 return $result;
624 }
625
632 public function checkRequired(array $rules, array $fields)
633 {
634 $result = new Result();
635
637 foreach ($this->collection as $propertyValue)
638 {
639 if ($propertyValue->getField('ORDER_PROPS_ID') > 0)
640 {
641 $key = $propertyValue->getField('ORDER_PROPS_ID');
642 }
643 else
644 {
645 $key = "n".$propertyValue->getInternalIndex();
646 }
647
648 if (!in_array($key, $rules))
649 {
650 continue;
651 }
652
653 $value = isset($fields['PROPERTIES'][$key]) ? $fields['PROPERTIES'][$key] : null;
654 if (!isset($fields['PROPERTIES'][$key]))
655 {
656 $value = $propertyValue->getValue();
657 }
658
659 $r = $propertyValue->checkRequiredValue($key, $value);
660 if (!$r->isSuccess())
661 {
662 $result->addErrors($r->getErrors());
663 }
664 }
665
666 return $result;
667 }
668
675 public static function deleteNoDemand($entityId)
676 {
677 $result = new Result();
678 $propertiesDataList = static::getAllItemsFromDb($entityId);
679
680 foreach($propertiesDataList as $propertyValue)
681 {
682 $res = self::delete($propertyValue);
683
684 if (!$res->isSuccess())
685 {
686 $result->addErrors($res->getErrors());
687 }
688 }
689
690 return $result;
691 }
692}
static getInstance($type)
Definition registry.php:183
getItemsByFilter(array $select, ?Filter $filter=null)