/catalog/controller/account/address.php

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