33 if (!Loader::includeModule(
"iblock") || !Loader::includeModule(
"photogallery"))
43 if (empty($queueOption))
49 $sectionId = ($queueOption[
"sectionId"] ?: 0);
50 $copiedSectionId = ($queueOption[
"copiedSectionId"] ?: 0);
51 $errorOffset = ($queueOption[
"errorOffset"] ?: 0);
54 $offset = $this->getOffset($copiedSectionId) + $errorOffset;
56 $enumRatio = ($queueOption[
"enumRatio"] ?: []);
57 $sectionsRatio = ($queueOption[
"sectionsRatio"] ?: []);
58 $mapIdsCopiedElements = ($queueOption[
"mapIdsCopiedElements"] ?: []);
62 list($elementIds, $selectedRowsCount) = $this->getElementIds($sectionId, $limit, $offset);
64 $elementCopier = $this->getElementCopier();
65 $containerCollection = $this->getContainerCollection($elementIds, $sectionsRatio, $enumRatio);
66 $result = $elementCopier->copy($containerCollection);
67 if (!$result->isSuccess())
69 $queueOption[
"errorOffset"] += $this->getErrorOffset($elementCopier);
72 $mapIdsCopiedElements = $elementCopier->getMapIdsCopiedEntity() + $mapIdsCopiedElements;
73 $queueOption[
"mapIdsCopiedElements"] = $mapIdsCopiedElements;
76 if ($selectedRowsCount < $limit)
78 $this->onAfterQueueCopy($queueOption);
84 $option[
"steps"] = $offset;
90 $this->onAfterQueueCopy($queueOption);
95 catch (\Exception $exception)
103 private function getElementIds(
int $sectionId,
int $limit,
int $offset): array
108 $sqlHelper = $connection->getSqlHelper();
110 $queryObject = $connection->query(
"SELECT ID FROM `b_iblock_element` WHERE `IBLOCK_SECTION_ID` = '".
111 $sqlHelper->forSql($sectionId).
"' ORDER BY ID ASC LIMIT ".$limit.
" OFFSET ".$offset);
112 $selectedRowsCount = $queryObject->getSelectedRowsCount();
113 while ($element = $queryObject->fetch())
115 $elementIds[] = $element[
"ID"];
118 return [$elementIds, $selectedRowsCount];
121 private function getOffset(
int $copiedSectionId): int
126 $sqlHelper = $connection->getSqlHelper();
128 $queryObject = $connection->query(
"SELECT ID FROM `b_iblock_element` WHERE `IBLOCK_SECTION_ID` = '".
129 $sqlHelper->forSql($copiedSectionId).
"' ORDER BY ID");
130 while ($element = $queryObject->fetch())
132 $elementIds[] = $element[
"ID"];
135 return count($elementIds);
138 private function getErrorOffset(EntityCopier $elementCopier): int
140 $numberIds = count($elementCopier->getMapIdsCopiedEntity());
141 $numberSuccessIds = count(array_filter($elementCopier->getMapIdsCopiedEntity()));
142 return $numberIds - $numberSuccessIds;
145 private function getContainerCollection($elementIds, array $sectionsRatio, array $enumRatio, $targetIblockId = 0)
147 $containerCollection =
new ContainerCollection();
149 foreach ($elementIds as $elementId)
151 $container =
new Container($elementId);
152 $dictionary =
new Dictionary(
154 "targetIblockId" => $targetIblockId,
155 "enumRatio" => $enumRatio,
156 "sectionsRatio" => $sectionsRatio
159 $container->setDictionary($dictionary);
161 $containerCollection[] = $container;
164 return $containerCollection;
167 private function getElementCopier()
169 $elementImplementer =
new ElementImplementer();
170 return new EntityCopier($elementImplementer);
173 private function onAfterQueueCopy(array $queueOption): void
175 $copiedSectionId = ($queueOption[
"copiedSectionId"] ?: 0);
176 if (!$copiedSectionId)
181 $queryObject = \CIBlockSection::getList([], [
182 "ID" => $copiedSectionId,
"CHECK_PERMISSIONS" =>
"N"],
false, [
"IBLOCK_ID"]);
183 if ($fields = $queryObject->fetch())
185 $iblockId = $fields[
"IBLOCK_ID"];
192 $queryObject = \CIBlockSection::getTreeList([
"IBLOCK_ID" => $iblockId], [
"ID"]);
193 while ($section = $queryObject->fetch())
195 $sectionIds[] = $section[
"ID"];
198 PClearComponentCacheEx($iblockId, $sectionIds);
209 $queueId = (string) current($queue);
210 $this->checkerName = (mb_strpos($this->checkerName, $queueId) ===
false ?
212 $this->baseName = (mb_strpos($this->baseName, $queueId) ===
false ?
214 $this->errorName = (mb_strpos($this->errorName, $queueId) ===
false ?
225 Option::set(static::$moduleId, $this->baseName, serialize($data));
233 Option::delete(static::$moduleId, [
"name" => $this->checkerName]);
234 Option::delete(static::$moduleId, [
"name" => $this->baseName]);
239 $queueId = current($queue);
240 $currentPos = array_search($queueId, $queue);
241 if ($currentPos !==
false)
243 unset($queue[$currentPos]);
244 Option::set(static::$moduleId, $this->queueName, serialize($queue));
251 return empty($queue);
256 $option = Option::get(static::$moduleId, $optionName);
257 $option = ($option !==
"" ? unserialize($option, [
'allowed_classes' =>
false]) : []);
258 return (is_array($option) ? $option : []);
263 Option::delete(static::$moduleId, [
"name" => $optionName]);
writeToLog(\Exception $exception)