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 / aftersavepackorder.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
aftersavepackorder.php
201 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 packages orders creation/update.
16 *
17 * @since 1.7
18 */
19 class VAPWebHookAfterSavePackorder 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 // order, 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('onAfterSavePackorder', $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_PACKORDER');
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 * Choose the loading mode of the records:
91 * - basic send only the specified data;
92 * - full send the whole table row;
93 * - extended send an extended version of the row.
94 *
95 * @var list
96 */
97 'load' => array(
98 'type' => 'select',
99 'label' => JText::translate('VAP_WEBHOOK_LOAD_PARAM'),
100 'help' => JText::translate('VAP_WEBHOOK_LOAD_PARAM_HELP'),
101 'default' => 'basic',
102 'options' => array(
103 'basic' => JText::translate('VAP_WEBHOOK_LOAD_PARAM_BASIC'),
104 'full' => JText::translate('VAP_WEBHOOK_LOAD_PARAM_FULL'),
105 'extended' => JText::translate('VAP_WEBHOOK_LOAD_PARAM_EXTENDED'),
106 ),
107 ),
108 );
109 }
110
111 /**
112 * @override
113 * Returns the registered payload.
114 *
115 * @param mixed $options Either an array or an object, which should contain
116 * the value of the specified parameters.
117 *
118 * @return mixed
119 */
120 public function getPayload($options = array())
121 {
122 $options = new JRegistry($options);
123 // fetch loading mode
124 $load = $options->get('load');
125 // fetch type setting
126 $type = $options->get('type');
127
128 if (($type == 'insert' && !$this->isNew) || ($type == 'update' && $this->isNew))
129 {
130 // do not dispatch the web hook in case of mismatch type
131 return false;
132 }
133
134 $payload = (array) $this->payload;
135
136 if (!empty($payload['id']))
137 {
138 if ($load == 'full')
139 {
140 // get all details of the packages order table
141 $payload = JModelVAP::getInstance('packorder')->getItem($payload['id']);
142 }
143 else if ($load == 'extended')
144 {
145 // load the extended version of the order details
146 VAPLoader::import('libraries.order.factory');
147 $payload = VAPOrderFactory::getPackages($payload['id'], VikAppointments::getDefaultLanguage());
148 }
149 else
150 {
151 // strip unset properties
152 $payload = array_filter($payload, function($value)
153 {
154 return $value !== null;
155 });
156 }
157 }
158
159 return $payload;
160 }
161
162 /**
163 * Returns the order ID.
164 *
165 * @return integer
166 */
167 public function getOrderID()
168 {
169 $payload = (array) $this->payload;
170
171 return !empty($payload['id']) ? (int) $payload['id'] : 0;
172 }
173
174 /**
175 * @override
176 * Comparator to check whether 2 instances share the same payload parent.
177 *
178 * @return boolean
179 */
180 public function equalsTo($webhook)
181 {
182 if (!$webhook instanceof VAPWebHookAfterSavePackorder)
183 {
184 // invalid instance
185 return false;
186 }
187
188 // the order ID
189 $id = $this->getOrderID();
190
191 if (!$id)
192 {
193 // invalid ID
194 return false;
195 }
196
197 // compare both the IDs
198 return $id == $webhook->getOrderID();
199 }
200 }
201