| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2026 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 |
* The ID of the external resource this message is linked to. |
| 80 |
* |
| 81 |
* @var string |
| 82 |
* @since 1.8.8 |
| 83 |
*/ |
| 84 |
protected $ref_id = null; |
| 85 |
|
| 86 |
/** |
| 87 |
* Whether the message has been read by the user. |
| 88 |
* |
| 89 |
* @var bool |
| 90 |
*/ |
| 91 |
protected $read = true; |
| 92 |
|
| 93 |
/** |
| 94 |
* Class constructor. |
| 95 |
* |
| 96 |
* @param VBOChatContext $context The message context. |
| 97 |
* @param array|object|null $data The message details. |
| 98 |
*/ |
| 99 |
public function __construct(VBOChatContext $context, $data = null) |
| 100 |
{ |
| 101 |
$this->context = $context; |
| 102 |
|
| 103 |
if ($data) { |
| 104 |
$this->bind($data); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Binds the message properties with the provided data. |
| 110 |
* |
| 111 |
* @param array|object $data The message data. |
| 112 |
* |
| 113 |
* @return self This object to support chaining. |
| 114 |
*/ |
| 115 |
public function bind($data) |
| 116 |
{ |
| 117 |
if (!is_array($data) && !is_object($data)) { |
| 118 |
throw new \InvalidArgumentException('Cannot bind chat message! Array or object expected, ' . gettype($data) . ' given.', 400); |
| 119 |
} |
| 120 |
|
| 121 |
foreach ($data as $k => $v) { |
| 122 |
// never update the context of the message |
| 123 |
if (!strcasecmp($k, 'context')) { |
| 124 |
continue; |
| 125 |
} |
| 126 |
|
| 127 |
if (!strcasecmp($k, 'attachments')) { |
| 128 |
// set attachments accordingly |
| 129 |
$this->setAttachments((array) $v); |
| 130 |
} else if (!strcasecmp($k, 'createdon')) { |
| 131 |
// set creation date accordingly |
| 132 |
$this->setCreationDate($v); |
| 133 |
} else if (property_exists($this, $k)) { |
| 134 |
// inject value only in case the property actually exists |
| 135 |
$this->{$k} = $v; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
return $this; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Returns the context of the message. |
| 144 |
* |
| 145 |
* @return VBOChatContext |
| 146 |
*/ |
| 147 |
public function getContext() |
| 148 |
{ |
| 149 |
return $this->context; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Returns the ID the message. |
| 154 |
* |
| 155 |
* @return int |
| 156 |
*/ |
| 157 |
public function getID() |
| 158 |
{ |
| 159 |
return (int) $this->id; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Returns the name of the user that wrote the message. |
| 164 |
* |
| 165 |
* @return string |
| 166 |
*/ |
| 167 |
public function getSenderName() |
| 168 |
{ |
| 169 |
return (string) $this->sender_name; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Returns the ID of the sender. |
| 174 |
* When the value is equal to 0, the message is sent by an administrator. |
| 175 |
* |
| 176 |
* @return int |
| 177 |
*/ |
| 178 |
public function getSenderID() |
| 179 |
{ |
| 180 |
return (int) $this->id_sender; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Sets the sender information. |
| 185 |
* |
| 186 |
* @param string $name The name of the sender. |
| 187 |
* @param int $id The ID of the sender (0 for admin). |
| 188 |
* |
| 189 |
* @return self |
| 190 |
*/ |
| 191 |
public function setSender(string $name, int $id = 0) |
| 192 |
{ |
| 193 |
$this->sender_name = $name; |
| 194 |
$this->id_sender = $id; |
| 195 |
|
| 196 |
return $this; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Returns the text of the message. |
| 201 |
* |
| 202 |
* @return string |
| 203 |
*/ |
| 204 |
public function getMessage() |
| 205 |
{ |
| 206 |
return (string) $this->message; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Sets the text of the message. |
| 211 |
* |
| 212 |
* @param string $message |
| 213 |
* |
| 214 |
* @return self |
| 215 |
*/ |
| 216 |
public function setMessage(string $message) |
| 217 |
{ |
| 218 |
$this->message = $message; |
| 219 |
|
| 220 |
return $this; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Returns an array of files. |
| 225 |
* |
| 226 |
* @return array |
| 227 |
*/ |
| 228 |
public function getAttachments() |
| 229 |
{ |
| 230 |
return $this->attachments; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Sets the specified files as attachments. |
| 235 |
* |
| 236 |
* @param VBOChatAttachment[] $attachments |
| 237 |
* |
| 238 |
* @return self |
| 239 |
*/ |
| 240 |
public function setAttachments(array $attachments) |
| 241 |
{ |
| 242 |
$this->attachments = []; |
| 243 |
|
| 244 |
foreach ($attachments as $attachment) { |
| 245 |
if ($attachment instanceof VBOChatAttachment) { |
| 246 |
$this->addAttachment($attachment); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
return $this; |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Adds a new file as attachment. |
| 255 |
* |
| 256 |
* @param VBOChatAttachment $attachment |
| 257 |
* |
| 258 |
* @return self |
| 259 |
* |
| 260 |
* @throws InvalidArgumentException |
| 261 |
*/ |
| 262 |
public function addAttachment(VBOChatAttachment $attachment) |
| 263 |
{ |
| 264 |
// make sure the file exists |
| 265 |
if (!$attachment->exists()) { |
| 266 |
throw new \InvalidArgumentException('The attached file does not exist: ' . $attachment->getName(), 404); |
| 267 |
} |
| 268 |
|
| 269 |
$this->attachments[] = $attachment; |
| 270 |
|
| 271 |
return $this; |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Returns the creation date of the message (UTC). |
| 276 |
* |
| 277 |
* @return string |
| 278 |
*/ |
| 279 |
public function getCreationDate() |
| 280 |
{ |
| 281 |
if (!$this->createdon) { |
| 282 |
$this->createdon = JFactory::getDate()->toSql(); |
| 283 |
} |
| 284 |
|
| 285 |
return $this->createdon; |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* Sets the creation date of the message (UTC). |
| 290 |
* |
| 291 |
* @param mixed $date Either a date string or a DateTime object. |
| 292 |
* |
| 293 |
* @return self |
| 294 |
*/ |
| 295 |
public function setCreationDate($date) |
| 296 |
{ |
| 297 |
try { |
| 298 |
if (!$date instanceof DateTime) { |
| 299 |
// make sure the provided date is correct |
| 300 |
$date = JFactory::getDate($date ?: 'now'); |
| 301 |
} |
| 302 |
|
| 303 |
// convert date object into a string |
| 304 |
$date = $date->toSql(); |
| 305 |
|
| 306 |
// make sure the specified date is not in the future |
| 307 |
if ($date > JFactory::getDate()->toSql()) { |
| 308 |
throw new UnexpectedValueException('Cannot accept a date in the future.', 400); |
| 309 |
} |
| 310 |
} catch (Exception $error) { |
| 311 |
// malformed date |
| 312 |
$date = null; |
| 313 |
} |
| 314 |
|
| 315 |
$this->createdon = $date; |
| 316 |
|
| 317 |
return $this; |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Returns the user ID of the message author. |
| 322 |
* |
| 323 |
* @return int |
| 324 |
*/ |
| 325 |
public function getAuthor() |
| 326 |
{ |
| 327 |
if (!$this->createdby) { |
| 328 |
$this->createdby = \JFactory::getUser()->id; |
| 329 |
} |
| 330 |
|
| 331 |
return $this->createdby; |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* Returns the ID of the external resource this message is linked to. |
| 336 |
* |
| 337 |
* @return string|null |
| 338 |
* |
| 339 |
* @since 1.8.8 |
| 340 |
*/ |
| 341 |
public function getReferenceID() |
| 342 |
{ |
| 343 |
return $this->ref_id; |
| 344 |
} |
| 345 |
|
| 346 |
/** |
| 347 |
* Sets the ID of the external resource this message is linked to. |
| 348 |
* |
| 349 |
* @param string|null $refId |
| 350 |
* |
| 351 |
* @return self |
| 352 |
* |
| 353 |
* @since 1.8.8 |
| 354 |
*/ |
| 355 |
public function setReferenceID(?string $refId) |
| 356 |
{ |
| 357 |
$this->ref_id = $refId; |
| 358 |
|
| 359 |
return $this; |
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
* Checks whether the message has been read by the current user. |
| 364 |
* |
| 365 |
* @return bool |
| 366 |
*/ |
| 367 |
public function isRead() |
| 368 |
{ |
| 369 |
return (bool) $this->read; |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* @inheritDoc |
| 374 |
* |
| 375 |
* @see JsonSerializable |
| 376 |
*/ |
| 377 |
#[ReturnTypeWillChange] |
| 378 |
public function jsonSerialize() |
| 379 |
{ |
| 380 |
return [ |
| 381 |
'id' => $this->getID(), |
| 382 |
'context' => $this->getContext()->getAlias(), |
| 383 |
'id_context' => $this->getContext()->getID(), |
| 384 |
'sender_name' => $this->getSenderName(), |
| 385 |
'id_sender' => $this->getSenderID(), |
| 386 |
'message' => $this->getMessage(), |
| 387 |
'attachments' => $this->getAttachments(), |
| 388 |
'createdon' => $this->getCreationDate(), |
| 389 |
'createdby' => $this->getAuthor(), |
| 390 |
'read' => $this->isRead(), |
| 391 |
'ref_id' => $this->ref_id, |
| 392 |
]; |
| 393 |
} |
| 394 |
} |
| 395 |
|