| 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 |
* Chat web push notification trait. |
| 16 |
* |
| 17 |
* @since 1.8 |
| 18 |
*/ |
| 19 |
trait VBOChatNotificationWebpush |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Enqueues a new messages within the notification center. |
| 23 |
* |
| 24 |
* @param VBOChatMessage $message The message instance. |
| 25 |
* @param VBOChatUser $user The user that sent the message. |
| 26 |
* |
| 27 |
* @return bool |
| 28 |
*/ |
| 29 |
public function sendWebPushNotification(VBOChatMessage $message, VBOChatUser $user) |
| 30 |
{ |
| 31 |
// display the whole message as summary |
| 32 |
$summary = (string) $message->getMessage(); |
| 33 |
|
| 34 |
if (strlen($summary) === 0) { |
| 35 |
// no message provided, display the number of attached files |
| 36 |
$attachmentsCount = count($message->getAttachments()); |
| 37 |
|
| 38 |
if ($attachmentsCount === 1) { |
| 39 |
// only one attachment |
| 40 |
$summary = JText::sprintf('VBO_CHAT_MESSAGE_WEBPUSH_NOTIFICATION_SUMMARY_N_FILES_1', $message->getSenderName()); |
| 41 |
} else { |
| 42 |
// multiple attachments |
| 43 |
$summary = JText::plural('VBO_CHAT_MESSAGE_WEBPUSH_NOTIFICATION_SUMMARY_N_FILES', $message->getSenderName(), $attachmentsCount); |
| 44 |
} |
| 45 |
} else { |
| 46 |
// message provided |
| 47 |
$summary = JText::sprintf('VBO_CHAT_MESSAGE_WEBPUSH_NOTIFICATION_SUMMARY', $message->getSenderName(), $summary); |
| 48 |
} |
| 49 |
|
| 50 |
try { |
| 51 |
/** @var VBOChatContext */ |
| 52 |
$context = $message->getContext(); |
| 53 |
|
| 54 |
// store the notification record |
| 55 |
VBOFactory::getNotificationCenter()->store([ |
| 56 |
[ |
| 57 |
'sender' => $context->getAlias() === 'session' ? 'website' : 'operators', |
| 58 |
'type' => 'chat.newmessage', |
| 59 |
'title' => JText::sprintf('VBO_CHAT_MESSAGE_WEBPUSH_NOTIFICATION_TITLE', $context->getSubject()), |
| 60 |
'summary' => $summary, |
| 61 |
'label' => JText::translate('VBO_REPLY'), |
| 62 |
'avatar' => $user->getAvatar(), |
| 63 |
'widget' => $context->getAlias() === 'session' ? 'inquiries_chat' : 'operators_chat', |
| 64 |
'widget_options' => [ |
| 65 |
'context_alias' => $context->getAlias(), |
| 66 |
'context_id' => $context->getID(), |
| 67 |
], |
| 68 |
// always skip signature check, so that we can allow a duplicate insert |
| 69 |
'_signature' => md5(time()), |
| 70 |
], |
| 71 |
]); |
| 72 |
} catch (Exception $e) { |
| 73 |
// silently catch the error |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
return true; |
| 78 |
} |
| 79 |
} |
| 80 |
|