PluginProbe
VikBooking Hotel Booking Engine & PMS / trunk
VikBooking Hotel Booking Engine & PMS vtrunk
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 1.7.4 All 35 releases
vikbooking / admin / controllers / messaging.php

messaging.php in VikBooking Hotel Booking Engine & PMS trunk, at admin/controllers/messaging.php

123 lines 4.1 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) 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 * VikBooking messaging controller (admin).
16 *
17 * @since 1.18.8 (J) - 1.8.8 (WP)
18 */
19 class VikBookingControllerMessaging extends JControllerAdmin
20 {
21 /**
22 * AJAX endpoint to send a message template through a CM messaging account.
23 *
24 * @return void
25 */
26 public function sendMessageTemplate()
27 {
28 $app = JFactory::getApplication();
29
30 if (!JSession::checkToken()) {
31 // missing CSRF-proof token
32 VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN'));
33 }
34
35 $accountId = $app->input->getString('account_id', '');
36 $phoneId = $app->input->getString('phone_id', '');
37 $configId = $app->input->getUInt('config_id', 0);
38 $bookingId = $app->input->getUInt('booking_id', 0);
39
40 if (empty($accountId) || empty($phoneId)) {
41 // missing account data
42 VBOHttpDocument::getInstance($app)->close(400, 'Missing messaging account information.');
43 }
44
45 // access registry for the given booking ID
46 try {
47 $booking = VBOBookingRegistry::getInstance(['id' => $bookingId]);
48 } catch (Exception $e) {
49 // propagate error
50 VBOHttpDocument::getInstance($app)->close($e->getCode() ?: 404, $e->getMessage());
51 }
52
53 // get recipient (guest) phone number
54 $phoneNumber = $booking->getPhoneNumber();
55 if (!$phoneNumber) {
56 // unable to proceed without a recipient
57 VBOHttpDocument::getInstance($app)->close(400, 'Missing guest recipient phone number.');
58 }
59
60 // access messaging accounts model
61 $maModel = VCMMessagingAccountsModel::getInstance();
62
63 // fetch requested messaging account record
64 $accountRecord = $maModel->getItem([
65 'account_id' => $accountId,
66 'phone_id' => $phoneId,
67 ]);
68
69 if (!$accountRecord || empty($accountRecord->settings['configurations'][$configId])) {
70 VBOHttpDocument::getInstance($app)->close(404, 'Could not find the requested messaging account.');
71 }
72
73 // load channel details
74 $channelDetails = VikChannelManager::getChannel($accountRecord->idchannel);
75 if (!$channelDetails) {
76 VBOHttpDocument::getInstance($app)->close(404, 'Messaging channel not found.');
77 }
78
79 // register booking messaging account provider
80 $maModel->setBookingAccountProvider($booking, $accountRecord);
81
82 // access chat handler first
83 $chatHandler = VikBooking::getVcmChatInstance($booking->getID(), $channelDetails['name']);
84
85 if (!$chatHandler) {
86 VBOHttpDocument::getInstance($app)->close(500, 'Could not invoke proper chat handler.');
87 }
88
89 // build chat message object
90 $chatMessage = new VCMChatMessage(
91 // no message content
92 '',
93 // no attachments
94 [],
95 // data to bind
96 [
97 'recipient' => $phoneNumber,
98 'account' => $accountRecord,
99 'message_template' => $configId,
100 'mark_previous_replied' => true,
101 ]
102 );
103
104 // inject thread ID with recipient phone number
105 $chatMessage->set('idthread', $phoneNumber);
106
107 try {
108 // send message
109 if (!$chatHandler->send($chatMessage)) {
110 throw new Exception($chatHandler->getError() ?: 'Could not send the message template to recipient phone number.', 500);
111 }
112 } catch (Exception $e) {
113 // propagate error
114 VBOHttpDocument::getInstance($app)->close($e->getCode() ?: 500, $e->getMessage());
115 }
116
117 // send the response to output
118 VBOHttpDocument::getInstance($app)->json([
119 'success' => true,
120 ]);
121 }
122 }
123