| 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 states controller. |
| 16 |
* |
| 17 |
* @since 1.16.0 (J) - 1.6.0 (WP) |
| 18 |
*/ |
| 19 |
class VikBookingControllerStates extends JControllerAdmin |
| 20 |
{ |
| 21 |
/** |
| 22 |
* AJAX endpoint to load the states of a given country. |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public function load_from_country() |
| 27 |
{ |
| 28 |
$dbo = JFactory::getDbo(); |
| 29 |
|
| 30 |
$id_country = VikRequest::getInt('id_country', 0, 'request'); |
| 31 |
$country_3_code = VikRequest::getString('country_3_code', '', 'request'); |
| 32 |
$country_2_code = VikRequest::getString('country_2_code', '', 'request'); |
| 33 |
$country_name = VikRequest::getString('country_name', '', 'request'); |
| 34 |
|
| 35 |
if (empty($id_country) && empty($country_3_code) && empty($country_2_code) && empty($country_name)) { |
| 36 |
VBOHttpDocument::getInstance()->close(500, 'Missing country identifier'); |
| 37 |
} |
| 38 |
|
| 39 |
if (!empty($id_country)) { |
| 40 |
$q = "SELECT * FROM `#__vikbooking_states` WHERE `id_country`=" . $id_country; |
| 41 |
$dbo->setQuery($q); |
| 42 |
$dbo->execute(); |
| 43 |
if (!$dbo->getNumRows()) { |
| 44 |
// no records found for this country |
| 45 |
VBOHttpDocument::getInstance()->json([]); |
| 46 |
} |
| 47 |
// output the JSON encoded list of states found |
| 48 |
VBOHttpDocument::getInstance()->json($dbo->loadAssocList()); |
| 49 |
} |
| 50 |
|
| 51 |
// find country ID by name or code |
| 52 |
$field_name = $dbo->qn('country_name'); |
| 53 |
$field_value = $country_name; |
| 54 |
if (!empty($country_3_code)) { |
| 55 |
$field_name = $dbo->qn('country_3_code'); |
| 56 |
$field_value = $country_3_code; |
| 57 |
} |
| 58 |
if (!empty($country_2_code)) { |
| 59 |
$field_name = $dbo->qn('country_2_code'); |
| 60 |
$field_value = $country_2_code; |
| 61 |
} |
| 62 |
|
| 63 |
$q = "SELECT `id` FROM `#__vikbooking_countries` WHERE {$field_name}=" . $dbo->quote($field_value); |
| 64 |
$dbo->setQuery($q, 0, 1); |
| 65 |
$id_country = $dbo->loadResult(); |
| 66 |
if (!$id_country) { |
| 67 |
// country not found |
| 68 |
VBOHttpDocument::getInstance()->close(404, sprintf('Country [%s] not found', $field_value)); |
| 69 |
} |
| 70 |
|
| 71 |
$q = "SELECT * FROM `#__vikbooking_states` WHERE `id_country`=" . $id_country; |
| 72 |
$dbo->setQuery($q); |
| 73 |
$states = $dbo->loadAssocList(); |
| 74 |
if (!$states) { |
| 75 |
// no records found for this country |
| 76 |
VBOHttpDocument::getInstance()->json([]); |
| 77 |
} |
| 78 |
|
| 79 |
// output the JSON encoded list of states found |
| 80 |
VBOHttpDocument::getInstance()->json($states); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Adds a new state. |
| 85 |
*/ |
| 86 |
public function add() |
| 87 |
{ |
| 88 |
$app = JFactory::getApplication(); |
| 89 |
$app->redirect('index.php?option=com_vikbooking&view=managestate&idcountry=' . $app->input->getInt('idcountry', 0)); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Modifies an existing state. |
| 94 |
*/ |
| 95 |
public function edit() |
| 96 |
{ |
| 97 |
$app = JFactory::getApplication(); |
| 98 |
|
| 99 |
$ids = $app->input->get('cid', [0], 'uint'); |
| 100 |
|
| 101 |
$app->redirect('index.php?option=com_vikbooking&view=managestate&id=' . $ids[0]); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Removes one or more existing states. |
| 106 |
*/ |
| 107 |
public function remove() |
| 108 |
{ |
| 109 |
$dbo = JFactory::getDbo(); |
| 110 |
$app = JFactory::getApplication(); |
| 111 |
|
| 112 |
$ids = $app->input->get('cid', [0], 'uint'); |
| 113 |
|
| 114 |
$q = "DELETE FROM `#__vikbooking_states` WHERE `id` IN (" . implode(', ', $ids) . ")"; |
| 115 |
$dbo->setQuery($q); |
| 116 |
$dbo->execute(); |
| 117 |
|
| 118 |
$app->redirect('index.php?option=com_vikbooking&view=states'); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Cancels the management operation. |
| 123 |
*/ |
| 124 |
public function cancel() |
| 125 |
{ |
| 126 |
$app = JFactory::getApplication(); |
| 127 |
$app->redirect('index.php?option=com_vikbooking&view=states'); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Adds a new state/province. |
| 132 |
*/ |
| 133 |
public function save() |
| 134 |
{ |
| 135 |
$dbo = JFactory::getDbo(); |
| 136 |
$app = JFactory::getApplication(); |
| 137 |
|
| 138 |
$pidcountry = $app->input->getInt('idcountry', 0); |
| 139 |
$pstate_name = $app->input->getString('state_name', ''); |
| 140 |
$pstate_3_code = $app->input->getString('state_3_code', ''); |
| 141 |
$pstate_2_code = $app->input->getString('state_2_code', ''); |
| 142 |
$ppublished = $app->input->getInt('published', 0); |
| 143 |
|
| 144 |
if (empty($pidcountry) || empty($pstate_name)) { |
| 145 |
$app->enqueueMessage('Missing required values', 'error'); |
| 146 |
$app->redirect('index.php?option=com_vikbooking&task=states.add'); |
| 147 |
$app->close(); |
| 148 |
} |
| 149 |
|
| 150 |
if (empty($pstate_3_code)) { |
| 151 |
$pstate_3_code = function_exists('mb_substr') ? mb_substr($pstate_name, 0, 3, 'UTF-8') : substr($pstate_name, 0, 3); |
| 152 |
} |
| 153 |
|
| 154 |
if (empty($pstate_2_code)) { |
| 155 |
$pstate_2_code = function_exists('mb_substr') ? mb_substr($pstate_name, 0, 2, 'UTF-8') : substr($pstate_name, 0, 2); |
| 156 |
} |
| 157 |
|
| 158 |
$add_state = new stdClass; |
| 159 |
$add_state->id_country = $pidcountry; |
| 160 |
$add_state->state_name = $pstate_name; |
| 161 |
$add_state->state_3_code = $pstate_3_code; |
| 162 |
$add_state->state_2_code = $pstate_2_code; |
| 163 |
$add_state->published = $ppublished; |
| 164 |
|
| 165 |
$dbo->insertObject('#__vikbooking_states', $add_state, 'id'); |
| 166 |
|
| 167 |
if (!isset($add_state->id)) { |
| 168 |
$app->enqueueMessage('Could not create the new state/province'); |
| 169 |
} else { |
| 170 |
$app->enqueueMessage(JText::translate('JLIB_APPLICATION_SAVE_SUCCESS')); |
| 171 |
} |
| 172 |
|
| 173 |
$app->redirect('index.php?option=com_vikbooking&view=states'); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Updates a state/province. |
| 178 |
*/ |
| 179 |
public function update() |
| 180 |
{ |
| 181 |
$this->do_update(); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Updates a state/province. |
| 186 |
*/ |
| 187 |
public function update_stay() |
| 188 |
{ |
| 189 |
$this->do_update($stay = true); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Updates a state/province. |
| 194 |
*/ |
| 195 |
protected function do_update($stay = false) |
| 196 |
{ |
| 197 |
$dbo = JFactory::getDbo(); |
| 198 |
$app = JFactory::getApplication(); |
| 199 |
|
| 200 |
$pid = $app->input->getInt('id', 0); |
| 201 |
$pidcountry = $app->input->getInt('idcountry', 0); |
| 202 |
$pstate_name = $app->input->getString('state_name', ''); |
| 203 |
$pstate_3_code = $app->input->getString('state_3_code', ''); |
| 204 |
$pstate_2_code = $app->input->getString('state_2_code', ''); |
| 205 |
$ppublished = $app->input->getInt('published', 0); |
| 206 |
|
| 207 |
if (empty($pid)) { |
| 208 |
$app->enqueueMessage('Missing record ID to update', 'error'); |
| 209 |
$app->redirect('index.php?option=com_vikbooking&view=states'); |
| 210 |
$app->close(); |
| 211 |
} |
| 212 |
|
| 213 |
if (empty($pidcountry) || empty($pstate_name)) { |
| 214 |
$app->enqueueMessage('Missing required values', 'error'); |
| 215 |
$app->redirect('index.php?option=com_vikbooking&task=states.edit&cid[]=' . $pid); |
| 216 |
$app->close(); |
| 217 |
} |
| 218 |
|
| 219 |
if (empty($pstate_3_code)) { |
| 220 |
$pstate_3_code = function_exists('mb_substr') ? mb_substr($pstate_name, 0, 3, 'UTF-8') : substr($pstate_name, 0, 3); |
| 221 |
} |
| 222 |
|
| 223 |
if (empty($pstate_2_code)) { |
| 224 |
$pstate_2_code = function_exists('mb_substr') ? mb_substr($pstate_name, 0, 2, 'UTF-8') : substr($pstate_name, 0, 2); |
| 225 |
} |
| 226 |
|
| 227 |
$set_state = new stdClass; |
| 228 |
$set_state->id = $pid; |
| 229 |
$set_state->id_country = $pidcountry; |
| 230 |
$set_state->state_name = $pstate_name; |
| 231 |
$set_state->state_3_code = $pstate_3_code; |
| 232 |
$set_state->state_2_code = $pstate_2_code; |
| 233 |
$set_state->published = $ppublished; |
| 234 |
|
| 235 |
$dbo->updateObject('#__vikbooking_states', $set_state, 'id'); |
| 236 |
|
| 237 |
$app->enqueueMessage(JText::translate('JLIB_APPLICATION_SAVE_SUCCESS')); |
| 238 |
|
| 239 |
if ($stay) { |
| 240 |
$app->redirect('index.php?option=com_vikbooking&task=states.edit&cid[]=' . $set_state->id); |
| 241 |
} else { |
| 242 |
$app->redirect('index.php?option=com_vikbooking&view=states'); |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
|