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 / aftersavecoupon.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
aftersavecoupon.php
183 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 coupons creation/update.
16 *
17 * @since 1.7
18 */
19 class VAPWebHookAfterSaveCoupon 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 // coupon, 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('onAfterSaveCoupon', $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_COUPON');
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 ),
106 ),
107 );
108 }
109
110 /**
111 * @override
112 * Returns the registered payload.
113 *
114 * @param mixed $options Either an array or an object, which should contain
115 * the value of the specified parameters.
116 *
117 * @return mixed
118 */
119 public function getPayload($options = array())
120 {
121 $options = new JRegistry($options);
122 // fetch loading mode
123 $load = $options->get('load');
124 // fetch type setting
125 $type = $options->get('type');
126
127 if (($type == 'insert' && !$this->isNew) || ($type == 'update' && $this->isNew))
128 {
129 // do not dispatch the web hook in case of mismatch type
130 return false;
131 }
132
133 $payload = (array) $this->payload;
134
135 if (!empty($payload['id']) && $load == 'full')
136 {
137 // get all details of the coupon table
138 $payload = JModelVAP::getInstance('coupon')->getItem($payload['id']);
139 }
140
141 return $payload;
142 }
143
144 /**
145 * Returns the coupon ID.
146 *
147 * @return integer
148 */
149 public function getCouponID()
150 {
151 $payload = (array) $this->payload;
152
153 return !empty($payload['id']) ? (int) $payload['id'] : 0;
154 }
155
156 /**
157 * @override
158 * Comparator to check whether 2 instances share the same payload parent.
159 *
160 * @return boolean
161 */
162 public function equalsTo($webhook)
163 {
164 if (!$webhook instanceof VAPWebHookAfterSaveCoupon)
165 {
166 // invalid instance
167 return false;
168 }
169
170 // the coupon ID
171 $id = $this->getCouponID();
172
173 if (!$id)
174 {
175 // invalid ID
176 return false;
177 }
178
179 // compare both the IDs
180 return $id == $webhook->getCouponID();
181 }
182 }
183