location.php (14187B)
1 <?php 2 class ControllerLocalisationLocation extends Controller { 3 private $error = array(); 4 5 public function index() { 6 $this->load->language('localisation/location'); 7 8 $this->document->setTitle($this->language->get('heading_title')); 9 10 $this->load->model('localisation/location'); 11 12 $this->getList(); 13 } 14 15 public function add() { 16 $this->load->language('localisation/location'); 17 18 $this->document->setTitle($this->language->get('heading_title')); 19 20 $this->load->model('localisation/location'); 21 22 if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { 23 $this->model_localisation_location->addLocation($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/location', '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/location'); 49 50 $this->document->setTitle($this->language->get('heading_title')); 51 52 $this->load->model('localisation/location'); 53 54 if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { 55 $this->model_localisation_location->editLocation($this->request->get['location_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/location', '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/location'); 81 82 $this->document->setTitle($this->language->get('heading_title')); 83 84 $this->load->model('localisation/location'); 85 86 if (isset($this->request->post['selected']) && $this->validateDelete()) { 87 foreach ($this->request->post['selected'] as $location_id) { 88 $this->model_localisation_location->deleteLocation($location_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/location', '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/location', 'user_token=' . $this->session->data['user_token'] . $url, true) 156 ); 157 158 $data['add'] = $this->url->link('localisation/location/add', 'user_token=' . $this->session->data['user_token'] . $url, true); 159 $data['delete'] = $this->url->link('localisation/location/delete', 'user_token=' . $this->session->data['user_token'] . $url, true); 160 161 $data['location'] = 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 $location_total = $this->model_localisation_location->getTotalLocations(); 171 172 $results = $this->model_localisation_location->getLocations($filter_data); 173 174 foreach ($results as $result) { 175 $data['location'][] = array( 176 'location_id' => $result['location_id'], 177 'name' => $result['name'], 178 'address' => $result['address'], 179 'edit' => $this->url->link('localisation/location/edit', 'user_token=' . $this->session->data['user_token'] . '&location_id=' . $result['location_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/location', 'user_token=' . $this->session->data['user_token'] . '&sort=name' . $url, true); 216 $data['sort_address'] = $this->url->link('localisation/location', 'user_token=' . $this->session->data['user_token'] . '&sort=address' . $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 = $location_total; 230 $pagination->page = $page; 231 $pagination->limit = $this->config->get('config_limit_admin'); 232 $pagination->url = $this->url->link('localisation/location', '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'), ($location_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($location_total - $this->config->get('config_limit_admin'))) ? $location_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $location_total, ceil($location_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/location_list', $data)); 246 } 247 248 protected function getForm() { 249 $data['text_form'] = !isset($this->request->get['location_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['address'])) { 264 $data['error_address'] = $this->error['address']; 265 } else { 266 $data['error_address'] = ''; 267 } 268 269 if (isset($this->error['telephone'])) { 270 $data['error_telephone'] = $this->error['telephone']; 271 } else { 272 $data['error_telephone'] = ''; 273 } 274 275 $url = ''; 276 277 if (isset($this->request->get['sort'])) { 278 $url .= '&sort=' . $this->request->get['sort']; 279 } 280 281 if (isset($this->request->get['order'])) { 282 $url .= '&order=' . $this->request->get['order']; 283 } 284 285 if (isset($this->request->get['page'])) { 286 $url .= '&page=' . $this->request->get['page']; 287 } 288 289 $data['breadcrumbs'] = array(); 290 291 $data['breadcrumbs'][] = array( 292 'text' => $this->language->get('text_home'), 293 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true) 294 ); 295 296 $data['breadcrumbs'][] = array( 297 'text' => $this->language->get('heading_title'), 298 'href' => $this->url->link('localisation/location', 'user_token=' . $this->session->data['user_token'] . $url, true) 299 ); 300 301 if (!isset($this->request->get['location_id'])) { 302 $data['action'] = $this->url->link('localisation/location/add', 'user_token=' . $this->session->data['user_token'] . $url, true); 303 } else { 304 $data['action'] = $this->url->link('localisation/location/edit', 'user_token=' . $this->session->data['user_token'] . '&location_id=' . $this->request->get['location_id'] . $url, true); 305 } 306 307 $data['cancel'] = $this->url->link('localisation/location', 'user_token=' . $this->session->data['user_token'] . $url, true); 308 309 if (isset($this->request->get['location_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) { 310 $location_info = $this->model_localisation_location->getLocation($this->request->get['location_id']); 311 } 312 313 $data['user_token'] = $this->session->data['user_token']; 314 315 $this->load->model('setting/store'); 316 317 if (isset($this->request->post['name'])) { 318 $data['name'] = $this->request->post['name']; 319 } elseif (!empty($location_info)) { 320 $data['name'] = $location_info['name']; 321 } else { 322 $data['name'] = ''; 323 } 324 325 if (isset($this->request->post['address'])) { 326 $data['address'] = $this->request->post['address']; 327 } elseif (!empty($location_info)) { 328 $data['address'] = $location_info['address']; 329 } else { 330 $data['address'] = ''; 331 } 332 333 if (isset($this->request->post['geocode'])) { 334 $data['geocode'] = $this->request->post['geocode']; 335 } elseif (!empty($location_info)) { 336 $data['geocode'] = $location_info['geocode']; 337 } else { 338 $data['geocode'] = ''; 339 } 340 341 if (isset($this->request->post['telephone'])) { 342 $data['telephone'] = $this->request->post['telephone']; 343 } elseif (!empty($location_info)) { 344 $data['telephone'] = $location_info['telephone']; 345 } else { 346 $data['telephone'] = ''; 347 } 348 349 if (isset($this->request->post['fax'])) { 350 $data['fax'] = $this->request->post['fax']; 351 } elseif (!empty($location_info)) { 352 $data['fax'] = $location_info['fax']; 353 } else { 354 $data['fax'] = ''; 355 } 356 357 if (isset($this->request->post['image'])) { 358 $data['image'] = $this->request->post['image']; 359 } elseif (!empty($location_info)) { 360 $data['image'] = $location_info['image']; 361 } else { 362 $data['image'] = ''; 363 } 364 365 $this->load->model('tool/image'); 366 367 if (isset($this->request->post['image']) && is_file(DIR_IMAGE . $this->request->post['image'])) { 368 $data['thumb'] = $this->model_tool_image->resize($this->request->post['image'], 100, 100); 369 } elseif (!empty($location_info) && is_file(DIR_IMAGE . $location_info['image'])) { 370 $data['thumb'] = $this->model_tool_image->resize($location_info['image'], 100, 100); 371 } else { 372 $data['thumb'] = $this->model_tool_image->resize('no_image.png', 100, 100); 373 } 374 375 $data['placeholder'] = $this->model_tool_image->resize('no_image.png', 100, 100); 376 377 if (isset($this->request->post['open'])) { 378 $data['open'] = $this->request->post['open']; 379 } elseif (!empty($location_info)) { 380 $data['open'] = $location_info['open']; 381 } else { 382 $data['open'] = ''; 383 } 384 385 if (isset($this->request->post['comment'])) { 386 $data['comment'] = $this->request->post['comment']; 387 } elseif (!empty($location_info)) { 388 $data['comment'] = $location_info['comment']; 389 } else { 390 $data['comment'] = ''; 391 } 392 393 $data['header'] = $this->load->controller('common/header'); 394 $data['column_left'] = $this->load->controller('common/column_left'); 395 $data['footer'] = $this->load->controller('common/footer'); 396 397 $this->response->setOutput($this->load->view('localisation/location_form', $data)); 398 } 399 400 protected function validateForm() { 401 if (!$this->user->hasPermission('modify', 'localisation/location')) { 402 $this->error['warning'] = $this->language->get('error_permission'); 403 } 404 405 if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 32)) { 406 $this->error['name'] = $this->language->get('error_name'); 407 } 408 409 if ((utf8_strlen($this->request->post['address']) < 3) || (utf8_strlen($this->request->post['address']) > 128)) { 410 $this->error['address'] = $this->language->get('error_address'); 411 } 412 413 if ((utf8_strlen($this->request->post['telephone']) < 3) || (utf8_strlen($this->request->post['telephone']) > 32)) { 414 $this->error['telephone'] = $this->language->get('error_telephone'); 415 } 416 417 return !$this->error; 418 } 419 420 protected function validateDelete() { 421 if (!$this->user->hasPermission('modify', 'localisation/location')) { 422 $this->error['warning'] = $this->language->get('error_permission'); 423 } 424 425 return !$this->error; 426 } 427 }