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
subscription.php
391 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 subscription controller. |
| 18 | * |
| 19 | * @since 1.7 |
| 20 | */ |
| 21 | class VikAppointmentsControllerSubscription 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 | $group = $app->input->getUint('group', null); |
| 35 | |
| 36 | if (!is_null($group)) |
| 37 | { |
| 38 | $data['group'] = $group; |
| 39 | } |
| 40 | |
| 41 | // unset user state for being recovered again |
| 42 | $app->setUserState('vap.subscription.data', $data); |
| 43 | |
| 44 | // check user permissions |
| 45 | if (!$user->authorise('core.create', 'com_vikappointments') || !$user->authorise('core.access.subscriptions', '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=managesubscription'); |
| 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.subscription.data', array()); |
| 71 | |
| 72 | // check user permissions |
| 73 | if (!$user->authorise('core.edit', 'com_vikappointments') || !$user->authorise('core.access.subscriptions', '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=managesubscription&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 | $input = JFactory::getApplication()->input; |
| 115 | |
| 116 | $url = 'index.php?option=com_vikappointments&task=subscription.add'; |
| 117 | |
| 118 | $group = $input->getUint('group', null); |
| 119 | |
| 120 | if (!is_null($group)) |
| 121 | { |
| 122 | // preserve group for the next record |
| 123 | $url .= '&group=' . $group; |
| 124 | } |
| 125 | |
| 126 | $this->setRedirect($url); |
| 127 | } |
| 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['amount'] = $input->getUint('amount', 1); |
| 163 | $args['type'] = $input->getUint('type', 1); |
| 164 | $args['price'] = $input->getFloat('price', 0.0); |
| 165 | $args['id_tax'] = $input->getUint('id_tax', 0); |
| 166 | $args['published'] = $input->getUint('published', 0); |
| 167 | $args['trial'] = $input->getUint('trial', 0); |
| 168 | $args['group'] = $input->getUint('group', 0); |
| 169 | $args['services'] = $input->getUint('services', array()); |
| 170 | $args['id'] = $input->getUint('id', 0); |
| 171 | |
| 172 | $rule = 'core.' . ($args['id'] > 0 ? 'edit' : 'create'); |
| 173 | |
| 174 | // check user permissions |
| 175 | if (!$user->authorise($rule, 'com_vikappointments') || !$user->authorise('core.access.subscriptions', 'com_vikappointments')) |
| 176 | { |
| 177 | // back to main list, not authorised to create/edit records |
| 178 | $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error'); |
| 179 | $this->cancel(); |
| 180 | |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | // get subscription model |
| 185 | $subscr = $this->getModel(); |
| 186 | |
| 187 | // try to save arguments |
| 188 | $id = $subscr->save($args); |
| 189 | |
| 190 | if (!$id) |
| 191 | { |
| 192 | // get string error |
| 193 | $error = $subscr->getError(null, true); |
| 194 | |
| 195 | // display error message |
| 196 | $app->enqueueMessage(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $error), 'error'); |
| 197 | |
| 198 | $url = 'index.php?option=com_vikappointments&view=managesubscription'; |
| 199 | |
| 200 | if ($args['id']) |
| 201 | { |
| 202 | $url .= '&cid[]=' . $args['id']; |
| 203 | } |
| 204 | |
| 205 | // redirect to new/edit page |
| 206 | $this->setRedirect($url); |
| 207 | |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | // display generic successful message |
| 212 | $app->enqueueMessage(JText::translate('JLIB_APPLICATION_SAVE_SUCCESS')); |
| 213 | |
| 214 | // redirect to edit page |
| 215 | $this->setRedirect('index.php?option=com_vikappointments&task=subscription.edit&cid[]=' . $id); |
| 216 | |
| 217 | return true; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Deletes a list of records set in the request. |
| 222 | * |
| 223 | * @return boolean |
| 224 | */ |
| 225 | public function delete() |
| 226 | { |
| 227 | $app = JFactory::getApplication(); |
| 228 | $user = JFactory::getUser(); |
| 229 | |
| 230 | /** |
| 231 | * Added token validation. |
| 232 | * Both GET and POST are supported. |
| 233 | * |
| 234 | * @since 1.7 |
| 235 | */ |
| 236 | if (!JSession::checkToken() && !JSession::checkToken('get')) |
| 237 | { |
| 238 | // back to main list, missing CSRF-proof token |
| 239 | $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error'); |
| 240 | $this->cancel(); |
| 241 | |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | $cid = $app->input->get('cid', array(), 'uint'); |
| 246 | |
| 247 | // check user permissions |
| 248 | if (!$user->authorise('core.delete', 'com_vikappointments') || !$user->authorise('core.access.subscriptions', 'com_vikappointments')) |
| 249 | { |
| 250 | // back to main list, not authorised to delete records |
| 251 | $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error'); |
| 252 | $this->cancel(); |
| 253 | |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | // delete selected records |
| 258 | $this->getModel()->delete($cid); |
| 259 | |
| 260 | // back to main list |
| 261 | $this->cancel(); |
| 262 | |
| 263 | return true; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Publishes the selected records. |
| 268 | * |
| 269 | * @return boolean |
| 270 | */ |
| 271 | public function publish() |
| 272 | { |
| 273 | $app = JFactory::getApplication(); |
| 274 | $user = JFactory::getUser(); |
| 275 | |
| 276 | /** |
| 277 | * Added token validation. |
| 278 | * Both GET and POST are supported. |
| 279 | * |
| 280 | * @since 1.7 |
| 281 | */ |
| 282 | if (!JSession::checkToken() && !JSession::checkToken('get')) |
| 283 | { |
| 284 | // back to main list, missing CSRF-proof token |
| 285 | $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error'); |
| 286 | $this->cancel(); |
| 287 | |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | $cid = $app->input->get('cid', array(), 'uint'); |
| 292 | $task = $app->input->get('task', null); |
| 293 | |
| 294 | $state = $task == 'unpublish' ? 0 : 1; |
| 295 | |
| 296 | // check user permissions |
| 297 | if (!$user->authorise('core.edit.state', 'com_vikappointments') || !$user->authorise('core.access.subscriptions', 'com_vikappointments')) |
| 298 | { |
| 299 | // back to main list, not authorised to edit records |
| 300 | $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error'); |
| 301 | $this->cancel(); |
| 302 | |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | // change state of selected records |
| 307 | $this->getModel()->publish($cid, $state); |
| 308 | |
| 309 | // back to main list |
| 310 | $this->cancel(); |
| 311 | |
| 312 | return true; |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Toggles the trial status of a subscription. |
| 317 | * Since there can be only one TRIAL subscription, |
| 318 | * this status will be unset from any other existing |
| 319 | * subscription. |
| 320 | * |
| 321 | * @return boolean |
| 322 | */ |
| 323 | public function trial() |
| 324 | { |
| 325 | $app = JFactory::getApplication(); |
| 326 | $user = JFactory::getUser(); |
| 327 | |
| 328 | /** |
| 329 | * Added token validation. |
| 330 | * Both GET and POST are supported. |
| 331 | * |
| 332 | * @since 1.7 |
| 333 | */ |
| 334 | if (!JSession::checkToken() && !JSession::checkToken('get')) |
| 335 | { |
| 336 | // back to main list, missing CSRF-proof token |
| 337 | $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error'); |
| 338 | $this->cancel(); |
| 339 | |
| 340 | return false; |
| 341 | } |
| 342 | |
| 343 | $cid = $app->input->get('cid', array(), 'uint'); |
| 344 | $task = $app->input->get('task', null); |
| 345 | |
| 346 | $state = $app->input->get('state', 0, 'uint'); |
| 347 | |
| 348 | $group = $app->input->get('group', null, 'uint'); |
| 349 | |
| 350 | // check user permissions |
| 351 | if (!$user->authorise('core.edit.state', 'com_vikappointments') || !$user->authorise('core.access.subscriptions', 'com_vikappointments')) |
| 352 | { |
| 353 | // back to main list, not authorised to edit records |
| 354 | $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error'); |
| 355 | $this->cancel(); |
| 356 | |
| 357 | return false; |
| 358 | } |
| 359 | |
| 360 | // change state of selected records |
| 361 | $this->getModel()->setTrialState($cid, $state, $group); |
| 362 | |
| 363 | // back to main list |
| 364 | $this->cancel(); |
| 365 | |
| 366 | return true; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Redirects the users to the main records list. |
| 371 | * |
| 372 | * @return void |
| 373 | */ |
| 374 | public function cancel() |
| 375 | { |
| 376 | $input = JFactory::getApplication()->input; |
| 377 | |
| 378 | $url = 'index.php?option=com_vikappointments&view=subscriptions'; |
| 379 | |
| 380 | $group = $input->getUint('group', null); |
| 381 | |
| 382 | if (!is_null($group)) |
| 383 | { |
| 384 | // preserve group to change list filtering |
| 385 | $url .= '&group=' . $group; |
| 386 | } |
| 387 | |
| 388 | $this->setRedirect($url); |
| 389 | } |
| 390 | } |
| 391 |