Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
entitycopier.php
1<?php
2namespace Bitrix\Main\Copy;
3
6
7class EntityCopier implements Copyable
8{
9 protected $implementer;
11
15 protected $result;
16
20 private $entitiesToCopy = [];
21
23 {
24 $this->implementer = $implementer;
25 $this->implementerName = get_class($this->implementer);
26
27 $this->result = new Result();
28 }
29
35 public function addEntityToCopy(Copyable $entity)
36 {
37 $this->entitiesToCopy[] = $entity;
38 }
39
45 protected function startCopyEntities(ContainerCollection $containerManager)
46 {
47 $results = [];
48
49 foreach ($this->entitiesToCopy as $entity)
50 {
51 $results[] = $entity->copy($containerManager);
52 }
53
54 $data = [];
55 foreach ($results as $result)
56 {
57 $data = $data + $result->getData();
58 if ($result->getErrors())
59 {
60 $this->result->addErrors($result->getErrors());
61 }
62 }
63 if ($data)
64 {
65 $this->result->setData($data);
66 }
67 }
68
75 public function copy(ContainerCollection $containerCollection)
76 {
77 $this->result->setData([]);
78
79 $result = [];
80 foreach ($containerCollection as $container)
81 {
82 $entityId = $this->getEntityIdToCopy($container);
83 if (!$entityId)
84 {
85 continue;
86 }
87
88 $fields = $this->getFields($container, $entityId);
89
90 if (empty($fields))
91 {
92 $result = $this->addToResultByName($result, [$entityId => false], $this->implementerName);
93 }
94 else
95 {
96 $dictionary = $this->getDictionary($container, $fields);
97
98 $fields = $this->prepareFieldsToCopy($container, $fields);
99
100 $copiedEntityId = $this->addEntity($container, $fields);
101 if (!$copiedEntityId)
102 {
103 $result = $this->addToResultByName($result, [$entityId => false], $this->implementerName);
104 $this->result->addErrors($this->implementer->getErrors());
105 continue;
106 }
107
108 $copyChildrenResult = $this->copyChildren($container, $entityId, $copiedEntityId);
109 if ($copyChildrenResult->getErrors())
110 {
111 $this->result->addErrors($copyChildrenResult->getErrors());
112 }
113 $result = $this->addToResult($result, $copyChildrenResult->getData());
114
115 $result = $this->addToResultByName($result, [$entityId => $copiedEntityId], $this->implementerName);
116
117 $this->setCopiedEntityId($container, $copiedEntityId);
118
119 $this->setDictionary($container, $dictionary);
120 }
121 }
122
123 $this->startCopyEntities($containerCollection);
124
125 $result = $this->addToResult($result, $this->result->getData());
126
127 $this->result->setData($result);
128
129 return $this->result;
130 }
131
137 public function getMapIdsCopiedEntity(): array
138 {
139 return $this->getMapIdsByImplementer($this->implementerName, $this->result->getData());
140 }
141
149 public function getMapIdsByImplementer(string $implementerName, array $data): array
150 {
151 $mapIds = [];
152
153 if (array_key_exists($implementerName, $data))
154 {
155 return $data[$implementerName];
156 }
157
158 return $mapIds;
159 }
160
161 protected function getEntityIdToCopy(Container $container)
162 {
163 return $container->getEntityId();
164 }
165
166 protected function getFields(Container $container, $entityId)
167 {
168 return $this->implementer->getFields($container, $entityId);
169 }
170
171 protected function getDictionary(Container $container, array $fields)
172 {
173 return $container->getDictionary();
174 }
175
176 protected function prepareFieldsToCopy(Container $container, $fields)
177 {
178 $fields = $this->implementer->prepareFieldsToCopy($container, $fields);
179
180 return $fields;
181 }
182
183 protected function addEntity(Container $container, $fields)
184 {
185 return $this->implementer->add($container, $fields);
186 }
187
188 protected function copyChildren(Container $container, $entityIdToCopy, $copiedEntityId)
189 {
190 return $this->implementer->copyChildren($container, $entityIdToCopy, $copiedEntityId);
191 }
192
193 protected function setCopiedEntityId(Container $container, $copiedEntityId)
194 {
195 $container->setCopiedEntityId($copiedEntityId);
196 }
197
198 protected function setDictionary(Container $container, Dictionary $dictionary)
199 {
200 $container->setDictionary($dictionary);
201 }
202
203 private function addToResultByName(array $result, array $mapIds, string $implementerName): array
204 {
205 if (!array_key_exists($implementerName, $result))
206 {
208 }
209
210 $result[$implementerName] += $mapIds;
211
212 return $result;
213 }
214
215 private function addToResult(array $result, array $mapIds): array
216 {
217 foreach ($mapIds as $key => $values)
218 {
219 if (array_key_exists($key, $result))
220 {
221 $result[$key] += $values;
222 }
223 else
224 {
225 $result[$key] = $values;
226 }
227 }
228
229 return $result;
230 }
231}
setDictionary(Dictionary $dictionary)
Definition container.php:80
addEntity(Container $container, $fields)
__construct(CopyImplementer $implementer)
prepareFieldsToCopy(Container $container, $fields)
copyChildren(Container $container, $entityIdToCopy, $copiedEntityId)
addEntityToCopy(Copyable $entity)
getFields(Container $container, $entityId)
startCopyEntities(ContainerCollection $containerManager)
setDictionary(Container $container, Dictionary $dictionary)
getMapIdsByImplementer(string $implementerName, array $data)
getEntityIdToCopy(Container $container)
copy(ContainerCollection $containerCollection)
setCopiedEntityId(Container $container, $copiedEntityId)
getDictionary(Container $container, array $fields)