Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
chain.php
1
<?php
8
namespace
Bitrix\Sender\Entity
;
9
10
use
Bitrix\Main\Localization\Loc
;
11
use
Bitrix\Sender\Internals\Model
;
12
13
Loc::loadMessages
(__FILE__);
14
19
class
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
}
Bitrix\Main\Localization\Loc
Definition
loc.php:11
Bitrix\Main\Localization\Loc\loadMessages
static loadMessages($file)
Definition
loc.php:64
Bitrix\Main\ORM\Query\Chain
Definition
chain.php:14
Bitrix\Sender\Entity\Chain\createInstanceById
static createInstanceById($id)
Definition
chain.php:231
Bitrix\Sender\Entity\Chain\removeLetter
removeLetter($letterId)
Definition
chain.php:116
Bitrix\Sender\Entity\Chain\sort
sort()
Definition
chain.php:162
Bitrix\Sender\Entity\Chain\getList
getList()
Definition
chain.php:32
Bitrix\Sender\Entity\Chain\moveUp
moveUp($letterId)
Definition
chain.php:141
Bitrix\Sender\Entity\Chain\$removeLetterList
$removeLetterList
Definition
chain.php:25
Bitrix\Sender\Entity\Chain\load
load($id)
Definition
chain.php:201
Bitrix\Sender\Entity\Chain\move
move($letterId, $offset)
Definition
chain.php:256
Bitrix\Sender\Entity\Chain\getLast
getLast()
Definition
chain.php:42
Bitrix\Sender\Entity\Chain\addLetter
addLetter($letterId)
Definition
chain.php:75
Bitrix\Sender\Entity\Chain\moveDown
moveDown($letterId)
Definition
chain.php:152
Bitrix\Sender\Entity\Chain\$letters
$letters
Definition
chain.php:22
Bitrix\Sender\Entity\Chain\shiftTime
shiftTime($letterId, $timeShift=0)
Definition
chain.php:99
Bitrix\Sender\Entity\Chain\getIdByParentId
static getIdByParentId(array $list, $parentId=null)
Definition
chain.php:242
Bitrix\Sender\Entity\Chain\getLetter
getLetter($letterId)
Definition
chain.php:56
Bitrix\Sender\Entity\Chain\save
save()
Definition
chain.php:179
Bitrix\Sender\Entity
Definition
ad.php:8
Bitrix\Sender\Internals\Model
Definition
abuse.php:3
modules
sender
lib
entity
chain.php
Создано системой
1.10.0