Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Toolbar.php
1<?
3
8
9class Toolbar
10{
11 private EO_Toolbar $entity;
12 private const CACHE_TTL = 3600;
13 private const CACHE_PATH = '/bx/main/sidepanel/toolbar/';
14
15 private function __construct(EO_Toolbar $entity)
16 {
17 $this->entity = $entity;
18 }
19
20 public function getId(): int
21 {
22 return $this->entity->getId();
23 }
24
25 public function getContext(): string
26 {
27 return $this->entity->getContext();
28 }
29
30 public function getUserId(): int
31 {
32 return $this->entity->getUserId();
33 }
34
35 public function isCollapsed(): bool
36 {
37 return $this->entity->getCollapsed();
38 }
39
40 public static function get(string $context, int $userId = 0): ?static
41 {
42 $toolbarUserId = $userId > 0 ? $userId : (int)\Bitrix\Main\Engine\CurrentUser::get()->getId();
43
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))
48 {
49 $vars = $cache->getVars();
50 $entity = is_array($vars['toolbar']) ? EO_Toolbar::wakeUp($vars['toolbar']) : null;
51
52 return $entity ? new static($entity) : null;
53 }
54
55 $entity = ToolbarTable::getList([
56 'filter' => [
57 '=CONTEXT' => $context,
58 '=USER_ID' => $toolbarUserId,
59 ]
60 ])->fetchObject();
61
62 $cache->startDataCache();
63 $cache->endDataCache(['toolbar' => $entity ? $entity->collectValues() : null]);
64
65 return $entity ? new static($entity) : null;
66 }
67
68 public static function getOrCreate(string $context, int $userId = 0): static
69 {
70 $toolbarEntity = static::get($context, $userId);
71 if ($toolbarEntity === null)
72 {
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();
77
78 if (!$result->isSuccess())
79 {
80 throw new \Bitrix\Main\SystemException($result->getErrors()[0]->getMessage(), $result->getErrors()[0]->getCode());
81 }
82
83 $toolbar = new static($toolbarEntity);
84 $toolbar->clearToolbarCache();
85
86 return $toolbar;
87 }
88
89 return $toolbarEntity;
90 }
91
92 public static function getCacheId(string $prefix, string $context, int $userId): string
93 {
94 return $prefix . '_' . $userId . '_' . md5($context);
95 }
96
97 public static function getCachePath(int $userId): string
98 {
99 return static::CACHE_PATH . $userId . '/';
100 }
101
102 public function createOrUpdateItem(array $options): Result
103 {
104 $result = new Result();
105 $entityType = $options['entityType'] ?? '';
106 $entityId = $options['entityId'] ?? '';
107
108 $item = $this->getItem($entityType, $entityId);
109 if ($item === null)
110 {
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())
119 {
120 $result->addErrors($saveResult->getErrors());
121 }
122 else
123 {
124 $result->setData(['item' => $item]);
125 }
126 }
127 else
128 {
129 $item->setLastUseDate(new DateTime());
130 $item->save();
131 $result->setData(['item' => $item]);
132 }
133
134 $this->clearItemsCache();
135
136 return $result;
137 }
138
139 public function getItem(string $entityType, string $entityId): ?EO_ToolbarItem
140 {
142 'filter' => [
143 '=TOOLBAR_ID' => $this->getId(),
144 '=ENTITY_TYPE' => $entityType,
145 '=ENTITY_ID' => $entityId,
146 ]
147 ])->fetchObject();
148 }
149
150 public function getItems(): EO_ToolbarItem_Collection
151 {
152 $cache = Cache::createInstance();
153 $cacheId = static::getCacheId('items', $this->getContext(), $this->getUserId());
154 $cachePath = static::getCachePath($this->getUserId());
155 if ($cache->initCache(static::CACHE_TTL, $cacheId, $cachePath))
156 {
157 $vars = $cache->getVars();
158
159 return EO_ToolbarItem_Collection::wakeUp($vars['items']);
160 }
161
162 $itemCollection = ToolbarItemTable::getList([
163 'filter' => [
164 '=TOOLBAR_ID' => $this->getId(),
165 ],
166 'order' => ['LAST_USE_DATE' => 'DESC'],
167 'limit' => 100,
168 ])->fetchCollection();
169
170 $items = [];
171 foreach ($itemCollection as $item)
172 {
173 $items[] = $item->collectValues();
174 }
175
176 $cache->startDataCache();
177 $cache->endDataCache(['items' => $items]);
178
179 return $itemCollection;
180 }
181
182 public function removeItem(string $entityType, string $entityId)
183 {
184 $item = $this->getItem($entityType, $entityId);
185 $item?->delete();
186
187 $this->clearItemsCache();
188 }
189
190 public function removeAll()
191 {
192 ToolbarItemTable::deleteByFilter([
193 '=TOOLBAR_ID' => $this->getId(),
194 ]);
195
196 $this->clearItemsCache();
197 }
198
199 public function clearCache(string $prefix)
200 {
201 $cache = Cache::createInstance();
202 $cacheId = static::getCacheId($prefix, $this->getContext(), $this->getUserId());
203 $cachePath = static::getCachePath($this->getUserId());
204 $cache->clean($cacheId, $cachePath);
205 }
206
207 public function clearToolbarCache(): void
208 {
209 $this->clearCache('toolbar');
210 }
211
212 public function clearItemsCache(): void
213 {
214 $this->clearCache('items');
215 }
216
217 public function collapse(): void
218 {
219 $this->clearToolbarCache();
220
221 $this->entity->setCollapsed(true);
222 $this->entity->save();
223 }
224
225 public function expand(): void
226 {
227 $this->clearToolbarCache();
228
229 $this->entity->setCollapsed(false);
230 $this->entity->save();
231 }
232}
static getList(array $parameters=array())
static getOrCreate(string $context, int $userId=0)
Definition Toolbar.php:68
getItem(string $entityType, string $entityId)
Definition Toolbar.php:139
clearCache(string $prefix)
Definition Toolbar.php:199
static getCacheId(string $prefix, string $context, int $userId)
Definition Toolbar.php:92
removeItem(string $entityType, string $entityId)
Definition Toolbar.php:182
createOrUpdateItem(array $options)
Definition Toolbar.php:102
static getCachePath(int $userId)
Definition Toolbar.php:97