12use Bitrix\CatalogMobile\StoreDocumentList;
13use Bitrix\CatalogMobile\RealizationList;
31 private static $instance;
35 if (!isset(self::$instance))
37 self::$instance = ServiceLocator::getInstance()->get(
'catalog.integration.pullmanager');
40 return self::$instance;
46 $this->isMobileIncluded =
47 Loader::includeModule(
'mobile')
48 && Loader::includeModule(
'catalogmobile')
52 private function __clone()
63 return Loader::includeModule(
'catalog') && Loader::includeModule(
'pull');
87 return $this->
sendItemEvent(self::EVENT_DOCUMENT_ADDED, $items, $params);
98 return $this->
sendItemEvent(self::EVENT_DOCUMENT_UPDATED, $items, $params);
108 return $this->
sendItemEvent(self::EVENT_DOCUMENT_DELETED, $items, $params);
117 protected function sendItemEvent(
string $eventName, array $items, ?array $params =
null): bool
119 $tag = $this->
getTag($params);
122 $eventParams[
'skipCurrentUser'] = (!isset($params[
'SKIP_CURRENT_USER']) || $params[
'SKIP_CURRENT_USER']);
124 return $this->
sendEvent($items, $tag, $eventParams);
131 protected function getTag(?array $params =
null): string
133 $entityType = ($params[
'TYPE'] ??
'');
134 return static::getEventName(static::EVENT_DOCUMENTS_LIST_UPDATED, $entityType);
142 protected static function getEventName(
string $eventName, $entityType =
''): string
144 if(!empty($entityType) && (is_string($entityType) || is_numeric($entityType)))
146 $eventName .=
'_' . $entityType;
156 protected function prepareItems(array &$items,
string $action = self::EVENT_DOCUMENT_UPDATED): void
158 $userId = CurrentUser::get()->getId();
160 foreach ($items as $key => $item)
162 $items[$key][
'userId'] = $userId;
163 if (!$this->isMobileIncluded)
169 isset($item[
'data'][
'oldFields'])
170 ? array_merge($item[
'data'][
'oldFields'], $item[
'data'][
'fields'])
171 : $item[
'data'][
'fields']
173 $document[
'ID'] = $item[
'id'];
176 $action === self::EVENT_DOCUMENT_UPDATED
177 || $action === self::EVENT_DOCUMENT_ADDED
182 $mobileItem =
new RealizationList\Item($document);
186 $mobileItem =
new StoreDocumentList\Item($document);
188 $preparedMobileItem = $mobileItem->prepareItem();
189 $items[$key][
'mobileData'] = (
190 is_array($preparedMobileItem)
191 ? $preparedMobileItem[
'data']
192 : $preparedMobileItem->data
199 unset($items[$key][
'data']);
210 protected static function getItemEventName(
string $eventName,
string $entityType,
int $itemId): ?string
212 if (!empty($entityType) && $itemId > 0)
214 return $eventName .
'_' . $entityType .
'_' . $itemId;
226 protected function sendEvent(array $items,
string $eventId, array $params = []): bool
231 if ($params[
'skipCurrentUser'])
233 $currentUser = CurrentUser::get()->getId();
234 unset($userIds[$currentUser]);
236 unset($params[
'skipCurrentUser']);
249 'eventName' => $eventName,
278 foreach($userIds as $userId)
280 $userId = (int)$userId;
281 if ($userId > 0 && AccessController::getInstance($userId)->check(ActionDictionary::ACTION_CATALOG_READ))
283 $result[$userId] = $userId;
299 $addResult = \CPullWatch::Add(CurrentUser::get()->getId(), $tag, $immediate);
315 protected function sendUserEvent(
string $tag, array $params = [], ?array $userIds =
null): bool
322 if(is_array($userIds))
326 return Event::add($userIds, [
327 'module_id' => static::MODULE_ID,
335 return \CPullWatch::AddToStack($tag, [
336 'module_id' => static::MODULE_ID,
351 'MODULE_ID' => static::MODULE_ID,
352 'USE' => [
'PUBLIC_SECTION'],
prepareItemEventParams(array $items, string $eventName='')
const EVENT_DOCUMENT_ADDED
const EVENT_DOCUMENT_DELETED
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)
static onGetDependentModule()
getTag(?array $params=null)
sendEvent(array $items, string $eventId, array $params=[])
sendDocumentDeletedEvent(array $items, ?array $params=null)
const EVENT_DOCUMENT_UPDATED
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)
const EVENT_DOCUMENTS_LIST_UPDATED
static getUserIdsByTag(string $tag)