zone.php (13008B)
1 <?php 2 class ControllerLocalisationZone extends Controller { 3 private $error = array(); 4 5 public function index() { 6 $this->load->language('localisation/zone'); 7 8 $this->document->setTitle($this->language->get('heading_title')); 9 10 $this->load->model('localisation/zone'); 11 12 $this->getList(); 13 } 14 15 public function add() { 16 $this->load->language('localisation/zone'); 17 18 $this->document->setTitle($this->language->get('heading_title')); 19 20 $this->load->model('localisation/zone'); 21 22 if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { 23 $this->model_localisation_zone->addZone($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/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/zone'); 49 50 $this->document->setTitle($this->language->get('heading_title')); 51 52 $this->load->model('localisation/zone'); 53 54 if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { 55 $this->model_localisation_zone->editZone($this->request->get['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/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/zone'); 81 82 $this->document->setTitle($this->language->get('heading_title')); 83 84 $this->load->model('localisation/zone'); 85 86 if (isset($this->request->post['selected']) && $this->validateDelete()) { 87 foreach ($this->request->post['selected'] as $zone_id) { 88 $this->model_localisation_zone->deleteZone($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/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 = 'c.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/zone', 'user_token=' . $this->session->data['user_token'] . $url, true) 156 ); 157 158 $data['add'] = $this->url->link('localisation/zone/add', 'user_token=' . $this->session->data['user_token'] . $url, true); 159 $data['delete'] = $this->url->link('localisation/zone/delete', 'user_token=' . $this->session->data['user_token'] . $url, true); 160 161 $data['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 $zone_total = $this->model_localisation_zone->getTotalZones(); 171 172 $results = $this->model_localisation_zone->getZones($filter_data); 173 174 foreach ($results as $result) { 175 $data['zones'][] = array( 176 'zone_id' => $result['zone_id'], 177 'country' => $result['country'], 178 'name' => $result['name'] . (($result['zone_id'] == $this->config->get('config_zone_id')) ? $this->language->get('text_default') : null), 179 'code' => $result['code'], 180 'edit' => $this->url->link('localisation/zone/edit', 'user_token=' . $this->session->data['user_token'] . '&zone_id=' . $result['zone_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_country'] = $this->url->link('localisation/zone', 'user_token=' . $this->session->data['user_token'] . '&sort=c.name' . $url, true); 217 $data['sort_name'] = $this->url->link('localisation/zone', 'user_token=' . $this->session->data['user_token'] . '&sort=z.name' . $url, true); 218 $data['sort_code'] = $this->url->link('localisation/zone', 'user_token=' . $this->session->data['user_token'] . '&sort=z.code' . $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 = $zone_total; 232 $pagination->page = $page; 233 $pagination->limit = $this->config->get('config_limit_admin'); 234 $pagination->url = $this->url->link('localisation/zone', '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'), ($zone_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($zone_total - $this->config->get('config_limit_admin'))) ? $zone_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $zone_total, ceil($zone_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/zone_list', $data)); 248 } 249 250 protected function getForm() { 251 $data['text_form'] = !isset($this->request->get['zone_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/zone', 'user_token=' . $this->session->data['user_token'] . $url, true) 289 ); 290 291 if (!isset($this->request->get['zone_id'])) { 292 $data['action'] = $this->url->link('localisation/zone/add', 'user_token=' . $this->session->data['user_token'] . $url, true); 293 } else { 294 $data['action'] = $this->url->link('localisation/zone/edit', 'user_token=' . $this->session->data['user_token'] . '&zone_id=' . $this->request->get['zone_id'] . $url, true); 295 } 296 297 $data['cancel'] = $this->url->link('localisation/zone', 'user_token=' . $this->session->data['user_token'] . $url, true); 298 299 if (isset($this->request->get['zone_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) { 300 $zone_info = $this->model_localisation_zone->getZone($this->request->get['zone_id']); 301 } 302 303 if (isset($this->request->post['status'])) { 304 $data['status'] = $this->request->post['status']; 305 } elseif (!empty($zone_info)) { 306 $data['status'] = $zone_info['status']; 307 } else { 308 $data['status'] = '1'; 309 } 310 311 if (isset($this->request->post['name'])) { 312 $data['name'] = $this->request->post['name']; 313 } elseif (!empty($zone_info)) { 314 $data['name'] = $zone_info['name']; 315 } else { 316 $data['name'] = ''; 317 } 318 319 if (isset($this->request->post['code'])) { 320 $data['code'] = $this->request->post['code']; 321 } elseif (!empty($zone_info)) { 322 $data['code'] = $zone_info['code']; 323 } else { 324 $data['code'] = ''; 325 } 326 327 if (isset($this->request->post['country_id'])) { 328 $data['country_id'] = $this->request->post['country_id']; 329 } elseif (!empty($zone_info)) { 330 $data['country_id'] = $zone_info['country_id']; 331 } else { 332 $data['country_id'] = ''; 333 } 334 335 $this->load->model('localisation/country'); 336 337 $data['countries'] = $this->model_localisation_country->getCountries(); 338 339 $data['header'] = $this->load->controller('common/header'); 340 $data['column_left'] = $this->load->controller('common/column_left'); 341 $data['footer'] = $this->load->controller('common/footer'); 342 343 $this->response->setOutput($this->load->view('localisation/zone_form', $data)); 344 } 345 346 protected function validateForm() { 347 if (!$this->user->hasPermission('modify', 'localisation/zone')) { 348 $this->error['warning'] = $this->language->get('error_permission'); 349 } 350 351 if ((utf8_strlen($this->request->post['name']) < 1) || (utf8_strlen($this->request->post['name']) > 64)) { 352 $this->error['name'] = $this->language->get('error_name'); 353 } 354 355 return !$this->error; 356 } 357 358 protected function validateDelete() { 359 if (!$this->user->hasPermission('modify', 'localisation/zone')) { 360 $this->error['warning'] = $this->language->get('error_permission'); 361 } 362 363 $this->load->model('setting/store'); 364 $this->load->model('customer/customer'); 365 $this->load->model('localisation/geo_zone'); 366 367 foreach ($this->request->post['selected'] as $zone_id) { 368 if ($this->config->get('config_zone_id') == $zone_id) { 369 $this->error['warning'] = $this->language->get('error_default'); 370 } 371 372 $store_total = $this->model_setting_store->getTotalStoresByZoneId($zone_id); 373 374 if ($store_total) { 375 $this->error['warning'] = sprintf($this->language->get('error_store'), $store_total); 376 } 377 378 $address_total = $this->model_customer_customer->getTotalAddressesByZoneId($zone_id); 379 380 if ($address_total) { 381 $this->error['warning'] = sprintf($this->language->get('error_address'), $address_total); 382 } 383 384 $zone_to_geo_zone_total = $this->model_localisation_geo_zone->getTotalZoneToGeoZoneByZoneId($zone_id); 385 386 if ($zone_to_geo_zone_total) { 387 $this->error['warning'] = sprintf($this->language->get('error_zone_to_geo_zone'), $zone_to_geo_zone_total); 388 } 389 } 390 391 return !$this->error; 392 } 393 }