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
empeditcoupon.php
275 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 coupon controller. |
| 18 | * |
| 19 | * @since 1.6 |
| 20 | */ |
| 21 | class VikAppointmentsControllerEmpeditcoupon 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.coupon.data', array()); |
| 37 | |
| 38 | // check user permissions |
| 39 | if (!$auth->manageCoupons()) |
| 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=empeditcoupon'); |
| 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.coupon.data', array()); |
| 67 | |
| 68 | $cid = $app->input->getUint('cid', array(0)); |
| 69 | |
| 70 | // check user permissions |
| 71 | if (!$auth->manageCoupons($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=empeditcoupon&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 coupon. |
| 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_coupon = $input->getUint('id', 0); |
| 127 | |
| 128 | // check user permissions |
| 129 | if (!$auth->manageCoupons($id_coupon)) |
| 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['code'] = $input->getString('code'); |
| 141 | $args['type'] = $input->getUint('type', 1); |
| 142 | $args['percentot'] = $input->getUint('percentot', 1); |
| 143 | $args['value'] = $input->getFloat('value', 0); |
| 144 | $args['mincost'] = $input->getFloat('mincost', 0); |
| 145 | $args['max_quantity'] = $input->getUint('max_quantity', 1); |
| 146 | $args['used_quantity'] = $input->getUint('used_quantity', 0); |
| 147 | $args['maxperuser'] = $input->getUint('maxperuser', 0); |
| 148 | $args['remove_gift'] = $input->getUint('remove_gift', 0); |
| 149 | $args['pubmode'] = $input->getUint('pubmode', 0); |
| 150 | $args['dstart'] = $input->getString('dstart', ''); |
| 151 | $args['dend'] = $input->getString('dend', ''); |
| 152 | $args['lastminute'] = $input->getUint('lastminute', 0); |
| 153 | $args['notes'] = $input->getString('notes', ''); |
| 154 | $args['services'] = $input->getUint('services', array()); |
| 155 | $args['id'] = $id_coupon; |
| 156 | |
| 157 | /** |
| 158 | * Convert timestamp from local timezone to UTC. |
| 159 | * |
| 160 | * @since 1.7 |
| 161 | */ |
| 162 | $args['dstart'] = VAPDateHelper::getSqlDateLocale($args['dstart']); |
| 163 | $args['dend'] = VAPDateHelper::getSqlDateLocale($args['dend']); |
| 164 | |
| 165 | // get coupon model |
| 166 | $model = $this->getModel(); |
| 167 | |
| 168 | // try to save arguments |
| 169 | $id = $model->save($args); |
| 170 | |
| 171 | if (!$id) |
| 172 | { |
| 173 | // get string error |
| 174 | $error = $model->getError(null, true); |
| 175 | |
| 176 | // display error message |
| 177 | $app->enqueueMessage(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $error), 'error'); |
| 178 | |
| 179 | $url = 'index.php?option=com_vikappointments&view=empeditcoupon'; |
| 180 | |
| 181 | if ($id_coupon) |
| 182 | { |
| 183 | $url .= '&cid[]=' . $id_coupon; |
| 184 | } |
| 185 | |
| 186 | // redirect to edit page |
| 187 | $this->setRedirect($url); |
| 188 | |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | // display generic successful message |
| 193 | $app->enqueueMessage(JText::translate('JLIB_APPLICATION_SAVE_SUCCESS')); |
| 194 | |
| 195 | // redirect to edit page |
| 196 | $this->setRedirect('index.php?option=com_vikappointments&task=empeditcoupon.edit&cid[]=' . $id); |
| 197 | |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Removes the coupon. |
| 203 | * |
| 204 | * @return void |
| 205 | */ |
| 206 | public function delete() |
| 207 | { |
| 208 | $app = JFactory::getApplication(); |
| 209 | $cid = $app->input->get('cid', array(), 'uint'); |
| 210 | |
| 211 | if ($id = $app->input->getUint('id')) |
| 212 | { |
| 213 | $cid[] = $id; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Added token validation. |
| 218 | * Both GET and POST are supported. |
| 219 | * |
| 220 | * @since 1.7 |
| 221 | */ |
| 222 | if (!JSession::checkToken() && !JSession::checkToken('get')) |
| 223 | { |
| 224 | // back to main list, missing CSRF-proof token |
| 225 | $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error'); |
| 226 | $this->cancel(); |
| 227 | |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | try |
| 232 | { |
| 233 | // delete selected records |
| 234 | if ($this->getModel()->delete($cid)) |
| 235 | { |
| 236 | $app->enqueueMessage(JText::translate('VAPEMPCOUPONREMOVED1')); |
| 237 | } |
| 238 | } |
| 239 | catch (Exception $e) |
| 240 | { |
| 241 | // an error occurred |
| 242 | $app->enqueueMessage($e->getMessage(), 'error'); |
| 243 | $this->cancel(); |
| 244 | |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | // back to main list (reset list limit) |
| 249 | $this->cancel(['listlimit' => 0]); |
| 250 | |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Redirects the users to the main records list. |
| 256 | * |
| 257 | * @param array $query An array of query arguments. |
| 258 | * |
| 259 | * @return void |
| 260 | * |
| 261 | * @since 1.7 |
| 262 | */ |
| 263 | public function cancel(array $query = array()) |
| 264 | { |
| 265 | $url = 'index.php?option=com_vikappointments&view=empcoupons'; |
| 266 | |
| 267 | if ($query) |
| 268 | { |
| 269 | $url .= '&' . http_build_query($query); |
| 270 | } |
| 271 | |
| 272 | $this->setRedirect($url); |
| 273 | } |
| 274 | } |
| 275 |