PageRenderTime 23ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/catalog/controller/checkout/payment_address.php

https://gitlab.com/reclamare/mao
PHP | 192 lines | 138 code | 47 blank | 7 comment | 48 complexity | 144e96447720437081e474228c630589 MD5 | raw file
  1. <?php
  2. class ControllerCheckoutPaymentAddress extends Controller {
  3. public function index() {
  4. $this->load->language('checkout/checkout');
  5. $data['text_address_existing'] = $this->language->get('text_address_existing');
  6. $data['text_address_new'] = $this->language->get('text_address_new');
  7. $data['text_select'] = $this->language->get('text_select');
  8. $data['text_none'] = $this->language->get('text_none');
  9. $data['text_loading'] = $this->language->get('text_loading');
  10. $data['entry_firstname'] = $this->language->get('entry_firstname');
  11. $data['entry_lastname'] = $this->language->get('entry_lastname');
  12. $data['entry_company'] = $this->language->get('entry_company');
  13. $data['entry_address_1'] = $this->language->get('entry_address_1');
  14. $data['entry_address_2'] = $this->language->get('entry_address_2');
  15. $data['entry_postcode'] = $this->language->get('entry_postcode');
  16. $data['entry_city'] = $this->language->get('entry_city');
  17. $data['entry_country'] = $this->language->get('entry_country');
  18. $data['entry_zone'] = $this->language->get('entry_zone');
  19. $data['button_continue'] = $this->language->get('button_continue');
  20. $data['button_upload'] = $this->language->get('button_upload');
  21. if (isset($this->session->data['payment_address']['address_id'])) {
  22. $data['address_id'] = $this->session->data['payment_address']['address_id'];
  23. } else {
  24. $data['address_id'] = $this->customer->getAddressId();
  25. }
  26. $this->load->model('account/address');
  27. $data['addresses'] = $this->model_account_address->getAddresses();
  28. if (isset($this->session->data['payment_address']['country_id'])) {
  29. $data['country_id'] = $this->session->data['payment_address']['country_id'];
  30. } else {
  31. $data['country_id'] = $this->config->get('config_country_id');
  32. }
  33. if (isset($this->session->data['payment_address']['zone_id'])) {
  34. $data['zone_id'] = $this->session->data['payment_address']['zone_id'];
  35. } else {
  36. $data['zone_id'] = '';
  37. }
  38. $this->load->model('localisation/country');
  39. $data['countries'] = $this->model_localisation_country->getCountries();
  40. // Custom Fields
  41. $this->load->model('account/custom_field');
  42. $data['custom_fields'] = $this->model_account_custom_field->getCustomFields($this->config->get('config_customer_group_id'));
  43. if (isset($this->session->data['payment_address']['custom_field'])) {
  44. $data['payment_address_custom_field'] = $this->session->data['payment_address']['custom_field'];
  45. } else {
  46. $data['payment_address_custom_field'] = array();
  47. }
  48. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/checkout/payment_address.tpl')) {
  49. $this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/checkout/payment_address.tpl', $data));
  50. } else {
  51. $this->response->setOutput($this->load->view('default/template/checkout/payment_address.tpl', $data));
  52. }
  53. }
  54. public function save() {
  55. $this->load->language('checkout/checkout');
  56. $json = array();
  57. // Validate if customer is logged in.
  58. if (!$this->customer->isLogged()) {
  59. $json['redirect'] = $this->url->link('checkout/checkout', '', 'SSL');
  60. }
  61. // Validate cart has products and has stock.
  62. if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
  63. $json['redirect'] = $this->url->link('checkout/cart');
  64. }
  65. // Validate minimum quantity requirements.
  66. $products = $this->cart->getProducts();
  67. foreach ($products as $product) {
  68. $product_total = 0;
  69. foreach ($products as $product_2) {
  70. if ($product_2['product_id'] == $product['product_id']) {
  71. $product_total += $product_2['quantity'];
  72. }
  73. }
  74. if ($product['minimum'] > $product_total) {
  75. $json['redirect'] = $this->url->link('checkout/cart');
  76. break;
  77. }
  78. }
  79. if (!$json) {
  80. if (isset($this->request->post['payment_address']) && $this->request->post['payment_address'] == 'existing') {
  81. $this->load->model('account/address');
  82. if (empty($this->request->post['address_id'])) {
  83. $json['error']['warning'] = $this->language->get('error_address');
  84. } elseif (!in_array($this->request->post['address_id'], array_keys($this->model_account_address->getAddresses()))) {
  85. $json['error']['warning'] = $this->language->get('error_address');
  86. }
  87. if (!$json) {
  88. // Default Payment Address
  89. $this->load->model('account/address');
  90. $this->session->data['payment_address'] = $this->model_account_address->getAddress($this->request->post['address_id']);
  91. unset($this->session->data['payment_method']);
  92. unset($this->session->data['payment_methods']);
  93. }
  94. } else {
  95. if ((utf8_strlen(trim($this->request->post['firstname'])) < 1) || (utf8_strlen(trim($this->request->post['firstname'])) > 32)) {
  96. $json['error']['firstname'] = $this->language->get('error_firstname');
  97. }
  98. if ((utf8_strlen(trim($this->request->post['lastname'])) < 1) || (utf8_strlen(trim($this->request->post['lastname'])) > 32)) {
  99. $json['error']['lastname'] = $this->language->get('error_lastname');
  100. }
  101. if ((utf8_strlen(trim($this->request->post['address_1'])) < 3) || (utf8_strlen(trim($this->request->post['address_1'])) > 128)) {
  102. $json['error']['address_1'] = $this->language->get('error_address_1');
  103. }
  104. if ((utf8_strlen($this->request->post['city']) < 2) || (utf8_strlen($this->request->post['city']) > 32)) {
  105. $json['error']['city'] = $this->language->get('error_city');
  106. }
  107. $this->load->model('localisation/country');
  108. $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);
  109. if ($country_info && $country_info['postcode_required'] && (utf8_strlen(trim($this->request->post['postcode'])) < 2 || utf8_strlen(trim($this->request->post['postcode'])) > 10)) {
  110. $json['error']['postcode'] = $this->language->get('error_postcode');
  111. }
  112. if ($this->request->post['country_id'] == '') {
  113. $json['error']['country'] = $this->language->get('error_country');
  114. }
  115. if (!isset($this->request->post['zone_id']) || $this->request->post['zone_id'] == '' || !is_numeric($this->request->post['zone_id'])) {
  116. $json['error']['zone'] = $this->language->get('error_zone');
  117. }
  118. // Custom field validation
  119. $this->load->model('account/custom_field');
  120. $custom_fields = $this->model_account_custom_field->getCustomFields($this->config->get('config_customer_group_id'));
  121. foreach ($custom_fields as $custom_field) {
  122. if (($custom_field['location'] == 'address') && $custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
  123. $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
  124. }
  125. }
  126. if (!$json) {
  127. // Default Payment Address
  128. $this->load->model('account/address');
  129. $address_id = $this->model_account_address->addAddress($this->request->post);
  130. $this->session->data['payment_address'] = $this->model_account_address->getAddress($address_id);
  131. unset($this->session->data['payment_method']);
  132. unset($this->session->data['payment_methods']);
  133. $this->load->model('account/activity');
  134. $activity_data = array(
  135. 'customer_id' => $this->customer->getId(),
  136. 'name' => $this->customer->getFirstName() . ' ' . $this->customer->getLastName()
  137. );
  138. $this->model_account_activity->addActivity('address_add', $activity_data);
  139. }
  140. }
  141. }
  142. $this->response->addHeader('Content-Type: application/json');
  143. $this->response->setOutput(json_encode($json));
  144. }
  145. }