39 return static::getList(
42 '=ENTITY_TYPE' => static::getEntityType(),
43 '=ENTITY_ID' => $entityId
45 "select" => [
'ID',
'ORDER_PROPS_ID']
59 public function getItemByOrderPropertyId($orderPropertyId)
62 foreach ($this->collection as $propertyValue)
64 if ($propertyValue->getField(
'ORDER_PROPS_ID') == $orderPropertyId)
66 return $propertyValue;
80 public function getArray(
bool $refreshData =
false)
82 $groups = $this->getGroups($refreshData);
87 foreach ($this->collection as $propertyValue)
89 $p = $propertyValue->getProperty();
93 if ($propertyValue->getField(
"ORDER_PROPS_ID") > 0)
95 $p[
"ID"] = $propertyValue->getField(
'ORDER_PROPS_ID');
99 $p[
"ID"] =
"n".$propertyValue->getInternalIndex();
103 $value = $propertyValue->getValue();
105 $value = $propertyValue->getValueId() ? $value : ($value ? $value : $p[
'DEFAULT_VALUE']);
107 $value = array_values(Manager::asMultiple($p, $value));
109 $p[
'VALUE'] = $value;
114 return [
'groups' => $groups,
'properties' => $properties];
121 protected static function delete(array $value)
123 $result =
new Result();
125 $r = static::deleteInternal($value[
'ID']);
131 $propertyClass = static::getPropertyClassName();
133 $property = $propertyClass::getObjectById($value[
'ORDER_PROPS_ID']);
134 if ($property && isset($value[
'VALUE']))
136 $property->onValueDelete($value[
'VALUE']);
141 $result->addErrors($r->getErrors());
155 public function createItem(array $prop)
158 $propertyValueClass = static::getPropertyValueClassName();
159 $property = $propertyValueClass::create($this, $prop);
160 $this->addItem($property);
169 public function getAttribute($name)
172 foreach ($this->collection as $item)
174 $property = $item->getPropertyObject();
175 if ($property->getField($name) ===
'Y')
190 return $this->getAttribute(
'IS_EMAIL');
199 return $this->getAttribute(
'IS_PAYER');
207 return $this->getAttribute(
'IS_LOCATION');
215 return $this->getAttribute(
'IS_LOCATION4TAX');
223 return $this->getAttribute(
'IS_PROFILE_NAME');
231 return $this->getAttribute(
'IS_ZIP');
239 return $this->getAttribute(
'IS_PHONE');
247 return $this->getAttribute(
'IS_ADDRESS');
255 return $this->getAttribute(
'IS_ADDRESS_FROM');
263 return $this->getAttribute(
'IS_ADDRESS_TO');
270 public function getGroups(
bool $refreshData =
false)
275 foreach ($this->collection as $propertyValue)
277 $property = $propertyValue->getPropertyObject();
278 $group = $property->getGroupInfo($refreshData);
279 if (!isset($result[$group[
'ID']]))
281 $result[$group[
'ID']] = $group;
292 public function getPropertiesByGroupId($groupId)
296 $groups = $this->getGroups();
299 foreach ($this->collection as $propertyValue)
301 $property = $propertyValue->getPropertyObject();
307 $propertyGroupId = (int)$property->getGroupId();
308 if (!isset($groups[$propertyGroupId]))
310 $propertyGroupId = 0;
313 if ($propertyGroupId === (
int)$groupId)
315 $result[] = $propertyValue;
331 foreach ($this->collection as $propertyValue)
333 if (!$filter($propertyValue))
338 $results[] = $propertyValue;
349 $result =
new Result();
352 foreach ($this->collection as $propertyValue)
354 $r = $propertyValue->verify();
355 if (!$r->isSuccess())
357 $result->addErrors($r->getErrors());
367 public function save()
369 $result =
new Result();
376 $itemsFromDb = $this->getOriginalItemsValues();
377 foreach ($itemsFromDb as $k => $v)
386 $r = static::delete($v);
387 if (!$r->isSuccess())
389 $result->addErrors($r->getErrors());
396 foreach ($this->collection as $property)
398 $r = $property->save();
399 if (!$r->isSuccess())
401 $result->addErrors($r->getErrors());
413 protected function getOriginalItemsValues()
418 throw new ObjectNotFoundException(
'Entity not found');
422 if ($entity->getId() > 0)
424 $itemsFromDbList = static::getList(
427 "=ENTITY_ID" => $entity->getId(),
428 "=ENTITY_TYPE" => static::getEntityType()
431 "ID",
"NAME",
"CODE",
"VALUE",
"ORDER_PROPS_ID",
"ENTITY_ID",
"ENTITY_TYPE"
436 while ($itemsFromDbItem = $itemsFromDbList->fetch())
438 $itemsFromDb[$itemsFromDbItem[
"ID"]] = $itemsFromDbItem;
450 $values[
'ENTITY_REGISTRY_TYPE'] = static::getRegistryType();
454 static::getOnValueDeletedEventName(),
455 [
'VALUES' => $values]
467 return $this->getItemsByFilter(
468 function ($propertyValue) use ($propertyCode)
471 $propertyValue->getField(
'CODE') == $propertyCode
484 return empty($items) ? null :
current($items);
492 $values[
'ENTITY_REGISTRY_TYPE'] = static::getRegistryType();
496 static::getOnBeforeValueDeletedEventName(),
497 [
'VALUES' => $values]
508 return OrderPropsValueTable::delete($primary);
515 public static function getList(array $parameters = [])
517 return OrderPropsValueTable::getList($parameters);
523 public function refreshRelated(): void
526 $propertyValueClassName = static::getPropertyValueClassName();
527 $props = $propertyValueClassName::loadForEntity($this->
getEntityParent());
530 foreach ($this->collection as $propertyValue)
532 if (!$propertyValue->needDeleteOnRefresh())
537 if ($propertyValue->getId() <= 0
538 && !isset($props[$propertyValue->getPropertyId()])
541 $propertyValue->delete();
546 foreach ($props as $propertyValue)
548 if (!$this->getItemByOrderPropertyId($propertyValue->getPropertyId()))
550 $propertyValue->setCollection($this);
551 $this->addItem($propertyValue);
561 public function setValuesFromPost($post, $files)
563 $post = File::getPostWithFiles($post, $files);
565 $result =
new Result();
568 foreach ($this->collection as $property)
570 $r = $property->setValueFromPost($post);
571 if (!$r->isSuccess())
573 $result->addErrors($r->getErrors());
586 public function checkErrors($fields, $files, $skipUtils =
false)
588 $result =
new Result();
590 $fields = File::getPostWithFiles($fields, $files);
593 foreach ($this->collection as $propertyValue)
595 if ($skipUtils && $propertyValue->isUtil())
600 if ($propertyValue->getField(
'ORDER_PROPS_ID') > 0)
602 $key = $propertyValue->getField(
'ORDER_PROPS_ID');
606 $key =
"n".$propertyValue->getInternalIndex();
609 $value = isset($fields[
'PROPERTIES'][$key]) ? $fields[
'PROPERTIES'][$key] :
null;
611 if (!isset($fields[
'PROPERTIES'][$key]))
613 $value = $propertyValue->getValue();
616 $r = $propertyValue->checkValue($key, $value);
617 if (!$r->isSuccess())
619 $result->addErrors($r->getErrors());
632 public function checkRequired(array $rules, array $fields)
634 $result =
new Result();
637 foreach ($this->collection as $propertyValue)
639 if ($propertyValue->getField(
'ORDER_PROPS_ID') > 0)
641 $key = $propertyValue->getField(
'ORDER_PROPS_ID');
645 $key =
"n".$propertyValue->getInternalIndex();
648 if (!in_array($key, $rules))
653 $value = isset($fields[
'PROPERTIES'][$key]) ? $fields[
'PROPERTIES'][$key] :
null;
654 if (!isset($fields[
'PROPERTIES'][$key]))
656 $value = $propertyValue->getValue();
659 $r = $propertyValue->checkRequiredValue($key, $value);
660 if (!$r->isSuccess())
662 $result->addErrors($r->getErrors());
678 $propertiesDataList = static::getAllItemsFromDb($entityId);
680 foreach($propertiesDataList as $propertyValue)
682 $res = self::delete($propertyValue);
684 if (!$res->isSuccess())
686 $result->addErrors($res->getErrors());