Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
groupstepper.php
1<?php
3
6use Bitrix\Iblock\Copy\Implement\Section as SectionImplementer;
7use Bitrix\Iblock\Copy\Section as SectionCopier;
12
13class GroupStepper extends Stepper
14{
15 protected static $moduleId = "photogallery";
16
17 protected $queueName = "PhotogalleryGroupQueue";
18 protected $checkerName = "PhotogalleryGroupChecker_";
19 protected $baseName = "PhotogalleryGroupStepper_";
20 protected $errorName = "PhotogalleryGroupError_";
21
31 public function execute(array &$option)
32 {
33 if (!Loader::includeModule("iblock") || !Loader::includeModule("photogallery"))
34 {
35 return false;
36 }
37
38 try
39 {
40 $queue = $this->getQueue();
41 $this->setQueue($queue);
42 $queueOption = $this->getOptionData($this->baseName);
43
44 $copiedGroupId = ($queueOption["copiedGroupId"] ?: 0);
45 $parentSectionId = ($queueOption["parentSectionId"] ?: 0);
46 $newSectionName = ($queueOption["newSectionName"] ?: "");
47
48 if ($parentSectionId && $newSectionName)
49 {
50 $containerCollection = new ContainerCollection();
51 $containerCollection[] = new Container($parentSectionId);
52
53 $elementImplementer = new ElementImplementer(ElementImplementer::SECTION_COPY_MODE);
54 $sectionImplementer = new SectionImplementer();
55 $sectionImplementer->setChangedFields([
56 "NAME" => $newSectionName,
57 "CODE" => "group_".$copiedGroupId,
58 "SOCNET_GROUP_ID" => $copiedGroupId,
59 ]);
60 $sectionImplementer->setChangedFieldsForChildSections(["CODE" => "group_".$copiedGroupId]);
61 $sectionImplementer->setChild($elementImplementer);
62
63 $sectionCopier = new SectionCopier($sectionImplementer);
64 $sectionCopier->copy($containerCollection);
65
66 $this->deleteQueueOption();
67 return !$this->isQueueEmpty();
68 }
69 else
70 {
71 $this->deleteQueueOption();
72 return !$this->isQueueEmpty();
73 }
74 }
75 catch (\Exception $exception)
76 {
77 $this->writeToLog($exception);
78 $this->deleteQueueOption();
79 return false;
80 }
81 }
82
83 protected function getQueue(): array
84 {
85 return $this->getOptionData($this->queueName);
86 }
87
88 protected function setQueue(array $queue): void
89 {
90 $queueId = (string) current($queue);
91 $this->checkerName = (mb_strpos($this->checkerName, $queueId) === false ?
92 $this->checkerName.$queueId : $this->checkerName);
93 $this->baseName = (mb_strpos($this->baseName, $queueId) === false ?
94 $this->baseName.$queueId : $this->baseName);
95 $this->errorName = (mb_strpos($this->errorName, $queueId) === false ?
96 $this->errorName.$queueId : $this->errorName);
97 }
98
99 protected function getQueueOption()
100 {
101 return $this->getOptionData($this->baseName);
102 }
103
104 protected function saveQueueOption(array $data)
105 {
106 Option::set(static::$moduleId, $this->baseName, serialize($data));
107 }
108
109 protected function deleteQueueOption()
110 {
111 $queue = $this->getQueue();
112 $this->setQueue($queue);
113 $this->deleteCurrentQueue($queue);
114 Option::delete(static::$moduleId, ["name" => $this->checkerName]);
115 Option::delete(static::$moduleId, ["name" => $this->baseName]);
116 }
117
118 protected function deleteCurrentQueue(array $queue): void
119 {
120 $queueId = current($queue);
121 $currentPos = array_search($queueId, $queue);
122 if ($currentPos !== false)
123 {
124 unset($queue[$currentPos]);
125 Option::set(static::$moduleId, $this->queueName, serialize($queue));
126 }
127 }
128
129 protected function isQueueEmpty()
130 {
131 $queue = $this->getOptionData($this->queueName);
132 return empty($queue);
133 }
134
135 protected function getOptionData($optionName)
136 {
137 $option = Option::get(static::$moduleId, $optionName);
138 $option = ($option !== "" ? unserialize($option, ['allowed_classes' => false]) : []);
139 return (is_array($option) ? $option : []);
140 }
141
142 protected function deleteOption($optionName)
143 {
144 Option::delete(static::$moduleId, ["name" => $optionName]);
145 }
146}
writeToLog(\Exception $exception)
Definition stepper.php:406