Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
section.php
1<?php
3
6use Bitrix\Iblock\Copy\Section as SectionCopier;
12
14{
15 const SECTION_COPY_ERROR = "SECTION_COPY_ERROR";
16
20 private $child;
21
22 private $changedFields = [];
23 private $changedFieldsForChildSections = [];
24
28 private $sectionCopier = null;
29
35 public function setChild(Child $child)
36 {
37 $this->child[] = $child;
38 }
39
45 public function setSectionCopier(SectionCopier $sectionCopier): void
46 {
47 $this->sectionCopier = $sectionCopier;
48 }
49
50 public function setChangedFields($changedFields)
51 {
52 $this->changedFields = array_merge($this->changedFields, $changedFields);
53 }
54
55 public function setChangedFieldsForChildSections($changedFieldsForChildSections)
56 {
57 $this->changedFieldsForChildSections = array_merge(
58 $this->changedFieldsForChildSections, $changedFieldsForChildSections);
59 }
60
68 public function add(Container $container, array $fields)
69 {
70 $sectionObject = new \CIBlockSection;
71
72 $result = $sectionObject->add($fields);
73
74 if (!$result)
75 {
76 if ($sectionObject->LAST_ERROR)
77 {
78 $this->result->addError(new Error($sectionObject->LAST_ERROR, self::SECTION_COPY_ERROR));
79 }
80 else
81 {
82 $this->result->addError(new Error("Unknown error", self::SECTION_COPY_ERROR));
83 }
84 }
85 return $result;
86 }
87
95 public function getFields(Container $container, $entityId)
96 {
97 $queryObject = \CIBlockSection::getList([], ["ID" => $entityId, "CHECK_PERMISSIONS" => "N"], false);
98 return (($fields = $queryObject->fetch()) ? $fields : []);
99 }
100
108 public function prepareFieldsToCopy(Container $container, array $fields)
109 {
110 if (!empty($this->changedFields))
111 {
112 $fields = $this->changeFields($fields);
113 }
114
115 if (!empty($fields["PICTURE"]))
116 {
117 $fields["PICTURE"] = \CFile::makeFileArray($fields["PICTURE"]);
118 }
119 if (!empty($fields["DETAIL_PICTURE"]))
120 {
121 $fields["DETAIL_PICTURE"] = \CFile::makeFileArray($fields["DETAIL_PICTURE"]);
122 }
123
124 if (!empty($container->getParentId()))
125 {
126 $fields["IBLOCK_SECTION_ID"] = $container->getParentId();
127 }
128
129 $fields["RIGHTS"] = $this->getRights($fields["IBLOCK_ID"], $fields["ID"]);
130
131 unset($fields["XML_ID"]);
132
133 return $fields;
134 }
135
146 public function copyChildren(Container $container, $sectionId, $copiedSectionId)
147 {
148 $results = [];
149
150 $results[] = $copyChildrenResult = $this->copyChildSections($sectionId, $copiedSectionId);
151
152 $copyResult = $copyChildrenResult->getData();
153 $sectionsRatio[$sectionId] = $this->getSectionsMapIds($copyResult);
154 $sectionsRatio[$sectionId] = $sectionsRatio[$sectionId] + [$sectionId => $copiedSectionId];
155
156 $enumRatio = [];
157
158 foreach ($this->child as $child)
159 {
160 if ($child instanceof ElementChild)
161 {
162 $child->setEnumRatio($enumRatio);
163 $child->setSectionsRatio($sectionsRatio);
164 }
165
166 $results[] = $child->copy($sectionId, $copiedSectionId);
167
168 if (method_exists($child, "getEnumRatio"))
169 {
170 $enumRatio = $child->getEnumRatio();
171 }
172 }
173
174 return $this->getResult();
175 }
176
177 private function copyChildSections(int $sectionId, int $copiedSectionId)
178 {
179 if (!$this->sectionCopier)
180 {
181 return new Result();
182 }
183
184 $this->cleanChangedFields();
185
186 $containerCollection = new ContainerCollection();
187
188 $queryObject = \CIBlockSection::getList([], [
189 "SECTION_ID" => $sectionId, "CHECK_PERMISSIONS" => "N"], false, ["ID"]);
190 while ($section = $queryObject->fetch())
191 {
192 $container = new Container($section["ID"]);
193 $container->setParentId($copiedSectionId);
194 $containerCollection[] = $container;
195 }
196
197 if (!$containerCollection->isEmpty())
198 {
199 return $this->sectionCopier->copy($containerCollection);
200 }
201
202 return new Result();
203 }
204
205 private function getSectionsMapIds(array $data): array
206 {
207 $sectionMapIds = [];
208 foreach ($data as $key => $values)
209 {
210 if ($key == get_class() && is_array($values))
211 {
212 $sectionMapIds = $sectionMapIds + $this->getSectionsMapIds($values);
213 }
214 elseif (is_int($key))
215 {
216 $sectionMapIds[$key] = $values;
217 }
218 }
219 return $sectionMapIds;
220 }
221
222 private function changeFields(array $fields)
223 {
224 foreach ($this->changedFields as $fieldId => $fieldValue)
225 {
226 if (array_key_exists($fieldId, $fields))
227 {
228 $fields[$fieldId] = $fieldValue;
229 }
230 }
231
232 foreach ($this->changedFieldsForChildSections as $fieldId => $fieldValue)
233 {
234 if (array_key_exists($fieldId, $fields))
235 {
236 $fields[$fieldId] = $fieldValue;
237 }
238 }
239
240 return $fields;
241 }
242
243 private function cleanChangedFields()
244 {
245 $this->changedFields = [];
246 }
247
248 private function getRights(int $iblockId, int $elementId)
249 {
250 $rights = [];
251
252 $objectRights = new \CIBlockSectionRights($iblockId, $elementId);
253
254 $groupCodeIgnoreList = $this->getGroupCodeIgnoreList($iblockId);
255
256 foreach ($objectRights->getRights() as $right)
257 {
258 if (!in_array($right["GROUP_CODE"], $groupCodeIgnoreList))
259 {
260 $rights["n".(count($rights))] = [
261 "GROUP_CODE" => $right["GROUP_CODE"],
262 "DO_CLEAN" => "N",
263 "TASK_ID" => $right["TASK_ID"],
264 ];
265 }
266 }
267
268 return $rights;
269 }
270
271 private function getGroupCodeIgnoreList(int $iblockId): array
272 {
273 $groupCodeIgnoreList = [];
274
275 $rightObject = new \CIBlockRights($iblockId);
276 foreach ($rightObject->getRights() as $right)
277 {
278 $groupCodeIgnoreList[] = $right["GROUP_CODE"];
279 }
280
281 return $groupCodeIgnoreList;
282 }
283}
setChangedFieldsForChildSections($changedFieldsForChildSections)
Definition section.php:55
copyChildren(Container $container, $sectionId, $copiedSectionId)
Definition section.php:146
setSectionCopier(SectionCopier $sectionCopier)
Definition section.php:45
getFields(Container $container, $entityId)
Definition section.php:95
add(Container $container, array $fields)
Definition section.php:68
prepareFieldsToCopy(Container $container, array $fields)
Definition section.php:108