Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
SectionRepository.php
1<?php
2
4
10
20{
22 protected $factory;
23
25 {
26 $this->factory = $factory;
27 }
28
29 public function getEntityById(int $id): ?BaseEntity
30 {
31 if ($id <= 0)
32 {
33 throw new \OutOfRangeException($id);
34 }
35
36 $entities = $this->getEntitiesBy([
37 'filter' => [
38 '=ID' => $id,
39 ],
40 ]);
41
42 return reset($entities) ?: null;
43 }
44
45 // ToDo custom load section entities by filter?
46 public function getEntitiesBy($params): array
47 {
48 $entities = [];
49
50 foreach ($this->getList((array)$params) as $item)
51 {
52 $entities[] = $this->createEntity($item);
53 }
54
55 return $entities;
56 }
57
58 public function save(BaseEntity ...$entities): Result
59 {
60 $result = new Result();
61
63 $parentEntity = null;
64 $sections = [];
65
67 foreach ($entities as $section)
68 {
69 if ($parentEntity && $parentEntity !== $section->getParent())
70 {
71 $result->addError(new Error('Saving should only be done with sections of a common parent.'));
72 }
73
74 if ($parentEntity === null)
75 {
76 $parentEntity = $section->getParent();
77 }
78
79 $sections[] = $section->getValue();
80 }
81
82 if (!$parentEntity)
83 {
84 $result->addError(new Error('Parent entity not found while saving sections.'));
85 }
86
87 if (!($parentEntity instanceof BaseProduct))
88 {
89 $result->addError(new Error(sprintf(
90 'Parent entity of section must be an instance of {%s}.',
91 BaseProduct::class
92 )));
93 }
94
95 if (!empty($sections) && $result->isSuccess())
96 {
97 \CIBlockElement::setElementSection(
98 $parentEntity->getId(),
99 $sections,
100 $parentEntity->isNew(),
101 0 // $arIBlock["RIGHTS_MODE"] === "E"? $arIBlock["ID"]: 0
102 );
103 }
104
105 return $result;
106 }
107
108 public function delete(BaseEntity ...$entities): Result
109 {
110 // ToDo: Implement delete() method.
111 return new Result();
112 }
113
115 {
116 if ($product->isNew())
117 {
118 return $this->createCollection();
119 }
120
121 $result = $this->getListByProductId($product->getId());
122
123 return $this->createCollection($result);
124 }
125
126 protected function getListByProductId(int $productId): array
127 {
128 $result = [];
129
130 $sectionIdsIterator = \CIBlockElement::getElementGroups(
131 $productId,
132 true,
133 ['ID']
134 );
135 while ($section = $sectionIdsIterator->fetch())
136 {
137 $result[] = [
138 'VALUE' => (int)$section['ID'],
139 ];
140 }
141
142 return $result;
143 }
144
145 // ToDo getList for "get by filter" sections
146 protected function getList(array $params): array
147 {
148 return [];
149 }
150
151 protected function createEntity(array $fields): Section
152 {
153 $entity = $this->factory->createEntity();
154
155 $entity->initFields($fields);
156
157 return $entity;
158 }
159
160 protected function createCollection(array $entityFields = []): SectionCollection
161 {
162 $collection = $this->factory->createCollection();
163
164 foreach ($entityFields as $fields)
165 {
166 $section = $this->createEntity($fields);
167 $collection->add($section);
168 }
169
170 return $collection;
171 }
172}
save(BaseEntity ... $entities)