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 / controllers / empeditservice.php
vikappointments / site / controllers Last commit date
calendarweek.php 2 days ago cart.php 2 days ago confirmapp.php 2 days ago empattachser.php 2 days ago empeditcoupon.php 2 days ago empeditcustfield.php 2 days ago empeditlocation.php 2 days ago empeditpay.php 2 days ago empeditprofile.php 2 days ago empeditservice.php 2 days ago empeditwdays.php 2 days ago emplocwdays.php 2 days ago emplogin.php 2 days ago employeesearch.php 2 days ago employeeslist.php 2 days ago empmakerecur.php 2 days ago empmanres.php 2 days ago empsettings.php 2 days ago empsubscr.php 2 days ago empsubscrorder.php 2 days ago index.html 2 days ago modules.php 2 days ago order.php 2 days ago packages.php 2 days ago packagesconfirm.php 2 days ago packagesorder.php 2 days ago servicesearch.php 2 days ago subscriptions.php 2 days ago subscrpayment.php 2 days ago userprofile.php 2 days ago waitinglist.php 2 days ago
empeditservice.php
341 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.employee.area.controller');
15
16 /**
17 * Employee area edit service controller.
18 *
19 * @since 1.6
20 */
21 class VikAppointmentsControllerEmpeditservice extends VAPEmployeeAreaController
22 {
23 /**
24 * Task used to access the creation page of a new record.
25 *
26 * @return boolean
27 *
28 * @since 1.7
29 */
30 public function add()
31 {
32 $app = JFactory::getApplication();
33 $auth = VAPEmployeeAuth::getInstance();
34
35 // unset user state for being recovered again
36 $app->setUserState('vap.emparea.service.data', array());
37
38 // check user permissions
39 if (!$auth->isEmployee() || !$auth->createService($count = true))
40 {
41 // back to main list, not authorised to edit records
42 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
43 $this->cancel();
44
45 return false;
46 }
47
48 $this->setRedirect('index.php?option=com_vikappointments&view=empeditservice');
49
50 return true;
51 }
52
53 /**
54 * Task used to access the management page of an existing record.
55 *
56 * @return boolean
57 *
58 * @since 1.7
59 */
60 public function edit()
61 {
62 $app = JFactory::getApplication();
63 $auth = VAPEmployeeAuth::getInstance();
64
65 // unset user state for being recovered again
66 $app->setUserState('vap.emparea.service.data', array());
67
68 $cid = $app->input->getUint('cid', array(0));
69
70 // check user permissions
71 if (!$auth->isEmployee() || (!$auth->manageServices($cid[0]) && !$auth->manageServicesRates()))
72 {
73 // back to main list, not authorised to edit records
74 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
75 $this->cancel();
76
77 return false;
78 }
79
80 $this->setRedirect('index.php?option=com_vikappointments&view=empeditservice&cid[]=' . $cid[0]);
81
82 return true;
83 }
84
85 /**
86 * Task used to save the record data set in the request.
87 * After saving, the user is redirected to the main list.
88 *
89 * @return void
90 *
91 * @since 1.7
92 */
93 public function saveclose()
94 {
95 if ($this->save())
96 {
97 $this->cancel();
98 }
99 }
100
101 /**
102 * Save employee service.
103 *
104 * @return void
105 */
106 public function save()
107 {
108 $app = JFactory::getApplication();
109 $input = $app->input;
110 $auth = VAPEmployeeAuth::getInstance();
111
112 /**
113 * Added token validation.
114 *
115 * @since 1.7
116 */
117 if (!JSession::checkToken())
118 {
119 // back to main list, missing CSRF-proof token
120 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
121 $this->cancel();
122
123 return false;
124 }
125
126 // check user permissions
127 if (!$auth->isEmployee())
128 {
129 // back to main list, not authorised to edit records
130 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
131 $this->cancel();
132
133 return false;
134 }
135
136 $id_service = $input->getUint('id', 0);
137
138 if ($id_service)
139 {
140 // check whether the employee is able to manage this service
141 $canEdit = $auth->manageServices($id_service);
142 $canEditRate = $auth->manageServicesRates();
143 }
144 else
145 {
146 // check whether the employee is able to create services
147 $canEdit = $auth->createService($count = true);
148 $canEditRate = false;
149 }
150
151 if (!$canEdit && !$canEditRate)
152 {
153 // back to main list, not authorised to edit records
154 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
155 $this->cancel();
156
157 return false;
158 }
159
160 // get args
161 $args = array();
162 $args['id'] = $id_service;
163 $args['price'] = $input->getFloat('price', 0);
164 $args['duration'] = $input->getUint('duration', 0);
165 $args['sleep'] = $input->getInt('sleep', 0);
166 $args['max_capacity'] = $input->getUint('max_capacity', 1);
167 $args['description'] = $input->getRaw('description', '');
168
169 if ($canEdit)
170 {
171 $args['name'] = $input->getString('name', '');
172 $args['id_group'] = $input->getUint('id_group', 0);
173 $args['interval'] = $input->getUint('interval', 0);
174 $args['min_per_res'] = $input->getUint('min_per_res', 1);
175 $args['max_per_res'] = $input->getUint('max_per_res', 1);
176 $args['priceperpeople'] = $input->getUint('priceperpeople', 0);
177 $args['app_per_slot'] = $input->getUint('app_per_slot', 0);
178 $args['display_seats'] = $input->getUint('display_seats', 0);
179 $args['published'] = $input->getUint('published', 0);
180 $args['has_own_cal'] = $input->getUint('has_own_cal', 0);
181 $args['enablezip'] = $input->getUint('enablezip', 0);
182 $args['use_recurrence'] = $input->getUint('use_recurrence', 0);
183 $args['start_publishing'] = $input->getString('start_publishing', '');
184 $args['end_publishing'] = $input->getString('end_publishing', '');
185
186 /**
187 * Convert timestamp from local timezone to UTC.
188 *
189 * @since 1.7
190 */
191 $args['start_publishing'] = VAPDateHelper::getSqlDateLocale($args['start_publishing']);
192 $args['end_publishing'] = VAPDateHelper::getSqlDateLocale($args['end_publishing']);
193 }
194
195 if ($canEdit)
196 {
197 // get service model
198 $model = $this->getModel();
199
200 // try to save arguments
201 $id = $model->save($args);
202 }
203 else
204 {
205 // get service-employee relations model
206 $model = $this->getModel('serempassoc');
207
208 $args['id'] = 0;
209 $args['id_employee'] = $auth->id;
210 $args['id_service'] = $id_service;
211 $args['global'] = 0;
212
213 $id = $model->save($args);
214
215 if ($id)
216 {
217 // use the ID service as PK
218 $id = $id_service;
219 }
220 }
221
222 if (!$id)
223 {
224 // get string error
225 $error = $model->getError(null, true);
226
227 // display error message
228 $app->enqueueMessage(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $error), 'error');
229
230 $url = 'index.php?option=com_vikappointments&view=empeditservice';
231
232 if ($id_service)
233 {
234 $url .= '&cid[]=' . $id_service;
235 }
236
237 // redirect to edit page
238 $this->setRedirect($url);
239
240 return false;
241 }
242
243 // display generic successful message
244 $app->enqueueMessage(JText::translate('JLIB_APPLICATION_SAVE_SUCCESS'));
245
246 // redirect to edit page
247 $this->setRedirect('index.php?option=com_vikappointments&task=empeditservice.edit&cid[]=' . $id);
248
249 return true;
250 }
251
252 /**
253 * Attaches the working days of the employee to the specified service.
254 *
255 * @param integer $id_service The service ID.
256 * @param integer $id_employee The employee ID.
257 *
258 * @return boolean True if attached, otherwise false.
259 *
260 * @deprecated 1.8 Use VikAppointments::attachWorkingDays() instead.
261 */
262 public function attachWorkingDays($id_service, $id_employee)
263 {
264 return VikAppointments::attachWorkingDays($id_service, $id_employee);
265 }
266
267 /**
268 * Removes the service or detach it from the employee.
269 *
270 * @return void
271 */
272 public function delete()
273 {
274 $app = JFactory::getApplication();
275 $cid = $app->input->get('cid', array(), 'uint');
276
277 if ($id = $app->input->getUint('id'))
278 {
279 $cid[] = $id;
280 }
281
282 /**
283 * Added token validation.
284 * Both GET and POST are supported.
285 *
286 * @since 1.7
287 */
288 if (!JSession::checkToken() && !JSession::checkToken('get'))
289 {
290 // back to main list, missing CSRF-proof token
291 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
292 $this->cancel();
293
294 return false;
295 }
296
297 try
298 {
299 // delete selected records
300 if ($this->getModel()->delete($cid))
301 {
302 $app->enqueueMessage(JText::translate('VAPEMPSERREMOVED1'));
303 }
304 }
305 catch (Exception $e)
306 {
307 // an error occurred
308 $app->enqueueMessage($e->getMessage(), 'error');
309 $this->cancel();
310
311 return false;
312 }
313
314 // back to main list (reset list limit)
315 $this->cancel(['listlimit' => 0]);
316
317 return true;
318 }
319
320 /**
321 * Redirects the users to the main records list.
322 *
323 * @param array $query An array of query arguments.
324 *
325 * @return void
326 *
327 * @since 1.7
328 */
329 public function cancel(array $query = array())
330 {
331 $url = 'index.php?option=com_vikappointments&view=empserviceslist';
332
333 if ($query)
334 {
335 $url .= '&' . http_build_query($query);
336 }
337
338 $this->setRedirect($url);
339 }
340 }
341