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