PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / trunk
VikAppointments Services Booking Calendar vtrunk
trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / controllers / package.php
vikappointments / admin / controllers Last commit date
analytics.php 4 years ago apiban.php 4 years ago apilog.php 4 years ago apiplugin.php 4 years ago apiuser.php 4 years ago backup.php 4 years ago calendar.php 4 years ago city.php 4 years ago closure.php 1 month ago configapp.php 4 years ago configcldays.php 2 years ago configcron.php 4 years ago configemp.php 4 years ago configsmsapi.php 4 years ago configuration.php 1 month ago conversion.php 1 year ago country.php 4 years ago coupon.php 4 years ago coupongroup.php 4 years ago cronjob.php 2 years ago cronjoblog.php 4 years ago customer.php 4 months ago customf.php 1 year ago dashboard.php 4 years ago emplocwdays.php 4 years ago employee.php 1 year ago emprates.php 4 years ago export.php 4 years ago exportres.php 4 years ago file.php 4 months ago findreservation.php 1 month ago group.php 4 years ago import.php 4 years ago index.html 4 years ago invoice.php 1 month ago langcustomf.php 4 years ago langemployee.php 4 years ago langgroup.php 4 years ago langmedia.php 4 years ago langoption.php 4 years ago langoptiongroup.php 4 years ago langpackage.php 4 years ago langpackgroup.php 4 years ago langpayment.php 4 years ago langservice.php 4 years ago langstatuscode.php 4 years ago langsubscr.php 4 years ago langtax.php 4 years ago location.php 4 years ago mailtext.php 2 years ago makerecurrence.php 1 month ago media.php 4 years ago multiorder.php 4 years ago option.php 4 months ago optiongroup.php 4 years ago package.php 2 years ago packgroup.php 4 years ago packorder.php 1 year ago payment.php 4 years ago rate.php 4 years ago reportsemp.php 4 years ago reportsser.php 4 years ago reservation.php 1 month ago restriction.php 4 years ago review.php 4 years ago service.php 1 year ago serworkday.php 4 months ago state.php 4 years ago statuscode.php 4 years ago subscription.php 4 years ago subscrorder.php 4 years ago tag.php 4 years ago tax.php 4 years ago usernote.php 4 years ago waitinglist.php 4 years ago webhook.php 4 years ago wizard.php 1 year ago
package.php
417 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.controllers.admin');
15
16 /**
17 * VikAppointments package controller.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsControllerPackage extends VAPControllerAdmin
22 {
23 /**
24 * Task used to access the creation page of a new record.
25 *
26 * @return boolean
27 */
28 public function add()
29 {
30 $app = JFactory::getApplication();
31 $user = JFactory::getUser();
32
33 $data = array();
34 $id_group = $app->input->getInt('id_group', 0);
35
36 if ($id_group > 0)
37 {
38 $data['id_group'] = $id_group;
39 }
40
41 // unset user state for being recovered again
42 $app->setUserState('vap.package.data', $data);
43
44 // check user permissions
45 if (!$user->authorise('core.create', 'com_vikappointments') || !$user->authorise('core.access.packages', 'com_vikappointments') || !VikAppointments::isPackagesEnabled())
46 {
47 // back to main list, not authorised to create records
48 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
49 $this->cancel();
50
51 return false;
52 }
53
54 $this->setRedirect('index.php?option=com_vikappointments&view=managepackage');
55
56 return true;
57 }
58
59 /**
60 * Task used to access the management page of an existing record.
61 *
62 * @return boolean
63 */
64 public function edit()
65 {
66 $app = JFactory::getApplication();
67 $user = JFactory::getUser();
68
69 // unset user state for being recovered again
70 $app->setUserState('vap.package.data', array());
71
72 // check user permissions
73 if (!$user->authorise('core.edit', 'com_vikappointments') || !$user->authorise('core.access.packages', 'com_vikappointments') || !VikAppointments::isPackagesEnabled())
74 {
75 // back to main list, not authorised to edit records
76 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
77 $this->cancel();
78
79 return false;
80 }
81
82 $cid = $app->input->getUint('cid', array(0));
83
84 $this->setRedirect('index.php?option=com_vikappointments&view=managepackage&cid[]=' . $cid[0]);
85
86 return true;
87 }
88
89 /**
90 * Task used to save the record data set in the request.
91 * After saving, the user is redirected to the main list.
92 *
93 * @return void
94 */
95 public function saveclose()
96 {
97 if ($this->save())
98 {
99 $this->cancel();
100 }
101 }
102
103 /**
104 * Task used to save the record data set in the request.
105 * After saving, the user is redirected to the creation
106 * page of a new record.
107 *
108 * @return void
109 */
110 public function savenew()
111 {
112 if ($this->save())
113 {
114 $this->setRedirect('index.php?option=com_vikappointments&task=package.add');
115 }
116 }
117
118 /**
119 * Task used to save the record data as a copy of the current item.
120 * After saving, the user is redirected to the management
121 * page of the record that has been saved.
122 *
123 * @return void
124 */
125 public function savecopy()
126 {
127 $this->save(true);
128 }
129
130 /**
131 * Task used to save the record data set in the request.
132 * After saving, the user is redirected to the management
133 * page of the record that has been saved.
134 *
135 * @param boolean $copy True to save the record as a copy.
136 *
137 * @return boolean
138 */
139 public function save($copy = false)
140 {
141 $app = JFactory::getApplication();
142 $input = $app->input;
143 $user = JFactory::getUser();
144
145 /**
146 * Added token validation.
147 *
148 * @since 1.7
149 */
150 if (!JSession::checkToken())
151 {
152 // back to main list, missing CSRF-proof token
153 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
154 $this->cancel();
155
156 return false;
157 }
158
159 $args = array();
160 $args['name'] = $input->getString('name', '');
161 $args['description'] = JComponentHelper::filterText($input->getRaw('description', ''));
162 $args['price'] = $input->getFloat('price', 0);
163 $args['num_app'] = $input->getUint('num_app', 1);
164 $args['published'] = $input->getUint('published', 0);
165 $args['start_ts'] = $input->getString('start_ts', '');
166 $args['end_ts'] = $input->getString('end_ts', '');
167 $args['validity'] = $input->getUint('validity', 0);
168 $args['level'] = $input->getUint('level', 0);
169 $args['id_tax'] = $input->getUint('id_tax', 0);
170 $args['id_group'] = $input->getInt('id_group', 0);
171 $args['services'] = $input->getUint('services', array());
172 $args['id'] = $input->getUint('id', 0);
173
174 if ($copy)
175 {
176 // unset ID to create a copy
177 $args['id'] = 0;
178 }
179
180 $rule = 'core.' . ($args['id'] > 0 ? 'edit' : 'create');
181
182 // check user permissions
183 if (!$user->authorise($rule, 'com_vikappointments') || !$user->authorise('core.access.packages', 'com_vikappointments') || !VikAppointments::isPackagesEnabled())
184 {
185 // back to main list, not authorised to create/edit records
186 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
187 $this->cancel();
188
189 return false;
190 }
191
192 /**
193 * Try to auto-create a new group before saving the package.
194 *
195 * @since 1.7
196 */
197 if ($args['id_group'] == 0 && ($group_name = $input->getString('group_name')))
198 {
199 $group = $this->getModel('packgroup');
200
201 // attempt to save group
202 $id_group = $group->save(array('title' => $group_name));
203
204 if ($id_group)
205 {
206 // overwrite the group ID
207 $args['id_group'] = $id_group;
208 }
209 }
210
211 /**
212 * Convert timestamp from local timezone to UTC.
213 *
214 * @since 1.7
215 */
216 $args['start_ts'] = VAPDateHelper::getSqlDateLocale($args['start_ts']);
217 $args['end_ts'] = VAPDateHelper::getSqlDateLocale($args['end_ts']);
218
219 // get db model
220 $package = $this->getModel();
221
222 // try to save arguments
223 $id = $package->save($args);
224
225 if (!$id)
226 {
227 // get string error
228 $error = $package->getError(null, true);
229
230 // display error message
231 $app->enqueueMessage(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $error), 'error');
232
233 $url = 'index.php?option=com_vikappointments&view=managepackage';
234
235 if ($args['id'])
236 {
237 $url .= '&cid[]=' . $args['id'];
238 }
239
240 // redirect to new/edit page
241 $this->setRedirect($url);
242
243 return false;
244 }
245
246 // display generic successful message
247 $app->enqueueMessage(JText::translate('JLIB_APPLICATION_SAVE_SUCCESS'));
248
249 // redirect to edit page
250 $this->setRedirect('index.php?option=com_vikappointments&task=package.edit&cid[]=' . $id);
251
252 return true;
253 }
254
255 /**
256 * Deletes a list of records set in the request.
257 *
258 * @return boolean
259 */
260 public function delete()
261 {
262 $app = JFactory::getApplication();
263 $user = JFactory::getUser();
264
265 /**
266 * Added token validation.
267 * Both GET and POST are supported.
268 *
269 * @since 1.7
270 */
271 if (!JSession::checkToken() && !JSession::checkToken('get'))
272 {
273 // back to main list, missing CSRF-proof token
274 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
275 $this->cancel();
276
277 return false;
278 }
279
280 $cid = $app->input->get('cid', array(), 'uint');
281
282 // check user permissions
283 if (!$user->authorise('core.delete', 'com_vikappointments') || !$user->authorise('core.access.packages', 'com_vikappointments') || !VikAppointments::isPackagesEnabled())
284 {
285 // back to main list, not authorised to delete records
286 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
287 $this->cancel();
288
289 return false;
290 }
291
292 // delete selected records
293 $this->getModel()->delete($cid);
294
295 // back to main list
296 $this->cancel();
297
298 return true;
299 }
300
301 /**
302 * Duplicates a list of records set in the request.
303 *
304 * @return boolean
305 */
306 public function duplicate()
307 {
308 $app = JFactory::getApplication();
309 $user = JFactory::getUser();
310
311 /**
312 * Added token validation.
313 * Both GET and POST are supported.
314 *
315 * @since 1.7
316 */
317 if (!JSession::checkToken() && !JSession::checkToken('get'))
318 {
319 // back to main list, missing CSRF-proof token
320 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
321 $this->cancel();
322
323 return false;
324 }
325
326 $cid = $app->input->get('cid', array(), 'uint');
327
328 // check user permissions
329 if (!$user->authorise('core.create', 'com_vikappointments') || !$user->authorise('core.access.packages', 'com_vikappointments') || !VikAppointments::isPackagesEnabled())
330 {
331 // back to main list, not authorised to delete records
332 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
333 $this->cancel();
334
335 return false;
336 }
337
338 // duplicate selected records
339 $result = $this->getModel()->duplicate($cid);
340
341 /**
342 * @todo should we display how many records have been created?
343 */
344
345 // back to main list
346 $this->cancel();
347
348 return true;
349 }
350
351 /**
352 * Publishes the selected records.
353 *
354 * @return boolean
355 */
356 public function publish()
357 {
358 $app = JFactory::getApplication();
359 $user = JFactory::getUser();
360
361 /**
362 * Added token validation.
363 * Both GET and POST are supported.
364 *
365 * @since 1.7
366 */
367 if (!JSession::checkToken() && !JSession::checkToken('get'))
368 {
369 // back to main list, missing CSRF-proof token
370 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
371 $this->cancel();
372
373 return false;
374 }
375
376 $cid = $app->input->get('cid', array(), 'uint');
377 $task = $app->input->get('task', null);
378
379 $state = $task == 'unpublish' ? 0 : 1;
380
381 // check user permissions
382 if (!$user->authorise('core.edit.state', 'com_vikappointments') || !$user->authorise('core.access.packages', 'com_vikappointments') || !VikAppointments::isPackagesEnabled())
383 {
384 // back to main list, not authorised to edit records
385 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
386 $this->cancel();
387
388 return false;
389 }
390
391 // change state of selected records
392 $this->getModel()->publish($cid, $state);
393
394 // back to main list
395 $this->cancel();
396
397 return true;
398 }
399
400 /**
401 * Redirects the users to the main records list.
402 *
403 * @return void
404 */
405 public function cancel()
406 {
407 if (VikAppointments::isPackagesEnabled())
408 {
409 $this->setRedirect('index.php?option=com_vikappointments&view=packages');
410 }
411 else
412 {
413 $this->setRedirect('index.php?option=com_vikappointments');
414 }
415 }
416 }
417