Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
UrlCollection.php
1<?php
2
4
8use Bitrix\Im\V2\Common\MigrationStatusCheckerTrait;
9use Bitrix\Im\V2\Common\SidebarFilterProcessorTrait;
15
21{
22 use SidebarFilterProcessorTrait;
23 use MigrationStatusCheckerTrait;
24
25 protected static string $migrationOptionName = 'im_link_url_migration';
26
27 public static function getCollectionElementClass(): string
28 {
29 return UrlItem::class;
30 }
31
32 public function save(bool $isGroupSave = false): Result
33 {
34 if (!static::isMigrationFinished())
35 {
36 return (new Result)->addError(new Entity\Url\UrlError(Entity\Url\UrlError::SAVE_BEFORE_MIGRATION_ERROR));
37 }
38
39 $saveResult = parent::save($isGroupSave);
40 LinkUrlIndexTable::indexInBackground();
41
42 return $saveResult;
43 }
44
45 public static function initByMessage(Message $message): self
46 {
47 $entities = \Bitrix\Im\V2\Entity\Url\UrlCollection::initByMessage($message);
48
49 return static::linkEntityToMessage($entities, $message);
50 }
51
52 public static function find(
53 array $filter,
54 array $order = ['MESSAGE_ID' => 'DESC'],
55 ?int $limit = null,
56 ?Context $context = null,
57 ?int $offset = null
58 ): self
59 {
60 $urlOrder = ['MESSAGE_ID' => 'DESC'];
61
62 if (isset($order['MESSAGE_ID']))
63 {
64 $urlOrder['MESSAGE_ID'] = $order['MESSAGE_ID'];
65 }
66
67 $query = LinkUrlTable::query()
68 ->setSelect(['ID', 'URL', 'DATE_CREATE', 'MESSAGE_ID', 'CHAT_ID', 'PREVIEW_URL_ID', 'AUTHOR_ID'])
69 ->setOrder($urlOrder)
70 ;
71 if (isset($limit))
72 {
73 $query->setLimit($limit);
74 }
75 if (isset($offset))
76 {
77 $query->setOffset($offset);
78 }
79 static::processFilters($query, $filter, $urlOrder);
80
81 $urlCollection = new static($query->fetchCollection());
82
83 return $urlCollection->fillMetadata();
84 }
85
86 public function fillMetadata(bool $withHtml = true): self
87 {
88 $previewUrlsIds = $this->getEntityIds();
89
90 $entities = \Bitrix\Im\V2\Entity\Url\UrlCollection::initByPreviewUrlsIds($previewUrlsIds, $withHtml);
91
92 foreach ($this as $url)
93 {
94 if ($entities->getById($url->getEntityId()) !== null)
95 {
96 $url->setEntity($entities->getById($url->getEntityId()));
97 }
98 else
99 {
100 $url->setEntity((new Entity\Url\UrlItem())->setUrl($url->getUrl()));
101 }
102 }
103
104 return $this;
105 }
106
107 public static function deleteByMessagesIds(array $messagesIds): void
108 {
109 LinkUrlTable::deleteByFilter(['=MESSAGE_ID' => $messagesIds]);
110 }
111
112 public static function deleteByChatsIds(array $chatsIds): void
113 {
114 LinkUrlTable::deleteByFilter(['=CHAT_ID' => $chatsIds]);
115 }
116
117 public static function deleteByAuthorsIds(array $authorsIds): void
118 {
119 LinkUrlTable::deleteByFilter(['=AUTHOR_ID' => $authorsIds]);
120 }
121
122 protected static function processFilters(Query $query, array $filter, array $order): void
123 {
124 static::processSidebarFilters($query, $filter, $order);
125
126 if (isset($filter['SEARCH_URL']))
127 {
128 $query->withSearchByUrl($filter['SEARCH_URL']);
129 }
130 }
131}
addError(Error $error)
Definition result.php:50