/catalog/controller/account/address.php

https://gitlab.com/shapcy/opencart · PHP · 541 lines · 410 code · 122 blank · 9 comment · 108 complexity · fbb4f4a2bd98e7b5300667cd4faa0a89 MD5 · raw file

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