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
3 months 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
3 months ago
conversion.php
2 years 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
6 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
6 months ago
findreservation.php
3 months ago
group.php
4 years ago
import.php
4 years ago
index.html
4 years ago
invoice.php
3 months 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
3 months ago
media.php
4 years ago
multiorder.php
4 years ago
option.php
6 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
3 months ago
restriction.php
4 years ago
review.php
4 years ago
service.php
1 year ago
serworkday.php
6 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
cronjob.php
413 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 cron job controller. |
| 18 | * |
| 19 | * @since 1.7 |
| 20 | */ |
| 21 | class VikAppointmentsControllerCronjob 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 | |
| 35 | $class = $app->input->getString('class', ''); |
| 36 | |
| 37 | if ($class) |
| 38 | { |
| 39 | $data['class'] = $class; |
| 40 | } |
| 41 | |
| 42 | // unset user state for being recovered again |
| 43 | $app->setUserState('vap.cronjob.data', $data); |
| 44 | |
| 45 | // check user permissions |
| 46 | if (!$user->authorise('core.create', 'com_vikappointments') || !$user->authorise('core.access.config', 'com_vikappointments')) |
| 47 | { |
| 48 | // back to main list, not authorised to create records |
| 49 | $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error'); |
| 50 | $this->cancel(); |
| 51 | |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | $this->setRedirect('index.php?option=com_vikappointments&view=managecronjob'); |
| 56 | |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Task used to access the management page of an existing record. |
| 62 | * |
| 63 | * @return boolean |
| 64 | */ |
| 65 | public function edit() |
| 66 | { |
| 67 | $app = JFactory::getApplication(); |
| 68 | $user = JFactory::getUser(); |
| 69 | |
| 70 | // unset user state for being recovered again |
| 71 | $app->setUserState('vap.cronjob.data', array()); |
| 72 | |
| 73 | // check user permissions |
| 74 | if (!$user->authorise('core.edit', 'com_vikappointments') || !$user->authorise('core.access.config', 'com_vikappointments')) |
| 75 | { |
| 76 | // back to main list, not authorised to edit records |
| 77 | $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error'); |
| 78 | $this->cancel(); |
| 79 | |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | $cid = $app->input->getUint('cid', array(0)); |
| 84 | |
| 85 | $this->setRedirect('index.php?option=com_vikappointments&view=managecronjob&cid[]=' . $cid[0]); |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Task used to save the record data set in the request. |
| 92 | * After saving, the user is redirected to the main list. |
| 93 | * |
| 94 | * @return void |
| 95 | */ |
| 96 | public function saveclose() |
| 97 | { |
| 98 | if ($this->save()) |
| 99 | { |
| 100 | $this->cancel(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Task used to save the record data set in the request. |
| 106 | * After saving, the user is redirected to the creation |
| 107 | * page of a new record. |
| 108 | * |
| 109 | * @return void |
| 110 | */ |
| 111 | public function savenew() |
| 112 | { |
| 113 | if ($this->save()) |
| 114 | { |
| 115 | $this->setRedirect('index.php?option=com_vikappointments&task=cronjob.add'); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Task used to save the record data as a copy of the current item. |
| 121 | * After saving, the user is redirected to the management |
| 122 | * page of the record that has been saved. |
| 123 | * |
| 124 | * @return void |
| 125 | */ |
| 126 | public function savecopy() |
| 127 | { |
| 128 | $this->save(true); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Task used to save the record data set in the request. |
| 133 | * After saving, the user is redirected to the management |
| 134 | * page of the record that has been saved. |
| 135 | * |
| 136 | * @param boolean $copy True to save the record as a copy. |
| 137 | * |
| 138 | * @return boolean |
| 139 | */ |
| 140 | public function save($copy = false) |
| 141 | { |
| 142 | $app = JFactory::getApplication(); |
| 143 | $input = $app->input; |
| 144 | $user = JFactory::getUser(); |
| 145 | |
| 146 | /** |
| 147 | * Added token validation. |
| 148 | * |
| 149 | * @since 1.7 |
| 150 | */ |
| 151 | if (!JSession::checkToken()) |
| 152 | { |
| 153 | // back to main list, missing CSRF-proof token |
| 154 | $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error'); |
| 155 | $this->cancel(); |
| 156 | |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | $args = array(); |
| 161 | $args['name'] = $input->getString('name'); |
| 162 | $args['class'] = $input->getString('class'); |
| 163 | $args['published'] = $input->getUint('published', 0); |
| 164 | $args['id'] = $input->getUint('id', 0); |
| 165 | |
| 166 | /** |
| 167 | * Check whether the paused notifications should be instantly resumed. |
| 168 | * |
| 169 | * @since 1.7.4 |
| 170 | */ |
| 171 | if ($input->getBool('resume_notif', false)) |
| 172 | { |
| 173 | $args['resume_notif'] = JFactory::getDbo()->getNullDate(); |
| 174 | } |
| 175 | |
| 176 | if ($copy) |
| 177 | { |
| 178 | // unset ID to create a copy |
| 179 | $args['id'] = 0; |
| 180 | } |
| 181 | |
| 182 | $rule = 'core.' . ($args['id'] > 0 ? 'edit' : 'create'); |
| 183 | |
| 184 | // check user permissions |
| 185 | if (!$user->authorise($rule, 'com_vikappointments') || !$user->authorise('core.access.config', 'com_vikappointments')) |
| 186 | { |
| 187 | // back to main list, not authorised to create/edit records |
| 188 | $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error'); |
| 189 | $this->cancel(); |
| 190 | |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | // get cron job model |
| 195 | $cron = $this->getModel(); |
| 196 | |
| 197 | // try to save arguments |
| 198 | $id = $cron->save($args); |
| 199 | |
| 200 | if (!$id) |
| 201 | { |
| 202 | // get string error |
| 203 | $error = $cron->getError(null, true); |
| 204 | |
| 205 | // display error message |
| 206 | $app->enqueueMessage(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $error), 'error'); |
| 207 | |
| 208 | $url = 'index.php?option=com_vikappointments&view=managecronjob'; |
| 209 | |
| 210 | if ($args['id']) |
| 211 | { |
| 212 | $url .= '&cid[]=' . $args['id']; |
| 213 | } |
| 214 | |
| 215 | // redirect to new/edit page |
| 216 | $this->setRedirect($url); |
| 217 | |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | // display generic successful message |
| 222 | $app->enqueueMessage(JText::translate('JLIB_APPLICATION_SAVE_SUCCESS')); |
| 223 | |
| 224 | // look for an error even if the record was saved |
| 225 | $error = $cron->getError(); |
| 226 | |
| 227 | if ($error) |
| 228 | { |
| 229 | // raise warning |
| 230 | $app->enqueueMessage($error, 'warning'); |
| 231 | } |
| 232 | |
| 233 | // redirect to edit page |
| 234 | $this->setRedirect('index.php?option=com_vikappointments&task=cronjob.edit&cid[]=' . $id); |
| 235 | |
| 236 | return true; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Deletes a list of records set in the request. |
| 241 | * |
| 242 | * @return boolean |
| 243 | */ |
| 244 | public function delete() |
| 245 | { |
| 246 | $app = JFactory::getApplication(); |
| 247 | $user = JFactory::getUser(); |
| 248 | |
| 249 | /** |
| 250 | * Added token validation. |
| 251 | * Both GET and POST are supported. |
| 252 | * |
| 253 | * @since 1.7 |
| 254 | */ |
| 255 | if (!JSession::checkToken() && !JSession::checkToken('get')) |
| 256 | { |
| 257 | // back to main list, missing CSRF-proof token |
| 258 | $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error'); |
| 259 | $this->cancel(); |
| 260 | |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | $cid = $app->input->get('cid', array(), 'uint'); |
| 265 | |
| 266 | // check user permissions |
| 267 | if (!$user->authorise('core.delete', 'com_vikappointments') || !$user->authorise('core.access.config', 'com_vikappointments')) |
| 268 | { |
| 269 | // back to main list, not authorised to delete records |
| 270 | $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error'); |
| 271 | $this->cancel(); |
| 272 | |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | $model = $this->getModel(); |
| 277 | |
| 278 | // delete selected records |
| 279 | $model->delete($cid); |
| 280 | |
| 281 | // look for an error even if the record was saved |
| 282 | $errors = $model->getErrors(); |
| 283 | |
| 284 | foreach ($errors as $error) |
| 285 | { |
| 286 | // raise warning |
| 287 | $app->enqueueMessage($error, 'warning'); |
| 288 | } |
| 289 | |
| 290 | // back to main list |
| 291 | $this->cancel(); |
| 292 | |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Publishes the selected records. |
| 298 | * |
| 299 | * @return boolean |
| 300 | */ |
| 301 | public function publish() |
| 302 | { |
| 303 | $app = JFactory::getApplication(); |
| 304 | $user = JFactory::getUser(); |
| 305 | |
| 306 | /** |
| 307 | * Added token validation. |
| 308 | * Both GET and POST are supported. |
| 309 | * |
| 310 | * @since 1.7 |
| 311 | */ |
| 312 | if (!JSession::checkToken() && !JSession::checkToken('get')) |
| 313 | { |
| 314 | // back to main list, missing CSRF-proof token |
| 315 | $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error'); |
| 316 | $this->cancel(); |
| 317 | |
| 318 | return false; |
| 319 | } |
| 320 | |
| 321 | $cid = $app->input->get('cid', array(), 'uint'); |
| 322 | $task = $app->input->get('task', null); |
| 323 | |
| 324 | $state = $task == 'unpublish' ? 0 : 1; |
| 325 | |
| 326 | // check user permissions |
| 327 | if (!$user->authorise('core.edit.state', 'com_vikappointments') || !$user->authorise('core.access.config', 'com_vikappointments')) |
| 328 | { |
| 329 | // back to main list, not authorised to edit records |
| 330 | $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error'); |
| 331 | $this->cancel(); |
| 332 | |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | // change state of selected records |
| 337 | $this->getModel()->publish($cid, $state); |
| 338 | |
| 339 | // back to main list |
| 340 | $this->cancel(); |
| 341 | |
| 342 | return true; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Redirects the users to the main records list. |
| 347 | * |
| 348 | * @return void |
| 349 | */ |
| 350 | public function cancel() |
| 351 | { |
| 352 | $this->setRedirect('index.php?option=com_vikappointments&view=cronjobs'); |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * AJAX end-point used to retrieve the configuration |
| 357 | * of the selected driver. |
| 358 | * |
| 359 | * @return void |
| 360 | */ |
| 361 | public function driverfields() |
| 362 | { |
| 363 | $input = JFactory::getApplication()->input; |
| 364 | |
| 365 | /** |
| 366 | * Added token validation. |
| 367 | * |
| 368 | * @since 1.7 |
| 369 | */ |
| 370 | if (!JSession::checkToken()) |
| 371 | { |
| 372 | // missing CSRF-proof token |
| 373 | UIErrorFactory::raiseError(403, JText::translate('JINVALID_TOKEN')); |
| 374 | } |
| 375 | |
| 376 | $driver = $input->getString('driver'); |
| 377 | $id = $input->getUint('id', 0); |
| 378 | |
| 379 | VikAppointments::loadCronLibrary(); |
| 380 | |
| 381 | // get cron job configuration |
| 382 | $config = VAPCronDispatcher::getJobConfiguration($driver); |
| 383 | |
| 384 | if ($config === null) |
| 385 | { |
| 386 | // an error occurred, raise error |
| 387 | UIErrorFactory::raiseError(404, JText::translate('VAPCRONJOBERROR1')); |
| 388 | } |
| 389 | |
| 390 | $params = array(); |
| 391 | |
| 392 | if ($id > 0) |
| 393 | { |
| 394 | // load cron job record |
| 395 | $item = $this->getModel()->getItem($id); |
| 396 | |
| 397 | if ($item) |
| 398 | { |
| 399 | // decode parameters |
| 400 | $params = (array) json_decode($item->params, true); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | // create form builder |
| 405 | $builder = new VikAppointmentsCronFormBuilder($config); |
| 406 | // render form HTML |
| 407 | $html = $builder->build($params); |
| 408 | |
| 409 | // send HTML form to caller |
| 410 | $this->sendJSON(json_encode($html)); |
| 411 | } |
| 412 | } |
| 413 |