40 public static function get(
string $context,
int $userId = 0): ?static
42 $toolbarUserId = $userId > 0 ? $userId : (int)\
Bitrix\Main\Engine\CurrentUser::get()->getId();
44 $cache = Cache::createInstance();
45 $cacheId = static::getCacheId(
'toolbar', $context, $toolbarUserId);
46 $cachePath = static::getCachePath($toolbarUserId);
47 if ($cache->initCache(static::CACHE_TTL, $cacheId, $cachePath))
49 $vars = $cache->getVars();
50 $entity = is_array($vars[
'toolbar']) ? EO_Toolbar::wakeUp($vars[
'toolbar']) :
null;
52 return $entity ?
new static($entity) :
null;
57 '=CONTEXT' => $context,
58 '=USER_ID' => $toolbarUserId,
62 $cache->startDataCache();
63 $cache->endDataCache([
'toolbar' => $entity ? $entity->collectValues() :
null]);
65 return $entity ?
new static($entity) :
null;
68 public static function getOrCreate(
string $context,
int $userId = 0): static
70 $toolbarEntity = static::get($context, $userId);
71 if ($toolbarEntity ===
null)
73 $toolbarEntity =
new EO_Toolbar();
74 $toolbarEntity->setContext($context);
75 $toolbarEntity->setUserId($userId > 0 ? $userId : (int)\
Bitrix\Main\Engine\CurrentUser::get()->getId());
76 $result = $toolbarEntity->save();
78 if (!$result->isSuccess())
80 throw new \Bitrix\Main\SystemException($result->getErrors()[0]->getMessage(), $result->getErrors()[0]->getCode());
83 $toolbar =
new static($toolbarEntity);
84 $toolbar->clearToolbarCache();
89 return $toolbarEntity;
105 $entityType = $options[
'entityType'] ??
'';
106 $entityId = $options[
'entityId'] ??
'';
108 $item = $this->
getItem($entityType, $entityId);
111 $item =
new EO_ToolbarItem();
112 $item->setToolbarId($this->
getId());
113 $item->setEntityType($entityType);
114 $item->setEntityId($entityId);
115 $item->setTitle($options[
'title'] ??
'');
116 $item->setUrl($options[
'url'] ??
'');
117 $saveResult = $item->save();
118 if (!$saveResult->isSuccess())
120 $result->addErrors($saveResult->getErrors());
124 $result->setData([
'item' => $item]);
129 $item->setLastUseDate(
new DateTime());
131 $result->setData([
'item' => $item]);
150 public function getItems(): EO_ToolbarItem_Collection
152 $cache = Cache::createInstance();
154 $cachePath = static::getCachePath($this->
getUserId());
155 if ($cache->initCache(static::CACHE_TTL, $cacheId, $cachePath))
157 $vars = $cache->getVars();
159 return EO_ToolbarItem_Collection::wakeUp($vars[
'items']);
164 '=TOOLBAR_ID' => $this->
getId(),
166 'order' => [
'LAST_USE_DATE' =>
'DESC'],
168 ])->fetchCollection();
171 foreach ($itemCollection as $item)
173 $items[] = $item->collectValues();
176 $cache->startDataCache();
177 $cache->endDataCache([
'items' => $items]);
179 return $itemCollection;