Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
chain.php
1<?php
9
12
13Loc::loadMessages(__FILE__);
14
19class Chain
20{
22 protected $letters = [];
23
25 protected $removeLetterList = [];
26
32 public function getList()
33 {
34 return $this->letters;
35 }
36
42 public function getLast()
43 {
44 $letter = end($this->letters);
45 reset($this->letters);
46
47 return $letter;
48 }
49
56 public function getLetter($letterId)
57 {
58 foreach ($this->letters as $letter)
59 {
60 if ($letter->getId() == $letterId)
61 {
62 return $letter;
63 }
64 }
65
66 return null;
67 }
68
75 public function addLetter($letterId)
76 {
77 if ($this->getLetter($letterId))
78 {
79 return $this;
80 }
81
82 $letter = self::createInstanceById($letterId);
83 if ($letter)
84 {
85 $this->letters[] = $letter;
86 $this->sort();
87 }
88
89 return $this;
90 }
91
99 public function shiftTime($letterId, $timeShift = 0)
100 {
101 $letter = $this->getLetter($letterId);
102 if ($letter)
103 {
104 $letter->set('TIME_SHIFT', $timeShift);
105 }
106
107 return $this;
108 }
109
116 public function removeLetter($letterId)
117 {
118 $list = [];
119 foreach ($this->letters as $letter)
120 {
121 if ($letter->getId() == $letterId)
122 {
123 $this->removeLetterList[] = $letterId;
124 continue;
125 }
126
127 $list[] = $letter;
128 }
129 $this->letters = $list;
130 $this->sort();
131
132 return $this;
133 }
134
141 public function moveUp($letterId)
142 {
143 return $this->move($letterId, -1);
144 }
145
152 public function moveDown($letterId)
153 {
154 return $this->move($letterId, 1);
155 }
156
162 public function sort()
163 {
164 $parentId = null;
165 foreach ($this->letters as $letter)
166 {
167 $letter->set('PARENT_ID', $parentId);
168 $parentId = $letter->getId();
169 }
170
171 return $this;
172 }
173
179 public function save()
180 {
181 foreach ($this->letters as $letter)
182 {
183 $letter->save();
184 }
185
186 foreach ($this->removeLetterList as $letterId)
187 {
188 Letter::removeById($letterId);
189 }
190 $this->removeLetterList = [];
191
192 return $this;
193 }
194
201 public function load($id)
202 {
203 $list = Model\LetterTable::getList([
204 'select' => ['ID', 'PARENT_ID'],
205 'filter' => ['=CAMPAIGN_ID' => $id],
206 ])->fetchAll();
207
208 $limiter = 100;
209 $parentId = null;
210 while (--$limiter > 0)
211 {
212 $id = self::getIdByParentId($list, $parentId);
213 if (!$id)
214 {
215 break;
216 }
217
218 $letter = self::createInstanceById($id);
219 if (!$letter)
220 {
221 continue;
222 }
223
224 $this->letters[] = $letter;
225 $parentId = $id;
226 }
227
228 return $this;
229 }
230
231 protected static function createInstanceById($id)
232 {
233 $letter = Letter::createInstanceById($id);
234 if ($letter)
235 {
236 $letter->load($id);
237 }
238
239 return $letter->getId() ? $letter : null;
240 }
241
242 protected static function getIdByParentId(array $list, $parentId = null)
243 {
244 foreach ($list as $item)
245 {
246 if ($item['PARENT_ID'] == $parentId)
247 {
248 return $item['ID'];
249 break;
250 }
251 }
252
253 return null;
254 }
255
256 protected function move($letterId, $offset)
257 {
258 $letter = $this->getLetter($letterId);
259 if (!$letter)
260 {
261 return $this;
262 }
263
264 $index = array_search($letter, $this->letters, true);
265 $previousIndex = $index + $offset;
266 if (!isset($this->letters[$previousIndex]))
267 {
268 return $this;
269 }
270 $previousLetter = $this->letters[$previousIndex];
271
272 $this->letters[$previousIndex] = $letter;
273 $this->letters[$index] = $previousLetter;
274
275 $this->sort();
276
277 return $this;
278 }
279}
static loadMessages($file)
Definition loc.php:64
static createInstanceById($id)
Definition chain.php:231
move($letterId, $offset)
Definition chain.php:256
shiftTime($letterId, $timeShift=0)
Definition chain.php:99
static getIdByParentId(array $list, $parentId=null)
Definition chain.php:242