address.php (18228B)
1 <?php 2 class ControllerAccountAddress extends Controller { 3 private $error = array(); 4 5 public function index() { 6 if (!$this->customer->isLogged()) { 7 $this->session->data['redirect'] = $this->url->link('account/address', '', true); 8 9 $this->response->redirect($this->url->link('account/login', '', true)); 10 } 11 12 $this->load->language('account/address'); 13 14 $this->document->setTitle($this->language->get('heading_title')); 15 16 $this->load->model('account/address'); 17 18 $this->getList(); 19 } 20 21 public function add() { 22 if (!$this->customer->isLogged()) { 23 $this->session->data['redirect'] = $this->url->link('account/address', '', true); 24 25 $this->response->redirect($this->url->link('account/login', '', true)); 26 } 27 28 $this->load->language('account/address'); 29 30 $this->document->setTitle($this->language->get('heading_title')); 31 32 $this->document->addScript('catalog/view/javascript/jquery/datetimepicker/moment/moment.min.js'); 33 $this->document->addScript('catalog/view/javascript/jquery/datetimepicker/moment/moment-with-locales.min.js'); 34 $this->document->addScript('catalog/view/javascript/jquery/datetimepicker/bootstrap-datetimepicker.min.js'); 35 $this->document->addStyle('catalog/view/javascript/jquery/datetimepicker/bootstrap-datetimepicker.min.css'); 36 37 $this->load->model('account/address'); 38 39 if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { 40 $this->model_account_address->addAddress($this->customer->getId(), $this->request->post); 41 42 $this->session->data['success'] = $this->language->get('text_add'); 43 44 $this->response->redirect($this->url->link('account/address', '', true)); 45 } 46 47 $this->getForm(); 48 } 49 50 public function edit() { 51 if (!$this->customer->isLogged()) { 52 $this->session->data['redirect'] = $this->url->link('account/address', '', true); 53 54 $this->response->redirect($this->url->link('account/login', '', true)); 55 } 56 57 $this->load->language('account/address'); 58 59 $this->document->setTitle($this->language->get('heading_title')); 60 61 $this->document->addScript('catalog/view/javascript/jquery/datetimepicker/moment/moment.min.js'); 62 $this->document->addScript('catalog/view/javascript/jquery/datetimepicker/moment/moment-with-locales.min.js'); 63 $this->document->addScript('catalog/view/javascript/jquery/datetimepicker/bootstrap-datetimepicker.min.js'); 64 $this->document->addStyle('catalog/view/javascript/jquery/datetimepicker/bootstrap-datetimepicker.min.css'); 65 66 $this->load->model('account/address'); 67 68 if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) { 69 $this->model_account_address->editAddress($this->request->get['address_id'], $this->request->post); 70 71 // Default Shipping Address 72 if (isset($this->session->data['shipping_address']['address_id']) && ($this->request->get['address_id'] == $this->session->data['shipping_address']['address_id'])) { 73 $this->session->data['shipping_address'] = $this->model_account_address->getAddress($this->request->get['address_id']); 74 75 unset($this->session->data['shipping_method']); 76 unset($this->session->data['shipping_methods']); 77 } 78 79 // Default Payment Address 80 if (isset($this->session->data['payment_address']['address_id']) && ($this->request->get['address_id'] == $this->session->data['payment_address']['address_id'])) { 81 $this->session->data['payment_address'] = $this->model_account_address->getAddress($this->request->get['address_id']); 82 83 unset($this->session->data['payment_method']); 84 unset($this->session->data['payment_methods']); 85 } 86 87 $this->session->data['success'] = $this->language->get('text_edit'); 88 89 $this->response->redirect($this->url->link('account/address', '', true)); 90 } 91 92 $this->getForm(); 93 } 94 95 public function delete() { 96 if (!$this->customer->isLogged()) { 97 $this->session->data['redirect'] = $this->url->link('account/address', '', true); 98 99 $this->response->redirect($this->url->link('account/login', '', true)); 100 } 101 102 $this->load->language('account/address'); 103 104 $this->document->setTitle($this->language->get('heading_title')); 105 106 $this->load->model('account/address'); 107 108 if (isset($this->request->get['address_id']) && $this->validateDelete()) { 109 $this->model_account_address->deleteAddress($this->request->get['address_id']); 110 111 // Default Shipping Address 112 if (isset($this->session->data['shipping_address']['address_id']) && ($this->request->get['address_id'] == $this->session->data['shipping_address']['address_id'])) { 113 unset($this->session->data['shipping_address']); 114 unset($this->session->data['shipping_method']); 115 unset($this->session->data['shipping_methods']); 116 } 117 118 // Default Payment Address 119 if (isset($this->session->data['payment_address']['address_id']) && ($this->request->get['address_id'] == $this->session->data['payment_address']['address_id'])) { 120 unset($this->session->data['payment_address']); 121 unset($this->session->data['payment_method']); 122 unset($this->session->data['payment_methods']); 123 } 124 125 $this->session->data['success'] = $this->language->get('text_delete'); 126 127 $this->response->redirect($this->url->link('account/address', '', true)); 128 } 129 130 $this->getList(); 131 } 132 133 protected function getList() { 134 $data['breadcrumbs'][] = array( 135 'text' => $this->language->get('text_home'), 136 'href' => $this->url->link('common/home') 137 ); 138 139 $data['breadcrumbs'][] = array( 140 'text' => $this->language->get('text_account'), 141 'href' => $this->url->link('account/account', '', true) 142 ); 143 144 $data['breadcrumbs'][] = array( 145 'text' => $this->language->get('heading_title'), 146 'href' => $this->url->link('account/address', '', true) 147 ); 148 149 if (isset($this->error['warning'])) { 150 $data['error_warning'] = $this->error['warning']; 151 } else { 152 $data['error_warning'] = ''; 153 } 154 155 if (isset($this->session->data['success'])) { 156 $data['success'] = $this->session->data['success']; 157 158 unset($this->session->data['success']); 159 } else { 160 $data['success'] = ''; 161 } 162 163 $data['addresses'] = array(); 164 165 $results = $this->model_account_address->getAddresses(); 166 167 foreach ($results as $result) { 168 if ($result['address_format']) { 169 $format = $result['address_format']; 170 } else { 171 $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}'; 172 } 173 174 $find = array( 175 '{firstname}', 176 '{lastname}', 177 '{company}', 178 '{address_1}', 179 '{address_2}', 180 '{city}', 181 '{postcode}', 182 '{zone}', 183 '{zone_code}', 184 '{country}' 185 ); 186 187 $replace = array( 188 'firstname' => $result['firstname'], 189 'lastname' => $result['lastname'], 190 'company' => $result['company'], 191 'address_1' => $result['address_1'], 192 'address_2' => $result['address_2'], 193 'city' => $result['city'], 194 'postcode' => $result['postcode'], 195 'zone' => $result['zone'], 196 'zone_code' => $result['zone_code'], 197 'country' => $result['country'] 198 ); 199 200 $data['addresses'][] = array( 201 'address_id' => $result['address_id'], 202 'address' => str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format)))), 203 'update' => $this->url->link('account/address/edit', 'address_id=' . $result['address_id'], true), 204 'delete' => $this->url->link('account/address/delete', 'address_id=' . $result['address_id'], true) 205 ); 206 } 207 208 $data['add'] = $this->url->link('account/address/add', '', true); 209 $data['back'] = $this->url->link('account/account', '', true); 210 211 $data['column_left'] = $this->load->controller('common/column_left'); 212 $data['column_right'] = $this->load->controller('common/column_right'); 213 $data['content_top'] = $this->load->controller('common/content_top'); 214 $data['content_bottom'] = $this->load->controller('common/content_bottom'); 215 $data['footer'] = $this->load->controller('common/footer'); 216 $data['header'] = $this->load->controller('common/header'); 217 218 $this->response->setOutput($this->load->view('account/address_list', $data)); 219 } 220 221 protected function getForm() { 222 $data['breadcrumbs'] = array(); 223 224 $data['breadcrumbs'][] = array( 225 'text' => $this->language->get('text_home'), 226 'href' => $this->url->link('common/home') 227 ); 228 229 $data['breadcrumbs'][] = array( 230 'text' => $this->language->get('text_account'), 231 'href' => $this->url->link('account/account', '', true) 232 ); 233 234 $data['breadcrumbs'][] = array( 235 'text' => $this->language->get('heading_title'), 236 'href' => $this->url->link('account/address', '', true) 237 ); 238 239 if (!isset($this->request->get['address_id'])) { 240 $data['breadcrumbs'][] = array( 241 'text' => $this->language->get('text_address_add'), 242 'href' => $this->url->link('account/address/add', '', true) 243 ); 244 } else { 245 $data['breadcrumbs'][] = array( 246 'text' => $this->language->get('text_address_edit'), 247 'href' => $this->url->link('account/address/edit', 'address_id=' . $this->request->get['address_id'], true) 248 ); 249 } 250 251 $data['text_address'] = !isset($this->request->get['address_id']) ? $this->language->get('text_address_add') : $this->language->get('text_address_edit'); 252 253 if (isset($this->error['firstname'])) { 254 $data['error_firstname'] = $this->error['firstname']; 255 } else { 256 $data['error_firstname'] = ''; 257 } 258 259 if (isset($this->error['lastname'])) { 260 $data['error_lastname'] = $this->error['lastname']; 261 } else { 262 $data['error_lastname'] = ''; 263 } 264 265 if (isset($this->error['address_1'])) { 266 $data['error_address_1'] = $this->error['address_1']; 267 } else { 268 $data['error_address_1'] = ''; 269 } 270 271 if (isset($this->error['city'])) { 272 $data['error_city'] = $this->error['city']; 273 } else { 274 $data['error_city'] = ''; 275 } 276 277 if (isset($this->error['postcode'])) { 278 $data['error_postcode'] = $this->error['postcode']; 279 } else { 280 $data['error_postcode'] = ''; 281 } 282 283 if (isset($this->error['country'])) { 284 $data['error_country'] = $this->error['country']; 285 } else { 286 $data['error_country'] = ''; 287 } 288 289 if (isset($this->error['zone'])) { 290 $data['error_zone'] = $this->error['zone']; 291 } else { 292 $data['error_zone'] = ''; 293 } 294 295 if (isset($this->error['custom_field'])) { 296 $data['error_custom_field'] = $this->error['custom_field']; 297 } else { 298 $data['error_custom_field'] = array(); 299 } 300 301 if (!isset($this->request->get['address_id'])) { 302 $data['action'] = $this->url->link('account/address/add', '', true); 303 } else { 304 $data['action'] = $this->url->link('account/address/edit', 'address_id=' . $this->request->get['address_id'], true); 305 } 306 307 if (isset($this->request->get['address_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) { 308 $address_info = $this->model_account_address->getAddress($this->request->get['address_id']); 309 } 310 311 if (isset($this->request->post['firstname'])) { 312 $data['firstname'] = $this->request->post['firstname']; 313 } elseif (!empty($address_info)) { 314 $data['firstname'] = $address_info['firstname']; 315 } else { 316 $data['firstname'] = ''; 317 } 318 319 if (isset($this->request->post['lastname'])) { 320 $data['lastname'] = $this->request->post['lastname']; 321 } elseif (!empty($address_info)) { 322 $data['lastname'] = $address_info['lastname']; 323 } else { 324 $data['lastname'] = ''; 325 } 326 327 if (isset($this->request->post['company'])) { 328 $data['company'] = $this->request->post['company']; 329 } elseif (!empty($address_info)) { 330 $data['company'] = $address_info['company']; 331 } else { 332 $data['company'] = ''; 333 } 334 335 if (isset($this->request->post['address_1'])) { 336 $data['address_1'] = $this->request->post['address_1']; 337 } elseif (!empty($address_info)) { 338 $data['address_1'] = $address_info['address_1']; 339 } else { 340 $data['address_1'] = ''; 341 } 342 343 if (isset($this->request->post['address_2'])) { 344 $data['address_2'] = $this->request->post['address_2']; 345 } elseif (!empty($address_info)) { 346 $data['address_2'] = $address_info['address_2']; 347 } else { 348 $data['address_2'] = ''; 349 } 350 351 if (isset($this->request->post['postcode'])) { 352 $data['postcode'] = $this->request->post['postcode']; 353 } elseif (!empty($address_info)) { 354 $data['postcode'] = $address_info['postcode']; 355 } else { 356 $data['postcode'] = ''; 357 } 358 359 if (isset($this->request->post['city'])) { 360 $data['city'] = $this->request->post['city']; 361 } elseif (!empty($address_info)) { 362 $data['city'] = $address_info['city']; 363 } else { 364 $data['city'] = ''; 365 } 366 367 if (isset($this->request->post['country_id'])) { 368 $data['country_id'] = (int)$this->request->post['country_id']; 369 } elseif (!empty($address_info)) { 370 $data['country_id'] = $address_info['country_id']; 371 } else { 372 $data['country_id'] = $this->config->get('config_country_id'); 373 } 374 375 if (isset($this->request->post['zone_id'])) { 376 $data['zone_id'] = (int)$this->request->post['zone_id']; 377 } elseif (!empty($address_info)) { 378 $data['zone_id'] = $address_info['zone_id']; 379 } else { 380 $data['zone_id'] = ''; 381 } 382 383 $this->load->model('localisation/country'); 384 385 $data['countries'] = $this->model_localisation_country->getCountries(); 386 387 // Custom fields 388 $data['custom_fields'] = array(); 389 390 $this->load->model('account/custom_field'); 391 392 $custom_fields = $this->model_account_custom_field->getCustomFields($this->config->get('config_customer_group_id')); 393 394 foreach ($custom_fields as $custom_field) { 395 if ($custom_field['location'] == 'address') { 396 $data['custom_fields'][] = $custom_field; 397 } 398 } 399 400 if (isset($this->request->post['custom_field']['address'])) { 401 $data['address_custom_field'] = $this->request->post['custom_field']['address']; 402 } elseif (isset($address_info)) { 403 $data['address_custom_field'] = $address_info['custom_field']; 404 } else { 405 $data['address_custom_field'] = array(); 406 } 407 408 if (isset($this->request->post['default'])) { 409 $data['default'] = $this->request->post['default']; 410 } elseif (isset($this->request->get['address_id'])) { 411 $data['default'] = $this->customer->getAddressId() == $this->request->get['address_id']; 412 } else { 413 $data['default'] = false; 414 } 415 416 $data['back'] = $this->url->link('account/address', '', true); 417 418 $data['column_left'] = $this->load->controller('common/column_left'); 419 $data['column_right'] = $this->load->controller('common/column_right'); 420 $data['content_top'] = $this->load->controller('common/content_top'); 421 $data['content_bottom'] = $this->load->controller('common/content_bottom'); 422 $data['footer'] = $this->load->controller('common/footer'); 423 $data['header'] = $this->load->controller('common/header'); 424 425 $this->response->setOutput($this->load->view('account/address_form', $data)); 426 } 427 428 protected function validateForm() { 429 if ((utf8_strlen(trim($this->request->post['firstname'])) < 1) || (utf8_strlen(trim($this->request->post['firstname'])) > 32)) { 430 $this->error['firstname'] = $this->language->get('error_firstname'); 431 } 432 433 if ((utf8_strlen(trim($this->request->post['lastname'])) < 1) || (utf8_strlen(trim($this->request->post['lastname'])) > 32)) { 434 $this->error['lastname'] = $this->language->get('error_lastname'); 435 } 436 437 if ((utf8_strlen(trim($this->request->post['address_1'])) < 3) || (utf8_strlen(trim($this->request->post['address_1'])) > 128)) { 438 $this->error['address_1'] = $this->language->get('error_address_1'); 439 } 440 441 if ((utf8_strlen(trim($this->request->post['city'])) < 2) || (utf8_strlen(trim($this->request->post['city'])) > 128)) { 442 $this->error['city'] = $this->language->get('error_city'); 443 } 444 445 $this->load->model('localisation/country'); 446 447 $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']); 448 449 if ($country_info && $country_info['postcode_required'] && (utf8_strlen(trim($this->request->post['postcode'])) < 2 || utf8_strlen(trim($this->request->post['postcode'])) > 10)) { 450 $this->error['postcode'] = $this->language->get('error_postcode'); 451 } 452 453 if ($this->request->post['country_id'] == '' || !is_numeric($this->request->post['country_id'])) { 454 $this->error['country'] = $this->language->get('error_country'); 455 } 456 457 if (!isset($this->request->post['zone_id']) || $this->request->post['zone_id'] == '' || !is_numeric($this->request->post['zone_id'])) { 458 $this->error['zone'] = $this->language->get('error_zone'); 459 } 460 461 // Custom field validation 462 $this->load->model('account/custom_field'); 463 464 $custom_fields = $this->model_account_custom_field->getCustomFields($this->config->get('config_customer_group_id')); 465 466 foreach ($custom_fields as $custom_field) { 467 if ($custom_field['location'] == 'address') { 468 if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']])) { 469 $this->error['custom_field'][$custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']); 470 } elseif (($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !filter_var($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $custom_field['validation'])))) { 471 $this->error['custom_field'][$custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']); 472 } 473 } 474 } 475 476 return !$this->error; 477 } 478 479 protected function validateDelete() { 480 if ($this->model_account_address->getTotalAddresses() == 1) { 481 $this->error['warning'] = $this->language->get('error_delete'); 482 } 483 484 if ($this->customer->getAddressId() == $this->request->get['address_id']) { 485 $this->error['warning'] = $this->language->get('error_default'); 486 } 487 488 return !$this->error; 489 } 490 }