Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
util.php
1<?php
2
4
14use CCalendar;
16use CExtranet;
17use CTimeZone;
18
19class Util
20{
28 public static function parseLocation($location): array
29 {
30 $res = [
31 'mrid' => false,
32 'mrevid' => false,
33 'room_id' => false,
34 'room_event_id' => false,
35 'str' => $location
36 ];
37
38 if (!$location || is_array($location))
39 {
40 $res['str'] = '';
41
42 return $res;
43 }
44
45 if (is_string($location))
46 {
47 $res['str'] = Emoji::decode($location);
48 }
49
50 if (
51 mb_strlen($location) > 5
52 && mb_strpos($location, 'ECMR_') === 0
53 )
54 {
55 $parsedLocation = explode('_', $location);
56 if (count($parsedLocation) >= 2)
57 {
58 if ((int)$parsedLocation[1] > 0)
59 {
60 $res['mrid'] = (int)$parsedLocation[1];
61 }
62 if ((int)$parsedLocation[2] > 0)
63 {
64 $res['mrevid'] = (int)$parsedLocation[2];
65 }
66 }
67 }
68 else if (
69 mb_strlen($location) > 9
70 && mb_strpos($location, 'calendar_') === 0
71 )
72 {
73 $parsedLocation = explode('_', $location);
74 if (count($parsedLocation) >= 2)
75 {
76 if ((int)$parsedLocation[1] > 0)
77 {
78 $res['room_id'] = (int)$parsedLocation[1];
79 }
80 if (isset($parsedLocation[2]) && (int)$parsedLocation[2] > 0)
81 {
82 $res['room_event_id'] = (int)$parsedLocation[2];
83 }
84 }
85 }
86
87 return $res;
88 }
89
97 public static function unParseTextLocation($loc = ''): array
98 {
99 $result = ['NEW' => $loc];
100 if ($loc != '')
101 {
102 $location = self::parseLocation($loc);
103 if ($location['mrid'] === false && $location['room_id'] === false)
104 {
106 $loc_ = mb_strtolower(trim($loc));
107 foreach($MRList as $MR)
108 {
109 if (mb_strtolower(trim($MR['NAME'])) == $loc_)
110 {
111 $result['NEW'] = 'ECMR_'.$MR['ID'];
112 break;
113 }
114 }
115
116 if (Bitrix24Manager::isFeatureEnabled('calendar_location'))
117 {
118 $locationList = Manager::getRoomsList();
119 foreach($locationList as $room)
120 {
121 if (mb_strtolower(trim($room['NAME'])) == $loc_)
122 {
123 $result['NEW'] = 'calendar_'.$room['ID'];
124 }
125 }
126 }
127
128 }
129 }
130
131 return $result;
132 }
133
142 public static function getTextLocation($loc = '')
143 {
144 $result = $loc;
145 if ($loc)
146 {
147 $location = self::parseLocation($loc);
148
149 if ($location['mrid'] === false && $location['room_id'] === false)
150 {
151 return $location['str'];
152 }
153
154 if ($location['room_id'] > 0)
155 {
156 $room = Manager::getRoomById($location['room_id']);
157 return $room ? ($room[0]['NAME'] ?? null) : '';
158 }
159
161 foreach ($MRList as $MR)
162 {
163 if ((int)$MR['ID'] === (int)$location['mrid'])
164 {
165 return $MR['NAME'];
166 }
167 }
168 }
169
170 return Emoji::decode($result);
171 }
172
183 public static function setLocation($old = '', $new = '', array $params = [])
184 {
185 $params = [
186 'bRecreateReserveMeetings' => $params['bRecreateReserveMeetings'] ?? null,
187 'dateFrom' => $params['dateFrom'] ?? null,
188 'dateTo' => $params['dateTo'] ?? null,
189 'name' => $params['name'] ?? null,
190 'persons' => $params['persons'] ?? null,
191 'attendees' => $params['attendees'] ?? null,
192 'parentParams' => $params['parentParams'] ?? null,
193 'checkPermission' => $params['checkPermission'] ?? null,
194 ];
195
196 $tzEnabled = CTimeZone::Enabled();
197 if ($tzEnabled)
198 {
199 CTimeZone::Disable();
200 }
201
202 // *** ADD MEETING ROOM ***
203 $locOld = self::parseLocation($old);
204 $locNew = self::parseLocation($new);
205 $res = $locNew['mrid'] ? $locNew['str'] : $new;
206 $settings = CCalendar::GetSettings(['request' => false]);
207 $RMiblockId = $settings['rm_iblock_id'] ?? null;
208 // If not allowed
209 if ($RMiblockId && $locOld['mrid'] !== false && $locOld['mrevid'] !== false) // Release MR
210 {
211 Util::releaseLocation($locOld);
212 }
213
214 if ($locNew['mrid'] !== false) // Reserve MR
215 {
216 $mrevid = false;
217 if ($params['bRecreateReserveMeetings'])
218 {
220 'RMiblockId' => $RMiblockId,
221 'mrid' => $locNew['mrid'],
222 'dateFrom' => $params['dateFrom'],
223 'dateTo' => $params['dateTo'],
224 'name' => $params['name'],
225 'description' => Loc::getMessage('EC_RESERVE_FOR_EVENT').': '.$params['name'],
226 'persons' => $params['persons'],
227 'members' => $params['attendees']
228 ]);
229 }
230
231 else if($locNew['mrevid'] !== false)
232 {
233 $mrevid = $locNew['mrevid'];
234 }
235
236 $locNew =
237 ($mrevid && $mrevid !== 'reserved' && $mrevid !== 'expire' && $mrevid > 0)
238 ? 'ECMR_'.$locNew['mrid'].'_'.$mrevid
239 : ''
240 ;
241 }
242
243 // Release room
244 if (
245 $locOld['room_id'] !== false
246 && $locOld['room_event_id'] !== false
247 && $locNew['room_id'] === false
248 )
249 {
250 Util::releaseLocation($locOld);
251
252 $locNew = $locNew['str'];
253 }
254 //Reserve room if it hasn't reserved before
255 else if($locNew['room_id'] && $locOld['room_id'] === false)
256 {
257 $roomEventId = Manager::reserveRoom([
258 'room_id' => $locNew['room_id'],
259 'room_event_id' => false,
260 'parentParams' => $params['parentParams']
261 ]);
262
263 $locNew = $roomEventId ? 'calendar_'.$locNew['room_id'].'_'.$roomEventId : '';
264 }
265 //Update room event if it has been reserved before
266 else if (
267 $locNew['room_id']
268 && $locOld['room_id']
269 && $locOld['room_event_id']
270 )
271 {
272 $roomEventId = Manager::reserveRoom([
273 'room_id' => $locNew['room_id'],
274 'room_event_id' => $locOld['room_event_id'],
275 'parentParams' => $params['parentParams'],
276 'checkPermission' => $params['checkPermission'],
277 ]);
278
279 $locNew = $roomEventId ? 'calendar_' . $locNew['room_id'] . '_' . $roomEventId : '';
280 }
281 //String value for location field
282 else
283 {
284 $locNew = $locNew['str'];
285 }
286
287 if ($locNew)
288 {
289 $res = $locNew;
290 }
291
292 if ($tzEnabled)
293 {
294 CTimeZone::Enable();
295 }
296
297 return $res;
298 }
299
305 public static function releaseLocation($loc)
306 {
307 $loc = [
308 'room_id' => $loc['room_id'] ?? false,
309 'room_event_id' => $loc['room_event_id'] ?? false,
310 'mrevid' => $loc['mrevid'] ?? false,
311 'mrid' => $loc['mrid'] ?? false,
312 ];
313
314 if ($loc['room_id'] && $loc['room_event_id'] !== false)
315 {
317 'room_id' => $loc['room_id'],
318 'room_event_id' => $loc['room_event_id']
319 ]);
320 }
321
322 // Old reserve meeting based on iblock module
323 if($loc['mrevid'] && $loc['mrid'])
324 {
325 $set = CCalendar::GetSettings(['request' => false]);
326 if ($set['rm_iblock_id'] ?? null)
327 {
329 'mrevid' => $loc['mrevid'],
330 'mrid' => $loc['mrid'],
331 'RMiblockId' => $set['rm_iblock_id']
332 ]);
333 }
334 }
335 }
336
344 public static function getLocationAccess($userId): bool
345 {
346 $typeModel = TypeModel::createFromXmlId(Dictionary::CALENDAR_TYPE['location']);
347
348 $access = (new TypeAccessController($userId))->check(ActionDictionary::ACTION_TYPE_EDIT, $typeModel, []);
349
350 if (Loader::includeModule('extranet'))
351 {
352 $access = $access && CExtranet::IsIntranetUser(SITE_ID, $userId);
353 }
354
355 return $access;
356 }
357}
static getRoomById(int $id, array $params=[])
Definition manager.php:217
static releaseRoom(array $params=[])
Definition manager.php:409
static reserveRoom(array $params=[])
Definition manager.php:271
static setLocation($old='', $new='', array $params=[])
Definition util.php:183
static releaseLocation($loc)
Definition util.php:305
static parseLocation($location)
Definition util.php:28
static unParseTextLocation($loc='')
Definition util.php:97
static getTextLocation($loc='')
Definition util.php:142
static getLocationAccess($userId)
Definition util.php:344
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29