Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ReminderCollection.php
1<?php
2
4
5use Bitrix\Im\Model\EO_LinkReminder_Collection;
8use Bitrix\Im\V2\Common\SidebarFilterProcessorTrait;
20
26{
27 use SidebarFilterProcessorTrait;
28
29 public static function getCollectionElementClass(): string
30 {
31 return ReminderItem::class;
32 }
33
34 public static function find(
35 array $filter,
36 array $order,
37 ?int $limit = null,
38 ?Context $context = null
39 ): self
40 {
41 $context = $context ?? Locator::getContext();
42
43 $reminderOrder = ['ID' => 'DESC'];
44
45 if (isset($order['ID']))
46 {
47 $reminderOrder['ID'] = $order['ID'];
48 }
49
51 ->setSelect(['ID', 'CHAT_ID', 'AUTHOR_ID', 'DATE_CREATE', 'MESSAGE_ID', 'DATE_REMIND', 'IS_REMINDED', 'MESSAGE'])
52 ->where('AUTHOR_ID', $context->getUserId())
53 ->setOrder($reminderOrder)
54 ;
55 if (isset($limit))
56 {
57 $query->setLimit($limit);
58 }
59 static::processFilters($query, $filter, $reminderOrder);
60
61 return static::initByEntityCollection($query->fetchCollection());
62 }
63
64 public static function initByEntityCollection(EO_LinkReminder_Collection $entityCollection): self
65 {
66 $reminderCollection = new static();
67
68 foreach ($entityCollection as $entity)
69 {
70 $reminderCollection[] = ReminderItem::initByEntity($entity);
71 }
72
73 return $reminderCollection;
74 }
75
76 public static function getByMessage(Message $message): self
77 {
78 $entities = LinkReminderTable::query()
79 ->setSelect(['ID', 'CHAT_ID', 'AUTHOR_ID', 'DATE_CREATE', 'MESSAGE_ID', 'DATE_REMIND', 'IS_REMINDED'])
80 ->where('MESSAGE_ID', $message->getMessageId())
81 ->fetchCollection()
82 ;
83
84 if ($entities === null)
85 {
86 return new static();
87 }
88
89 $links = static::initByEntityCollection($entities);
90
91 foreach ($links as $link)
92 {
93 $link->setEntity($message);
94 }
95
96 return $links;
97 }
98
99 public static function getByMessagesAndAuthorId(MessageCollection $messages, int $userId): self
100 {
101 $links = static::getByMessageIdsAndAuthorId($messages->getIds(), $userId);
102
103 foreach ($links as $link)
104 {
105 $link->setEntity($messages[$link->getMessageId()]);
106 }
107
108 return $links;
109 }
110
111 public static function getByMessageIdsAndAuthorId(array $messageIds, int $userId): self
112 {
113 if (empty($messageIds))
114 {
115 return new static();
116 }
117
118 $entities = LinkReminderTable::query()
119 ->setSelect(['ID', 'CHAT_ID', 'AUTHOR_ID', 'DATE_CREATE', 'MESSAGE_ID', 'DATE_REMIND', 'IS_REMINDED'])
120 ->whereIn('MESSAGE_ID', $messageIds)
121 ->where('AUTHOR_ID', $userId)
122 ->fetchCollection()
123 ;
124
125 if ($entities === null)
126 {
127 return new static();
128 }
129
130 return static::initByEntityCollection($entities);
131 }
132
133 public static function getNeedReminded(): self
134 {
135 $entities = LinkReminderTable::query()
136 ->setSelect(['ID', 'CHAT_ID', 'AUTHOR_ID', 'DATE_CREATE', 'MESSAGE_ID', 'DATE_REMIND', 'IS_REMINDED', 'MESSAGE'])
137 ->where('DATE_REMIND', '<', new DateTime())
138 ->where('IS_REMINDED', false)
139 ->fetchCollection()
140 ;
141
142 if ($entities === null)
143 {
144 return new static();
145 }
146
147 return static::initByEntityCollection($entities);
148 }
149
151 {
152 $messageCollection = new MessageCollection();
153
154 foreach ($this as $reminderItem)
155 {
156 $messageCollection->add($reminderItem->getEntity());
157 }
158
159 return $messageCollection;
160 }
161
162 public function getPopupData(array $excludedList = []): PopupData
163 {
164 $excludedList[] = ReminderPopupItem::class;
165
166 if (!in_array(FilePopupItem::class, $excludedList, true))
167 {
168 $this->getMessageCollection()->fillFiles();
169 }
170
171 return parent::getPopupData($excludedList);
172 }
173
174 public function toRestFormat(array $option = []): array
175 {
176 if (!isset($option['WITHOUT_MESSAGES']) || $option['WITHOUT_MESSAGES'] !== 'Y')
177 {
178 $this->getMessageCollection()->fillAllForRest();
179 }
180
181 return parent::toRestFormat($option);
182 }
183
184 protected static function processFilters(Query $query, array $filter, array $order): void
185 {
186 static::processSidebarFilters(
187 $query,
188 $filter,
189 $order,
190 ['AUTHOR_ID' => 'MESSAGE.AUTHOR_ID', 'DATE_CREATE' => 'MESSAGE.DATE_CREATE']
191 );
192
193 if (isset($filter['SEARCH_MESSAGE']))
194 {
195 $query->whereLike('MESSAGE.MESSAGE', "%{$filter['SEARCH_MESSAGE']}%");
196 }
197
198 if (isset($filter['IS_REMINDED']))
199 {
200 $query->where('IS_REMINDED', (bool)$filter['IS_REMINDED']);
201 }
202 }
203}