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
5 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
5 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
5 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
5 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
country.php
372 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 country controller. |
| 18 | * |
| 19 | * @since 1.7 |
| 20 | */ |
| 21 | class VikAppointmentsControllerCountry 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 | // unset user state for being recovered again |
| 34 | $app->setUserState('vap.country.data', array()); |
| 35 | |
| 36 | // check user permissions |
| 37 | if (!$user->authorise('core.create', 'com_vikappointments') || !$user->authorise('core.access.countries', 'com_vikappointments')) |
| 38 | { |
| 39 | // back to main list, not authorised to create records |
| 40 | $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error'); |
| 41 | $this->cancel(); |
| 42 | |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | $url = 'index.php?option=com_vikappointments&view=managecountry'; |
| 47 | |
| 48 | if ($tmpl = $app->input->get('tmpl')) |
| 49 | { |
| 50 | // propagate specified tmpl |
| 51 | $url .= '&tmpl=' . $tmpl; |
| 52 | } |
| 53 | |
| 54 | $this->setRedirect($url); |
| 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.country.data', array()); |
| 71 | |
| 72 | // check user permissions |
| 73 | if (!$user->authorise('core.edit', 'com_vikappointments') || !$user->authorise('core.access.countries', '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 | $url = 'index.php?option=com_vikappointments&view=managecountry&cid[]=' . $cid[0]; |
| 85 | |
| 86 | if ($tmpl = $app->input->get('tmpl')) |
| 87 | { |
| 88 | // propagate specified tmpl |
| 89 | $url .= '&tmpl=' . $tmpl; |
| 90 | } |
| 91 | |
| 92 | $this->setRedirect($url); |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Task used to save the record data set in the request. |
| 99 | * After saving, the user is redirected to the main list. |
| 100 | * |
| 101 | * @return void |
| 102 | */ |
| 103 | public function saveclose() |
| 104 | { |
| 105 | if ($this->save()) |
| 106 | { |
| 107 | $this->cancel(); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Task used to save the record data set in the request. |
| 113 | * After saving, the user is redirected to the creation |
| 114 | * page of a new record. |
| 115 | * |
| 116 | * @return void |
| 117 | */ |
| 118 | public function savenew() |
| 119 | { |
| 120 | if ($this->save()) |
| 121 | { |
| 122 | $this->setRedirect('index.php?option=com_vikappointments&task=country.add'); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Task used to save the record data set in the request. |
| 128 | * After saving, the user is redirected to the management |
| 129 | * page of the record that has been saved. |
| 130 | * |
| 131 | * @param boolean $copy True to save the record as a copy. |
| 132 | * |
| 133 | * @return boolean |
| 134 | */ |
| 135 | public function save($copy = false) |
| 136 | { |
| 137 | $app = JFactory::getApplication(); |
| 138 | $input = $app->input; |
| 139 | $user = JFactory::getUser(); |
| 140 | |
| 141 | /** |
| 142 | * Added token validation. |
| 143 | * |
| 144 | * @since 1.7 |
| 145 | */ |
| 146 | if (!JSession::checkToken()) |
| 147 | { |
| 148 | // back to main list, missing CSRF-proof token |
| 149 | $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error'); |
| 150 | $this->cancel(); |
| 151 | |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | $args = array(); |
| 156 | $args['country_name'] = $input->getString('country_name'); |
| 157 | $args['country_2_code'] = $input->getString('country_2_code'); |
| 158 | $args['country_3_code'] = $input->getString('country_3_code'); |
| 159 | $args['phone_prefix'] = $input->getString('phone_prefix'); |
| 160 | $args['published'] = $input->getUint('published', 0); |
| 161 | $args['id'] = $input->getUint('id', 0); |
| 162 | |
| 163 | $rule = 'core.' . ($args['id'] > 0 ? 'edit' : 'create'); |
| 164 | |
| 165 | // check user permissions |
| 166 | if (!$user->authorise($rule, 'com_vikappointments') || !$user->authorise('core.access.countries', 'com_vikappointments')) |
| 167 | { |
| 168 | // back to main list, not authorised to create/edit records |
| 169 | $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error'); |
| 170 | $this->cancel(); |
| 171 | |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | $tmpl = $input->get('tmpl'); |
| 176 | |
| 177 | // get country model |
| 178 | $country = $this->getModel(); |
| 179 | |
| 180 | // try to save arguments |
| 181 | $id = $country->save($args); |
| 182 | |
| 183 | if (!$id) |
| 184 | { |
| 185 | // get string error |
| 186 | $error = $country->getError(null, true); |
| 187 | |
| 188 | // display error message |
| 189 | $app->enqueueMessage(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $error), 'error'); |
| 190 | |
| 191 | $url = 'index.php?option=com_vikappointments&view=managecountry'; |
| 192 | |
| 193 | if ($args['id']) |
| 194 | { |
| 195 | $url .= '&cid[]=' . $args['id']; |
| 196 | } |
| 197 | |
| 198 | if ($tmpl) |
| 199 | { |
| 200 | // propagate specified tmpl |
| 201 | $url .= '&tmpl=' . $tmpl; |
| 202 | } |
| 203 | |
| 204 | // redirect to new/edit page |
| 205 | $this->setRedirect($url); |
| 206 | |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | // display generic successful message |
| 211 | $app->enqueueMessage(JText::translate('JLIB_APPLICATION_SAVE_SUCCESS')); |
| 212 | |
| 213 | $url = 'index.php?option=com_vikappointments&task=country.edit&cid[]=' . $id; |
| 214 | |
| 215 | if ($tmpl) |
| 216 | { |
| 217 | // propagate specified tmpl |
| 218 | $url .= '&tmpl=' . $tmpl; |
| 219 | } |
| 220 | |
| 221 | // redirect to edit page |
| 222 | $this->setRedirect($url); |
| 223 | |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Deletes a list of records set in the request. |
| 229 | * |
| 230 | * @return boolean |
| 231 | */ |
| 232 | public function delete() |
| 233 | { |
| 234 | $app = JFactory::getApplication(); |
| 235 | $user = JFactory::getUser(); |
| 236 | |
| 237 | /** |
| 238 | * Added token validation. |
| 239 | * Both GET and POST are supported. |
| 240 | * |
| 241 | * @since 1.7 |
| 242 | */ |
| 243 | if (!JSession::checkToken() && !JSession::checkToken('get')) |
| 244 | { |
| 245 | // back to main list, missing CSRF-proof token |
| 246 | $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error'); |
| 247 | $this->cancel(); |
| 248 | |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | $cid = $app->input->get('cid', array(), 'uint'); |
| 253 | |
| 254 | // check user permissions |
| 255 | if (!$user->authorise('core.delete', 'com_vikappointments') || !$user->authorise('core.access.countries', 'com_vikappointments')) |
| 256 | { |
| 257 | // back to main list, not authorised to delete records |
| 258 | $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error'); |
| 259 | $this->cancel(); |
| 260 | |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | // delete selected records |
| 265 | $this->getModel()->delete($cid); |
| 266 | |
| 267 | // back to main list |
| 268 | $this->cancel(); |
| 269 | |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Publishes the selected records. |
| 275 | * |
| 276 | * @return boolean |
| 277 | */ |
| 278 | public function publish() |
| 279 | { |
| 280 | $app = JFactory::getApplication(); |
| 281 | $user = JFactory::getUser(); |
| 282 | |
| 283 | /** |
| 284 | * Added token validation. |
| 285 | * Both GET and POST are supported. |
| 286 | * |
| 287 | * @since 1.7 |
| 288 | */ |
| 289 | if (!JSession::checkToken() && !JSession::checkToken('get')) |
| 290 | { |
| 291 | // back to main list, missing CSRF-proof token |
| 292 | $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error'); |
| 293 | $this->cancel(); |
| 294 | |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | $cid = $app->input->get('cid', array(), 'uint'); |
| 299 | $task = $app->input->get('task', null); |
| 300 | |
| 301 | $state = $task == 'unpublish' ? 0 : 1; |
| 302 | |
| 303 | // check user permissions |
| 304 | if (!$user->authorise('core.edit.state', 'com_vikappointments') || !$user->authorise('core.access.countries', 'com_vikappointments')) |
| 305 | { |
| 306 | // back to main list, not authorised to edit records |
| 307 | $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error'); |
| 308 | $this->cancel(); |
| 309 | |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | // change state of selected records |
| 314 | $this->getModel()->publish($cid, $state); |
| 315 | |
| 316 | // back to main list |
| 317 | $this->cancel(); |
| 318 | |
| 319 | return true; |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Redirects the users to the main records list. |
| 324 | * |
| 325 | * @return void |
| 326 | */ |
| 327 | public function cancel() |
| 328 | { |
| 329 | $input = JFactory::getApplication()->input; |
| 330 | |
| 331 | $url = 'index.php?option=com_vikappointments&view=countries'; |
| 332 | |
| 333 | if ($tmpl = $input->get('tmpl')) |
| 334 | { |
| 335 | // propagate specified tmpl |
| 336 | $url .= '&tmpl=' . $tmpl; |
| 337 | } |
| 338 | |
| 339 | $this->setRedirect($url); |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * AJAX end-point used to obtain all the states assigned to the given country. |
| 344 | * The task expects the following parameters to be set in request. |
| 345 | * |
| 346 | * @param integer id_country The country ID. |
| 347 | * |
| 348 | * @return void |
| 349 | */ |
| 350 | function statesajax() |
| 351 | { |
| 352 | $input = JFactory::getApplication()->input; |
| 353 | |
| 354 | /** |
| 355 | * Added token validation. |
| 356 | * |
| 357 | * @since 1.7 |
| 358 | */ |
| 359 | if (!JSession::checkToken()) |
| 360 | { |
| 361 | // missing CSRF-proof token |
| 362 | UIErrorFactory::raiseError(403, JText::translate('JINVALID_TOKEN')); |
| 363 | } |
| 364 | |
| 365 | $id_country = $input->getUint('id_country', 0); |
| 366 | $states = VAPLocations::getStates($id_country, 'state_name'); |
| 367 | |
| 368 | // send states to caller |
| 369 | $this->sendJSON($states); |
| 370 | } |
| 371 | } |
| 372 |