| 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 APP push notification trait. |
| 16 |
* |
| 17 |
* @since 1.8.15 |
| 18 |
*/ |
| 19 |
trait VBOChatNotificationApp |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Schedules a notification for the specified device account. |
| 23 |
* |
| 24 |
* @param VBOChatMessage $message The message instance. |
| 25 |
* @param string $deviceAccount The account (email or "admin") of the device to notify. |
| 26 |
* |
| 27 |
* @return bool |
| 28 |
*/ |
| 29 |
public function scheduleAppNotification(VBOChatMessage $message, string $deviceAccount) |
| 30 |
{ |
| 31 |
// do not notify messages wrote from the front-end chat |
| 32 |
if ($message->getContext()->getAlias() === 'session') { |
| 33 |
return false; |
| 34 |
} |
| 35 |
|
| 36 |
// make sure VCM exposes an object to dispatch notifications |
| 37 |
if (!method_exists('VCMFactory', 'getContainer')) { |
| 38 |
// required VCM 1.9.27 |
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
/** @var VBOChatContext */ |
| 43 |
$context = $message->getContext(); |
| 44 |
|
| 45 |
// display the whole message as summary |
| 46 |
$summary = (string) $message->getMessage(); |
| 47 |
|
| 48 |
if (strlen($summary) === 0) { |
| 49 |
// no message provided, display the number of attached files |
| 50 |
$attachmentsCount = count($message->getAttachments()); |
| 51 |
|
| 52 |
if ($attachmentsCount === 1) { |
| 53 |
// only one attachment |
| 54 |
$summary = JText::sprintf('VBO_CHAT_MESSAGE_WEBPUSH_NOTIFICATION_SUMMARY_N_FILES_1', $message->getSenderName()); |
| 55 |
} else { |
| 56 |
// multiple attachments |
| 57 |
$summary = JText::plural('VBO_CHAT_MESSAGE_WEBPUSH_NOTIFICATION_SUMMARY_N_FILES', $message->getSenderName(), $attachmentsCount); |
| 58 |
} |
| 59 |
} else { |
| 60 |
// message provided |
| 61 |
$summary = JText::sprintf('VBO_CHAT_MESSAGE_WEBPUSH_NOTIFICATION_SUMMARY', $message->getSenderName(), $summary); |
| 62 |
} |
| 63 |
|
| 64 |
// prepare notification data |
| 65 |
$data = [ |
| 66 |
'type' => 'Messaging', |
| 67 |
'from' => [ |
| 68 |
'id' => $message->getSenderID(), |
| 69 |
'name' => $message->getSenderName(), |
| 70 |
], |
| 71 |
'context' => [ |
| 72 |
'alias' => $context->getAlias(), |
| 73 |
'id' => $context->getID(), |
| 74 |
], |
| 75 |
'preview' => $summary, |
| 76 |
]; |
| 77 |
|
| 78 |
// schedule notification delivery to the involved devices |
| 79 |
VCMFactory::getContainer()->get('vikchannelmanager.pnd.lazy')->sendNotification( |
| 80 |
new E4J\VCM\PND\Notification($data, $deviceAccount) |
| 81 |
); |
| 82 |
|
| 83 |
return true; |
| 84 |
} |
| 85 |
} |
| 86 |
|