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 / empeditcoupon.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
empeditcoupon.php
120 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
16 /**
17 * VikAppointments employee area coupon management model.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsModelEmpeditcoupon extends JModelVAP
22 {
23 /**
24 * Basic save implementation.
25 *
26 * @param mixed $data Either an array or an object of data to save.
27 *
28 * @return mixed The ID of the record on success, false otherwise.
29 */
30 public function save($data)
31 {
32 $data = (array) $data;
33
34 $auth = VAPEmployeeAuth::getInstance();
35
36 JFactory::getApplication()->setUserState('vap.emparea.coupon.data', $data);
37
38 // get coupon model
39 $couponModel = JModelVAP::getInstance('coupon');
40
41 // validation
42 $data['max_quantity'] = max(array(1, $data['max_quantity']));
43
44 // assign to this employee only
45 $data['employees'] = array($auth->id);
46
47 if (!empty($data['services']))
48 {
49 // load all the services assigned to this employee
50 $services = JModelVAP::getInstance('employee')->getServices($auth->id, $strict = false);
51
52 // take only the ID of the services
53 $services = array_map(function($elem)
54 {
55 return $elem->id;
56 }, $services);
57
58 // get rid of those services that do not belong to the employee
59 $data['services'] = array_intersect($data['services'], $services);
60 }
61
62 // delegate save to coupon model
63 $id = $couponModel->save($data);
64
65 if (!$id)
66 {
67 // obtain error from model
68 $error = $couponModel->getError();
69
70 if ($error)
71 {
72 // propagate error
73 $this->setError($error);
74 }
75
76 return false;
77 }
78
79 return $id;
80 }
81
82 /**
83 * Extend delete implementation to delete any related records
84 * stored within a separated table.
85 *
86 * @param mixed $ids Either the record ID or a list of records.
87 *
88 * @return boolean True on success, false otherwise.
89 */
90 public function delete($ids)
91 {
92 $auth = VAPEmployeeAuth::getInstance();
93
94 if (!$auth->isEmployee())
95 {
96 throw new Exception(JText::translate('JERROR_ALERTNOAUTHOR'), 403);
97 }
98
99 $result = false;
100
101 // only int values are accepted
102 $ids = array_map('intval', (array) $ids);
103
104 // get rid of those records that do not belong to this employee
105 $ids = array_values(array_filter($ids, function($id) use ($auth)
106 {
107 // make sure the employee can manage this record
108 return $auth->manageCoupons($id);
109 }));
110
111 if (!$ids)
112 {
113 throw new Exception(JText::translate('JERROR_ALERTNOAUTHOR'), 403);
114 }
115
116 // delegate delete to coupon model
117 return JModelVAP::getInstance('coupon')->delete($ids);
118 }
119 }
120