Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
queue.php
1
<?php
8
namespace
Bitrix\Sender\Internals
;
9
10
use
Bitrix\Main\Application
;
11
use
Bitrix\Main\Loader
;
12
use
Bitrix\Main\Localization\Loc
;
13
use
Bitrix\Main\ModuleManager
;
14
use
Bitrix\Main\UserTable
;
15
16
use
Bitrix\Sender\Internals\Model\QueueTable
;
17
18
Loc::loadMessages
(__FILE__);
19
25
class
Queue
26
{
27
protected
$list
= [];
28
protected
$lastItem
=
null
;
29
protected
$previousStack
= [];
30
protected
$isLastItemRestored
=
false
;
31
protected
$type
=
null
;
32
protected
$id
=
null
;
33
34
protected
$isWorkTimeCheckEnabled
=
false
;
35
protected
$isUserCheckEnabled
=
false
;
36
protected
$isAutoSaveEnabled
=
true
;
37
45
public
function
__construct
(
$type
,
$id
, array
$list
= [])
46
{
47
$this->type =
$type
;
48
$this->
setId
(
$id
);
49
$this->
setValues
(
$list
);
50
}
51
57
public
function
disableAutoSave
()
58
{
59
$this->isAutoSaveEnabled =
false
;
60
return
$this;
61
}
62
68
public
function
enableWorkTimeCheck
()
69
{
70
$this->
isWorkTimeCheckEnabled
=
true
;
71
return
$this;
72
}
73
79
public
function
enableUserCheck
()
80
{
81
$this->isUserCheckEnabled =
true
;
82
return
$this;
83
}
84
90
public
function
isWorkTimeCheckEnabled
()
91
{
92
return
$this->isWorkTimeCheckEnabled
;
93
}
94
100
public
function
getId
()
101
{
102
return
$this->id
;
103
}
104
111
public
function
setId
(
$id
)
112
{
113
$this->
id
=
$id
;
114
return
$this;
115
}
116
123
public
function
setValues
(array
$list
)
124
{
125
$this->list =
$list
;
126
$this->previousStack = [];
127
return
$this;
128
}
129
135
public
function
getValues
()
136
{
137
return
$this->list
;
138
}
139
145
public
function
delete
()
146
{
147
QueueTable::delete([
'ENTITY_TYPE'
=> $this->type,
'ENTITY_ID'
=> $this->
id
]);
148
return
$this;
149
}
150
156
public
static
function
isSupportedWorkTime
()
157
{
158
return
ModuleManager::isModuleInstalled(
'timeman'
);
159
}
160
166
public
function
current
()
167
{
168
if
(!$this->isLastItemRestored)
169
{
170
$this->
restore
();
171
$this->isLastItemRestored =
true
;
172
}
173
174
return
$this->lastItem
;
175
}
176
182
public
function
save
()
183
{
184
$sqlHelper =
Application::getConnection
()->getSqlHelper();
185
$item = $this->
current
();
186
187
if
($item)
188
{
189
$insert = [
190
'ENTITY_TYPE'
=>
$this->type
,
191
'ENTITY_ID'
=>
$this->id
,
192
'LAST_ITEM'
=> $item,
193
];
194
$update = [
195
'LAST_ITEM'
=> $item,
196
];
197
$tableName =
QueueTable::getTableName
();
198
$queries = $sqlHelper->prepareMerge($tableName,
QueueTable::getConflictFields
(), $insert, $update);
199
Application::getConnection
()->query($queries[0]);
200
}
201
else
202
{
203
$this->
delete
();
204
}
205
206
return
$this;
207
}
208
214
public
function
restore
()
215
{
216
$row = QueueTable::getRow([
217
'select'
=> [
'LAST_ITEM'
],
218
'filter'
=> [
'=ENTITY_TYPE'
=> $this->type,
'=ENTITY_ID'
=> $this->
id
]
219
]);
220
$this->
setLastItem
($row ? $row[
'LAST_ITEM'
] :
null
);
221
222
return
$this;
223
}
224
233
public
function
next
()
234
{
235
if
(count($this->list) == 0)
236
{
237
return
null
;
238
}
239
240
$nextItem =
null
;
241
$reservedItem =
null
;
242
$list
= $this->
getStack
();
243
foreach
(
$list
as $item)
244
{
245
if
($this->isUserCheckEnabled && !$this->
checkUser
($item))
246
{
247
continue
;
248
}
249
250
if
($this->
isWorkTimeCheckEnabled
&& !$this->
checkUserWorkTime
($item))
251
{
252
if
(!$reservedItem)
253
{
254
$reservedItem = $item;
255
}
256
257
continue
;
258
}
259
260
$nextItem = $item;
261
break
;
262
}
263
264
if
(!$nextItem)
265
{
266
$nextItem = $reservedItem ? $reservedItem :
$list
[0];
267
}
268
269
$this->
setLastItem
($nextItem);
270
271
if
($this->isAutoSaveEnabled)
272
{
273
$this->
save
();
274
}
275
276
return
$nextItem;
277
}
278
285
public
function
previous
()
286
{
287
if
(count($this->previousStack) === 0)
288
{
289
$this->isLastItemRestored =
false
;
290
$this->lastItem =
null
;
291
}
292
else
293
{
294
$this->lastItem = array_pop($this->previousStack);
295
}
296
297
return
$this->lastItem
;
298
}
299
300
protected
function
setLastItem
($item)
301
{
302
if
($this->lastItem)
303
{
304
if
(count($this->previousStack) >= 3)
305
{
306
array_shift($this->previousStack);
307
}
308
array_push($this->previousStack, $this->lastItem);
309
}
310
$this->lastItem = $item;
311
312
return
$this;
313
}
314
315
protected
function
getStack
()
316
{
317
if
(!$this->
current
() || !in_array($this->
current
(), $this->list))
318
{
319
return
$this->list
;
320
}
321
322
$lastPosition = array_search($this->
current
(), $this->list);
323
$lastPosition++;
324
if
($lastPosition >= count($this->list))
325
{
326
$lastPosition = 0;
327
}
328
$list
= array_slice($this->list, $lastPosition);
329
if
($lastPosition > 0)
330
{
331
$list
= array_merge(
332
$list
,
333
array_slice($this->list, 0, $lastPosition)
334
);
335
}
336
337
return
$list
;
338
}
339
340
protected
static
function
checkUser
($userId)
341
{
342
if
(!is_numeric($userId))
343
{
344
return
false
;
345
}
346
347
$row = UserTable::getRowById($userId);
348
return
is_array($row);
349
}
350
351
protected
static
function
checkUserWorkTime
($userId)
352
{
353
if
(!self::isSupportedWorkTime())
354
{
355
return
true
;
356
}
357
358
if
(!Loader::includeModule(
'timeman'
))
359
{
360
return
true
;
361
}
362
363
$timeManUser = new \CTimeManUser($userId);
364
$timeManSettings = $timeManUser->GetSettings(Array(
'UF_TIMEMAN'
));
365
if
(!$timeManSettings[
'UF_TIMEMAN'
])
366
{
367
$result =
true
;
368
}
369
else
370
{
371
$timeManUser->GetCurrentInfo(
true
);
// need for reload cache
372
373
if
($timeManUser->State() ==
'OPENED'
)
374
{
375
$result =
true
;
376
}
377
else
378
{
379
$result =
false
;
380
}
381
}
382
383
return
$result;
384
}
385
}
Bitrix\Main\Application
Definition
application.php:28
Bitrix\Main\Application\getConnection
static getConnection($name="")
Definition
application.php:611
Bitrix\Main\Loader
Definition
loader.php:12
Bitrix\Main\Localization\Loc
Definition
loc.php:11
Bitrix\Main\Localization\Loc\loadMessages
static loadMessages($file)
Definition
loc.php:64
Bitrix\Main\ModuleManager
Definition
modulemanager.php:5
Bitrix\Main\UserTable
Definition
user.php:46
Bitrix\Sender\Internals\Model\QueueTable
Definition
queue.php:34
Bitrix\Sender\Internals\Model\QueueTable\getConflictFields
static getConflictFields()
Definition
queue.php:63
Bitrix\Sender\Internals\Model\QueueTable\getTableName
static getTableName()
Definition
queue.php:35
Bitrix\Sender\Internals\Queue
Definition
queue.php:26
Bitrix\Sender\Internals\Queue\__construct
__construct($type, $id, array $list=[])
Definition
queue.php:45
Bitrix\Sender\Internals\Queue\getId
getId()
Definition
queue.php:100
Bitrix\Sender\Internals\Queue\$isAutoSaveEnabled
$isAutoSaveEnabled
Definition
queue.php:36
Bitrix\Sender\Internals\Queue\$isUserCheckEnabled
$isUserCheckEnabled
Definition
queue.php:35
Bitrix\Sender\Internals\Queue\enableUserCheck
enableUserCheck()
Definition
queue.php:79
Bitrix\Sender\Internals\Queue\$list
$list
Definition
queue.php:27
Bitrix\Sender\Internals\Queue\disableAutoSave
disableAutoSave()
Definition
queue.php:57
Bitrix\Sender\Internals\Queue\setLastItem
setLastItem($item)
Definition
queue.php:300
Bitrix\Sender\Internals\Queue\getStack
getStack()
Definition
queue.php:315
Bitrix\Sender\Internals\Queue\setValues
setValues(array $list)
Definition
queue.php:123
Bitrix\Sender\Internals\Queue\getValues
getValues()
Definition
queue.php:135
Bitrix\Sender\Internals\Queue\isWorkTimeCheckEnabled
isWorkTimeCheckEnabled()
Definition
queue.php:90
Bitrix\Sender\Internals\Queue\enableWorkTimeCheck
enableWorkTimeCheck()
Definition
queue.php:68
Bitrix\Sender\Internals\Queue\setId
setId($id)
Definition
queue.php:111
Bitrix\Sender\Internals\Queue\$type
$type
Definition
queue.php:31
Bitrix\Sender\Internals\Queue\$previousStack
$previousStack
Definition
queue.php:29
Bitrix\Sender\Internals\Queue\restore
restore()
Definition
queue.php:214
Bitrix\Sender\Internals\Queue\$isLastItemRestored
$isLastItemRestored
Definition
queue.php:30
Bitrix\Sender\Internals\Queue\$isWorkTimeCheckEnabled
$isWorkTimeCheckEnabled
Definition
queue.php:34
Bitrix\Sender\Internals\Queue\previous
previous()
Definition
queue.php:285
Bitrix\Sender\Internals\Queue\next
next()
Definition
queue.php:233
Bitrix\Sender\Internals\Queue\checkUserWorkTime
static checkUserWorkTime($userId)
Definition
queue.php:351
Bitrix\Sender\Internals\Queue\isSupportedWorkTime
static isSupportedWorkTime()
Definition
queue.php:156
Bitrix\Sender\Internals\Queue\$id
$id
Definition
queue.php:32
Bitrix\Sender\Internals\Queue\$lastItem
$lastItem
Definition
queue.php:28
Bitrix\Sender\Internals\Queue\current
current()
Definition
queue.php:166
Bitrix\Sender\Internals\Queue\checkUser
static checkUser($userId)
Definition
queue.php:340
Bitrix\Sender\Internals\Queue\save
save()
Definition
queue.php:182
Bitrix\Sender\Internals
modules
sender
lib
internals
queue.php
Создано системой
1.10.0