country.php (14982B)
1 <?php 2 class ControllerLocalisationCountry extends Controller { 3 private $error = array(); 4 5 public function index() { 6 $this->load->language('localisation/country'); 7 8 $this->document->setTitle($this->language->get('heading_title')); 9 10 $this->load->model('localisation/country'); 11 12 $this->getList(); 13 } 14 15 public function add() { 16 $this->load->language('localisation/country'); 17 18 $this->document->setTitle($this->language->get('heading_title')); 19 20 $this->load->model('localisation/country'); 21 22 if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { 23 $this->model_localisation_country->addCountry($this->request->post); 24 25 $this->session->data['success'] = $this->language->get('text_success'); 26 27 $url = ''; 28 29 if (isset($this->request->get['sort'])) { 30 $url .= '&sort=' . $this->request->get['sort']; 31 } 32 33 if (isset($this->request->get['order'])) { 34 $url .= '&order=' . $this->request->get['order']; 35 } 36 37 if (isset($this->request->get['page'])) { 38 $url .= '&page=' . $this->request->get['page']; 39 } 40 41 $this->response->redirect($this->url->link('localisation/country', 'user_token=' . $this->session->data['user_token'] . $url, true)); 42 } 43 44 $this->getForm(); 45 } 46 47 public function edit() { 48 $this->load->language('localisation/country'); 49 50 $this->document->setTitle($this->language->get('heading_title')); 51 52 $this->load->model('localisation/country'); 53 54 if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { 55 $this->model_localisation_country->editCountry($this->request->get['country_id'], $this->request->post); 56 57 $this->session->data['success'] = $this->language->get('text_success'); 58 59 $url = ''; 60 61 if (isset($this->request->get['sort'])) { 62 $url .= '&sort=' . $this->request->get['sort']; 63 } 64 65 if (isset($this->request->get['order'])) { 66 $url .= '&order=' . $this->request->get['order']; 67 } 68 69 if (isset($this->request->get['page'])) { 70 $url .= '&page=' . $this->request->get['page']; 71 } 72 73 $this->response->redirect($this->url->link('localisation/country', 'user_token=' . $this->session->data['user_token'] . $url, true)); 74 } 75 76 $this->getForm(); 77 } 78 79 public function delete() { 80 $this->load->language('localisation/country'); 81 82 $this->document->setTitle($this->language->get('heading_title')); 83 84 $this->load->model('localisation/country'); 85 86 if (isset($this->request->post['selected']) && $this->validateDelete()) { 87 foreach ($this->request->post['selected'] as $country_id) { 88 $this->model_localisation_country->deleteCountry($country_id); 89 } 90 91 $this->session->data['success'] = $this->language->get('text_success'); 92 93 $url = ''; 94 95 if (isset($this->request->get['sort'])) { 96 $url .= '&sort=' . $this->request->get['sort']; 97 } 98 99 if (isset($this->request->get['order'])) { 100 $url .= '&order=' . $this->request->get['order']; 101 } 102 103 if (isset($this->request->get['page'])) { 104 $url .= '&page=' . $this->request->get['page']; 105 } 106 107 $this->response->redirect($this->url->link('localisation/country', 'user_token=' . $this->session->data['user_token'] . $url, true)); 108 } 109 110 $this->getList(); 111 } 112 113 protected function getList() { 114 if (isset($this->request->get['sort'])) { 115 $sort = $this->request->get['sort']; 116 } else { 117 $sort = 'name'; 118 } 119 120 if (isset($this->request->get['order'])) { 121 $order = $this->request->get['order']; 122 } else { 123 $order = 'ASC'; 124 } 125 126 if (isset($this->request->get['page'])) { 127 $page = $this->request->get['page']; 128 } else { 129 $page = 1; 130 } 131 132 $url = ''; 133 134 if (isset($this->request->get['sort'])) { 135 $url .= '&sort=' . $this->request->get['sort']; 136 } 137 138 if (isset($this->request->get['order'])) { 139 $url .= '&order=' . $this->request->get['order']; 140 } 141 142 if (isset($this->request->get['page'])) { 143 $url .= '&page=' . $this->request->get['page']; 144 } 145 146 $data['breadcrumbs'] = array(); 147 148 $data['breadcrumbs'][] = array( 149 'text' => $this->language->get('text_home'), 150 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true) 151 ); 152 153 $data['breadcrumbs'][] = array( 154 'text' => $this->language->get('heading_title'), 155 'href' => $this->url->link('localisation/country', 'user_token=' . $this->session->data['user_token'] . $url, true) 156 ); 157 158 $data['add'] = $this->url->link('localisation/country/add', 'user_token=' . $this->session->data['user_token'] . $url, true); 159 $data['delete'] = $this->url->link('localisation/country/delete', 'user_token=' . $this->session->data['user_token'] . $url, true); 160 161 $data['countries'] = array(); 162 163 $filter_data = array( 164 'sort' => $sort, 165 'order' => $order, 166 'start' => ($page - 1) * $this->config->get('config_limit_admin'), 167 'limit' => $this->config->get('config_limit_admin') 168 ); 169 170 $country_total = $this->model_localisation_country->getTotalCountries(); 171 172 $results = $this->model_localisation_country->getCountries($filter_data); 173 174 foreach ($results as $result) { 175 $data['countries'][] = array( 176 'country_id' => $result['country_id'], 177 'name' => $result['name'] . (($result['country_id'] == $this->config->get('config_country_id')) ? $this->language->get('text_default') : null), 178 'iso_code_2' => $result['iso_code_2'], 179 'iso_code_3' => $result['iso_code_3'], 180 'edit' => $this->url->link('localisation/country/edit', 'user_token=' . $this->session->data['user_token'] . '&country_id=' . $result['country_id'] . $url, true) 181 ); 182 } 183 184 if (isset($this->error['warning'])) { 185 $data['error_warning'] = $this->error['warning']; 186 } else { 187 $data['error_warning'] = ''; 188 } 189 190 if (isset($this->session->data['success'])) { 191 $data['success'] = $this->session->data['success']; 192 193 unset($this->session->data['success']); 194 } else { 195 $data['success'] = ''; 196 } 197 198 if (isset($this->request->post['selected'])) { 199 $data['selected'] = (array)$this->request->post['selected']; 200 } else { 201 $data['selected'] = array(); 202 } 203 204 $url = ''; 205 206 if ($order == 'ASC') { 207 $url .= '&order=DESC'; 208 } else { 209 $url .= '&order=ASC'; 210 } 211 212 if (isset($this->request->get['page'])) { 213 $url .= '&page=' . $this->request->get['page']; 214 } 215 216 $data['sort_name'] = $this->url->link('localisation/country', 'user_token=' . $this->session->data['user_token'] . '&sort=name' . $url, true); 217 $data['sort_iso_code_2'] = $this->url->link('localisation/country', 'user_token=' . $this->session->data['user_token'] . '&sort=iso_code_2' . $url, true); 218 $data['sort_iso_code_3'] = $this->url->link('localisation/country', 'user_token=' . $this->session->data['user_token'] . '&sort=iso_code_3' . $url, true); 219 220 $url = ''; 221 222 if (isset($this->request->get['sort'])) { 223 $url .= '&sort=' . $this->request->get['sort']; 224 } 225 226 if (isset($this->request->get['order'])) { 227 $url .= '&order=' . $this->request->get['order']; 228 } 229 230 $pagination = new Pagination(); 231 $pagination->total = $country_total; 232 $pagination->page = $page; 233 $pagination->limit = $this->config->get('config_limit_admin'); 234 $pagination->url = $this->url->link('localisation/country', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}', true); 235 236 $data['pagination'] = $pagination->render(); 237 238 $data['results'] = sprintf($this->language->get('text_pagination'), ($country_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($country_total - $this->config->get('config_limit_admin'))) ? $country_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $country_total, ceil($country_total / $this->config->get('config_limit_admin'))); 239 240 $data['sort'] = $sort; 241 $data['order'] = $order; 242 243 $data['header'] = $this->load->controller('common/header'); 244 $data['column_left'] = $this->load->controller('common/column_left'); 245 $data['footer'] = $this->load->controller('common/footer'); 246 247 $this->response->setOutput($this->load->view('localisation/country_list', $data)); 248 } 249 250 protected function getForm() { 251 $data['text_form'] = !isset($this->request->get['country_id']) ? $this->language->get('text_add') : $this->language->get('text_edit'); 252 253 if (isset($this->error['warning'])) { 254 $data['error_warning'] = $this->error['warning']; 255 } else { 256 $data['error_warning'] = ''; 257 } 258 259 if (isset($this->error['name'])) { 260 $data['error_name'] = $this->error['name']; 261 } else { 262 $data['error_name'] = ''; 263 } 264 265 $url = ''; 266 267 if (isset($this->request->get['sort'])) { 268 $url .= '&sort=' . $this->request->get['sort']; 269 } 270 271 if (isset($this->request->get['order'])) { 272 $url .= '&order=' . $this->request->get['order']; 273 } 274 275 if (isset($this->request->get['page'])) { 276 $url .= '&page=' . $this->request->get['page']; 277 } 278 279 $data['breadcrumbs'] = array(); 280 281 $data['breadcrumbs'][] = array( 282 'text' => $this->language->get('text_home'), 283 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true) 284 ); 285 286 $data['breadcrumbs'][] = array( 287 'text' => $this->language->get('heading_title'), 288 'href' => $this->url->link('localisation/country', 'user_token=' . $this->session->data['user_token'] . $url, true) 289 ); 290 291 if (!isset($this->request->get['country_id'])) { 292 $data['action'] = $this->url->link('localisation/country/add', 'user_token=' . $this->session->data['user_token'] . $url, true); 293 } else { 294 $data['action'] = $this->url->link('localisation/country/edit', 'user_token=' . $this->session->data['user_token'] . '&country_id=' . $this->request->get['country_id'] . $url, true); 295 } 296 297 $data['cancel'] = $this->url->link('localisation/country', 'user_token=' . $this->session->data['user_token'] . $url, true); 298 299 if (isset($this->request->get['country_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) { 300 $country_info = $this->model_localisation_country->getCountry($this->request->get['country_id']); 301 } 302 303 if (isset($this->request->post['name'])) { 304 $data['name'] = $this->request->post['name']; 305 } elseif (!empty($country_info)) { 306 $data['name'] = $country_info['name']; 307 } else { 308 $data['name'] = ''; 309 } 310 311 if (isset($this->request->post['iso_code_2'])) { 312 $data['iso_code_2'] = $this->request->post['iso_code_2']; 313 } elseif (!empty($country_info)) { 314 $data['iso_code_2'] = $country_info['iso_code_2']; 315 } else { 316 $data['iso_code_2'] = ''; 317 } 318 319 if (isset($this->request->post['iso_code_3'])) { 320 $data['iso_code_3'] = $this->request->post['iso_code_3']; 321 } elseif (!empty($country_info)) { 322 $data['iso_code_3'] = $country_info['iso_code_3']; 323 } else { 324 $data['iso_code_3'] = ''; 325 } 326 327 if (isset($this->request->post['address_format'])) { 328 $data['address_format'] = $this->request->post['address_format']; 329 } elseif (!empty($country_info)) { 330 $data['address_format'] = $country_info['address_format']; 331 } else { 332 $data['address_format'] = ''; 333 } 334 335 if (isset($this->request->post['postcode_required'])) { 336 $data['postcode_required'] = $this->request->post['postcode_required']; 337 } elseif (!empty($country_info)) { 338 $data['postcode_required'] = $country_info['postcode_required']; 339 } else { 340 $data['postcode_required'] = 0; 341 } 342 343 if (isset($this->request->post['status'])) { 344 $data['status'] = $this->request->post['status']; 345 } elseif (!empty($country_info)) { 346 $data['status'] = $country_info['status']; 347 } else { 348 $data['status'] = '1'; 349 } 350 351 $data['header'] = $this->load->controller('common/header'); 352 $data['column_left'] = $this->load->controller('common/column_left'); 353 $data['footer'] = $this->load->controller('common/footer'); 354 355 $this->response->setOutput($this->load->view('localisation/country_form', $data)); 356 } 357 358 protected function validateForm() { 359 if (!$this->user->hasPermission('modify', 'localisation/country')) { 360 $this->error['warning'] = $this->language->get('error_permission'); 361 } 362 363 if ((utf8_strlen($this->request->post['name']) < 1) || (utf8_strlen($this->request->post['name']) > 128)) { 364 $this->error['name'] = $this->language->get('error_name'); 365 } 366 367 return !$this->error; 368 } 369 370 protected function validateDelete() { 371 if (!$this->user->hasPermission('modify', 'localisation/country')) { 372 $this->error['warning'] = $this->language->get('error_permission'); 373 } 374 375 $this->load->model('setting/store'); 376 $this->load->model('customer/customer'); 377 $this->load->model('localisation/zone'); 378 $this->load->model('localisation/geo_zone'); 379 380 foreach ($this->request->post['selected'] as $country_id) { 381 if ($this->config->get('config_country_id') == $country_id) { 382 $this->error['warning'] = $this->language->get('error_default'); 383 } 384 385 $store_total = $this->model_setting_store->getTotalStoresByCountryId($country_id); 386 387 if ($store_total) { 388 $this->error['warning'] = sprintf($this->language->get('error_store'), $store_total); 389 } 390 391 $address_total = $this->model_customer_customer->getTotalAddressesByCountryId($country_id); 392 393 if ($address_total) { 394 $this->error['warning'] = sprintf($this->language->get('error_address'), $address_total); 395 } 396 397 $zone_total = $this->model_localisation_zone->getTotalZonesByCountryId($country_id); 398 399 if ($zone_total) { 400 $this->error['warning'] = sprintf($this->language->get('error_zone'), $zone_total); 401 } 402 403 $zone_to_geo_zone_total = $this->model_localisation_geo_zone->getTotalZoneToGeoZoneByCountryId($country_id); 404 405 if ($zone_to_geo_zone_total) { 406 $this->error['warning'] = sprintf($this->language->get('error_zone_to_geo_zone'), $zone_to_geo_zone_total); 407 } 408 } 409 410 return !$this->error; 411 } 412 413 public function country() { 414 $json = array(); 415 416 $this->load->model('localisation/country'); 417 418 $country_info = $this->model_localisation_country->getCountry($this->request->get['country_id']); 419 420 if ($country_info) { 421 $this->load->model('localisation/zone'); 422 423 $json = array( 424 'country_id' => $country_info['country_id'], 425 'name' => $country_info['name'], 426 'iso_code_2' => $country_info['iso_code_2'], 427 'iso_code_3' => $country_info['iso_code_3'], 428 'address_format' => $country_info['address_format'], 429 'postcode_required' => $country_info['postcode_required'], 430 'zone' => $this->model_localisation_zone->getZonesByCountryId($this->request->get['country_id']), 431 'status' => $country_info['status'] 432 ); 433 } 434 435 $this->response->addHeader('Content-Type: application/json'); 436 $this->response->setOutput(json_encode($json)); 437 } 438 }