/catalog/controller/affiliate/edit.php

https://github.com/sansanwawa/e-commerse · PHP · 337 lines · 271 code · 66 blank · 0 comment · 83 complexity · 9dd801c4f4aced6eca54ac383876b045 MD5 · raw file

  1. <?php
  2. class ControllerAffiliateEdit extends Controller {
  3. private $error = array();
  4. public function index() {
  5. if (!$this->affiliate->isLogged()) {
  6. $this->session->data['redirect'] = $this->url->link('affiliate/edit', '', 'SSL');
  7. $this->redirect($this->url->link('affiliate/login', '', 'SSL'));
  8. }
  9. $this->language->load('affiliate/edit');
  10. $this->document->setTitle($this->language->get('heading_title'));
  11. $this->load->model('affiliate/affiliate');
  12. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
  13. $this->model_affiliate_affiliate->editAffiliate($this->request->post);
  14. $this->session->data['success'] = $this->language->get('text_success');
  15. $this->redirect($this->url->link('affiliate/account', '', 'SSL'));
  16. }
  17. $this->data['breadcrumbs'] = array();
  18. $this->data['breadcrumbs'][] = array(
  19. 'text' => $this->language->get('text_home'),
  20. 'href' => $this->url->link('common/home'),
  21. 'separator' => false
  22. );
  23. $this->data['breadcrumbs'][] = array(
  24. 'text' => $this->language->get('text_account'),
  25. 'href' => $this->url->link('affiliate/account', '', 'SSL'),
  26. 'separator' => $this->language->get('text_separator')
  27. );
  28. $this->data['breadcrumbs'][] = array(
  29. 'text' => $this->language->get('text_edit'),
  30. 'href' => $this->url->link('affiliate/edit', '', 'SSL'),
  31. 'separator' => $this->language->get('text_separator')
  32. );
  33. $this->data['heading_title'] = $this->language->get('heading_title');
  34. $this->data['text_select'] = $this->language->get('text_select');
  35. $this->data['text_your_details'] = $this->language->get('text_your_details');
  36. $this->data['text_your_address'] = $this->language->get('text_your_address');
  37. $this->data['entry_firstname'] = $this->language->get('entry_firstname');
  38. $this->data['entry_lastname'] = $this->language->get('entry_lastname');
  39. $this->data['entry_email'] = $this->language->get('entry_email');
  40. $this->data['entry_telephone'] = $this->language->get('entry_telephone');
  41. $this->data['entry_fax'] = $this->language->get('entry_fax');
  42. $this->data['entry_company'] = $this->language->get('entry_company');
  43. $this->data['entry_website'] = $this->language->get('entry_website');
  44. $this->data['entry_address_1'] = $this->language->get('entry_address_1');
  45. $this->data['entry_address_2'] = $this->language->get('entry_address_2');
  46. $this->data['entry_postcode'] = $this->language->get('entry_postcode');
  47. $this->data['entry_city'] = $this->language->get('entry_city');
  48. $this->data['entry_country'] = $this->language->get('entry_country');
  49. $this->data['entry_zone'] = $this->language->get('entry_zone');
  50. $this->data['button_continue'] = $this->language->get('button_continue');
  51. $this->data['button_back'] = $this->language->get('button_back');
  52. if (isset($this->error['warning'])) {
  53. $this->data['error_warning'] = $this->error['warning'];
  54. } else {
  55. $this->data['error_warning'] = '';
  56. }
  57. if (isset($this->error['firstname'])) {
  58. $this->data['error_firstname'] = $this->error['firstname'];
  59. } else {
  60. $this->data['error_firstname'] = '';
  61. }
  62. if (isset($this->error['lastname'])) {
  63. $this->data['error_lastname'] = $this->error['lastname'];
  64. } else {
  65. $this->data['error_lastname'] = '';
  66. }
  67. if (isset($this->error['email'])) {
  68. $this->data['error_email'] = $this->error['email'];
  69. } else {
  70. $this->data['error_email'] = '';
  71. }
  72. if (isset($this->error['telephone'])) {
  73. $this->data['error_telephone'] = $this->error['telephone'];
  74. } else {
  75. $this->data['error_telephone'] = '';
  76. }
  77. if (isset($this->error['address_1'])) {
  78. $this->data['error_address_1'] = $this->error['address_1'];
  79. } else {
  80. $this->data['error_address_1'] = '';
  81. }
  82. if (isset($this->error['city'])) {
  83. $this->data['error_city'] = $this->error['city'];
  84. } else {
  85. $this->data['error_city'] = '';
  86. }
  87. if (isset($this->error['postcode'])) {
  88. $this->data['error_postcode'] = $this->error['postcode'];
  89. } else {
  90. $this->data['error_postcode'] = '';
  91. }
  92. if (isset($this->error['country'])) {
  93. $this->data['error_country'] = $this->error['country'];
  94. } else {
  95. $this->data['error_country'] = '';
  96. }
  97. if (isset($this->error['zone'])) {
  98. $this->data['error_zone'] = $this->error['zone'];
  99. } else {
  100. $this->data['error_zone'] = '';
  101. }
  102. $this->data['action'] = $this->url->link('affiliate/edit', '', 'SSL');
  103. if ($this->request->server['REQUEST_METHOD'] != 'POST') {
  104. $affiliate_info = $this->model_affiliate_affiliate->getAffiliate($this->affiliate->getId());
  105. }
  106. if (isset($this->request->post['firstname'])) {
  107. $this->data['firstname'] = $this->request->post['firstname'];
  108. } elseif (isset($affiliate_info)) {
  109. $this->data['firstname'] = $affiliate_info['firstname'];
  110. } else {
  111. $this->data['firstname'] = '';
  112. }
  113. if (isset($this->request->post['lastname'])) {
  114. $this->data['lastname'] = $this->request->post['lastname'];
  115. } elseif (isset($affiliate_info)) {
  116. $this->data['lastname'] = $affiliate_info['lastname'];
  117. } else {
  118. $this->data['lastname'] = '';
  119. }
  120. if (isset($this->request->post['email'])) {
  121. $this->data['email'] = $this->request->post['email'];
  122. } elseif (isset($affiliate_info)) {
  123. $this->data['email'] = $affiliate_info['email'];
  124. } else {
  125. $this->data['email'] = '';
  126. }
  127. if (isset($this->request->post['telephone'])) {
  128. $this->data['telephone'] = $this->request->post['telephone'];
  129. } elseif (isset($affiliate_info)) {
  130. $this->data['telephone'] = $affiliate_info['telephone'];
  131. } else {
  132. $this->data['telephone'] = '';
  133. }
  134. if (isset($this->request->post['fax'])) {
  135. $this->data['fax'] = $this->request->post['fax'];
  136. } elseif (isset($affiliate_info)) {
  137. $this->data['fax'] = $affiliate_info['fax'];
  138. } else {
  139. $this->data['fax'] = '';
  140. }
  141. if (isset($this->request->post['company'])) {
  142. $this->data['company'] = $this->request->post['company'];
  143. } elseif (isset($affiliate_info)) {
  144. $this->data['company'] = $affiliate_info['company'];
  145. } else {
  146. $this->data['company'] = '';
  147. }
  148. if (isset($this->request->post['website'])) {
  149. $this->data['website'] = $this->request->post['website'];
  150. } elseif (isset($affiliate_info)) {
  151. $this->data['website'] = $affiliate_info['website'];
  152. } else {
  153. $this->data['website'] = '';
  154. }
  155. if (isset($this->request->post['address_1'])) {
  156. $this->data['address_1'] = $this->request->post['address_1'];
  157. } elseif (isset($affiliate_info)) {
  158. $this->data['address_1'] = $affiliate_info['address_1'];
  159. } else {
  160. $this->data['address_1'] = '';
  161. }
  162. if (isset($this->request->post['address_2'])) {
  163. $this->data['address_2'] = $this->request->post['address_2'];
  164. } elseif (isset($affiliate_info)) {
  165. $this->data['address_2'] = $affiliate_info['address_2'];
  166. } else {
  167. $this->data['address_2'] = '';
  168. }
  169. if (isset($this->request->post['postcode'])) {
  170. $this->data['postcode'] = $this->request->post['postcode'];
  171. } elseif (isset($affiliate_info)) {
  172. $this->data['postcode'] = $affiliate_info['postcode'];
  173. } else {
  174. $this->data['postcode'] = '';
  175. }
  176. if (isset($this->request->post['city'])) {
  177. $this->data['city'] = $this->request->post['city'];
  178. } elseif (isset($affiliate_info)) {
  179. $this->data['city'] = $affiliate_info['city'];
  180. } else {
  181. $this->data['city'] = '';
  182. }
  183. if (isset($this->request->post['country_id'])) {
  184. $this->data['country_id'] = $this->request->post['country_id'];
  185. } elseif (isset($affiliate_info)) {
  186. $this->data['country_id'] = $affiliate_info['country_id'];
  187. } else {
  188. $this->data['country_id'] = $this->config->get('config_country_id');
  189. }
  190. if (isset($this->request->post['zone_id'])) {
  191. $this->data['zone_id'] = $this->request->post['zone_id'];
  192. } elseif (isset($affiliate_info)) {
  193. $this->data['zone_id'] = $affiliate_info['zone_id'];
  194. } else {
  195. $this->data['zone_id'] = '';
  196. }
  197. $this->load->model('localisation/country');
  198. $this->data['countries'] = $this->model_localisation_country->getCountries();
  199. $this->data['back'] = $this->url->link('affiliate/account', '', 'SSL');
  200. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/affiliate/edit.tpl')) {
  201. $this->template = $this->config->get('config_template') . '/template/affiliate/edit.tpl';
  202. } else {
  203. $this->template = 'default/template/affiliate/edit.tpl';
  204. }
  205. $this->children = array(
  206. 'common/column_left',
  207. 'common/column_right',
  208. 'common/content_top',
  209. 'common/content_bottom',
  210. 'common/footer',
  211. 'common/header'
  212. );
  213. $this->response->setOutput($this->render());
  214. }
  215. private function validate() {
  216. if ((strlen(utf8_decode($this->request->post['firstname'])) < 1) || (strlen(utf8_decode($this->request->post['firstname'])) > 32)) {
  217. $this->error['firstname'] = $this->language->get('error_firstname');
  218. }
  219. if ((strlen(utf8_decode($this->request->post['lastname'])) < 1) || (strlen(utf8_decode($this->request->post['lastname'])) > 32)) {
  220. $this->error['lastname'] = $this->language->get('error_lastname');
  221. }
  222. if ((strlen(utf8_decode($this->request->post['email'])) > 96) || !preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $this->request->post['email'])) {
  223. $this->error['email'] = $this->language->get('error_email');
  224. }
  225. if (($this->affiliate->getEmail() != $this->request->post['email']) && $this->model_affiliate_affiliate->getTotalAffiliatesByEmail($this->request->post['email'])) {
  226. $this->error['warning'] = $this->language->get('error_exists');
  227. }
  228. if ((strlen(utf8_decode($this->request->post['telephone'])) < 3) || (strlen(utf8_decode($this->request->post['telephone'])) > 32)) {
  229. $this->error['telephone'] = $this->language->get('error_telephone');
  230. }
  231. if ((strlen(utf8_decode($this->request->post['address_1'])) < 3) || (strlen(utf8_decode($this->request->post['address_1'])) > 128)) {
  232. $this->error['address_1'] = $this->language->get('error_address_1');
  233. }
  234. if ((strlen(utf8_decode($this->request->post['city'])) < 2) || (strlen(utf8_decode($this->request->post['city'])) > 128)) {
  235. $this->error['city'] = $this->language->get('error_city');
  236. }
  237. $this->load->model('localisation/country');
  238. $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);
  239. if ($country_info && $country_info['postcode_required'] && (strlen(utf8_decode($this->request->post['postcode'])) < 2) || (strlen(utf8_decode($this->request->post['postcode'])) > 10)) {
  240. $this->error['postcode'] = $this->language->get('error_postcode');
  241. }
  242. if ($this->request->post['country_id'] == '') {
  243. $this->error['country'] = $this->language->get('error_country');
  244. }
  245. if ($this->request->post['zone_id'] == '') {
  246. $this->error['zone'] = $this->language->get('error_zone');
  247. }
  248. if (!$this->error) {
  249. return true;
  250. } else {
  251. return false;
  252. }
  253. }
  254. public function zone() {
  255. $output = '<option value="">' . $this->language->get('text_select') . '</option>';
  256. $this->load->model('localisation/zone');
  257. $results = $this->model_localisation_zone->getZonesByCountryId($this->request->get['country_id']);
  258. foreach ($results as $result) {
  259. $output .= '<option value="' . $result['zone_id'] . '"';
  260. if (isset($this->request->get['zone_id']) && ($this->request->get['zone_id'] == $result['zone_id'])) {
  261. $output .= ' selected="selected"';
  262. }
  263. $output .= '>' . $result['name'] . '</option>';
  264. }
  265. if (!$results) {
  266. $output .= '<option value="0">' . $this->language->get('text_none') . '</option>';
  267. }
  268. $this->response->setOutput($output);
  269. }
  270. }
  271. ?>