1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
entitypropertyvaluecollection.php
См. документацию.
1<?php
2
3namespace Bitrix\Sale;
4
5use Bitrix\Main\ArgumentOutOfRangeException;
6use Bitrix\Main\Event;
7use Bitrix\Main\NotImplementedException;
8use Bitrix\Main\ObjectNotFoundException;
9use Bitrix\Sale\Internals\EntityCollection;
10use Bitrix\Sale\Internals\Input\File;
11use Bitrix\Sale\Internals\Input\Manager;
12use Bitrix\Sale\Internals\OrderPropsValueTable;
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 {
171 $selectedEntityPropertyValueList = [];
172
174 foreach ($this->collection as $item)
175 {
176 $property = $item->getPropertyObject();
177 if ($property->getField($name) === 'Y')
178 {
179 $selectedEntityPropertyValueList[] = $item;
180 }
181 }
182
183 if (count($selectedEntityPropertyValueList) > 1)
184 {
185 foreach ($selectedEntityPropertyValueList as $item)
186 {
187 if (!empty($item->getValue()))
188 {
189 return $item;
190 }
191 }
192 }
193
194 return $selectedEntityPropertyValueList[0] ?? null;
195 }
196
201 public function getUserEmail()
202 {
203 return $this->getAttribute('IS_EMAIL');
204 }
205
210 public function getPayerName()
211 {
212 return $this->getAttribute('IS_PAYER');
213 }
214
218 public function getDeliveryLocation()
219 {
220 return $this->getAttribute('IS_LOCATION');
221 }
222
226 public function getTaxLocation()
227 {
228 return $this->getAttribute('IS_LOCATION4TAX');
229 }
230
234 public function getProfileName()
235 {
236 return $this->getAttribute('IS_PROFILE_NAME');
237 }
238
242 public function getDeliveryLocationZip()
243 {
244 return $this->getAttribute('IS_ZIP');
245 }
246
250 public function getPhone()
251 {
252 return $this->getAttribute('IS_PHONE');
253 }
254
258 public function getAddress()
259 {
260 return $this->getAttribute('IS_ADDRESS');
261 }
262
266 public function getAddressFrom()
267 {
268 return $this->getAttribute('IS_ADDRESS_FROM');
269 }
270
274 public function getAddressTo()
275 {
276 return $this->getAttribute('IS_ADDRESS_TO');
277 }
278
283 public function getGroups(bool $refreshData = false)
284 {
285 $result = [];
286
288 foreach ($this->collection as $propertyValue)
289 {
290 $property = $propertyValue->getPropertyObject();
291 $group = $property->getGroupInfo($refreshData);
292 if (!isset($result[$group['ID']]))
293 {
294 $result[$group['ID']] = $group;
295 }
296 }
297
298 return $result;
299 }
300
305 public function getPropertiesByGroupId($groupId)
306 {
307 $result = [];
308
309 $groups = $this->getGroups();
310
312 foreach ($this->collection as $propertyValue)
313 {
314 $property = $propertyValue->getPropertyObject();
315 if (!$property)
316 {
317 continue;
318 }
319
320 $propertyGroupId = (int)$property->getGroupId();
321 if (!isset($groups[$propertyGroupId]))
322 {
323 $propertyGroupId = 0;
324 }
325
326 if ($propertyGroupId === (int)$groupId)
327 {
329 }
330 }
331
332 return $result;
333 }
334
339 public function getItemsByFilter(callable $filter)
340 {
341 $results = [];
342
344 foreach ($this->collection as $propertyValue)
345 {
347 {
348 continue;
349 }
350
351 $results[] = $propertyValue;
352 }
353
354 return $results;
355 }
356
360 public function verify()
361 {
362 $result = new Result();
363
365 foreach ($this->collection as $propertyValue)
366 {
367 $r = $propertyValue->verify();
368 if (!$r->isSuccess())
369 {
370 $result->addErrors($r->getErrors());
371 }
372 }
373
374 return $result;
375 }
376
380 public function save()
381 {
382 $result = new Result();
383
384 if (!$this->isChanged())
385 {
386 return $result;
387 }
388
389 $itemsFromDb = $this->getOriginalItemsValues();
390 foreach ($itemsFromDb as $k => $v)
391 {
392 if ($this->getItemById($k))
393 {
394 continue;
395 }
396
398
399 $r = static::delete($v);
400 if (!$r->isSuccess())
401 {
402 $result->addErrors($r->getErrors());
403 }
404
406 }
407
409 foreach ($this->collection as $property)
410 {
411 $r = $property->save();
412 if (!$r->isSuccess())
413 {
414 $result->addErrors($r->getErrors());
415 }
416 }
417
418 return $result;
419 }
420
426 protected function getOriginalItemsValues()
427 {
429 if (!$entity = $this->getEntityParent())
430 {
431 throw new ObjectNotFoundException('Entity not found');
432 }
433
434 $itemsFromDb = [];
435 if ($entity->getId() > 0)
436 {
437 $itemsFromDbList = static::getList(
438 [
439 "filter" => [
440 "=ENTITY_ID" => $entity->getId(),
441 "=ENTITY_TYPE" => static::getEntityType()
442 ],
443 "select" => [
444 "ID", "NAME", "CODE", "VALUE", "ORDER_PROPS_ID", "ENTITY_ID", "ENTITY_TYPE"
445 ]
446 ]
447 );
448
449 while ($itemsFromDbItem = $itemsFromDbList->fetch())
450 {
451 $itemsFromDb[$itemsFromDbItem["ID"]] = $itemsFromDbItem;
452 }
453 }
454
455 return $itemsFromDb;
456 }
457
461 protected function callEventOnSalePropertyValueDeleted($values)
462 {
463 $values['ENTITY_REGISTRY_TYPE'] = static::getRegistryType();
464
465 $event = new Event(
466 'sale',
467 static::getOnValueDeletedEventName(),
468 ['VALUES' => $values]
469 );
470
471 $event->send();
472 }
473
478 public function getItemsByOrderPropertyCode(string $propertyCode)
479 {
480 return $this->getItemsByFilter(
481 function ($propertyValue) use ($propertyCode)
482 {
483 return (
484 $propertyValue->getField('CODE') == $propertyCode
485 );
486 }
487 );
488 }
489
494 public function getItemByOrderPropertyCode(string $propertyCode)
495 {
496 $items = $this->getItemsByOrderPropertyCode($propertyCode);
497 return empty($items) ? null : current($items);
498 }
499
504 {
505 $values['ENTITY_REGISTRY_TYPE'] = static::getRegistryType();
506
507 $event = new Event(
508 'sale',
509 static::getOnBeforeValueDeletedEventName(),
510 ['VALUES' => $values]
511 );
512
513 $event->send();
514 }
515
519 protected static function deleteInternal($primary)
520 {
521 return OrderPropsValueTable::delete($primary);
522 }
523
528 public static function getList(array $parameters = [])
529 {
530 return OrderPropsValueTable::getList($parameters);
531 }
532
533 /*
534 * Refreshes related properties
535 */
536 public function refreshRelated(): void
537 {
539 $propertyValueClassName = static::getPropertyValueClassName();
540 $props = $propertyValueClassName::loadForEntity($this->getEntityParent());
541
543 foreach ($this->collection as $propertyValue)
544 {
545 if (!$propertyValue->needDeleteOnRefresh())
546 {
547 continue;
548 }
549
550 if ($propertyValue->getId() <= 0
551 && !isset($props[$propertyValue->getPropertyId()])
552 )
553 {
554 $propertyValue->delete();
555 }
556 }
557
559 foreach ($props as $propertyValue)
560 {
561 if (!$this->getItemByOrderPropertyId($propertyValue->getPropertyId()))
562 {
563 $propertyValue->setCollection($this);
564 $this->addItem($propertyValue);
565 }
566 }
567 }
568
574 public function setValuesFromPost($post, $files)
575 {
576 $post = File::getPostWithFiles($post, $files);
577
578 $result = new Result();
579
581 foreach ($this->collection as $property)
582 {
583 $r = $property->setValueFromPost($post);
584 if (!$r->isSuccess())
585 {
586 $result->addErrors($r->getErrors());
587 }
588 }
589
590 return $result;
591 }
592
599 public function checkErrors($fields, $files, $skipUtils = false)
600 {
601 $result = new Result();
602
603 $fields = File::getPostWithFiles($fields, $files);
604
606 foreach ($this->collection as $propertyValue)
607 {
608 if ($skipUtils && $propertyValue->isUtil())
609 {
610 continue;
611 }
612
613 if ($propertyValue->getField('ORDER_PROPS_ID') > 0)
614 {
615 $key = $propertyValue->getField('ORDER_PROPS_ID');
616 }
617 else
618 {
619 $key = "n".$propertyValue->getInternalIndex();
620 }
621
622 $value = isset($fields['PROPERTIES'][$key]) ? $fields['PROPERTIES'][$key] : null;
623
624 if (!isset($fields['PROPERTIES'][$key]))
625 {
626 $value = $propertyValue->getValue();
627 }
628
629 $r = $propertyValue->checkValue($key, $value);
630 if (!$r->isSuccess())
631 {
632 $result->addErrors($r->getErrors());
633 }
634 }
635
636 return $result;
637 }
638
645 public function checkRequired(array $rules, array $fields)
646 {
647 $result = new Result();
648
650 foreach ($this->collection as $propertyValue)
651 {
652 if ($propertyValue->getField('ORDER_PROPS_ID') > 0)
653 {
654 $key = $propertyValue->getField('ORDER_PROPS_ID');
655 }
656 else
657 {
658 $key = "n".$propertyValue->getInternalIndex();
659 }
660
661 if (!in_array($key, $rules))
662 {
663 continue;
664 }
665
666 $value = isset($fields['PROPERTIES'][$key]) ? $fields['PROPERTIES'][$key] : null;
667 if (!isset($fields['PROPERTIES'][$key]))
668 {
669 $value = $propertyValue->getValue();
670 }
671
672 $r = $propertyValue->checkRequiredValue($key, $value);
673 if (!$r->isSuccess())
674 {
675 $result->addErrors($r->getErrors());
676 }
677 }
678
679 return $result;
680 }
681
688 public static function deleteNoDemand($entityId)
689 {
690 $result = new Result();
691 $propertiesDataList = static::getAllItemsFromDb($entityId);
692
693 foreach($propertiesDataList as $propertyValue)
694 {
695 $res = self::delete($propertyValue);
696
697 if (!$res->isSuccess())
698 {
699 $result->addErrors($res->getErrors());
700 }
701 }
702
703 return $result;
704 }
705}
getItemByOrderPropertyCode(string $propertyCode)
getItemsByOrderPropertyCode(string $propertyCode)
static getList(array $parameters=[])
static getInstance($type)
Определения registry.php:183
const REGISTRY_TYPE_ORDER
Определения registry.php:16
</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
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
$entity
$p
Определения group_list_element_edit.php:23
$filter
Определения iblock_catalog_list.php:54
$groups
Определения options.php:30
$name
Определения menu_edit.php:35
getItemsByFilter(array $select, ?Filter $filter=null)
Определения itemfinder.php:88
$value
Определения Param.php:39
$files
Определения mysql_to_pgsql.php:30
Определения buffer.php:3
$entityId
Определения payment.php:4
$event
Определения prolog_after.php:141
if(empty($signedUserToken)) $key
Определения quickway.php:257
$props
Определения template.php:269
$post
Определения template.php:8
$k
Определения template_pdf.php:567
$fields
Определения yandex_run.php:501