Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
FavoriteItem.php
1<?php
2
4
5use Bitrix\Im\Model\EO_LinkFavorite;
7use Bitrix\Im\V2\Common\ContextCustomer;
15
20{
21 use ContextCustomer;
22
23 public function __construct($source = null)
24 {
25 $this->initByDefault();
26
27 if (!empty($source))
28 {
29 $this->load($source);
30 }
31 }
32
33 public static function getDataClass(): string
34 {
35 return LinkFavoriteTable::class;
36 }
37
38 protected static function getEntityIdFieldName(): string
39 {
40 return 'MESSAGE_ID';
41 }
42
43 public static function getEntityClassName(): string
44 {
45 return Message::class;
46 }
47
48 public static function getRestEntityName(): string
49 {
50 return 'link';
51 }
52
53 public static function initByEntity(EO_LinkFavorite $entity): self
54 {
55 $favoriteMessage = new static($entity);
56
57 if ($entity->getMessage() !== null)
58 {
59 $favoriteMessage->setEntity(new Message($entity->getMessage()));
60 }
61
62 return $favoriteMessage;
63 }
64
65 public static function createFromMessage(Message $message, ?Context $context = null): self
66 {
67 $favoriteMessage = new static();
68 $favoriteMessage->setContext($context);
69
70 $favoriteMessage
71 ->setEntity($message)
72 ->setAuthorId($favoriteMessage->getContext()->getUserId())
73 ->setChatId($message->getChatId())
74 ;
75
76 return $favoriteMessage;
77 }
78
79 public static function getByMessageAndUserId(Message $message, int $userId): ?self
80 {
81 if ($message->getMessageId() === null)
82 {
83 return null;
84 }
85
86 $entity = LinkFavoriteTable::query()
87 ->setSelect(['CHAT_ID', 'AUTHOR_ID', 'DATE_CREATE', 'MESSAGE_ID'])
88 ->where('MESSAGE_ID', $message->getMessageId())
89 ->where('AUTHOR_ID', $userId)
90 ->setLimit(1)
91 ->fetchObject()
92 ;
93
94 if ($entity === null)
95 {
96 return null;
97 }
98
99 return static::initByEntity($entity)->setEntity($message);
100 }
101
102 public static function linkEntityToMessage(Entity $entity, Message $message): BaseLinkItem
103 {
104 throw new NotImplementedException();
105 }
106
107 public function setMessageInfo(Message $message): BaseLinkItem
108 {
109 $this->setEntity($message);
110
111 return $this;
112 }
113
114 public function setMessageId(?int $messageId): BaseLinkItem
115 {
116 $this->setEntityId($messageId);
117
118 return parent::setMessageId($messageId);
119 }
120
121 public function toRestFormat(array $option = []): array
122 {
123 return [
124 'id' => $this->getPrimaryId(),
125 'messageId' => $this->getMessageId(),
126 'chatId' => $this->getChatId(),
127 'authorId' => $this->getAuthorId(),
128 'dateCreate' => $this->getDateCreate()->format('c'),
129 'message' => $this->getEntity()->toRestFormat(),
130 ];
131 }
132
133 public function getMessageId(): ?int
134 {
135 return $this->getEntityId();
136 }
137
138 public function getPopupData(array $excludedList = []): PopupData
139 {
140 $data = new PopupData([
144 ], $excludedList);
145
146 return $data->merge(parent::getPopupData($excludedList));
147 }
148}