| @@ -1,0 +1,208 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikBooking | |
| 4 | + * @subpackage core | |
| 5 | + * @author E4J s.r.l. | |
| 6 | + * @copyright Copyright (C) 2019 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 | +JLoader::import('adapter.mvc.controllers.admin'); | |
| 15 | + | |
| 16 | +/** | |
| 17 | + * VikBooking plugin Shortcode controller. | |
| 18 | + * | |
| 19 | + * @since 1.0 | |
| 20 | + * @see JControllerAdmin | |
| 21 | + */ | |
| 22 | +class VikBookingControllerShortcode extends JControllerAdmin | |
| 23 | +{ | |
| 24 | + public function savenew() | |
| 25 | + { | |
| 26 | + $this->save(2); | |
| 27 | + } | |
| 28 | + | |
| 29 | + public function saveclose() | |
| 30 | + { | |
| 31 | + $this->save(1); | |
| 32 | + } | |
| 33 | + | |
| 34 | + public function save($close = 0) | |
| 35 | + { | |
| 36 | + $app = JFactory::getApplication(); | |
| 37 | + | |
| 38 | + // get return URL | |
| 39 | + $encoded = $app->input->getBase64('return', ''); | |
| 40 | + | |
| 41 | + // set up redirect url in case of error | |
| 42 | + $this->setRedirect('admin.php?page=vikbooking&view=shortcodes' . ($encoded ? '&return=' . $encoded : '')); | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * Added token validation. | |
| 46 | + * | |
| 47 | + * @since 1.6.7 | |
| 48 | + */ | |
| 49 | + if (!JSession::checkToken() && !JSession::checkToken('get')) | |
| 50 | + { | |
| 51 | + if (wp_doing_ajax()) | |
| 52 | + { | |
| 53 | + VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN')); | |
| 54 | + } | |
| 55 | + | |
| 56 | + // missing CSRF token | |
| 57 | + $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error'); | |
| 58 | + return false; | |
| 59 | + } | |
| 60 | + | |
| 61 | + // make sure the user is authorised to change shortcodes | |
| 62 | + if (!JFactory::getUser()->authorise('core.admin', 'com_vikbooking')) | |
| 63 | + { | |
| 64 | + if (wp_doing_ajax()) | |
| 65 | + { | |
| 66 | + VBOHttpDocument::getInstance($app)->close(403, JText::translate('JERROR_ALERTNOAUTHOR')); | |
| 67 | + } | |
| 68 | + | |
| 69 | + // not authorized | |
| 70 | + return false; | |
| 71 | + } | |
| 72 | + | |
| 73 | + // get item from request | |
| 74 | + $data = $this->model->getFormData(); | |
| 75 | + | |
| 76 | + // dispatch model to save the item | |
| 77 | + $id = $this->model->save($data); | |
| 78 | + | |
| 79 | + if (!$id) | |
| 80 | + { | |
| 81 | + // get string error | |
| 82 | + $error = $this->model->getError(null, true); | |
| 83 | + $error = JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $error); | |
| 84 | + | |
| 85 | + if (wp_doing_ajax()) | |
| 86 | + { | |
| 87 | + VBOHttpDocument::getInstance($app)->close(500, $error); | |
| 88 | + } | |
| 89 | + | |
| 90 | + // display error message | |
| 91 | + $app->enqueueMessage($error, 'error'); | |
| 92 | + | |
| 93 | + $url = 'admin.php?page=vikbooking&view=shortcode'; | |
| 94 | + | |
| 95 | + if ($data->id) | |
| 96 | + { | |
| 97 | + $url .= '&cid[]=' . $data->id; | |
| 98 | + } | |
| 99 | + | |
| 100 | + // redirect to new/edit page | |
| 101 | + $this->setRedirect($url); | |
| 102 | + return false; | |
| 103 | + } | |
| 104 | + | |
| 105 | + if (wp_doing_ajax()) | |
| 106 | + { | |
| 107 | + VBOHttpDocument::getInstance($app)->json($this->model->getItem($id)); | |
| 108 | + } | |
| 109 | + | |
| 110 | + if ($close == 2) | |
| 111 | + { | |
| 112 | + // save and new | |
| 113 | + $return = 'admin.php?page=vikbooking&task=shortcodes.create&return=' . $encoded; | |
| 114 | + } | |
| 115 | + else if ($close == 1) | |
| 116 | + { | |
| 117 | + // save and close | |
| 118 | + $return = 'admin.php?page=vikbooking&view=shortcodes&return=' . $encoded; | |
| 119 | + } | |
| 120 | + else | |
| 121 | + { | |
| 122 | + // save and stay in edit page | |
| 123 | + $return = 'admin.php?page=vikbooking&task=shortcodes.edit&cid[]=' . $id . '&return=' . $encoded; | |
| 124 | + } | |
| 125 | + | |
| 126 | + $app->redirect($return); | |
| 127 | + } | |
| 128 | + | |
| 129 | + public function params() | |
| 130 | + { | |
| 131 | + $input = JFactory::getApplication()->input; | |
| 132 | + | |
| 133 | + $id = $input->getInt('id', 0); | |
| 134 | + $type = $input->getString('type', ''); | |
| 135 | + | |
| 136 | + // dispatch model to get the item (an empty ITEM if not exists) | |
| 137 | + $item = $this->model->getItem($id); | |
| 138 | + | |
| 139 | + // inject the type to load the right form | |
| 140 | + $item->type = $type; | |
| 141 | + | |
| 142 | + // obtain the type form | |
| 143 | + $form = $this->model->getTypeForm($item); | |
| 144 | + | |
| 145 | + // if the form doesn't exist, the type is probably empty | |
| 146 | + if (!$form) | |
| 147 | + { | |
| 148 | + // return an empty HTML | |
| 149 | + echo ""; | |
| 150 | + } | |
| 151 | + // render the form and encode the response | |
| 152 | + else | |
| 153 | + { | |
| 154 | + $args = json_decode($item->json); | |
| 155 | + echo json_encode($form->renderForm($args)); | |
| 156 | + } | |
| 157 | + | |
| 158 | + exit; | |
| 159 | + } | |
| 160 | + | |
| 161 | + /** | |
| 162 | + * This task will create a page on WordPress with the requested Shortcode inside it. | |
| 163 | + * This is useful to automatically link Shortcodes in pages with no manual actions. | |
| 164 | + * | |
| 165 | + * @since 1.3.6 | |
| 166 | + */ | |
| 167 | + public function add_to_page() | |
| 168 | + { | |
| 169 | + $app = JFactory::getApplication(); | |
| 170 | + $input = $app->input; | |
| 171 | + | |
| 172 | + // get return URL | |
| 173 | + $encoded = $input->getBase64('return', ''); | |
| 174 | + | |
| 175 | + // always redirect to the shortcodes list | |
| 176 | + $this->setRedirect('admin.php?option=com_vikbooking&view=shortcodes&return=' . $encoded); | |
| 177 | + | |
| 178 | + // make sure the user is authorised to change shortcodes | |
| 179 | + if (!JFactory::getUser()->authorise('core.admin', 'com_vikbooking')) | |
| 180 | + { | |
| 181 | + return; | |
| 182 | + } | |
| 183 | + | |
| 184 | + // get selected shortcodes | |
| 185 | + $cid = $input->getUint('cid', array()); | |
| 186 | + | |
| 187 | + // attempt to assign the shortcodes to a page | |
| 188 | + if ($this->model->addPage($cid)) | |
| 189 | + { | |
| 190 | + // add success message and redirect | |
| 191 | + $app->enqueueMessage(JText::translate('VBO_SC_ADDTOPAGE_OK')); | |
| 192 | + } | |
| 193 | + | |
| 194 | + // fetch all registered errors (if any) | |
| 195 | + $errors = $this->model->getErrors(); | |
| 196 | + | |
| 197 | + foreach ($errors as $error) | |
| 198 | + { | |
| 199 | + if ($error instanceof Exception) | |
| 200 | + { | |
| 201 | + $error = $error->getMessage(); | |
| 202 | + } | |
| 203 | + | |
| 204 | + // enqueue error message | |
| 205 | + $app->enqueueMessage($error, 'error'); | |
| 206 | + } | |
| 207 | + } | |
| 208 | +} | |