| @@ -1,0 +1,210 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikBooking | |
| 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 | +/** | |
| 15 | + * VikBooking cron jobs controller. | |
| 16 | + * | |
| 17 | + * @since 1.5.10 | |
| 18 | + */ | |
| 19 | +class VikBookingControllerCronjob extends JControllerAdmin | |
| 20 | +{ | |
| 21 | + /** | |
| 22 | + * Task used to access the creation page of a new record. | |
| 23 | + * | |
| 24 | + * @return boolean | |
| 25 | + */ | |
| 26 | + public function add() | |
| 27 | + { | |
| 28 | + $app = JFactory::getApplication(); | |
| 29 | + $user = JFactory::getUser(); | |
| 30 | + | |
| 31 | + // check user permissions | |
| 32 | + if (!$user->authorise('core.create', 'com_vikbooking') || !$user->authorise('core.vbo.management', 'com_vikbooking')) | |
| 33 | + { | |
| 34 | + // back to main list, not authorised to create records | |
| 35 | + $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error'); | |
| 36 | + $this->cancel(); | |
| 37 | + | |
| 38 | + return false; | |
| 39 | + } | |
| 40 | + | |
| 41 | + $this->setRedirect('index.php?option=com_vikbooking&view=managecron'); | |
| 42 | + | |
| 43 | + return true; | |
| 44 | + } | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * Task used to access the management page of an existing record. | |
| 48 | + * | |
| 49 | + * @return boolean | |
| 50 | + */ | |
| 51 | + public function edit() | |
| 52 | + { | |
| 53 | + $app = JFactory::getApplication(); | |
| 54 | + $user = JFactory::getUser(); | |
| 55 | + | |
| 56 | + // check user permissions | |
| 57 | + if (!$user->authorise('core.edit', 'com_vikbooking') || !$user->authorise('core.vbo.management', 'com_vikbooking')) | |
| 58 | + { | |
| 59 | + // back to main list, not authorised to edit records | |
| 60 | + $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error'); | |
| 61 | + $this->cancel(); | |
| 62 | + | |
| 63 | + return false; | |
| 64 | + } | |
| 65 | + | |
| 66 | + $cid = $app->input->getUint('cid', array(0)); | |
| 67 | + | |
| 68 | + $this->setRedirect('index.php?option=com_vikbooking&view=managecron&cid[]=' . $cid[0]); | |
| 69 | + | |
| 70 | + return true; | |
| 71 | + } | |
| 72 | + | |
| 73 | + /** | |
| 74 | + * Task used to save the record data set in the request. | |
| 75 | + * After saving, the user is redirected to the main list. | |
| 76 | + * | |
| 77 | + * @return void | |
| 78 | + */ | |
| 79 | + public function saveclose() | |
| 80 | + { | |
| 81 | + if ($this->save()) | |
| 82 | + { | |
| 83 | + $this->cancel(); | |
| 84 | + } | |
| 85 | + } | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * Task used to save the record data set in the request. | |
| 89 | + * After saving, the user is redirected to the management | |
| 90 | + * page of the record that has been saved. | |
| 91 | + * | |
| 92 | + * @return boolean | |
| 93 | + */ | |
| 94 | + public function save() | |
| 95 | + { | |
| 96 | + $app = JFactory::getApplication(); | |
| 97 | + $user = JFactory::getUser(); | |
| 98 | + | |
| 99 | + if (!JSession::checkToken()) | |
| 100 | + { | |
| 101 | + // back to main list, missing CSRF-proof token | |
| 102 | + $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error'); | |
| 103 | + $this->cancel(); | |
| 104 | + | |
| 105 | + return false; | |
| 106 | + } | |
| 107 | + | |
| 108 | + $data = []; | |
| 109 | + $data['id'] = $app->input->get('id', 0, 'uint'); | |
| 110 | + $data['cron_name'] = $app->input->get('cron_name', '', 'string'); | |
| 111 | + $data['class_file'] = $app->input->get('class_file', '', 'string'); | |
| 112 | + $data['published'] = $app->input->get('published', 0, 'uint'); | |
| 113 | + $data['params'] = $app->input->get('vikcronparams', [], 'array'); | |
| 114 | + | |
| 115 | + $rule = 'core.' . ($data['id'] > 0 ? 'edit' : 'create'); | |
| 116 | + | |
| 117 | + // check user permissions | |
| 118 | + if (!$user->authorise($rule, 'com_vikbooking') || !$user->authorise('core.vbo.management', 'com_vikbooking')) | |
| 119 | + { | |
| 120 | + // back to main list, not authorised to create/edit records | |
| 121 | + $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error'); | |
| 122 | + $this->cancel(); | |
| 123 | + | |
| 124 | + return false; | |
| 125 | + } | |
| 126 | + | |
| 127 | + $model = VBOMvcModel::getInstance('cronjob'); | |
| 128 | + | |
| 129 | + // try to save data | |
| 130 | + $id = $model->save($data); | |
| 131 | + | |
| 132 | + if (!$id) | |
| 133 | + { | |
| 134 | + $error = $model->getError(null, true) ?: 'Unknown error.'; | |
| 135 | + | |
| 136 | + // display error message | |
| 137 | + $app->enqueueMessage(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $error), 'error'); | |
| 138 | + | |
| 139 | + $url = 'index.php?option=com_vikbooking&view=managecron'; | |
| 140 | + | |
| 141 | + if ($data['id']) | |
| 142 | + { | |
| 143 | + $url .= '&cid[]=' . $data['id']; | |
| 144 | + } | |
| 145 | + | |
| 146 | + // redirect to new/edit page | |
| 147 | + $this->setRedirect($url); | |
| 148 | + | |
| 149 | + return false; | |
| 150 | + } | |
| 151 | + | |
| 152 | + // display generic successful message | |
| 153 | + $app->enqueueMessage(JText::translate('JLIB_APPLICATION_SAVE_SUCCESS')); | |
| 154 | + | |
| 155 | + // redirect to edit page | |
| 156 | + $this->setRedirect('index.php?option=com_vikbooking&task=cronjob.edit&cid[]=' . $id); | |
| 157 | + | |
| 158 | + return true; | |
| 159 | + } | |
| 160 | + | |
| 161 | + /** | |
| 162 | + * Deletes a list of records set in the request. | |
| 163 | + * | |
| 164 | + * @return boolean | |
| 165 | + */ | |
| 166 | + public function delete() | |
| 167 | + { | |
| 168 | + $app = JFactory::getApplication(); | |
| 169 | + $user = JFactory::getUser(); | |
| 170 | + | |
| 171 | + if (!JSession::checkToken() && !JSession::checkToken('get')) | |
| 172 | + { | |
| 173 | + // back to main list, missing CSRF-proof token | |
| 174 | + $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error'); | |
| 175 | + $this->cancel(); | |
| 176 | + | |
| 177 | + return false; | |
| 178 | + } | |
| 179 | + | |
| 180 | + $cid = $app->input->get('cid', array(), 'uint'); | |
| 181 | + | |
| 182 | + // check user permissions | |
| 183 | + if (!$user->authorise('core.delete', 'com_vikbooking') || !$user->authorise('core.vbo.management', 'com_vikbooking')) | |
| 184 | + { | |
| 185 | + // back to main list, not authorised to delete records | |
| 186 | + $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error'); | |
| 187 | + $this->cancel(); | |
| 188 | + | |
| 189 | + return false; | |
| 190 | + } | |
| 191 | + | |
| 192 | + // delete selected records | |
| 193 | + VBOMvcModel::getInstance('cronjob')->delete($cid); | |
| 194 | + | |
| 195 | + // back to main list | |
| 196 | + $this->cancel(); | |
| 197 | + | |
| 198 | + return true; | |
| 199 | + } | |
| 200 | + | |
| 201 | + /** | |
| 202 | + * Redirects the users to the main records list. | |
| 203 | + * | |
| 204 | + * @return void | |
| 205 | + */ | |
| 206 | + public function cancel() | |
| 207 | + { | |
| 208 | + $this->setRedirect('index.php?option=com_vikbooking&view=crons'); | |
| 209 | + } | |
| 210 | +} | |