Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
user.php
1<?php
9
13
14class User
15{
16 public static function getVacationList(): array
17 {
18 global $CACHE_MANAGER;
19
20 $result = [];
21
22 $cacheTTL = 3600 * 24 * 30;
23 $cacheDir = '/sonet/user_absence';
24
25 $cache = new \CPHPCache();
26 if ($cache->initCache($cacheTTL, 'intranet_absence', $cacheDir))
27 {
28 $result = $cache->getVars();
29 }
30 else
31 {
32 $cache->startDataCache();
33 $CACHE_MANAGER->startTagCache($cacheDir);
34
35 if (
36 ModuleManager::isModuleInstalled('intranet')
37 && Loader::includeModule('iblock')
38 && ($absenceIblockId = (int)Option::get('intranet', 'iblock_absence'))
39 )
40 {
41 $CACHE_MANAGER->registerTag('iblock_id_'.$absenceIblockId);
42
43 $res = \CIBlockProperty::getList(
44 [],
45 [
46 'IBLOCK_ID' => $absenceIblockId,
47 'ACTIVE' => 'Y',
48 'CODE' => 'ABSENCE_TYPE',
49 ]
50 );
51
52 if (
53 ($property = $res->fetch())
54 && ($absenceTypePropertyId = (int)$property['ID'])
55 )
56 {
57 $vacationXMLIdList = [];
58 $res = \CIBlockPropertyEnum::getList(
59 [],
60 [
61 'PROPERTY_ID' => $absenceTypePropertyId,
62 'XML_ID' => [ 'VACATION', 'LEAVEMATERINITY' ],
63 ]
64 );
65
66 while ($enum = $res->fetch())
67 {
68 $vacationXMLIdList[$enum['XML_ID']] = (int)$enum['ID'];
69 }
70
71 if (isset($vacationXMLIdList['LEAVEMATERINITY']))
72 {
73 $result = array_merge($result, self::getVacationListOfType([
74 'absenceIblockId' => $absenceIblockId,
75 'absenceTypeId' => $vacationXMLIdList['LEAVEMATERINITY'],
76 ]));
77 }
78
79 if (isset($vacationXMLIdList['VACATION']))
80 {
81 $result = array_merge($result, self::getVacationListOfType([
82 'absenceIblockId' => $absenceIblockId,
83 'absenceTypeId' => $vacationXMLIdList['VACATION'],
84 'fromTimestamp' => time() - (3600 * 24 * 120),
85 ]));
86 }
87 }
88 }
89
90 $CACHE_MANAGER->endTagCache();
91 $cache->endDataCache($result);
92 }
93
94 return $result;
95 }
96
97 private static function getVacationListOfType(array $params = []): array
98 {
99 $result = [];
100
101 $absenceIblockId = (int)($params['absenceIblockId'] ?? 0);
102 $absenceTypeId = (int)($params['absenceTypeId'] ?? 0);
103 $fromTimestamp = ($params['fromTimestamp'] ?? false);
104
105 if (
106 $absenceIblockId <= 0
107 || $absenceTypeId <= 0
108 || !Loader::includeModule('iblock')
109 )
110 {
111 return $result;
112 }
113
114 $filter = [
115 'IBLOCK_ID' => $absenceIblockId,
116 'PROPERTY_ABSENCE_TYPE' => $absenceTypeId,
117 'ACTIVE' => 'Y',
118 ];
119
120 if ($fromTimestamp)
121 {
122 $filter['>=DATE_ACTIVE_FROM'] = Date(\Bitrix\Main\Type\Date::convertFormatToPhp(FORMAT_DATETIME), $fromTimestamp);
123 }
124
125 $res = \CIBlockElement::getList(
126 [],
127 $filter,
128 false,
129 false,
130 [ 'ID', 'DATE_ACTIVE_FROM', 'DATE_ACTIVE_TO', 'PROPERTY_ABSENCE_TYPE', 'PROPERTY_USER' ]
131 );
132
133 while ($absence = $res->fetch())
134 {
135 $result[] = [
136 'USER_ID' => $absence['PROPERTY_USER_VALUE'],
137 'DATE_FROM' => $absence['DATE_ACTIVE_FROM'],
138 'DATE_TO' => $absence['DATE_ACTIVE_TO'],
139 'ABSENCE_TYPE' => $absence['PROPERTY_ABSENCE_TYPE_ENUM_ID'],
140 ];
141 }
142
143 return $result;
144 }
145
146 public static function getDayVacationList($params = array()): array
147 {
148 $result = [];
149
150 $userList = (isset($params['userList']) && is_array($params['userList']) ? $params['userList'] : []);
151 $vacationList = self::getVacationList();
152
153 if (empty($vacationList))
154 {
155 return $result;
156 }
157
158 $ts = time();
159
160 foreach ($vacationList as $vacation)
161 {
162 if (
163 !empty($userList)
164 && !in_array($vacation['USER_ID'], $userList)
165 )
166 {
167 continue;
168 }
169
170 $vacationTSStart = makeTimeStamp($vacation['DATE_FROM'], FORMAT_DATETIME);
171
172 if ($vacationTSStart < $ts)
173 {
174 $vacationTSFinish = makeTimeStamp($vacation['DATE_TO'], FORMAT_DATETIME) + 86400;
175
176 if ($vacationTSFinish > $ts)
177 {
178 $result[$vacation['USER_ID']] = $vacation['ABSENCE_TYPE'];
179 }
180 }
181 }
182
183 return $result;
184 }
185}