Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
FavoriteCollection.php
1<?php
2
4
5use Bitrix\Im\Model\EO_LinkFavorite_Collection;
7use Bitrix\Im\V2\Common\SidebarFilterProcessorTrait;
17
23{
24 use SidebarFilterProcessorTrait;
25
26 public static function getCollectionElementClass(): string
27 {
28 return FavoriteItem::class;
29 }
30
31 public static function find(
32 array $filter = [],
33 array $order = ['ID' => 'DESC'],
34 ?int $limit = null,
35 ?Context $context = null
36 ): self
37 {
38 $context = $context ?? Locator::getContext();
39
40 $messageOrder = ['ID' => 'DESC'];
41
42 if (isset($order['ID']))
43 {
44 $messageOrder['ID'] = $order['ID'];
45 }
46
47 $query = LinkFavoriteTable::query()
48 ->setSelect(['MESSAGE', 'ID', 'CHAT_ID', 'AUTHOR_ID', 'DATE_CREATE', 'MESSAGE_ID'])
49 ->where('AUTHOR_ID', $context->getUserId())
50 ->setOrder($messageOrder)
51 ;
52 if (isset($limit))
53 {
54 $query->setLimit($limit);
55 }
56 static::processFilters($query, $filter, $messageOrder);
57
58 return static::initByEntityCollection($query->fetchCollection());
59 }
60
61 public static function initByEntityCollection(EO_LinkFavorite_Collection $entityCollection): self
62 {
63 $favoriteMessageCollection = new static();
64
65 foreach ($entityCollection as $entity)
66 {
67 $favoriteMessageCollection[] = FavoriteItem::initByEntity($entity);
68 }
69
70 return $favoriteMessageCollection;
71 }
72
73 public static function getByMessage(Message $message): self
74 {
75 $entities = LinkFavoriteTable::query()
76 ->setSelect(['ID', 'CHAT_ID', 'AUTHOR_ID', 'DATE_CREATE', 'MESSAGE_ID'])
77 ->where('MESSAGE_ID', $message->getMessageId())
78 ->fetchCollection()
79 ;
80
81 if ($entities === null)
82 {
83 return new static();
84 }
85
86 $links = static::initByEntityCollection($entities);
87
88 foreach ($links as $link)
89 {
90 $link->setEntity($message);
91 }
92
93 return $links;
94 }
95
97 {
98 $messageCollection = new MessageCollection();
99
100 foreach ($this as $favoriteMessage)
101 {
102 $messageCollection->add($favoriteMessage->getEntity());
103 }
104
105 return $messageCollection;
106 }
107
108 public function getPopupData(array $excludedList = []): PopupData
109 {
110 $messages = $this->getMessageCollection()->fillAllForRest();
111 $data = new PopupData([
114 new ReminderPopupItem($messages->getReminders())
115 ], $excludedList);
116 $excludedList[] = ReminderPopupItem::class;
117
118 return $data->merge(parent::getPopupData($excludedList));
119 }
120
121 public function toRestFormat(array $option = []): array
122 {
123 $this->getMessageCollection()->fillAllForRest();
124
125 return parent::toRestFormat($option);
126 }
127
128 protected static function processFilters(Query $query, array $filter, array $order): void
129 {
130 static::processSidebarFilters(
131 $query,
132 $filter,
133 $order,
134 ['AUTHOR_ID' => 'MESSAGE.AUTHOR_ID', 'DATE_CREATE' => 'MESSAGE.DATE_CREATE']
135 );
136
137 if (isset($filter['SEARCH_MESSAGE']))
138 {
139 $query->whereLike('MESSAGE.MESSAGE', "%{$filter['SEARCH_MESSAGE']}%");
140 }
141 }
142}