Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
groupstepper.php
1<?php
3
8
9class GroupStepper extends Stepper
10{
11 protected static $moduleId = "blog";
12
13 protected $queueName = "BlogGroupQueue";
14 protected $checkerName = "BlogGroupChecker_";
15 protected $baseName = "BlogGroupStepper_";
16 protected $errorName = "BlogGroupError_";
17
24 public function execute(array &$option)
25 {
26 if (!Loader::includeModule(self::$moduleId))
27 {
28 return false;
29 }
30
31 try
32 {
33 $queue = $this->getQueue();
34 $this->setQueue($queue);
35
36 $queueOption = $this->getQueueOption();
37 if (empty($queueOption))
38 {
39 $this->deleteQueueOption();
40 return !$this->isQueueEmpty();
41 }
42
43 $executiveUserId = ($queueOption["executiveUserId"] ?? 0);
44 $groupId = ($queueOption["groupId"] ?? 0);
45 $copiedGroupId = ($queueOption["copiedGroupId"] ?? 0);
46 $errorOffset = ($queueOption["errorOffset"] ?? 0);
47
48 $limit = 10;
49 $offset = $this->getOffset($copiedGroupId) + $errorOffset;
50
51 $blogPostIds = $this->getBlogPostIdsByGroupId($groupId);
52 $count = count($blogPostIds);
53 $blogPostIds = array_slice($blogPostIds, $offset, $limit);
54 $features = ($queueOption["features"] ?: []);
55
56 if ($blogPostIds)
57 {
58 $option["count"] = $count;
59
60 $copyManager = new BlogPostManager($executiveUserId, $blogPostIds);
61 $copyManager->setChangedRights([
62 "SG" => [$groupId => $copiedGroupId]
63 ]);
64
65 $featuresToBlogPost = [];
66 if (in_array("comments", $features))
67 {
68 $featuresToBlogPost[] = "comments";
69 }
70 if (in_array("voteResult", $features))
71 {
72 $featuresToBlogPost[] = "voteResult";
73 }
74
75 $copyManager->setFeatures($featuresToBlogPost);
76
77 $result = $copyManager->startCopy();
78 if (!$result->isSuccess())
79 {
80 $queueOption["errorOffset"] += $this->getErrorOffset($copyManager);
81 $this->saveQueueOption($queueOption);
82 }
83
84 $option["steps"] = $offset;
85
86 return true;
87 }
88 else
89 {
90 $this->deleteQueueOption();
91 return !$this->isQueueEmpty();
92 }
93 }
94 catch (\Exception $exception)
95 {
96 $this->writeToLog($exception);
97 $this->deleteQueueOption();
98 return false;
99 }
100 }
101
102 private function getBlogPostIdsByGroupId($groupId)
103 {
104 $blogPostIds = [];
105
106 $queryObject = \CBlogPost::getList([], ["SOCNET_GROUP_ID" => $groupId]);
107 while ($blogPost = $queryObject->fetch())
108 {
109 $blogPostIds[] = $blogPost["ID"];
110 }
111
112 return $blogPostIds;
113 }
114
115 private function getOffset(int $copiedGroupId): int
116 {
117 $blogPostIds = $this->getBlogPostIdsByGroupId($copiedGroupId);
118 return count($blogPostIds);
119 }
120
121 private function getErrorOffset(BlogPostManager $copyManager): int
122 {
123 $numberIds = count($copyManager->getMapIdsCopiedPosts());
124 $numberSuccessIds = count(array_filter($copyManager->getMapIdsCopiedPosts()));
125 return $numberIds - $numberSuccessIds;
126 }
127
128 protected function getQueue(): array
129 {
130 return $this->getOptionData($this->queueName);
131 }
132
133 protected function setQueue(array $queue): void
134 {
135 $queueId = (string) current($queue);
136 $this->checkerName = (mb_strpos($this->checkerName, $queueId) === false ?
137 $this->checkerName.$queueId : $this->checkerName);
138 $this->baseName = (mb_strpos($this->baseName, $queueId) === false ?
139 $this->baseName.$queueId : $this->baseName);
140 $this->errorName = (mb_strpos($this->errorName, $queueId) === false ?
141 $this->errorName.$queueId : $this->errorName);
142 }
143
144 protected function getQueueOption()
145 {
146 return $this->getOptionData($this->baseName);
147 }
148
149 protected function saveQueueOption(array $data)
150 {
151 Option::set(static::$moduleId, $this->baseName, serialize($data));
152 }
153
154 protected function deleteQueueOption()
155 {
156 $queue = $this->getQueue();
157 $this->setQueue($queue);
158 $this->deleteCurrentQueue($queue);
159 Option::delete(static::$moduleId, ["name" => $this->checkerName]);
160 Option::delete(static::$moduleId, ["name" => $this->baseName]);
161 }
162
163 protected function deleteCurrentQueue(array $queue): void
164 {
165 $queueId = current($queue);
166 $currentPos = array_search($queueId, $queue);
167 if ($currentPos !== false)
168 {
169 unset($queue[$currentPos]);
170 Option::set(static::$moduleId, $this->queueName, serialize($queue));
171 }
172 }
173
174 protected function isQueueEmpty()
175 {
176 $queue = $this->getOptionData($this->queueName);
177 return empty($queue);
178 }
179
180 protected function getOptionData($optionName)
181 {
182 $option = Option::get(static::$moduleId, $optionName);
183 $option = ($option !== "" ? unserialize($option, ['allowed_classes' => false]) : []);
184 return (is_array($option) ? $option : []);
185 }
186
187 protected function deleteOption($optionName)
188 {
189 Option::delete(static::$moduleId, ["name" => $optionName]);
190 }
191}
writeToLog(\Exception $exception)
Definition stepper.php:406