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 / empeditcustfield.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
empeditcustfield.php
380 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 custom field controller.
18 *
19 * @since 1.6
20 */
21 class VikAppointmentsControllerEmpeditcustfield extends VAPControllerAdmin
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.field.data', array());
37
38 // check user permissions
39 if (!$auth->manageCustomFields())
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=empeditcustfield');
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.field.data', array());
67
68 $cid = $app->input->getUint('cid', array(0));
69
70 // check user permissions
71 if (!$auth->manageCustomFields($cid[0]))
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=empeditcustfield&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 custom field.
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 $id_field = $input->getUint('id', 0);
127
128 // check user permissions
129 if (!$auth->manageCustomFields($id_field))
130 {
131 // back to main list, not authorised to edit records
132 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
133 $this->cancel();
134
135 return false;
136 }
137
138 // get args
139 $args = array();
140 $args['name'] = $input->getString('name', '');
141 $args['description'] = JComponentHelper::filterText($input->getRaw('description', ''));
142 $args['type'] = $input->getString('type', '');
143 $args['required'] = $input->getUint('required', 0);
144 $args['repeat'] = $input->getUint('repeat', 0);
145 $args['rule'] = $input->getString('rule', '');
146 $args['services'] = $input->getUint('services', array());
147 $args['multiple'] = 0;
148 $args['poplink'] = '';
149 $args['choose'] = '';
150 $args['id'] = $id_field;
151
152 if ($args['type'] == 'select')
153 {
154 /**
155 * Do not use a string filter so that we can preserve the keys
156 * of the options. Use array_filter instead to get rid of the
157 * options with blank contents.
158 *
159 * @since 1.7
160 */
161 $args['choose'] = array_filter($input->get('choose', array(), 'array'));
162 $args['multiple'] = $input->getUint('multiple', 0);
163 }
164 else if ($args['type'] == 'textarea')
165 {
166 $args['choose'] = array(
167 'editor' => $input->getUint('use_editor', 0),
168 );
169 }
170 else if ($args['type'] == 'number')
171 {
172 $args['choose'] = array(
173 'min' => $input->getString('number_min', ''),
174 'max' => $input->getString('number_max', ''),
175 'decimals' => $input->getUint('number_decimals', 0),
176 );
177
178 if (strlen($args['choose']['min']))
179 {
180 $args['choose']['min'] = (float) $args['choose']['min'];
181 }
182
183 if (strlen($args['choose']['max']))
184 {
185 $args['choose']['max'] = (float) $args['choose']['max'];
186 }
187 }
188 else if ($args['type'] == 'checkbox')
189 {
190 $args['poplink'] = $input->getString('poplink', '');
191 }
192 else if ($args['type'] == 'file')
193 {
194 $args['choose'] = $input->getString('filters', '');
195 $args['multiple'] = $input->getUint('multiple', 0);
196 }
197 else if ($args['type'] == 'separator')
198 {
199 $args['choose'] = $input->getString('sep_suffix', '');
200 }
201
202 if ($args['rule'] == 'phone')
203 {
204 $args['choose'] = $input->getString('country_code', '');
205 }
206
207 // get custom field model
208 $model = $this->getModel();
209
210 // try to save arguments
211 $id = $model->save($args);
212
213 if (!$id)
214 {
215 // get string error
216 $error = $model->getError(null, true);
217
218 // display error message
219 $app->enqueueMessage(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $error), 'error');
220
221 $url = 'index.php?option=com_vikappointments&view=empeditcustfield';
222
223 if ($id_field)
224 {
225 $url .= '&cid[]=' . $id_field;
226 }
227
228 // redirect to edit page
229 $this->setRedirect($url);
230
231 return false;
232 }
233
234 // display generic successful message
235 $app->enqueueMessage(JText::translate('JLIB_APPLICATION_SAVE_SUCCESS'));
236
237 // redirect to edit page
238 $this->setRedirect('index.php?option=com_vikappointments&task=empeditcustfield.edit&cid[]=' . $id);
239
240 return true;
241 }
242
243 /**
244 * Removes the custom field.
245 *
246 * @return void
247 */
248 public function delete()
249 {
250 $app = JFactory::getApplication();
251 $cid = $app->input->get('cid', array(), 'uint');
252
253 if ($id = $app->input->getUint('id'))
254 {
255 $cid[] = $id;
256 }
257
258 /**
259 * Added token validation.
260 * Both GET and POST are supported.
261 *
262 * @since 1.7
263 */
264 if (!JSession::checkToken() && !JSession::checkToken('get'))
265 {
266 // back to main list, missing CSRF-proof token
267 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
268 $this->cancel();
269
270 return false;
271 }
272
273 try
274 {
275 // delete selected records
276 if ($this->getModel()->delete($cid))
277 {
278 $app->enqueueMessage(JText::translate('VAPEMPCUSTOMFREMOVED1'));
279 }
280 }
281 catch (Exception $e)
282 {
283 // an error occurred
284 $app->enqueueMessage($e->getMessage(), 'error');
285 $this->cancel();
286
287 return false;
288 }
289
290 // back to main list (reset list limit)
291 $this->cancel(['listlimit' => 0]);
292
293 return true;
294 }
295
296 /**
297 * Publishes the custom field.
298 *
299 * @return void
300 */
301 public function publish()
302 {
303 $app = JFactory::getApplication();
304
305 $cid = $app->input->get('cid', array(), 'uint');
306 $task = $app->input->get('task', null);
307
308 $state = $task == 'unpublish' ? 0 : 1;
309
310 /**
311 * Added token validation.
312 * Both GET and POST are supported.
313 *
314 * @since 1.7
315 */
316 if (!JSession::checkToken() && !JSession::checkToken('get'))
317 {
318 // back to main list, missing CSRF-proof token
319 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
320 $this->cancel();
321
322 return false;
323 }
324
325 // publish selected records
326 $this->getModel()->publish($cid, $state, 'required');
327
328 // back to main list
329 $this->cancel();
330
331 return true;
332 }
333
334 /**
335 * Redirects the users to the main records list.
336 *
337 * @param array $query An array of query arguments.
338 *
339 * @return void
340 *
341 * @since 1.7
342 */
343 public function cancel(array $query = array())
344 {
345 $url = 'index.php?option=com_vikappointments&view=empcustfields';
346
347 if ($query)
348 {
349 $url .= '&' . http_build_query($query);
350 }
351
352 $this->setRedirect($url);
353 }
354
355 /**
356 * Method to save the submitted ordering values for records via AJAX.
357 *
358 * @return void
359 */
360 public function saveOrderAjax()
361 {
362 $app = JFactory::getApplication();
363 $auth = VAPEmployeeAuth::getInstance();
364
365 // get filters set in request
366 $filters = $app->input->get('filters', array(), 'array');
367
368 // register group alias within the filters array to apply
369 // the rearrangement of the records only to those fields
370 // that belong to the customers
371 $filters['group'] = 0;
372
373 // inject updated filters within the request
374 $app->input->set('filters', $filters);
375
376 // invoke parent to commit the new ordering
377 parent::saveOrderAjax();
378 }
379 }
380