Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
statusbase.php
1<?php
2
3namespace Bitrix\Sale;
4
11
12Loc::loadMessages(__FILE__);
13
18abstract class StatusBase
19{
20 const TYPE = '';
21
29 public static function getList(array $parameters = array())
30 {
31 if (static::TYPE !== '')
32 {
33 if (!isset($parameters['filter']))
34 {
35 $parameters['filter'] = ['=TYPE' => static::TYPE];
36 }
37 else
38 {
39 $parameters['filter'] = [
40 '=TYPE' => static::TYPE,
41 $parameters['filter']
42 ];
43 }
44 }
45
46 return StatusTable::getList($parameters);
47 }
48
55 protected static function getUserGroups($userId)
56 {
57 global $USER;
58
59 if ($userId == $USER->GetID())
60 {
61 $groups = $USER->GetUserGroupArray();
62 }
63 else
64 {
65 static $cacheGroups;
66
67 if (isset($cacheGroups[$userId]))
68 {
69 $groups = $cacheGroups[$userId];
70 }
71 else
72 {
73 // TODO: DATE_ACTIVE_FROM >=< DATE_ACTIVE_TO
74 $result = UserGroupTable::getList(array(
75 'select' => array('GROUP_ID'),
76 'filter' => array('USER_ID' => $userId)
77 ));
78
79 $groups = array();
80 while ($row = $result->fetch())
81 $groups []= $row['GROUP_ID'];
82
83 $cacheGroups[$userId] = $groups;
84 }
85 }
86
87 return $groups;
88 }
89
98 public static function canGroupDoOperations($groupId, $fromStatus, array $operations)
99 {
100 if (!$operations)
101 {
102 throw new SystemException('provide at least one operation', 0, __FILE__, __LINE__);
103 }
104
105 if (!is_array($groupId))
106 {
107 $groupId = array($groupId);
108 }
109
110 if (in_array('1', $groupId, true) || \CMain::GetUserRight('sale', $groupId) >= 'W') // Admin
111 {
112 return true;
113 }
114
115 $operations = static::convertNamesToOperations($operations);
116
117 $result = static::getList(array(
118 'select' => array(
119 'NAME' => 'Bitrix\Sale\Internals\StatusGroupTaskTable:STATUS.TASK.Bitrix\Main\TaskOperation:TASK.OPERATION.NAME',
120 ),
121 'filter' => array(
122 '=ID' => $fromStatus,
123 '=Bitrix\Sale\Internals\StatusGroupTaskTable:STATUS.GROUP_ID' => $groupId,
124 '=Bitrix\Sale\Internals\StatusGroupTaskTable:STATUS.TASK.Bitrix\Main\TaskOperation:TASK.OPERATION.NAME' => $operations,
125 ),
126 ));
127
128 while ($row = $result->fetch())
129 {
130 if (($key = array_search($row['NAME'], $operations)) !== false)
131 {
132 unset($operations[$key]);
133 }
134 }
135
136 return !$operations;
137 }
138
148 public static function getAllowedUserStatuses($userId, $fromStatus)
149 {
150 return static::getAllowedGroupStatuses(static::getUserGroups($userId), $fromStatus);
151 }
152
160 protected static function getAllowedGroupStatuses($groupId, $fromStatus)
161 {
162 static $cacheAllowStatuses = array();
163
164 if (! is_array($groupId))
165 $groupId = array($groupId);
166
167 $cacheKey = md5(join('|', $groupId) . "_".(is_array($fromStatus) ? join('|', $fromStatus) : $fromStatus));
168
169 if (in_array('1', $groupId, true) || \CMain::GetUserRight('sale', $groupId) >= 'W') // Admin
170 {
171 if (!array_key_exists($cacheKey, $cacheAllowStatuses))
172 {
173 $result = static::getList(array(
174 'select' => array(
175 'ID',
176 'NAME' => 'Bitrix\Sale\Internals\StatusLangTable:STATUS.NAME'
177 ),
178 'filter' => array(
179 '=TYPE' => static::TYPE,
180 '=Bitrix\Sale\Internals\StatusLangTable:STATUS.LID' => LANGUAGE_ID),
181 'order' => array(
182 'SORT'
183 ),
184 ));
185
186 while ($row = $result->fetch())
187 {
188 $cacheAllowStatuses[$cacheKey][$row['ID']] = $row['NAME'];
189 }
190 }
191 }
192 else
193 {
194 if (!array_key_exists($cacheKey, $cacheAllowStatuses))
195 {
196 $cacheAllowStatuses[$cacheKey] = array();
197
198 $dbRes = static::getList(array( // check if group can change from status
199 'select' => array('ID'),
200 'filter' => array(
201 '=ID' => $fromStatus,
202 '=TYPE' => static::TYPE,
203 '=Bitrix\Sale\Internals\StatusGroupTaskTable:STATUS.GROUP_ID' => $groupId,
204 '=Bitrix\Sale\Internals\StatusGroupTaskTable:STATUS.TASK.Bitrix\Main\TaskOperation:TASK.OPERATION.NAME' => 'sale_status_from',
205 ),
206 'limit' => 1,
207 ));
208
209 if ($dbRes->fetch())
210 {
211 $result = static::getList(array(
212 'select' => array('ID', 'NAME' => 'Bitrix\Sale\Internals\StatusLangTable:STATUS.NAME'),
213 'filter' => array(
214 '=TYPE' => static::TYPE,
215 '=Bitrix\Sale\Internals\StatusLangTable:STATUS.LID' => LANGUAGE_ID,
216 '=Bitrix\Sale\Internals\StatusGroupTaskTable:STATUS.GROUP_ID' => $groupId,
217 '=Bitrix\Sale\Internals\StatusGroupTaskTable:STATUS.TASK.Bitrix\Main\TaskOperation:TASK.OPERATION.NAME' => 'sale_status_to',
218 ),
219 'order' => array('SORT'),
220 ));
221
222 while ($row = $result->fetch())
223 {
224 $cacheAllowStatuses[$cacheKey][$row['ID']] = $row['NAME'];
225 }
226 }
227 }
228 }
229
230 return $cacheAllowStatuses[$cacheKey] ?? [];
231 }
232
237 private static function convertNamesToOperations($names)
238 {
239 $operations = array();
240
241 foreach ($names as $name)
242 {
243 $operations[] = 'sale_status_'.mb_strtolower($name);
244 }
245
246 return $operations;
247 }
248
255 public static function getAllStatuses()
256 {
257 static $statusList = [];
258
259 if (!isset($statusList[static::TYPE]))
260 {
261 $statusList[static::TYPE] = [];
262
263 $result = static::getList([
264 'select' => ['ID'],
265 'filter' => ['=TYPE' => static::TYPE],
266 'order' => ['SORT' => 'ASC']
267 ]);
268
269 while ($row = $result->fetch())
270 {
271 $statusList[static::TYPE][$row['ID']] = $row['ID'];
272 }
273 }
274
275 return $statusList[static::TYPE];
276 }
277
285 public static function getAllStatusesNames($lang = null)
286 {
287 $parameters = [
288 'select' => ["ID", "NAME" => 'Bitrix\Sale\Internals\StatusLangTable:STATUS.NAME'],
289 'filter' => [
290 '=TYPE' => static::TYPE,
291 ],
292 'order' => ['SORT' => 'ASC']
293 ];
294
295 if ($lang !== null)
296 {
297 $parameters['filter']['=Bitrix\Sale\Internals\StatusLangTable:STATUS.LID'] = $lang;
298 }
299 elseif (defined("LANGUAGE_ID"))
300 {
301 $parameters['filter']['=Bitrix\Sale\Internals\StatusLangTable:STATUS.LID'] = LANGUAGE_ID;
302 }
303
304 static $allStatusesNames = [];
305
306 if (!isset($allStatusesNames[static::TYPE]))
307 {
308 $allStatusesNames[static::TYPE] = [];
309
310 $result = static::getList($parameters);
311 while ($row = $result->fetch())
312 {
313 $allStatusesNames[static::TYPE][$row['ID']] = $row['NAME'];
314 }
315 }
316
317 return $allStatusesNames[static::TYPE];
318 }
319
329 public static function getStatusesUserCanDoOperations($userId, array $operations)
330 {
331 return static::getStatusesGroupCanDoOperations(static::getUserGroups($userId), $operations);
332 }
333
340 public static function getStatusesGroupCanDoOperations($groupId, array $operations)
341 {
342 static $cacheStatuses = array();
343
344 if (!is_array($groupId))
345 $groupId = array($groupId);
346
347 $cacheHash = md5(static::TYPE."|".join('_', $groupId)."|".join('_', $operations));
348
349 if (!empty($cacheStatuses[$cacheHash]))
350 {
351 return $cacheStatuses[$cacheHash];
352 }
353
354 if (in_array('1', $groupId, true) || \CMain::GetUserRight('sale', $groupId) >= 'W') // Admin
355 {
356 $statuses = static::getAllStatuses();
357 }
358 else
359 {
360 $statuses = static::getStatusesByGroupId($groupId, $operations);
361 }
362
363 $cacheStatuses[$cacheHash] = $statuses;
364
365 return $statuses;
366 }
367
374 private static function getStatusesByGroupId(array $groupId, array $operations = array())
375 {
376 $operations = static::convertNamesToOperations($operations);
377
378 $parameters = array(
379 'select' => array(
380 'ID',
381 'OPERATION' => 'Bitrix\Sale\Internals\StatusGroupTaskTable:STATUS.TASK.Bitrix\Main\TaskOperation:TASK.OPERATION.NAME',
382 ),
383 'filter' => array(
384 '=TYPE' => static::TYPE,
385 '=Bitrix\Sale\Internals\StatusGroupTaskTable:STATUS.GROUP_ID' => $groupId,
386 ),
387 'order' => array('SORT'),
388 );
389
390 if (!empty($operations))
391 {
392 $parameters['filter']['=Bitrix\Sale\Internals\StatusGroupTaskTable:STATUS.TASK.Bitrix\Main\TaskOperation:TASK.OPERATION.NAME'] = $operations;
393 };
394
395 $statuses = array();
396 $dbRes = static::getList($parameters);
397 while ($row = $dbRes->fetch())
398 {
399 if ((string)$row['OPERATION'] === '')
400 {
401 continue;
402 }
403
404 $statuses[$row['ID']] = $row['ID'];
405 }
406
407 return $statuses;
408 }
409
413 public static function getInitialStatus()
414 {
416 }
417
421 public static function getFinalStatus()
422 {
424 }
425
432 public static function install(array $data)
433 {
434 if (! ($statusId = $data['ID']) || ! is_string($statusId))
435 {
436 throw new SystemException('invalid status ID', 0, __FILE__, __LINE__);
437 }
438
439 if ($languages = $data['LANG'])
440 {
441 unset($data['LANG']);
442
443 if (! is_array($languages))
444 throw new SystemException('invalid status LANG', 0, __FILE__, __LINE__);
445 }
446
447 $data['TYPE'] = static::TYPE;
448
449 // install status if it is not installed
450
451 if (! StatusTable::getById($statusId)->fetch())
452 {
453 StatusTable::add($data);
454 }
455
456 // install status languages if they are not installed
457
458 if ($languages)
459 {
460 $installedLanguages = array();
461
462 $result = StatusLangTable::getList(array(
463 'select' => array('LID'),
464 'filter' => array('=STATUS_ID' => $statusId),
465 ));
466
467 while ($row = $result->fetch())
468 {
469 $installedLanguages[$row['LID']] = true;
470 }
471
472 foreach ($languages as $language)
473 {
474 if (! is_array($language))
475 throw new SystemException('invalid status language', 0, __FILE__, __LINE__);
476
477 if (! $installedLanguages[$language['LID']])
478 {
479 $language['STATUS_ID'] = $statusId;
480
481 StatusLangTable::add($language);
482 }
483 }
484 }
485 }
486}
static loadMessages($file)
Definition loc.php:64
static getUserGroups($userId)
static getStatusesGroupCanDoOperations($groupId, array $operations)
static getAllStatusesNames($lang=null)
static getAllowedUserStatuses($userId, $fromStatus)
static getList(array $parameters=array())
static install(array $data)
static getAllowedGroupStatuses($groupId, $fromStatus)
static getStatusesUserCanDoOperations($userId, array $operations)
static canGroupDoOperations($groupId, $fromStatus, array $operations)