Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
pullmanager.php
1<?php
2
4
12use Bitrix\CatalogMobile\StoreDocumentList;
13use Bitrix\CatalogMobile\RealizationList;
16
18{
19 public const MODULE_ID = 'catalog';
20
21 public const EVENT_DOCUMENTS_LIST_UPDATED = 'CATALOG_DOCUMENTS_LIST_UPDATED';
22
23 protected const EVENT_DOCUMENT_ADDED = 'ADDED';
24 protected const EVENT_DOCUMENT_UPDATED = 'UPDATED';
25 protected const EVENT_DOCUMENT_DELETED = 'DELETED';
26
27 protected $eventIds = [];
28 protected $isEnabled = false;
29 protected $isMobileIncluded = false;
30
31 private static $instance;
32
33 public static function getInstance(): PullManager
34 {
35 if (!isset(self::$instance))
36 {
37 self::$instance = ServiceLocator::getInstance()->get('catalog.integration.pullmanager');
38 }
39
40 return self::$instance;
41 }
42
43 public function __construct()
44 {
45 $this->isEnabled = $this->includeModule();
46 $this->isMobileIncluded =
47 Loader::includeModule('mobile')
48 && Loader::includeModule('catalogmobile')
49 ;
50 }
51
52 private function __clone()
53 {
54 }
55
59 protected function includeModule(): bool
60 {
61 try
62 {
63 return Loader::includeModule('catalog') && Loader::includeModule('pull');
64 }
65 catch(LoaderException $exception)
66 {
67 return false;
68 }
69 }
70
74 public function isEnabled(): bool
75 {
76 return $this->isEnabled;
77 }
78
84 public function sendDocumentAddedEvent(array $items, ?array $params = null): bool
85 {
86 $this->prepareItems($items, self::EVENT_DOCUMENT_ADDED);
87 return $this->sendItemEvent(self::EVENT_DOCUMENT_ADDED, $items, $params);
88 }
89
95 public function sendDocumentsUpdatedEvent(array $items, ?array $params = null): bool
96 {
97 $this->prepareItems($items);
98 return $this->sendItemEvent(self::EVENT_DOCUMENT_UPDATED, $items, $params);
99 }
100
106 public function sendDocumentDeletedEvent(array $items, ?array $params = null): bool
107 {
108 return $this->sendItemEvent(self::EVENT_DOCUMENT_DELETED, $items, $params);
109 }
110
117 protected function sendItemEvent(string $eventName, array $items, ?array $params = null): bool
118 {
119 $tag = $this->getTag($params);
120
121 $eventParams = $this->prepareItemEventParams($items, $eventName);
122 $eventParams['skipCurrentUser'] = (!isset($params['SKIP_CURRENT_USER']) || $params['SKIP_CURRENT_USER']);
123
124 return $this->sendEvent($items, $tag, $eventParams);
125 }
126
131 protected function getTag(?array $params = null): string
132 {
133 $entityType = ($params['TYPE'] ?? '');
134 return static::getEventName(static::EVENT_DOCUMENTS_LIST_UPDATED, $entityType);
135 }
136
142 protected static function getEventName(string $eventName, $entityType = ''): string
143 {
144 if(!empty($entityType) && (is_string($entityType) || is_numeric($entityType)))
145 {
146 $eventName .= '_' . $entityType;
147 }
148
149 return $eventName;
150 }
151
156 protected function prepareItems(array &$items, string $action = self::EVENT_DOCUMENT_UPDATED): void
157 {
158 $userId = CurrentUser::get()->getId();
159
160 foreach ($items as $key => $item)
161 {
162 $items[$key]['userId'] = $userId;
163 if (!$this->isMobileIncluded)
164 {
165 continue;
166 }
167
168 $document = (
169 isset($item['data']['oldFields'])
170 ? array_merge($item['data']['oldFields'], $item['data']['fields'])
171 : $item['data']['fields']
172 );
173 $document['ID'] = $item['id'];
174
175 if (
176 $action === self::EVENT_DOCUMENT_UPDATED
177 || $action === self::EVENT_DOCUMENT_ADDED
178 )
179 {
180 if ($document['DOC_TYPE'] === StoreDocumentTable::TYPE_SALES_ORDERS)
181 {
182 $mobileItem = new RealizationList\Item($document);
183 }
184 else
185 {
186 $mobileItem = new StoreDocumentList\Item($document);
187 }
188 $preparedMobileItem = $mobileItem->prepareItem();
189 $items[$key]['mobileData'] = (
190 is_array($preparedMobileItem) // @todo For compatibility. Delete after exiting dto in the mobile.
191 ? $preparedMobileItem['data']
192 : $preparedMobileItem->data
193 );
194
195 /*
196 * Because we not have the realtime on desktop, I temporarily remove the raw data
197 * when we do, then it will be necessary to process this raw data
198 */
199 unset($items[$key]['data']);
200 }
201 }
202 }
203
210 protected static function getItemEventName(string $eventName, string $entityType, int $itemId): ?string
211 {
212 if (!empty($entityType) && $itemId > 0)
213 {
214 return $eventName . '_' . $entityType . '_' . $itemId;
215 }
216
217 return null;
218 }
219
226 protected function sendEvent(array $items, string $eventId, array $params = []): bool
227 {
228 //$params['eventId'] = $eventId;
229 $userIds = $this->getSubscribedUserIdsWithItemPermissions($items, $eventId);
230
231 if ($params['skipCurrentUser'])
232 {
233 $currentUser = CurrentUser::get()->getId();
234 unset($userIds[$currentUser]);
235 }
236 unset($params['skipCurrentUser']);
237
238 return $this->sendUserEvent($eventId, $params, $userIds);
239 }
240
246 protected function prepareItemEventParams(array $items, string $eventName = ''): array
247 {
248 return [
249 'eventName' => $eventName,
250 'items' => $items
251 ];
252 }
253
259 protected function getSubscribedUserIdsWithItemPermissions(array $items, string $eventName): array
260 {
261 if(!$this->isEnabled())
262 {
263 return [];
264 }
265 $userIds = WatchTable::getUserIdsByTag($eventName);
266 return $this->filterUserIdsWhoCanViewItem($items, $userIds);
267 }
268
274 protected function filterUserIdsWhoCanViewItem(array $items, array $userIds): array
275 {
276 $result = [];
277
278 foreach($userIds as $userId)
279 {
280 $userId = (int)$userId;
281 if ($userId > 0 && AccessController::getInstance($userId)->check(ActionDictionary::ACTION_CATALOG_READ))
282 {
283 $result[$userId] = $userId;
284 }
285 }
286
287 return $result;
288 }
289
295 protected function subscribeOnEvent(string $tag, bool $immediate = true): ?string
296 {
297 if($this->isEnabled && !empty($tag))
298 {
299 $addResult = \CPullWatch::Add(CurrentUser::get()->getId(), $tag, $immediate);
300 if($addResult)
301 {
302 return $tag;
303 }
304 }
305
306 return null;
307 }
308
315 protected function sendUserEvent(string $tag, array $params = [], ?array $userIds = null): bool
316 {
317 if(!$this->isEnabled())
318 {
319 return false;
320 }
321
322 if(is_array($userIds))
323 {
324 if(!empty($userIds))
325 {
326 return Event::add($userIds, [
327 'module_id' => static::MODULE_ID,
328 'command' => $tag,
329 'params' => $params,
330 ]);
331 }
332 }
333 else
334 {
335 return \CPullWatch::AddToStack($tag, [
336 'module_id' => static::MODULE_ID,
337 'command' => $tag,
338 'params' => $params,
339 ]);
340 }
341
342 return false;
343 }
344
348 public static function onGetDependentModule(): array
349 {
350 return [
351 'MODULE_ID' => static::MODULE_ID,
352 'USE' => ['PUBLIC_SECTION'],
353 ];
354 }
355}
prepareItemEventParams(array $items, string $eventName='')
getSubscribedUserIdsWithItemPermissions(array $items, string $eventName)
sendDocumentAddedEvent(array $items, ?array $params=null)
sendItemEvent(string $eventName, array $items, ?array $params=null)
sendDocumentsUpdatedEvent(array $items, ?array $params=null)
sendEvent(array $items, string $eventId, array $params=[])
sendDocumentDeletedEvent(array $items, ?array $params=null)
sendUserEvent(string $tag, array $params=[], ?array $userIds=null)
prepareItems(array &$items, string $action=self::EVENT_DOCUMENT_UPDATED)
subscribeOnEvent(string $tag, bool $immediate=true)
filterUserIdsWhoCanViewItem(array $items, array $userIds)
static getEventName(string $eventName, $entityType='')
static getItemEventName(string $eventName, string $entityType, int $itemId)
static getUserIdsByTag(string $tag)