/upload/catalog/controller/checkout/register.php

https://bitbucket.org/mjalajel/opencart · PHP · 242 lines · 178 code · 59 blank · 5 comment · 62 complexity · daf779335924a66dbf804bba6aaafd94 MD5 · raw file

  1. <?php
  2. class ControllerCheckoutRegister extends Controller {
  3. public function index() {
  4. $this->language->load('checkout/checkout');
  5. $this->data['text_checkout_payment_address'] = $this->language->get('text_checkout_payment_address');
  6. $this->data['text_your_details'] = $this->language->get('text_your_details');
  7. $this->data['text_your_address'] = $this->language->get('text_your_address');
  8. $this->data['text_your_password'] = $this->language->get('text_your_password');
  9. $this->data['text_select'] = $this->language->get('text_select');
  10. $this->data['text_none'] = $this->language->get('text_none');
  11. $this->data['text_modify'] = $this->language->get('text_modify');
  12. $this->data['entry_firstname'] = $this->language->get('entry_firstname');
  13. $this->data['entry_lastname'] = $this->language->get('entry_lastname');
  14. $this->data['entry_email'] = $this->language->get('entry_email');
  15. $this->data['entry_telephone'] = $this->language->get('entry_telephone');
  16. $this->data['entry_fax'] = $this->language->get('entry_fax');
  17. $this->data['entry_company'] = $this->language->get('entry_company');
  18. $this->data['entry_customer_group'] = $this->language->get('entry_customer_group');
  19. $this->data['entry_address_1'] = $this->language->get('entry_address_1');
  20. $this->data['entry_address_2'] = $this->language->get('entry_address_2');
  21. $this->data['entry_postcode'] = $this->language->get('entry_postcode');
  22. $this->data['entry_city'] = $this->language->get('entry_city');
  23. $this->data['entry_country'] = $this->language->get('entry_country');
  24. $this->data['entry_zone'] = $this->language->get('entry_zone');
  25. $this->data['entry_newsletter'] = sprintf($this->language->get('entry_newsletter'), $this->config->get('config_name'));
  26. $this->data['entry_password'] = $this->language->get('entry_password');
  27. $this->data['entry_confirm'] = $this->language->get('entry_confirm');
  28. $this->data['entry_shipping'] = $this->language->get('entry_shipping');
  29. $this->data['button_continue'] = $this->language->get('button_continue');
  30. $this->data['customer_groups'] = array();
  31. if (is_array($this->config->get('config_customer_group_display'))) {
  32. $this->load->model('account/customer_group');
  33. $customer_groups = $this->model_account_customer_group->getCustomerGroups();
  34. foreach ($customer_groups as $customer_group) {
  35. if (in_array($customer_group['customer_group_id'], $this->config->get('config_customer_group_display'))) {
  36. $this->data['customer_groups'][] = $customer_group;
  37. }
  38. }
  39. }
  40. $this->data['customer_group_id'] = $this->config->get('config_customer_group_id');
  41. if (isset($this->session->data['shipping_addess']['postcode'])) {
  42. $this->data['postcode'] = $this->session->data['shipping_addess']['postcode'];
  43. } else {
  44. $this->data['postcode'] = '';
  45. }
  46. if (isset($this->session->data['shipping_addess']['country_id'])) {
  47. $this->data['country_id'] = $this->session->data['shipping_addess']['country_id'];
  48. } else {
  49. $this->data['country_id'] = $this->config->get('config_country_id');
  50. }
  51. if (isset($this->session->data['shipping_addess']['zone_id'])) {
  52. $this->data['zone_id'] = $this->session->data['shipping_addess']['zone_id'];
  53. } else {
  54. $this->data['zone_id'] = '';
  55. }
  56. $this->load->model('localisation/country');
  57. $this->data['countries'] = $this->model_localisation_country->getCountries();
  58. if ($this->config->get('config_account_id')) {
  59. $this->load->model('catalog/information');
  60. $information_info = $this->model_catalog_information->getInformation($this->config->get('config_account_id'));
  61. if ($information_info) {
  62. $this->data['text_agree'] = sprintf($this->language->get('text_agree'), $this->url->link('information/information/info', 'information_id=' . $this->config->get('config_account_id'), 'SSL'), $information_info['title'], $information_info['title']);
  63. } else {
  64. $this->data['text_agree'] = '';
  65. }
  66. } else {
  67. $this->data['text_agree'] = '';
  68. }
  69. $this->data['shipping_required'] = $this->cart->hasShipping();
  70. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/checkout/register.tpl')) {
  71. $this->template = $this->config->get('config_template') . '/template/checkout/register.tpl';
  72. } else {
  73. $this->template = 'default/template/checkout/register.tpl';
  74. }
  75. $this->response->setOutput($this->render());
  76. }
  77. public function save() {
  78. $this->language->load('checkout/checkout');
  79. $json = array();
  80. // Validate if customer is already logged out.
  81. if ($this->customer->isLogged()) {
  82. $json['redirect'] = $this->url->link('checkout/checkout', '', 'SSL');
  83. }
  84. // Validate cart has products and has stock.
  85. if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
  86. $json['redirect'] = $this->url->link('checkout/cart');
  87. }
  88. // Validate minimum quantity requirments.
  89. $products = $this->cart->getProducts();
  90. foreach ($products as $product) {
  91. $product_total = 0;
  92. foreach ($products as $product_2) {
  93. if ($product_2['product_id'] == $product['product_id']) {
  94. $product_total += $product_2['quantity'];
  95. }
  96. }
  97. if ($product['minimum'] > $product_total) {
  98. $json['redirect'] = $this->url->link('checkout/cart');
  99. break;
  100. }
  101. }
  102. if (!$json) {
  103. $this->load->model('account/customer');
  104. if ((utf8_strlen($this->request->post['firstname']) < 1) || (utf8_strlen($this->request->post['firstname']) > 32)) {
  105. $json['error']['firstname'] = $this->language->get('error_firstname');
  106. }
  107. if ((utf8_strlen($this->request->post['lastname']) < 1) || (utf8_strlen($this->request->post['lastname']) > 32)) {
  108. $json['error']['lastname'] = $this->language->get('error_lastname');
  109. }
  110. if ((utf8_strlen($this->request->post['email']) > 96) || !preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $this->request->post['email'])) {
  111. $json['error']['email'] = $this->language->get('error_email');
  112. }
  113. if ($this->model_account_customer->getTotalCustomersByEmail($this->request->post['email'])) {
  114. $json['error']['warning'] = $this->language->get('error_exists');
  115. }
  116. if ((utf8_strlen($this->request->post['telephone']) < 3) || (utf8_strlen($this->request->post['telephone']) > 32)) {
  117. $json['error']['telephone'] = $this->language->get('error_telephone');
  118. }
  119. // Customer Group
  120. $this->load->model('account/customer_group');
  121. if (isset($this->request->post['customer_group_id']) && is_array($this->config->get('config_customer_group_display')) && in_array($this->request->post['customer_group_id'], $this->config->get('config_customer_group_display'))) {
  122. $customer_group_id = $this->request->post['customer_group_id'];
  123. } else {
  124. $customer_group_id = $this->config->get('config_customer_group_id');
  125. }
  126. $customer_group = $this->model_account_customer_group->getCustomerGroup($customer_group_id);
  127. if ($customer_group) {
  128. }
  129. if ((utf8_strlen($this->request->post['address_1']) < 3) || (utf8_strlen($this->request->post['address_1']) > 128)) {
  130. $json['error']['address_1'] = $this->language->get('error_address_1');
  131. }
  132. if ((utf8_strlen($this->request->post['city']) < 2) || (utf8_strlen($this->request->post['city']) > 128)) {
  133. $json['error']['city'] = $this->language->get('error_city');
  134. }
  135. $this->load->model('localisation/country');
  136. $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);
  137. if ($country_info && $country_info['postcode_required'] && (utf8_strlen($this->request->post['postcode']) < 2) || (utf8_strlen($this->request->post['postcode']) > 10)) {
  138. $json['error']['postcode'] = $this->language->get('error_postcode');
  139. }
  140. if ($this->request->post['country_id'] == '') {
  141. $json['error']['country'] = $this->language->get('error_country');
  142. }
  143. if (!isset($this->request->post['zone_id']) || $this->request->post['zone_id'] == '') {
  144. $json['error']['zone'] = $this->language->get('error_zone');
  145. }
  146. if ((utf8_strlen($this->request->post['password']) < 4) || (utf8_strlen($this->request->post['password']) > 20)) {
  147. $json['error']['password'] = $this->language->get('error_password');
  148. }
  149. if ($this->request->post['confirm'] != $this->request->post['password']) {
  150. $json['error']['confirm'] = $this->language->get('error_confirm');
  151. }
  152. if ($this->config->get('config_account_id')) {
  153. $this->load->model('catalog/information');
  154. $information_info = $this->model_catalog_information->getInformation($this->config->get('config_account_id'));
  155. if ($information_info && !isset($this->request->post['agree'])) {
  156. $json['error']['warning'] = sprintf($this->language->get('error_agree'), $information_info['title']);
  157. }
  158. }
  159. }
  160. if (!$json) {
  161. $this->model_account_customer->addCustomer($this->request->post);
  162. $this->session->data['account'] = 'register';
  163. if ($customer_group && !$customer_group['approval']) {
  164. $this->customer->login($this->request->post['email'], $this->request->post['password']);
  165. // Default Payment Address
  166. $this->load->model('account/address');
  167. $this->session->data['payment_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
  168. if (!empty($this->request->post['shipping_address'])) {
  169. $this->session->data['shipping_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
  170. }
  171. } else {
  172. $json['redirect'] = $this->url->link('account/success');
  173. }
  174. unset($this->session->data['guest']);
  175. unset($this->session->data['shipping_method']);
  176. unset($this->session->data['shipping_methods']);
  177. unset($this->session->data['payment_method']);
  178. unset($this->session->data['payment_methods']);
  179. }
  180. $this->response->setOutput(json_encode($json));
  181. }
  182. }
  183. ?>