Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
queue.php
1<?php
9
15
17
18Loc::loadMessages(__FILE__);
19
25class 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 {
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}
static getConnection($name="")
static loadMessages($file)
Definition loc.php:64
__construct($type, $id, array $list=[])
Definition queue.php:45
static checkUserWorkTime($userId)
Definition queue.php:351
static checkUser($userId)
Definition queue.php:340