Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ReactionItem.php
1<?php
2
4
7use Bitrix\Im\V2\Common\ActiveRecordImplementation;
8use Bitrix\Im\V2\Common\ContextCustomer;
9use Bitrix\Im\V2\Common\RegistryEntryImplementation;
18
20{
21 use ContextCustomer;
22 use RegistryEntryImplementation;
23 use ActiveRecordImplementation;
24
25 public const LIKE = 'LIKE';
26 public const KISS = 'KISS';
27 public const LAUGH = 'LAUGH';
28 public const WONDER = 'WONDER';
29 public const CRY = 'CRY';
30 public const ANGRY = 'ANGRY';
31 public const FACEPALM = 'FACEPALM';
32
33 public const ALLOWED_REACTION = [
41 ];
42
43 private int $id;
44 private int $chatId;
45 private int $messageId;
46 private int $userId;
47 private string $reaction;
48 private DateTime $dateCreate;
49
50 public function __construct($source = null)
51 {
52 $this->initByDefault();
53
54 if (!empty($source))
55 {
56 $this->load($source);
57 }
58 }
59
60 public static function getByMessage(int $messageId, string $reaction, int $userId): ?self
61 {
62 $reactionObject = ReactionTable::query()
63 ->setSelect(['*'])
64 ->where('MESSAGE_ID', $messageId)
65 ->where('REACTION', $reaction)
66 ->where('USER_ID', $userId)
67 ->fetchObject()
68 ;
69
70 if ($reactionObject === null)
71 {
72 return null;
73 }
74
75 return new static($reactionObject);
76 }
77
78 public function getPopupData(array $excludedList = []): PopupData
79 {
80 return new PopupData([new UserPopupItem([$this->getUserId()])], $excludedList);
81 }
82
83 public function getLocName(?string $languageId = null): ?string
84 {
85 return Loc::getMessage("IM_MESSAGE_REACTION_NAME_{$this->reaction}", null, $languageId);
86 }
87
88 public function getPrimaryId(): ?int
89 {
90 return $this->id ?? null;
91 }
92
93 public function setPrimaryId(int $primaryId): self
94 {
95 $this->id = $primaryId;
96
97 return $this;
98 }
99
100 public function getChatId(): int
101 {
102 return $this->chatId;
103 }
104
105 public function setChatId(int $chatId): ReactionItem
106 {
107 $this->chatId = $chatId;
108 return $this;
109 }
110
111 public function getMessageId(): int
112 {
113 return $this->messageId;
114 }
115
116 public function setMessageId(int $messageId): ReactionItem
117 {
118 $this->messageId = $messageId;
119 return $this;
120 }
121
122 public function getUserId(): int
123 {
124 return $this->userId;
125 }
126
127 public function setUserId(int $userId): ReactionItem
128 {
129 $this->userId = $userId;
130 return $this;
131 }
132
133 public function getReaction(): string
134 {
135 return $this->reaction;
136 }
137
138 public function setReaction(string $reaction): ReactionItem
139 {
140 $this->reaction = $reaction;
141 return $this;
142 }
143
144 public function getDateCreate(): DateTime
145 {
146 return $this->dateCreate;
147 }
148
149 public function setDateCreate(DateTime $dateCreate): ReactionItem
150 {
151 $this->dateCreate = $dateCreate;
152 return $this;
153 }
154
155 public function getDefaultReaction(): string
156 {
157 return self::LIKE;
158 }
159
160 public function beforeSaveReaction(): Result
161 {
162 return static::validateReaction($this->reaction);
163 }
164
166 {
167 return new DateTime();
168 }
169
170 public static function validateReaction(string $reaction): Result
171 {
172 $result = new Result();
173
174 if (!in_array($reaction, self::ALLOWED_REACTION, true))
175 {
176 $result->addError(new ReactionError(ReactionError::NOT_FOUND));
177 }
178
179 return $result;
180 }
181
185 protected static function mirrorDataEntityFields(): array
186 {
187 return [
188 'ID' => [
189 'primary' => true,
190 'field' => 'id',
191 'set' => 'setPrimaryId',
192 'get' => 'getPrimaryId',
193 ],
194 'CHAT_ID' => [
195 'field' => 'chatId',
196 'set' => 'setChatId',
197 'get' => 'getChatId',
198 ],
199 'MESSAGE_ID' => [
200 'field' => 'messageId',
201 'set' => 'setMessageId',
202 'get' => 'getMessageId',
203 ],
204 'USER_ID' => [
205 'field' => 'userId',
206 'set' => 'setUserId',
207 'get' => 'getUserId',
208 ],
209 'REACTION' => [
210 'field' => 'reaction',
211 'set' => 'setReaction',
212 'get' => 'getReaction',
213 'default' => 'getDefaultReaction',
214 'beforeSave' => 'beforeSaveReaction',
215 ],
216 'DATE_CREATE' => [
217 'field' => 'dateCreate',
218 'get' => 'getDateCreate',
219 'set' => 'setDateCreate',
220 'default' => 'getDefaultDateCreate',
221 ],
222 ];
223 }
224
225 public static function getDataClass(): string
226 {
227 return ReactionTable::class;
228 }
229
230 public static function getRestEntityName(): string
231 {
232 return 'reaction';
233 }
234
235 public function toRestFormat(array $option = []): array
236 {
237 return [
238 'id' => $this->getPrimaryId(),
239 'messageId' => $this->getMessageId(),
240 'userId' => $this->getUserId(),
241 'reaction' => $this->getReaction(),
242 'dateCreate' => $this->getDateCreate()->format('c'),
243 ];
244 }
245}
static getByMessage(int $messageId, string $reaction, int $userId)
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29