PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / trunk
VikAppointments Services Booking Calendar vtrunk
trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / controllers / coupon.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 5 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 5 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 5 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 5 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
coupon.php
303 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 coupon controller.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsControllerCoupon 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.coupon.data', $data);
43
44 // check user permissions
45 if (!$user->authorise('core.create', 'com_vikappointments') || !$user->authorise('core.access.coupons', 'com_vikappointments'))
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=managecoupon');
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.coupon.data', array());
71
72 // check user permissions
73 if (!$user->authorise('core.edit', 'com_vikappointments') || !$user->authorise('core.access.coupons', 'com_vikappointments'))
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=managecoupon&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=coupon.add');
115 }
116 }
117
118 /**
119 * Task used to save the record data set in the request.
120 * After saving, the user is redirected to the management
121 * page of the record that has been saved.
122 *
123 * @param boolean $copy True to save the record as a copy.
124 *
125 * @return boolean
126 */
127 public function save($copy = false)
128 {
129 $app = JFactory::getApplication();
130 $input = $app->input;
131 $user = JFactory::getUser();
132
133 /**
134 * Added token validation.
135 *
136 * @since 1.7
137 */
138 if (!JSession::checkToken())
139 {
140 // back to main list, missing CSRF-proof token
141 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
142 $this->cancel();
143
144 return false;
145 }
146
147 $args = array();
148 $args['code'] = $input->getString('code', '');
149 $args['type'] = $input->getUint('type', 1);
150 $args['max_quantity'] = $input->getUint('max_quantity');
151 $args['used_quantity'] = $input->getUint('used_quantity');
152 $args['maxperuser'] = $input->getUint('maxperuser', 0);
153 $args['applicable'] = $input->getString('applicable', '');
154 $args['remove_gift'] = $input->getUint('remove_gift', 0);
155 $args['percentot'] = $input->getUint('percentot', 1);
156 $args['value'] = $input->getFloat('value', 0);
157 $args['mincost'] = $input->getFloat('mincost', 0);
158 $args['pubmode'] = $input->getUint('pubmode', 1);
159 $args['dstart'] = $input->getString('dstart', '');
160 $args['dend'] = $input->getString('dend', '');
161 $args['lastminute'] = $input->getUint('lastminute', 0);
162 $args['notes'] = $input->getString('notes', '');
163 $args['services'] = $input->getUint('services', array());
164 $args['employees'] = $input->getUint('employees', array());
165 $args['id_group'] = $input->getUint('id_group', 0);
166 $args['id'] = $input->getUint('id', 0);
167
168 $rule = 'core.' . ($args['id'] > 0 ? 'edit' : 'create');
169
170 // check user permissions
171 if (!$user->authorise($rule, 'com_vikappointments') || !$user->authorise('core.access.coupons', 'com_vikappointments'))
172 {
173 // back to main list, not authorised to create/edit records
174 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
175 $this->cancel();
176
177 return false;
178 }
179
180 /**
181 * Try to auto-create a new group before saving the coupon.
182 *
183 * @since 1.7
184 */
185 if ($args['id_group'] == 0 && ($group_name = $input->getString('group_name')))
186 {
187 // make sure the user is authorised
188 if ($user->authorise('core.create', 'com_vikappointments'))
189 {
190 $group = $this->getModel('coupongroup');
191
192 // attempt to save group
193 $id_group = $group->save(array('name' => $group_name));
194
195 if ($id_group)
196 {
197 // overwrite the group ID
198 $args['id_group'] = $id_group;
199 }
200 }
201 }
202
203 /**
204 * Convert timestamp from local timezone to UTC.
205 *
206 * @since 1.7
207 */
208 $args['dstart'] = VAPDateHelper::getSqlDateLocale($args['dstart']);
209 $args['dend'] = VAPDateHelper::getSqlDateLocale($args['dend']);
210
211 // get coupon model
212 $coupon = $this->getModel();
213
214 // try to save arguments
215 $id = $coupon->save($args);
216
217 if (!$id)
218 {
219 // get string error
220 $error = $coupon->getError(null, true);
221
222 // display error message
223 $app->enqueueMessage(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $error), 'error');
224
225 $url = 'index.php?option=com_vikappointments&view=managecoupon';
226
227 if ($args['id'])
228 {
229 $url .= '&cid[]=' . $args['id'];
230 }
231
232 // redirect to new/edit page
233 $this->setRedirect($url);
234
235 return false;
236 }
237
238 // display generic successful message
239 $app->enqueueMessage(JText::translate('JLIB_APPLICATION_SAVE_SUCCESS'));
240
241 // redirect to edit page
242 $this->setRedirect('index.php?option=com_vikappointments&task=coupon.edit&cid[]=' . $id);
243
244 return true;
245 }
246
247 /**
248 * Deletes a list of records set in the request.
249 *
250 * @return boolean
251 */
252 public function delete()
253 {
254 $app = JFactory::getApplication();
255 $user = JFactory::getUser();
256
257 /**
258 * Added token validation.
259 * Both GET and POST are supported.
260 *
261 * @since 1.7
262 */
263 if (!JSession::checkToken() && !JSession::checkToken('get'))
264 {
265 // back to main list, missing CSRF-proof token
266 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
267 $this->cancel();
268
269 return false;
270 }
271
272 $cid = $app->input->get('cid', array(), 'uint');
273
274 // check user permissions
275 if (!$user->authorise('core.delete', 'com_vikappointments') || !$user->authorise('core.access.coupons', 'com_vikappointments'))
276 {
277 // back to main list, not authorised to delete records
278 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
279 $this->cancel();
280
281 return false;
282 }
283
284 // delete selected records
285 $this->getModel()->delete($cid);
286
287 // back to main list
288 $this->cancel();
289
290 return true;
291 }
292
293 /**
294 * Redirects the users to the main records list.
295 *
296 * @return void
297 */
298 public function cancel()
299 {
300 $this->setRedirect('index.php?option=com_vikappointments&view=coupons');
301 }
302 }
303