Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
StartIdFilter.php
1<?php
2
4
10
11class StartIdFilter extends Base
12{
13 public function onBeforeAction(Event $event)
14 {
15 foreach ($this->getAction()->getArguments() as $argument)
16 {
17 if ($argument instanceof Message)
18 {
19 if (!$argument->getId())
20 {
21 return null;
22 }
23
24 if ($argument->getId() < $argument->getChat()->getStartId())
25 {
26 $this->addError(new Message\MessageError(Message\MessageError::MESSAGE_ACCESS_ERROR));
27
28 return new EventResult(EventResult::ERROR, null, null, $this);
29 }
30 }
31
32 if ($argument instanceof MessageCollection)
33 {
34 $messages = $argument;
35 $this->filterMessagesByStartId($messages);
36 if ($messages->count() === 0)
37 {
38 $this->addError(new Message\MessageError(Message\MessageError::MESSAGE_ACCESS_ERROR));
39
40 return new EventResult(EventResult::ERROR, null, null, $this);
41 }
42 }
43 }
44
45 return null;
46 }
47
48 private function filterMessagesByStartId(MessageCollection $messages): void
49 {
50 $keysToUnset = [];
51
52 foreach ($messages as $key => $message)
53 {
54 if ($message->getMessageId() && $message->getMessageId() < $message->getChat()->getStartId())
55 {
56 $keysToUnset[$key] = $key;
57 }
58 }
59
60 $messages->unsetByKeys($keysToUnset);
61 }
62}
unsetByKeys(array $keys)
Definition Registry.php:10