PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.20
VikAppointments Services Booking Calendar v1.2.20
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / webhook / classes / aftersavereservation.php
vikappointments / site / helpers / libraries / webhook / classes Last commit date
afterdeletecoupon.php 1 month ago afterdeletecustomer.php 1 month ago afterdeleteemployee.php 1 month ago afterdeletepackorder.php 1 month ago afterdeletereservation.php 1 month ago afterdeleteservice.php 1 month ago aftersavecoupon.php 1 month ago aftersavecustomer.php 1 month ago aftersaveemployee.php 1 month ago aftersavepackorder.php 1 month ago aftersavereservation.php 1 month ago aftersaveservice.php 1 month ago index.html 1 month ago statuschangepackageorder.php 1 month ago statuschangereservation.php 1 month ago
aftersavereservation.php
238 lines
1 <?php
2 /**
3 * @package VikAppointments
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 * Web hook handler for appointments creation/update.
16 *
17 * @since 1.7
18 */
19 class VAPWebHookAfterSaveReservation extends VAPWebHook
20 {
21 /**
22 * Flag used to check whether the selected record has been inserted or updated.
23 *
24 * @var boolean
25 */
26 protected $isNew = false;
27
28 /**
29 * @override
30 * Class constructor.
31 *
32 * @param string $hook The hook name.
33 * @param mixed $payload The payload to deliver.
34 */
35 public function __construct($hook, $payload)
36 {
37 if (is_array($payload) && count($payload) > 1)
38 {
39 // The payload is an array containing the properties of the saved
40 // reservation, a flag to check whether the record has been inserted
41 // or updated and a JTable instance. We need to use only the first
42 // argument of the list.
43 $this->isNew = (bool) $payload[1];
44 $payload = array_shift($payload);
45 }
46
47 // construct through parent
48 parent::__construct('onAfterSaveReservation', $payload);
49 }
50
51 /**
52 * @override
53 * Returns a readable name of the hook.
54 *
55 * @return string
56 */
57 public function getName()
58 {
59 return JText::translate('VAP_WEBHOOK_SAVE_RESERVATION');
60 }
61
62 /**
63 * @override
64 * Returns an associative array of supported parameters.
65 *
66 * @return array
67 */
68 public function getForm()
69 {
70 return array(
71 /**
72 * Choose whether the webhook should observe the created
73 * records, the updated records or both.
74 *
75 * @var list
76 */
77 'type' => array(
78 'type' => 'select',
79 'label' => JText::translate('VAP_WEBHOOK_SAVETYPE_PARAM'),
80 'help' => JText::translate('VAP_WEBHOOK_SAVETYPE_PARAM_HELP'),
81 'default' => 'insert',
82 'options' => array(
83 'insert' => JText::translate('VAP_WEBHOOK_SAVETYPE_PARAM_INSERT'),
84 'update' => JText::translate('VAP_WEBHOOK_SAVETYPE_PARAM_UPDATE'),
85 'both' => JText::translate('VAP_WEBHOOK_SAVETYPE_PARAM_BOTH'),
86 ),
87 ),
88
89 /**
90 * A list of statuses to observe. The web hook will be dispatched
91 * only in case the status of the appointment is contained within
92 * this list. Leave empty to accept all the statuses.
93 *
94 * @var list
95 */
96 'statuses' => array(
97 'type' => 'select',
98 'label' => JText::translate('VAP_WEBHOOK_STATUSES_PARAM'),
99 'help' => JText::translate('VAP_WEBHOOK_STATUSES_PARAM_HELP'),
100 'default' => array(),
101 'multiple' => true,
102 'options' => JHtml::fetch('vaphtml.admin.statuscodes', 'appointments'),
103 ),
104
105 /**
106 * Choose the loading mode of the records:
107 * - basic send only the specified data;
108 * - full send the whole table row;
109 * - extended send an extended version of the row.
110 *
111 * @var list
112 */
113 'load' => array(
114 'type' => 'select',
115 'label' => JText::translate('VAP_WEBHOOK_LOAD_PARAM'),
116 'help' => JText::translate('VAP_WEBHOOK_LOAD_PARAM_HELP'),
117 'default' => 'basic',
118 'options' => array(
119 'basic' => JText::translate('VAP_WEBHOOK_LOAD_PARAM_BASIC'),
120 'full' => JText::translate('VAP_WEBHOOK_LOAD_PARAM_FULL'),
121 'extended' => JText::translate('VAP_WEBHOOK_LOAD_PARAM_EXTENDED'),
122 ),
123 ),
124 );
125 }
126
127 /**
128 * @override
129 * Returns the registered payload.
130 *
131 * @param mixed $options Either an array or an object, which should contain
132 * the value of the specified parameters.
133 *
134 * @return mixed
135 */
136 public function getPayload($options = array())
137 {
138 $options = new JRegistry($options);
139 // fetch loading mode
140 $load = $options->get('load');
141 // fetch type setting
142 $type = $options->get('type');
143 // get statuses
144 $statuses = (array) $options->get('statuses');
145
146 if (($type == 'insert' && !$this->isNew) || ($type == 'update' && $this->isNew))
147 {
148 // do not dispatch the web hook in case of mismatch type
149 return false;
150 }
151
152 $payload = (array) $this->payload;
153
154 if (!empty($payload['id_parent']) && $payload['id_parent'] <= 0)
155 {
156 // handling a multi-order parent, ignore it
157 return false;
158 }
159
160 $status = !empty($payload['status']) ? $payload['status'] : null;
161
162 if (!empty($payload['id']))
163 {
164 if ($load == 'full')
165 {
166 // get all details of the reservation table
167 $payload = JModelVAP::getInstance('reservation')->getItem($payload['id']);
168 // fetch correct status
169 $status = $payload ? $payload->status : null;
170 }
171 else if ($load == 'extended')
172 {
173 // load the extended version of the order details
174 VAPLoader::import('libraries.order.factory');
175 $payload = VAPOrderFactory::getAppointments($payload['id'], VikAppointments::getDefaultLanguage());
176 // fetch correct status
177 $status = $payload->status;
178 }
179 else
180 {
181 // strip unset properties
182 $payload = array_filter($payload, function($value)
183 {
184 return $value !== null;
185 });
186 }
187 }
188
189 // check whether this webhook observes the status of the appointment
190 if ($statuses && !in_array($status, $statuses))
191 {
192 // this status is not observed
193 return false;
194 }
195
196 return $payload;
197 }
198
199 /**
200 * Returns the appointment ID.
201 *
202 * @return integer
203 */
204 public function getOrderID()
205 {
206 $payload = (array) $this->payload;
207
208 return !empty($payload['id']) ? (int) $payload['id'] : 0;
209 }
210
211 /**
212 * @override
213 * Comparator to check whether 2 instances share the same payload parent.
214 *
215 * @return boolean
216 */
217 public function equalsTo($webhook)
218 {
219 if (!$webhook instanceof VAPWebHookAfterSaveReservation)
220 {
221 // invalid instance
222 return false;
223 }
224
225 // the order ID
226 $id = $this->getOrderID();
227
228 if (!$id)
229 {
230 // invalid ID
231 return false;
232 }
233
234 // compare both the IDs
235 return $id == $webhook->getOrderID();
236 }
237 }
238