PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / models / packagescart.php
vikappointments / site / models Last commit date
allorders.php 1 day ago calendarweek.php 1 day ago cart.php 1 day ago confirmapp.php 1 day ago empaccountstat.php 1 day ago empattachser.php 1 day ago empcoupons.php 1 day ago empcustfields.php 1 day ago empeditcoupon.php 1 day ago empeditcustfield.php 1 day ago empeditlocation.php 1 day ago empeditpay.php 1 day ago empeditprofile.php 1 day ago empeditservice.php 1 day ago empeditwdays.php 1 day ago emplocations.php 1 day ago emplocwdays.php 1 day ago emplogin.php 1 day ago employeesearch.php 1 day ago employeeslist.php 1 day ago empmanres.php 1 day ago emppaylist.php 1 day ago empserviceslist.php 1 day ago empsettingsman.php 1 day ago empsubscrcart.php 1 day ago empsubscrhistory.php 1 day ago empsubscrorder.php 1 day ago empwdays.php 1 day ago index.html 1 day ago packages.php 1 day ago packagescart.php 1 day ago packagesconfirm.php 1 day ago packorders.php 1 day ago servicesearch.php 1 day ago serviceslist.php 1 day ago subscrcart.php 1 day ago subscrhistory.php 1 day ago subscrpayment.php 1 day ago
packagescart.php
312 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 VAPLoader::import('libraries.mvc.model');
15 // load cart framework
16 VikAppointments::loadCartPackagesLibrary();
17
18 /**
19 * VikAppointments packages cart model.
20 *
21 * @since 1.7
22 */
23 class VikAppointmentsModelPackagescart extends JModelVAP
24 {
25 /**
26 * Registers a new package within the cart.
27 *
28 * @param mixed $data Either an array or an object holding
29 * the package details.
30 *
31 * @return mixed The added item on success, false otherwise.
32 */
33 public function addItem($data)
34 {
35 $data = (array) $data;
36
37 $dispatcher = VAPFactory::getEventDispatcher();
38
39 // load package details
40 $package = JModelVAP::getInstance('package')->getItem($data['id']);
41
42 if (!$package)
43 {
44 // the specified package doesn't exist
45 $this->setError(JText::translate('VAPPACKNOTFOUNDERR'));
46 return false;
47 }
48
49 // get cart instance
50 $cart = $this->getCart();
51
52 // create cart item instance
53 $item = new VAPCartPackagesItem($package->id, $package->name, $package->price, $package->num_app);
54
55 // junk variable used by plugins to set custom errors
56 $err = '';
57
58 /**
59 * Trigger event before adding an item into the cart.
60 *
61 * @param mixed $cart The cart instance.
62 * @param mixed $item The cart item object.
63 * @param string &$err String used to raise custom errors.
64 *
65 * @return boolean False to avoid adding the item.
66 *
67 * @since 1.7
68 */
69 if ($dispatcher->not('onAddPackageItemCart', array($cart, $item, &$err)))
70 {
71 // Avoid pushing the item into the cart in case at least a plugin
72 // returns a negative value. If no plugin is attached to this event,
73 // the item will be added correctly.
74 $this->setError($err ? $err : JText::translate('ERROR'));
75 return false;
76 }
77
78 // try to add the item into the cart
79 $res = $cart->addPackage($item);
80
81 // check package integrity on cart
82 if (!$res)
83 {
84 // we probably reached the maximum cart limit
85 $this->setError(JText::translate('VAPCARTPACKADDERR'));
86 return false;
87 }
88
89 // save cart data
90 $cart->store();
91
92 // search the item inside the cart because the element might have been
93 // updated an existing record
94 return $cart->getPackageAt($cart->indexOf($item->getID()));
95 }
96
97 /**
98 * Decreases the units of the specified package.
99 *
100 * @param mixed $data Either an array or an object holding
101 * the package details.
102 *
103 * @return boolean True on success, false otherwise.
104 */
105 public function removeItem($data)
106 {
107 $data = (array) $data;
108
109 $dispatcher = VAPFactory::getEventDispatcher();
110
111 // get cart handler
112 $cart = $this->getCart();
113
114 /**
115 * Trigger event before detaching a package from the cart.
116 *
117 * @param mixed $cart The cart instance.
118 * @param integer $id_package The package ID.
119 *
120 * @return boolean False to avoid detaching the option.
121 *
122 * @since 1.7
123 */
124 if ($dispatcher->not('onRemovePackageItemCart', array($cart, $data['id'])))
125 {
126 // Avoid detaching the option from the item in case at least a plugin
127 // returns a negative value. If no plugin is attached to this event,
128 // the option will be detached correctly.
129
130 $this->setError(JText::translate('VAPCARTOPTDELERR'));
131 return false;
132 }
133
134 // validate units to decrease
135 $units = isset($data['units']) ? $data['units'] : 1;
136 // try to delete the option
137 $res = $cart->removePackage($data['id'], $units);
138
139 if (!$res)
140 {
141 // unable to delete the package
142 $this->setError(JText::translate('VAPPACKNOTFOUNDERR'));
143 return false;
144 }
145
146 // revalidate coupon code
147 $this->revalidateCoupon();
148
149 // save changes
150 $cart->store();
151
152 return true;
153 }
154
155 /**
156 * Removes all the packages from the cart.
157 *
158 * @return void
159 */
160 public function emptyCart()
161 {
162 $dispatcher = VAPFactory::getEventDispatcher();
163
164 // get cart handler
165 $cart = $this->getCart();
166
167 /**
168 * Trigger event before flushing the cart.
169 *
170 * @param mixed $cart The cart instance.
171 *
172 * @return void
173 *
174 * @since 1.7
175 */
176 $dispatcher->not('onEmptyPackagesCart', array($cart));
177
178 // flush the cart
179 $cart->emptyCart();
180
181 // revalidate coupon code
182 $this->revalidateCoupon();
183
184 // apply the changes
185 $cart->store();
186 }
187
188 /**
189 * Helper method used to redeem the specified coupon code.
190 *
191 * @param mixed $coupon Either the coupon details or its code.
192 *
193 * @return boolean True on success, false otherwise.
194 */
195 public function redeemCoupon($coupon)
196 {
197 if (empty($coupon))
198 {
199 // coupon code not specified
200 $this->setError(JText::translate('VAPCOUPONNOTVALID'));
201 return false;
202 }
203
204 if (is_string($coupon))
205 {
206 // get model to load coupon details
207 $couponModel = JModelVAP::getInstance('coupon');
208 $coupon = $couponModel->getCoupon($coupon);
209
210 if (!$coupon)
211 {
212 // coupon not found in database
213 $this->setError(JText::translate('VAPCOUPONNOTVALID'));
214 return false;
215 }
216 }
217
218 // get cart instance
219 $cart = $this->getCart();
220
221 // validate the coupon code
222 if (!VikAppointments::validatePackagesCoupon($coupon, $cart))
223 {
224 // cannot apply the coupon code
225 $this->setError(JText::translate('VAPCOUPONNOTVALID'));
226 return false;
227 }
228
229 // coupon valid, create discount object
230 $discount = new VAPCartDiscount('coupon', $coupon->value, $coupon->percentot == 1);
231 // register coupon data for later use
232 $discount->set('couponData', $coupon);
233
234 // apply discount, by replacing any other coupon discount previously set
235 $cart->setDiscount($discount);
236 // commit cart changes
237 $cart->store();
238
239 return true;
240 }
241
242 /**
243 * Revalidates the internal coupon code, since the cart might be no more
244 * compliant with the coupon restrictions after some changes.
245 *
246 * @param boolean $store True to commit the changes.
247 *
248 * @return boolean True in case of valid coupon, false otherwise.
249 */
250 public function revalidateCoupon($store = false)
251 {
252 // get cart instance
253 $cart = $this->getCart();
254 // get coupon discount, if any
255 $discount = $cart->getDiscount('coupon');
256
257 if (!$discount)
258 {
259 // coupon discount not set
260 return false;
261 }
262
263 // extract coupon data
264 $coupon = $discount->get('couponData');
265
266 // try to redeem the coupon code
267 $res = $this->redeemCoupon($coupon);
268
269 if (!$res)
270 {
271 // coupon no more valid, unset it
272 $cart->removeDiscount($discount);
273
274 if ($store)
275 {
276 // commit changes
277 $cart->store();
278 }
279
280 return false;
281 }
282
283 // coupon still valid
284 return true;
285 }
286
287 /**
288 * Helper method used to obtain an instance of the cart.
289 *
290 * @return VAPCartPackages
291 */
292 public function getCart()
293 {
294 static $cart = null;
295
296 if (!$cart)
297 {
298 // load cart instance
299 $cart = VAPCartPackages::getInstance();
300
301 $config = VAPFactory::getConfig();
302
303 // set cart configuration
304 $cart->setParams(array(
305 VAPCartPackages::MAX_SIZE => $config->getInt('maxpackscart'),
306 ));
307 }
308
309 return $cart;
310 }
311 }
312