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 / empeditservice.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
empeditservice.php
208 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 service management model.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsModelEmpeditservice 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 $dbo = JFactory::getDbo();
33
34 $auth = VAPEmployeeAuth::getInstance();
35
36 // set user state for being recovered again
37 JFactory::getApplication()->setUserState('vap.emparea.service.data', $data);
38
39 if (!empty($data['id_group']))
40 {
41 // make sure the group exists
42 $q = $dbo->getQuery(true)
43 ->select(1)
44 ->from($dbo->qn('#__vikappointments_group'))
45 ->where($dbo->qn('id') . ' = ' . (int) $data['id_group']);
46
47 $dbo->setQuery($q, 0, 1);
48 $dbo->execute();
49
50 if (!$dbo->getNumRows())
51 {
52 $data['id_group'] = 0;
53 }
54 }
55
56 // get service model
57 $serviceModel = JModelVAP::getInstance('service');
58
59 $prev = null;
60
61 if ($data['id'])
62 {
63 // get previous details
64 $prev = $serviceModel->getItem($data['id']);
65 }
66
67 if (!empty($data['name']) && $data['name'] != $prev->name)
68 {
69 // recalculate alias when the name changes
70 $data['alias'] = $data['name'];
71 }
72
73 // get media model
74 $media = JModelVAP::getInstance('media');
75
76 $image = $oldImage = null;
77
78 // attempt to upload the new specified file
79 if ($media->save(['file' => 'image']))
80 {
81 if (!empty($prev->image))
82 {
83 // register currently set image
84 $oldImage = $prev->image;
85 }
86
87 // get saved image data
88 $image = $media->getData();
89 // replace image file
90 $data['image'] = $image['id'];
91 }
92
93 if (!$data['id'])
94 {
95 // force author while creating a new record
96 $data['createdby'] = $auth->jid;
97 }
98
99 // delegate save to service model
100 $result = $serviceModel->save($data);
101
102 if (!$result)
103 {
104 // obtain error from model
105 $error = $serviceModel->getError();
106
107 if ($error)
108 {
109 // propagate error
110 $this->setError($error);
111 }
112 }
113
114 if ($result)
115 {
116 $assoc = $data;
117
118 $assoc['id'] = 0;
119 $assoc['id_service'] = $result;
120 $assoc['id_employee'] = $auth->id;
121
122 // create/update service-employee relation too
123 JModelVAP::getInstance('serempassoc')->save($assoc);
124 }
125
126 if (!$result && $image)
127 {
128 // delete newly uploaded file in case of error
129 $media->delete($image['id']);
130 }
131
132 if ($result && $oldImage)
133 {
134 // delete previous image on success
135 $media->delete($oldImage);
136 }
137
138 return $result;
139 }
140
141 /**
142 * Extend delete implementation to delete any related records
143 * stored within a separated table.
144 *
145 * @param mixed $ids Either the record ID or a list of records.
146 *
147 * @return boolean True on success, false otherwise.
148 */
149 public function delete($ids)
150 {
151 $auth = VAPEmployeeAuth::getInstance();
152
153 if (!$auth->isEmployee())
154 {
155 throw new Exception(JText::translate('JERROR_ALERTNOAUTHOR'), 403);
156 }
157
158 $serviceModel = JModelVAP::getInstance('service');
159 $assocModel = JModelVAP::getInstance('serempassoc');
160
161 $result = false;
162
163 // only int values are accepted
164 $ids = array_map('intval', (array) $ids);
165
166 foreach ($ids as $id)
167 {
168 if ($auth->manageServices($id))
169 {
170 // the employee is the owner of this service, delete it directly
171 $result = $serviceModel->delete($id) || $result;
172 }
173 else
174 {
175 // we need to detect the service relation
176 $assoc = $assocModel->getItem(array(
177 'id_service' => $id,
178 'id_employee' => $auth->id,
179 ));
180
181 if ($assoc)
182 {
183 // detect employee from this service
184 $result = $assocModel->delete($assoc->id) || $result;
185 }
186 }
187 }
188
189 return $result;
190 }
191
192 /**
193 * Method to get a table object.
194 *
195 * @param string $name The table name.
196 * @param string $prefix The class prefix.
197 * @param array $options Configuration array for table.
198 *
199 * @return JTable A table object.
200 *
201 * @throws Exception
202 */
203 public function getTable($name = 'service', $prefix = '', $options = array())
204 {
205 return parent::getTable($name, $prefix, $options);
206 }
207 }
208