geo_zone.php (12696B)
1 <?php 2 class ControllerLocalisationGeoZone extends Controller { 3 private $error = array(); 4 5 public function index() { 6 $this->load->language('localisation/geo_zone'); 7 8 $this->document->setTitle($this->language->get('heading_title')); 9 10 $this->load->model('localisation/geo_zone'); 11 12 $this->getList(); 13 } 14 15 public function add() { 16 $this->load->language('localisation/geo_zone'); 17 18 $this->document->setTitle($this->language->get('heading_title')); 19 20 $this->load->model('localisation/geo_zone'); 21 22 if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { 23 $this->model_localisation_geo_zone->addGeoZone($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/geo_zone', '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/geo_zone'); 49 50 $this->document->setTitle($this->language->get('heading_title')); 51 52 $this->load->model('localisation/geo_zone'); 53 54 if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { 55 $this->model_localisation_geo_zone->editGeoZone($this->request->get['geo_zone_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/geo_zone', '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/geo_zone'); 81 82 $this->document->setTitle($this->language->get('heading_title')); 83 84 $this->load->model('localisation/geo_zone'); 85 86 if (isset($this->request->post['selected']) && $this->validateDelete()) { 87 foreach ($this->request->post['selected'] as $geo_zone_id) { 88 $this->model_localisation_geo_zone->deleteGeoZone($geo_zone_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/geo_zone', '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/geo_zone', 'user_token=' . $this->session->data['user_token'] . $url, true) 156 ); 157 158 $data['add'] = $this->url->link('localisation/geo_zone/add', 'user_token=' . $this->session->data['user_token'] . $url, true); 159 $data['delete'] = $this->url->link('localisation/geo_zone/delete', 'user_token=' . $this->session->data['user_token'] . $url, true); 160 161 $data['geo_zones'] = 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 $geo_zone_total = $this->model_localisation_geo_zone->getTotalGeoZones(); 171 172 $results = $this->model_localisation_geo_zone->getGeoZones($filter_data); 173 174 foreach ($results as $result) { 175 $data['geo_zones'][] = array( 176 'geo_zone_id' => $result['geo_zone_id'], 177 'name' => $result['name'], 178 'description' => $result['description'], 179 'edit' => $this->url->link('localisation/geo_zone/edit', 'user_token=' . $this->session->data['user_token'] . '&geo_zone_id=' . $result['geo_zone_id'] . $url, true) 180 ); 181 } 182 183 if (isset($this->error['warning'])) { 184 $data['error_warning'] = $this->error['warning']; 185 } else { 186 $data['error_warning'] = ''; 187 } 188 189 if (isset($this->session->data['success'])) { 190 $data['success'] = $this->session->data['success']; 191 192 unset($this->session->data['success']); 193 } else { 194 $data['success'] = ''; 195 } 196 197 if (isset($this->request->post['selected'])) { 198 $data['selected'] = (array)$this->request->post['selected']; 199 } else { 200 $data['selected'] = array(); 201 } 202 203 $url = ''; 204 205 if ($order == 'ASC') { 206 $url .= '&order=DESC'; 207 } else { 208 $url .= '&order=ASC'; 209 } 210 211 if (isset($this->request->get['page'])) { 212 $url .= '&page=' . $this->request->get['page']; 213 } 214 215 $data['sort_name'] = $this->url->link('localisation/geo_zone', 'user_token=' . $this->session->data['user_token'] . '&sort=name' . $url, true); 216 $data['sort_description'] = $this->url->link('localisation/geo_zone', 'user_token=' . $this->session->data['user_token'] . '&sort=description' . $url, true); 217 218 $url = ''; 219 220 if (isset($this->request->get['sort'])) { 221 $url .= '&sort=' . $this->request->get['sort']; 222 } 223 224 if (isset($this->request->get['order'])) { 225 $url .= '&order=' . $this->request->get['order']; 226 } 227 228 $pagination = new Pagination(); 229 $pagination->total = $geo_zone_total; 230 $pagination->page = $page; 231 $pagination->limit = $this->config->get('config_limit_admin'); 232 $pagination->url = $this->url->link('localisation/geo_zone', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}', true); 233 234 $data['pagination'] = $pagination->render(); 235 236 $data['results'] = sprintf($this->language->get('text_pagination'), ($geo_zone_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($geo_zone_total - $this->config->get('config_limit_admin'))) ? $geo_zone_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $geo_zone_total, ceil($geo_zone_total / $this->config->get('config_limit_admin'))); 237 238 $data['sort'] = $sort; 239 $data['order'] = $order; 240 241 $data['header'] = $this->load->controller('common/header'); 242 $data['column_left'] = $this->load->controller('common/column_left'); 243 $data['footer'] = $this->load->controller('common/footer'); 244 245 $this->response->setOutput($this->load->view('localisation/geo_zone_list', $data)); 246 } 247 248 protected function getForm() { 249 $data['text_form'] = !isset($this->request->get['geo_zone_id']) ? $this->language->get('text_add') : $this->language->get('text_edit'); 250 251 if (isset($this->error['warning'])) { 252 $data['error_warning'] = $this->error['warning']; 253 } else { 254 $data['error_warning'] = ''; 255 } 256 257 if (isset($this->error['name'])) { 258 $data['error_name'] = $this->error['name']; 259 } else { 260 $data['error_name'] = ''; 261 } 262 263 if (isset($this->error['description'])) { 264 $data['error_description'] = $this->error['description']; 265 } else { 266 $data['error_description'] = ''; 267 } 268 269 $url = ''; 270 271 if (isset($this->request->get['sort'])) { 272 $url .= '&sort=' . $this->request->get['sort']; 273 } 274 275 if (isset($this->request->get['order'])) { 276 $url .= '&order=' . $this->request->get['order']; 277 } 278 279 if (isset($this->request->get['page'])) { 280 $url .= '&page=' . $this->request->get['page']; 281 } 282 283 $data['breadcrumbs'] = array(); 284 285 $data['breadcrumbs'][] = array( 286 'text' => $this->language->get('text_home'), 287 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true) 288 ); 289 290 $data['breadcrumbs'][] = array( 291 'text' => $this->language->get('heading_title'), 292 'href' => $this->url->link('localisation/geo_zone', 'user_token=' . $this->session->data['user_token'] . $url, true) 293 ); 294 295 if (!isset($this->request->get['geo_zone_id'])) { 296 $data['action'] = $this->url->link('localisation/geo_zone/add', 'user_token=' . $this->session->data['user_token'] . $url, true); 297 } else { 298 $data['action'] = $this->url->link('localisation/geo_zone/edit', 'user_token=' . $this->session->data['user_token'] . '&geo_zone_id=' . $this->request->get['geo_zone_id'] . $url, true); 299 } 300 301 $data['cancel'] = $this->url->link('localisation/geo_zone', 'user_token=' . $this->session->data['user_token'] . $url, true); 302 303 if (isset($this->request->get['geo_zone_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) { 304 $geo_zone_info = $this->model_localisation_geo_zone->getGeoZone($this->request->get['geo_zone_id']); 305 } 306 307 $data['user_token'] = $this->session->data['user_token']; 308 309 if (isset($this->request->post['name'])) { 310 $data['name'] = $this->request->post['name']; 311 } elseif (!empty($geo_zone_info)) { 312 $data['name'] = $geo_zone_info['name']; 313 } else { 314 $data['name'] = ''; 315 } 316 317 if (isset($this->request->post['description'])) { 318 $data['description'] = $this->request->post['description']; 319 } elseif (!empty($geo_zone_info)) { 320 $data['description'] = $geo_zone_info['description']; 321 } else { 322 $data['description'] = ''; 323 } 324 325 $this->load->model('localisation/country'); 326 327 $data['countries'] = $this->model_localisation_country->getCountries(); 328 329 if (isset($this->request->post['zone_to_geo_zone'])) { 330 $data['zone_to_geo_zones'] = $this->request->post['zone_to_geo_zone']; 331 } elseif (isset($this->request->get['geo_zone_id'])) { 332 $data['zone_to_geo_zones'] = $this->model_localisation_geo_zone->getZoneToGeoZones($this->request->get['geo_zone_id']); 333 } else { 334 $data['zone_to_geo_zones'] = array(); 335 } 336 337 $data['header'] = $this->load->controller('common/header'); 338 $data['column_left'] = $this->load->controller('common/column_left'); 339 $data['footer'] = $this->load->controller('common/footer'); 340 341 $this->response->setOutput($this->load->view('localisation/geo_zone_form', $data)); 342 } 343 344 protected function validateForm() { 345 if (!$this->user->hasPermission('modify', 'localisation/geo_zone')) { 346 $this->error['warning'] = $this->language->get('error_permission'); 347 } 348 349 if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 32)) { 350 $this->error['name'] = $this->language->get('error_name'); 351 } 352 353 if ((utf8_strlen($this->request->post['description']) < 3) || (utf8_strlen($this->request->post['description']) > 255)) { 354 $this->error['description'] = $this->language->get('error_description'); 355 } 356 357 return !$this->error; 358 } 359 360 protected function validateDelete() { 361 if (!$this->user->hasPermission('modify', 'localisation/geo_zone')) { 362 $this->error['warning'] = $this->language->get('error_permission'); 363 } 364 365 $this->load->model('localisation/tax_rate'); 366 367 foreach ($this->request->post['selected'] as $geo_zone_id) { 368 $tax_rate_total = $this->model_localisation_tax_rate->getTotalTaxRatesByGeoZoneId($geo_zone_id); 369 370 if ($tax_rate_total) { 371 $this->error['warning'] = sprintf($this->language->get('error_tax_rate'), $tax_rate_total); 372 } 373 } 374 375 return !$this->error; 376 } 377 }