PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.8.6
VikBooking Hotel Booking Engine & PMS v1.8.6
1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.7.3 All 36 releases
vikbooking / admin / helpers / src / chat / message.php

message.php in VikBooking Hotel Booking Engine & PMS 1.8.6, at admin/helpers/src/chat/message.php

353 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikBooking
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * This class holds the information of a generic chat message.
16 *
17 * @since 1.8
18 */
19 class VBOChatMessage implements JsonSerializable
20 {
21 /**
22 * The context of the message.
23 *
24 * @var VBOChatContext
25 */
26 protected $context;
27
28 /**
29 * The message identifier.
30 *
31 * @var int
32 */
33 protected $id;
34
35 /**
36 * The name of the user that sent the message.
37 *
38 * @var string
39 */
40 protected $sender_name;
41
42 /**
43 * The ID of the user that sent the message.
44 * When empty, the sender will be equal to the administrator.
45 *
46 * @var int
47 */
48 protected $id_sender;
49
50 /**
51 * The content of the message.
52 *
53 * @var string
54 */
55 protected $message;
56
57 /**
58 * An array of attached files.
59 *
60 * @var VBOChatAttachment[]
61 */
62 protected $attachments = [];
63
64 /**
65 * The creation date of the message.
66 *
67 * @var string
68 */
69 protected $createdon;
70
71 /**
72 * The ID of the user that created the message.
73 *
74 * @var int
75 */
76 protected $createdby;
77
78 /**
79 * Whether the message has been read by the user.
80 *
81 * @var bool
82 */
83 protected $read = true;
84
85 /**
86 * Class constructor.
87 *
88 * @param VBOChatContext $context The message context.
89 * @param array|object|null $data The message details.
90 */
91 public function __construct(VBOChatContext $context, $data = null)
92 {
93 $this->context = $context;
94
95 if ($data) {
96 $this->bind($data);
97 }
98 }
99
100 /**
101 * Binds the message properties with the provided data.
102 *
103 * @param array|object $data The message data.
104 *
105 * @return self This object to support chaining.
106 */
107 public function bind($data)
108 {
109 if (!is_array($data) && !is_object($data)) {
110 throw new \InvalidArgumentException('Cannot bind chat message! Array or object expected, ' . gettype($data) . ' given.', 400);
111 }
112
113 foreach ($data as $k => $v) {
114 // never update the context of the message
115 if (!strcasecmp($k, 'context')) {
116 continue;
117 }
118
119 if (!strcasecmp($k, 'attachments')) {
120 // set attachments accordingly
121 $this->setAttachments((array) $v);
122 } else if (!strcasecmp($k, 'createdon')) {
123 // set creation date accordingly
124 $this->setCreationDate($v);
125 } else if (property_exists($this, $k)) {
126 // inject value only in case the property actually exists
127 $this->{$k} = $v;
128 }
129 }
130
131 return $this;
132 }
133
134 /**
135 * Returns the context of the message.
136 *
137 * @return VBOChatContext
138 */
139 public function getContext()
140 {
141 return $this->context;
142 }
143
144 /**
145 * Returns the ID the message.
146 *
147 * @return int
148 */
149 public function getID()
150 {
151 return (int) $this->id;
152 }
153
154 /**
155 * Returns the name of the user that wrote the message.
156 *
157 * @return string
158 */
159 public function getSenderName()
160 {
161 return (string) $this->sender_name;
162 }
163
164 /**
165 * Returns the ID of the sender.
166 * When the value is equal to 0, the message is sent by an administrator.
167 *
168 * @return int
169 */
170 public function getSenderID()
171 {
172 return (int) $this->id_sender;
173 }
174
175 /**
176 * Sets the sender information.
177 *
178 * @param string $name The name of the sender.
179 * @param int $id The ID of the sender (0 for admin).
180 *
181 * @return self
182 */
183 public function setSender(string $name, int $id = 0)
184 {
185 $this->sender_name = $name;
186 $this->id_sender = $id;
187
188 return $this;
189 }
190
191 /**
192 * Returns the text of the message.
193 *
194 * @return string
195 */
196 public function getMessage()
197 {
198 return (string) $this->message;
199 }
200
201 /**
202 * Sets the text of the message.
203 *
204 * @param string $message
205 *
206 * @return self
207 */
208 public function setMessage(string $message)
209 {
210 $this->message = $message;
211
212 return $this;
213 }
214
215 /**
216 * Returns an array of files.
217 *
218 * @return array
219 */
220 public function getAttachments()
221 {
222 return $this->attachments;
223 }
224
225 /**
226 * Sets the specified files as attachments.
227 *
228 * @param VBOChatAttachment[] $attachments
229 *
230 * @return self
231 */
232 public function setAttachments(array $attachments)
233 {
234 $this->attachments = [];
235
236 foreach ($attachments as $attachment) {
237 if ($attachment instanceof VBOChatAttachment) {
238 $this->addAttachment($attachment);
239 }
240 }
241
242 return $this;
243 }
244
245 /**
246 * Adds a new file as attachment.
247 *
248 * @param VBOChatAttachment $attachment
249 *
250 * @return self
251 *
252 * @throws InvalidArgumentException
253 */
254 public function addAttachment(VBOChatAttachment $attachment)
255 {
256 // make sure the file exists
257 if (!$attachment->exists()) {
258 throw new \InvalidArgumentException('The attached file does not exist: ' . $attachment->getName(), 404);
259 }
260
261 $this->attachments[] = $attachment;
262
263 return $this;
264 }
265
266 /**
267 * Returns the creation date of the message (UTC).
268 *
269 * @return string
270 */
271 public function getCreationDate()
272 {
273 if (!$this->createdon) {
274 $this->createdon = JFactory::getDate()->toSql();
275 }
276
277 return $this->createdon;
278 }
279
280 /**
281 * Sets the creation date of the message (UTC).
282 *
283 * @param mixed $date Either a date string or a DateTime object.
284 *
285 * @return self
286 */
287 public function setCreationDate($date)
288 {
289 try {
290 if (is_string($date)) {
291 // make sure the provided date is correct
292 $date = JFactory::getDate($date);
293 }
294
295 // convert date object into a string
296 $date = $date->toSql();
297 } catch (Exception $error) {
298 // malformed date
299 $date = null;
300 }
301
302 $this->createdon = $date;
303
304 return $this;
305 }
306
307 /**
308 * Returns the user ID of the message author.
309 *
310 * @return int
311 */
312 public function getAuthor()
313 {
314 if (!$this->createdby) {
315 $this->createdby = \JFactory::getUser()->id;
316 }
317
318 return $this->createdby;
319 }
320
321 /**
322 * Checks whether the message has been read by the current user.
323 *
324 * @return bool
325 */
326 public function isRead()
327 {
328 return (bool) $this->read;
329 }
330
331 /**
332 * @inheritDoc
333 *
334 * @see JsonSerializable
335 */
336 #[ReturnTypeWillChange]
337 public function jsonSerialize()
338 {
339 return [
340 'id' => $this->getID(),
341 'context' => $this->getContext()->getAlias(),
342 'id_context' => $this->getContext()->getID(),
343 'sender_name' => $this->getSenderName(),
344 'id_sender' => $this->getSenderID(),
345 'message' => $this->getMessage(),
346 'attachments' => $this->getAttachments(),
347 'createdon' => $this->getCreationDate(),
348 'createdby' => $this->getAuthor(),
349 'read' => $this->isRead(),
350 ];
351 }
352 }
353