Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
placement.php
1<?php
2namespace Bitrix\Rest;
3
12
13Loc::loadMessages(__FILE__);
14
44class PlacementTable extends Main\Entity\DataManager
45{
46 public const PREFIX_EVENT_ON_AFTER_ADD = 'onAfterPlacementAdd::';
47 public const PREFIX_EVENT_ON_AFTER_DELETE = 'onAfterPlacementDelete::';
48
49 public const DEFAULT_USER_ID_VALUE = 0;
50
51 const PLACEMENT_DEFAULT = 'DEFAULT';
52
53 const ERROR_PLACEMENT_NOT_FOUND = 'ERROR_PLACEMENT_NOT_FOUND';
54 const ERROR_PLACEMENT_MAX_COUNT = 'ERROR_PLACEMENT_MAX_COUNT';
55 const ERROR_PLACEMENT_USER_MODE = 'ERROR_PLACEMENT_USER_MODE';
56
57 const CACHE_TTL = 86400;
58 const CACHE_DIR = 'rest/placement';
59
60 protected static $handlersListCache = [];
61 private static $beforeDeleteList = [];
62
68 public static function getTableName()
69 {
70 return 'b_rest_placement';
71 }
72
78 public static function getMap()
79 {
80 return array(
81 'ID' => array(
82 'data_type' => 'integer',
83 'primary' => true,
84 'autocomplete' => true,
85 ),
86 'APP_ID' => array(
87 'data_type' => 'integer',
88 ),
89 'USER_ID' => array(
90 'data_type' => 'integer',
91 ),
92 'PLACEMENT' => array(
93 'data_type' => 'string',
94 'required' => true,
95 ),
96 'PLACEMENT_HANDLER' => array(
97 'data_type' => 'string',
98 'required' => true,
99 ),
104 'GROUP_NAME' => array(
105 'data_type' => 'string',
106 ),
107 'ICON_ID' => array(
108 'data_type' => 'integer',
109 ),
114 'TITLE' => array(
115 'data_type' => 'string',
116 ),
121 'COMMENT' => array(
122 'data_type' => 'string',
123 ),
124 'DATE_CREATE' => array(
125 'data_type' => 'datetime',
126 ),
127 'ADDITIONAL' => array(
128 'data_type' => 'string',
129 ),
130 'OPTIONS' => new ArrayField('OPTIONS'),
131 'REST_APP' => array(
132 'data_type' => 'Bitrix\Rest\AppTable',
133 'reference' => array('=this.APP_ID' => 'ref.ID'),
134 ),
135 (new OneToMany(
136 'LANG_ALL',
137 \Bitrix\Rest\PlacementLangTable::class,
138 'PLACEMENT'
139 ))->configureJoinType('left'),
140 );
141 }
142
143 private static function getUserFilter($userId)
144 {
145 if (is_null($userId))
146 {
147 global $USER;
148 if ($USER instanceof \CUser)
149 {
150 $userId = (int)$USER->getId();
151 }
152 }
153
154 if ($userId > 0)
155 {
156 $result = [
157 static::DEFAULT_USER_ID_VALUE,
158 $userId,
159 ];
160 }
161 else
162 {
163 $result = static::DEFAULT_USER_ID_VALUE;
164 }
165
166 return $result;
167 }
168
176 public static function getHandlers($placement, int $userId = null)
177 {
178 $dbRes = static::getList(
179 [
180 'filter' => [
181 '=PLACEMENT' => $placement,
182 '=REST_APP.ACTIVE' => AppTable::ACTIVE,
183 '=USER_ID' => static::getUserFilter($userId),
184 ],
185 'select' => [
186 'ID',
187 'ICON_ID',
188 'TITLE',
189 'GROUP_NAME',
190 'OPTIONS',
191 'COMMENT',
192 'APP_ID',
193 'ADDITIONAL',
194 'INSTALLED' => 'REST_APP.INSTALLED',
195 'APP_NAME' => 'REST_APP.APP_NAME',
196 'APP_ACCESS' => 'REST_APP.ACCESS',
197 ],
198 ]
199 );
200
201 return $dbRes;
202 }
203
211 public static function deleteByApp($appId)
212 {
214 $connection = Main\Application::getConnection();
215
216 $res = static::getList(
217 [
218 'filter' => [
219 '=APP_ID' => (int)$appId,
220 ],
221 'select' => [
222 'ID',
223 'APP_ID',
224 'PLACEMENT',
225 'PLACEMENT_HANDLER',
226 'ICON_ID',
227 'ADDITIONAL',
228 'OPTIONS',
229 ],
230 'limit' => 1,
231 ]
232 );
233 $eventList = [];
234 while ($placement = $res->fetch())
235 {
236 $eventList[] = new Event(
237 'rest',
238 static::PREFIX_EVENT_ON_AFTER_DELETE . $placement['PLACEMENT'],
239 [
240 'ID' => $placement['ID'],
241 'PLACEMENT' => $placement['PLACEMENT'],
242 ]
243 );
244 }
245
246 $queryResult = $connection->query("DELETE FROM ".static::getTableName()." WHERE APP_ID='".intval($appId)."'");
247
248 foreach ($eventList as $event)
249 {
250 EventManager::getInstance()->send($event);
251 }
252
253 static::clearHandlerCache();
254
255 return $queryResult;
256 }
257
266 public static function getHandlersList($placement, $skipInstallCheck = false, int $userId = null)
267 {
268 if(!array_key_exists($placement, static::$handlersListCache))
269 {
270 static::$handlersListCache[$placement] = array();
271
272 $cache = Main\Application::getInstance()->getManagedCache();
273 if($cache->read(static::CACHE_TTL, static::getCacheId($placement), static::CACHE_DIR))
274 {
275 static::$handlersListCache = $cache->get(static::getCacheId($placement));
276 }
277 else
278 {
279 $res = static::getHandlers($placement, $userId);
280 foreach ($res->fetchCollection() as $handler)
281 {
282 $id = $handler->getId();
283 $app = $handler->getRestApp();
284 $placementItem = [
285 'ID' => $id,
286 'APP_ID' => $handler->getAppId(),
287 'USER_ID' => $handler->getUserId(),
288 'ICON_ID' => $handler->getIconId(),
289 'ADDITIONAL' => $handler->getAdditional(),
290 'TITLE' => '',
295 'COMMENT' => '',
296 'DESCRIPTION' => '',
297 'GROUP_NAME' => '',
298 'OPTIONS' => $handler->getOptions(),
299 'INSTALLED' => $app->getInstalled(),
300 'APP_NAME' => $app->getAppName(),
301 'APP_ACCESS' => $app->getAccess(),
302 'LANG_ALL' => [],
303 ];
304
305 if ($placementItem['ICON_ID'] > 0 && ($file = \CFile::GetFileArray($placementItem['ICON_ID'])))
306 {
307 $placementItem['ICON'] = array_change_key_case($file, CASE_LOWER);
308 }
309
310 $handler->fillLangAll();
311 if (!is_null($handler->getLangAll()))
312 {
313 foreach ($handler->getLangAll() as $lang)
314 {
315 $placementItem['LANG_ALL'][$lang->getLanguageId()] = [
316 'TITLE' => $lang->getTitle(),
317 'DESCRIPTION' => $lang->getDescription(),
318 'GROUP_NAME' => $lang->getGroupName(),
319 ];
320 }
321 }
322 static::$handlersListCache[$placement][] = $placementItem;
323 }
324
325 $cache->set(static::getCacheId($placement), static::$handlersListCache);
326 }
327 }
328
329 $result = static::$handlersListCache[$placement];
330
331 foreach($result as $key => $handler)
332 {
333 if(!$skipInstallCheck && $handler['INSTALLED'] === AppTable::NOT_INSTALLED)
334 {
335 unset($result[$key]);
336 }
337 elseif(
338 $placement !== Api\UserFieldType::PLACEMENT_UF_TYPE
339 && !\CRestUtil::checkAppAccess($handler['APP_ID'], array(
340 'ACCESS' => $handler['APP_ACCESS']
341 )
342 )
343 )
344 {
345 unset($result[$key]);
346 }
347 else
348 {
349 $result[$key] = Lang::mergeFromLangAll($handler);
350 if (empty($result[$key]['TITLE']))
351 {
352 $result[$key]['TITLE'] = static::getDefaultTitle($handler['ID']);
353 }
354 }
355 }
356
357 $result = array_values($result);
358
359 return $result;
360 }
361
369 public static function getDefaultTitle(int $placementId, $language = null): ?string
370 {
371 return Loc::getMessage(
372 'REST_PLACEMENT_DEFAULT_TITLE',
373 [
374 '#ID#' => $placementId,
375 ],
376 $language
377 );
378 }
379
380 public static function clearHandlerCache()
381 {
382 $cache = Main\Application::getInstance()->getManagedCache();
383 $cache->cleanDir(static::CACHE_DIR);
384 static::$handlersListCache = array();
385 if (defined('BX_COMP_MANAGED_CACHE'))
386 {
387 global $CACHE_MANAGER;
388 $CACHE_MANAGER->clearByTag('intranet_menu_binding');
389 }
390 }
391
392 public static function onBeforeUpdate(Main\Entity\Event $event)
393 {
394 $result = static::checkUniq($event);
395 static::modifyFields($event, $result);
396 return $result;
397 }
398
399 public static function onBeforeAdd(Main\Entity\Event $event)
400 {
401 $result = static::checkUniq($event, true);
402 static::modifyFields($event, $result);
403 return $result;
404 }
405
406 public static function onBeforeDelete(Main\Entity\Event $event)
407 {
408 $result = new Main\ORM\EventResult();
409 $id = $event->getParameter('id');
410 $id = (int)$id['ID'];
411 $res = static::getList(
412 [
413 'filter' => [
414 '=ID' => $id,
415 ],
416 'select' => [
417 'ID',
418 'APP_ID',
419 'PLACEMENT',
420 'PLACEMENT_HANDLER',
421 'ICON_ID',
422 'ADDITIONAL',
423 'OPTIONS',
424 ],
425 'limit' => 1,
426 ]
427 );
428 if ($placement = $res->fetch())
429 {
430 static::$beforeDeleteList[$placement['ID']] = $placement;
431 if ((int)$placement['ICON_ID'] > 0)
432 {
433 \CFile::Delete((int)$placement['ICON_ID']);
434 }
435 }
437
438 return $result;
439 }
440
441 public static function onAfterAdd(Main\Entity\Event $event)
442 {
443 $fields = $event->getParameter('fields');
444 if (!empty($fields['PLACEMENT']) && (int)$fields['APP_ID'] > 0)
445 {
446 $app = AppTable::getByClientId((int)$fields['APP_ID']);
447 if ($app['ACTIVE'] === AppTable::ACTIVE && $app['INSTALLED'] === AppTable::INSTALLED)
448 {
449 $data = new Event(
450 'rest',
451 static::PREFIX_EVENT_ON_AFTER_ADD . $fields['PLACEMENT'],
452 [
453 'ID' => $event->getParameter('id'),
454 'PLACEMENT' => $fields['PLACEMENT'],
455 ]
456 );
457 EventManager::getInstance()->send($data);
458 }
459 }
460
461 static::clearHandlerCache();
462 }
463
464 public static function onAfterUpdate(Main\Entity\Event $event)
465 {
466 static::clearHandlerCache();
467 }
468
469 public static function onAfterDelete(Main\Entity\Event $event)
470 {
471 $id = $event->getParameter('id');
472 $id = (int)$id['ID'];
473 if ($id > 0)
474 {
475 $data = new Event(
476 'rest',
477 static::PREFIX_EVENT_ON_AFTER_DELETE . static::$beforeDeleteList[$id]['PLACEMENT'],
478 [
479 'ID' => $id,
480 'PLACEMENT' => static::$beforeDeleteList[$id]['PLACEMENT'],
481 ]
482 );
483 unset(static::$beforeDeleteList[$id]);
484 EventManager::getInstance()->send($data);
485 }
486
487 static::clearHandlerCache();
488 }
489
490 protected static function getCacheId($placement)
491 {
492 return 'rest_placement_list|'.$placement.'|'.LANGUAGE_ID;
493 }
494
495 protected static function checkUniq(Main\Entity\Event $event, $add = false)
496 {
497 $result = new Main\Entity\EventResult();
498 $data = $event->getParameter('fields');
499
500 $filter = array(
501 '=APP_ID' => $data['APP_ID'],
502 '=PLACEMENT' => $data['PLACEMENT'],
503 '=PLACEMENT_HANDLER' => $data['PLACEMENT_HANDLER'],
504 );
505
506 if(!empty($data['ADDITIONAL']))
507 {
508 $filter = array(
509 'LOGIC' => 'OR',
510 array('=ADDITIONAL' => $data['ADDITIONAL']),
511 $filter
512 );
513 }
514
515 $dbRes = static::getList(array(
516 'filter' => $filter,
517 'select' => array('ID')
518 ));
519
520 if($dbRes->fetch())
521 {
522 $result->addError(new Main\Entity\EntityError(
523 "Handler already binded"
524 ));
525 }
526 elseif($add)
527 {
528 $result->modifyFields(array(
529 "DATE_CREATE" => new Main\Type\DateTime(),
530 ));
531 }
532
533 return $result;
534 }
535
536 private static function modifyFields(Main\ORM\Event $event, Main\ORM\EventResult $result)
537 {
538 if ($result->getType() !== Main\Entity\EventResult::ERROR)
539 {
540 $fieldChanged = [];
541 $data = array_merge($event->getParameter('fields'), $result->getModified());
542 if (array_key_exists('ICON', $data))
543 {
544 if ($str = \CFile::CheckImageFile($data['ICON']))
545 {
546 $result->addError(new Main\ORM\Fields\FieldError(static::getEntity()->getField('ICON_ID'), $str));
547 }
548 else
549 {
550 \CFile::ResizeImage($data['ICON'], [
551 'width' => Main\Config\Option::get('rest', 'icon_size', 100),
552 'height' => Main\Config\Option::get('rest', 'icon_size', 100)]);
553 $data['ICON']['MODULE_ID'] = 'rest';
554 if ($id = $event->getParameter('id'))
555 {
556 $id = is_integer($id) ? $id : $id['ID'];
557 if ($id > 0 && ($icon = PlacementTable::getById($id)->fetchObject()))
558 {
559 $data['ICON']['old_file'] = $icon->getIconId();
560 }
561 }
562 if (\CFile::SaveForDB($data, 'ICON', 'rest/placementicon'))
563 {
564 $fieldChanged['ICON_ID'] = $data['ICON'];
565 }
566 }
567 $result->unsetField('ICON');
568 }
569
570 if (array_key_exists('DESCRIPTION', $data))
571 {
572 $fieldChanged['COMMENT'] = $data['DESCRIPTION'];
573 $result->unsetField('DESCRIPTION');
574 }
575
576 if (!empty($fieldChanged))
577 {
578 $result->modifyFields(array_merge($result->getModified(), $fieldChanged));
579 }
580 }
581
582 return $result;
583 }
584
585}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getByClientId($clientId)
Definition app.php:929
const NOT_INSTALLED
Definition app.php:63
static mergeFromLangAll($data)
Definition lang.php:59
static deleteByPlacement(int $placementId)
static getCacheId($placement)
static onBeforeAdd(Main\Entity\Event $event)
static onAfterDelete(Main\Entity\Event $event)
static checkUniq(Main\Entity\Event $event, $add=false)
static getDefaultTitle(int $placementId, $language=null)
static onBeforeDelete(Main\Entity\Event $event)
static onAfterUpdate(Main\Entity\Event $event)
static getHandlersList($placement, $skipInstallCheck=false, int $userId=null)
static onBeforeUpdate(Main\Entity\Event $event)
static deleteByApp($appId)
static onAfterAdd(Main\Entity\Event $event)
static getHandlers($placement, int $userId=null)