Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
groupstepper.php
1<?php
3
11
12class GroupStepper extends Stepper
13{
14 protected static $moduleId = "lists";
15
16 protected $queueName = "ListsGroupQueue";
17 protected $checkerName = "ListsGroupChecker_";
18 protected $baseName = "ListsGroupStepper_";
19 protected $errorName = "ListsGroupError_";
20
30 public function execute(array &$option)
31 {
32 if (!Loader::includeModule(self::$moduleId))
33 {
34 return false;
35 }
36
37 try
38 {
39 $queue = $this->getQueue();
40 $this->setQueue($queue);
41 $queueOption = $this->getQueueOption();
42 if (empty($queueOption))
43 {
44 $this->deleteQueueOption();
45 return !$this->isQueueEmpty();
46 }
47
48 $groupId = ($queueOption["groupId"] ?: 0);
49 $copiedGroupId = ($queueOption["copiedGroupId"] ?: 0);
50 $iblockTypeId = ($queueOption["iblockTypeId"] ?: "");
51 $errorOffset = ($queueOption["errorOffset"] ?: 0);
52
53 $limit = 5;
54 $offset = $this->getOffset($iblockTypeId, $copiedGroupId) + $errorOffset;
55
56 $iblockIds = $this->getIblockIdsToCopy($iblockTypeId, $groupId);
57 $count = count($iblockIds);
58 $iblockIds = array_slice($iblockIds, $offset, $limit);
59 $features = ($queueOption["features"] ?: []);
60
61 if ($iblockIds)
62 {
63 $option["count"] = $count;
64
65 $copyManager = new Manager($iblockTypeId, $iblockIds, $groupId);
66 $copyManager->setTargetLocation($iblockTypeId, $copiedGroupId);
67 $copyManager->setIblockImplementer(new Iblock());
68 $copyManager->setFieldImplementer(new Field());
69 $copyManager->setWorkflowImplementer(new Workflow($iblockTypeId));
70
71 if (!in_array("field", $features))
72 {
73 $copyManager->removeFeature("field");
74 }
75 if (!in_array("section", $features))
76 {
77 $copyManager->removeFeature("section");
78 }
79 if (!in_array("element", $features))
80 {
81 $copyManager->removeFeature("element");
82 }
83 if (!in_array("workflow", $features))
84 {
85 $copyManager->removeFeature("workflow");
86 }
87
88 $result = $copyManager->startCopy();
89 if (!$result->isSuccess())
90 {
91 $queueOption["errorOffset"] += $this->getErrorOffset($copyManager);
92 $this->saveQueueOption($queueOption);
93 }
94
95 $option["steps"] = $offset;
96
97 return true;
98 }
99 else
100 {
101 $this->deleteQueueOption();
102 return !$this->isQueueEmpty();
103 }
104 }
105 catch (\Exception $exception)
106 {
107 $this->writeToLog($exception);
108 $this->deleteQueueOption();
109 return false;
110 }
111 }
112
113 private function getIblockIdsToCopy($iblockTypeId, $groupId)
114 {
115 $iblockIds = [];
116
117 $filter = [
118 "ACTIVE" => "Y",
119 "TYPE" => $iblockTypeId,
120 "CHECK_PERMISSIONS" => "N",
121 "=SOCNET_GROUP_ID" => $groupId
122 ];
123
124 $queryObject = \CIBlock::getList([], $filter);
125 while ($iblock = $queryObject->fetch())
126 {
127 $iblockIds[] = $iblock["ID"];
128 }
129
130 return $iblockIds;
131 }
132
133 private function getOffset(string $iblockTypeId, int $copiedGroupId): int
134 {
135 $iblockIds = $this->getIblockIdsToCopy($iblockTypeId, $copiedGroupId);
136 return count($iblockIds);
137 }
138
139 private function getErrorOffset(Manager $copyManager): int
140 {
141 $numberIds = count($copyManager->getMapIdsCopiedEntity());
142 $numberSuccessIds = count(array_filter($copyManager->getMapIdsCopiedEntity()));
143 return $numberIds - $numberSuccessIds;
144 }
145
146 protected function getQueue(): array
147 {
148 return $this->getOptionData($this->queueName);
149 }
150
151 protected function setQueue(array $queue): void
152 {
153 $queueId = (string) current($queue);
154 $this->checkerName = (mb_strpos($this->checkerName, $queueId) === false ?
155 $this->checkerName.$queueId : $this->checkerName);
156 $this->baseName = (mb_strpos($this->baseName, $queueId) === false ?
157 $this->baseName.$queueId : $this->baseName);
158 $this->errorName = (mb_strpos($this->errorName, $queueId) === false ?
159 $this->errorName.$queueId : $this->errorName);
160 }
161
162 protected function getQueueOption()
163 {
164 return $this->getOptionData($this->baseName);
165 }
166
167 protected function saveQueueOption(array $data)
168 {
169 Option::set(static::$moduleId, $this->baseName, serialize($data));
170 }
171
172 protected function deleteQueueOption()
173 {
174 $queue = $this->getQueue();
175 $this->setQueue($queue);
176 $this->deleteCurrentQueue($queue);
177 Option::delete(static::$moduleId, ["name" => $this->checkerName]);
178 Option::delete(static::$moduleId, ["name" => $this->baseName]);
179 }
180
181 protected function deleteCurrentQueue(array $queue): void
182 {
183 $queueId = current($queue);
184 $currentPos = array_search($queueId, $queue);
185 if ($currentPos !== false)
186 {
187 unset($queue[$currentPos]);
188 Option::set(static::$moduleId, $this->queueName, serialize($queue));
189 }
190 }
191
192 protected function isQueueEmpty()
193 {
194 $queue = $this->getOptionData($this->queueName);
195 return empty($queue);
196 }
197
198 protected function getOptionData($optionName)
199 {
200 $option = Option::get(static::$moduleId, $optionName);
201 $option = ($option !== "" ? unserialize($option) : []);
202 return (is_array($option) ? $option : []);
203 }
204
205 protected function deleteOption($optionName)
206 {
207 Option::delete(static::$moduleId, ["name" => $optionName]);
208 }
209}
writeToLog(\Exception $exception)
Definition stepper.php:406