/admin/controller/localisation/geo_zone.php

https://github.com/sansanwawa/e-commerse · PHP · 438 lines · 323 code · 115 blank · 0 comment · 70 complexity · 4aae7446c20fd303f34e8efe71f28ffc MD5 · raw file

  1. <?php
  2. class ControllerLocalisationGeoZone extends Controller {
  3. private $error = array();
  4. public function index() {
  5. $this->load->language('localisation/geo_zone');
  6. $this->document->setTitle($this->language->get('heading_title'));
  7. $this->load->model('localisation/geo_zone');
  8. $this->getList();
  9. }
  10. public function insert() {
  11. $this->load->language('localisation/geo_zone');
  12. $this->document->setTitle($this->language->get('heading_title'));
  13. $this->load->model('localisation/geo_zone');
  14. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
  15. $this->model_localisation_geo_zone->addGeoZone($this->request->post);
  16. $this->session->data['success'] = $this->language->get('text_success');
  17. $url = '';
  18. if (isset($this->request->get['sort'])) {
  19. $url .= '&sort=' . $this->request->get['sort'];
  20. }
  21. if (isset($this->request->get['order'])) {
  22. $url .= '&order=' . $this->request->get['order'];
  23. }
  24. if (isset($this->request->get['page'])) {
  25. $url .= '&page=' . $this->request->get['page'];
  26. }
  27. $this->redirect($this->url->link('localisation/geo_zone', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  28. }
  29. $this->getForm();
  30. }
  31. public function update() {
  32. $this->load->language('localisation/geo_zone');
  33. $this->document->setTitle($this->language->get('heading_title'));
  34. $this->load->model('localisation/geo_zone');
  35. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
  36. $this->model_localisation_geo_zone->editGeoZone($this->request->get['geo_zone_id'], $this->request->post);
  37. $this->session->data['success'] = $this->language->get('text_success');
  38. $url = '';
  39. if (isset($this->request->get['sort'])) {
  40. $url .= '&sort=' . $this->request->get['sort'];
  41. }
  42. if (isset($this->request->get['order'])) {
  43. $url .= '&order=' . $this->request->get['order'];
  44. }
  45. if (isset($this->request->get['page'])) {
  46. $url .= '&page=' . $this->request->get['page'];
  47. }
  48. $this->redirect($this->url->link('localisation/geo_zone', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  49. }
  50. $this->getForm();
  51. }
  52. public function delete() {
  53. $this->load->language('localisation/geo_zone');
  54. $this->document->setTitle($this->language->get('heading_title'));
  55. $this->load->model('localisation/geo_zone');
  56. if (isset($this->request->post['selected']) && $this->validateDelete()) {
  57. foreach ($this->request->post['selected'] as $geo_zone_id) {
  58. $this->model_localisation_geo_zone->deleteGeoZone($geo_zone_id);
  59. }
  60. $this->session->data['success'] = $this->language->get('text_success');
  61. $url = '';
  62. if (isset($this->request->get['sort'])) {
  63. $url .= '&sort=' . $this->request->get['sort'];
  64. }
  65. if (isset($this->request->get['order'])) {
  66. $url .= '&order=' . $this->request->get['order'];
  67. }
  68. if (isset($this->request->get['page'])) {
  69. $url .= '&page=' . $this->request->get['page'];
  70. }
  71. $this->redirect($this->url->link('localisation/geo_zone', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  72. }
  73. $this->getList();
  74. }
  75. private function getList() {
  76. if (isset($this->request->get['sort'])) {
  77. $sort = $this->request->get['sort'];
  78. } else {
  79. $sort = 'name';
  80. }
  81. if (isset($this->request->get['order'])) {
  82. $order = $this->request->get['order'];
  83. } else {
  84. $order = 'ASC';
  85. }
  86. if (isset($this->request->get['page'])) {
  87. $page = $this->request->get['page'];
  88. } else {
  89. $page = 1;
  90. }
  91. $url = '';
  92. if (isset($this->request->get['sort'])) {
  93. $url .= '&sort=' . $this->request->get['sort'];
  94. }
  95. if (isset($this->request->get['order'])) {
  96. $url .= '&order=' . $this->request->get['order'];
  97. }
  98. if (isset($this->request->get['page'])) {
  99. $url .= '&page=' . $this->request->get['page'];
  100. }
  101. $this->data['breadcrumbs'] = array();
  102. $this->data['breadcrumbs'][] = array(
  103. 'text' => $this->language->get('text_home'),
  104. 'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
  105. 'separator' => false
  106. );
  107. $this->data['breadcrumbs'][] = array(
  108. 'text' => $this->language->get('heading_title'),
  109. 'href' => $this->url->link('localisation/geo_zone', 'token=' . $this->session->data['token'] . $url, 'SSL'),
  110. 'separator' => ' :: '
  111. );
  112. $this->data['insert'] = $this->url->link('localisation/geo_zone/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
  113. $this->data['delete'] = $this->url->link('localisation/geo_zone/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');
  114. $this->data['geo_zones'] = array();
  115. $data = array(
  116. 'sort' => $sort,
  117. 'order' => $order,
  118. 'start' => ($page - 1) * $this->config->get('config_admin_limit'),
  119. 'limit' => $this->config->get('config_admin_limit')
  120. );
  121. $geo_zone_total = $this->model_localisation_geo_zone->getTotalGeoZones();
  122. $results = $this->model_localisation_geo_zone->getGeoZones($data);
  123. foreach ($results as $result) {
  124. $action = array();
  125. $action[] = array(
  126. 'text' => $this->language->get('text_edit'),
  127. 'href' => $this->url->link('localisation/geo_zone/update', 'token=' . $this->session->data['token'] . '&geo_zone_id=' . $result['geo_zone_id'] . $url, 'SSL')
  128. );
  129. $this->data['geo_zones'][] = array(
  130. 'geo_zone_id' => $result['geo_zone_id'],
  131. 'name' => $result['name'],
  132. 'description' => $result['description'],
  133. 'selected' => isset($this->request->post['selected']) && in_array($result['geo_zone_id'], $this->request->post['selected']),
  134. 'action' => $action
  135. );
  136. }
  137. $this->data['heading_title'] = $this->language->get('heading_title');
  138. $this->data['text_no_results'] = $this->language->get('text_no_results');
  139. $this->data['column_name'] = $this->language->get('column_name');
  140. $this->data['column_description'] = $this->language->get('column_description');
  141. $this->data['column_action'] = $this->language->get('column_action');
  142. $this->data['button_insert'] = $this->language->get('button_insert');
  143. $this->data['button_delete'] = $this->language->get('button_delete');
  144. if (isset($this->error['warning'])) {
  145. $this->data['error_warning'] = $this->error['warning'];
  146. } else {
  147. $this->data['error_warning'] = '';
  148. }
  149. if (isset($this->session->data['success'])) {
  150. $this->data['success'] = $this->session->data['success'];
  151. unset($this->session->data['success']);
  152. } else {
  153. $this->data['success'] = '';
  154. }
  155. $url = '';
  156. if ($order == 'ASC') {
  157. $url .= '&order=DESC';
  158. } else {
  159. $url .= '&order=ASC';
  160. }
  161. if (isset($this->request->get['page'])) {
  162. $url .= '&page=' . $this->request->get['page'];
  163. }
  164. $this->data['sort_name'] = $this->url->link('localisation/geo_zone', 'token=' . $this->session->data['token'] . '&sort=name' . $url, 'SSL');
  165. $this->data['sort_description'] = $this->url->link('localisation/geo_zone', 'token=' . $this->session->data['token'] . '&sort=description' . $url, 'SSL');
  166. $url = '';
  167. if (isset($this->request->get['sort'])) {
  168. $url .= '&sort=' . $this->request->get['sort'];
  169. }
  170. if (isset($this->request->get['order'])) {
  171. $url .= '&order=' . $this->request->get['order'];
  172. }
  173. $pagination = new Pagination();
  174. $pagination->total = $geo_zone_total;
  175. $pagination->page = $page;
  176. $pagination->limit = $this->config->get('config_admin_limit');
  177. $pagination->text = $this->language->get('text_pagination');
  178. $pagination->url = $this->url->link('localisation/geo_zone', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
  179. $this->data['pagination'] = $pagination->render();
  180. $this->data['sort'] = $sort;
  181. $this->data['order'] = $order;
  182. $this->template = 'localisation/geo_zone_list.tpl';
  183. $this->children = array(
  184. 'common/header',
  185. 'common/footer',
  186. );
  187. $this->response->setOutput($this->render());
  188. }
  189. private function getForm() {
  190. $this->data['heading_title'] = $this->language->get('heading_title');
  191. $this->data['entry_name'] = $this->language->get('entry_name');
  192. $this->data['entry_description'] = $this->language->get('entry_description');
  193. $this->data['entry_country'] = $this->language->get('entry_country');
  194. $this->data['entry_zone'] = $this->language->get('entry_zone');
  195. $this->data['button_save'] = $this->language->get('button_save');
  196. $this->data['button_cancel'] = $this->language->get('button_cancel');
  197. $this->data['button_add_geo_zone'] = $this->language->get('button_add_geo_zone');
  198. $this->data['button_remove'] = $this->language->get('button_remove');
  199. $this->data['tab_general'] = $this->language->get('tab_general');
  200. $this->data['token'] = $this->session->data['token'];
  201. if (isset($this->error['warning'])) {
  202. $this->data['error_warning'] = $this->error['warning'];
  203. } else {
  204. $this->data['error_warning'] = '';
  205. }
  206. if (isset($this->error['name'])) {
  207. $this->data['error_name'] = $this->error['name'];
  208. } else {
  209. $this->data['error_name'] = '';
  210. }
  211. if (isset($this->error['description'])) {
  212. $this->data['error_description'] = $this->error['description'];
  213. } else {
  214. $this->data['error_description'] = '';
  215. }
  216. $url = '';
  217. if (isset($this->request->get['sort'])) {
  218. $url .= '&sort=' . $this->request->get['sort'];
  219. }
  220. if (isset($this->request->get['order'])) {
  221. $url .= '&order=' . $this->request->get['order'];
  222. }
  223. if (isset($this->request->get['page'])) {
  224. $url .= '&page=' . $this->request->get['page'];
  225. }
  226. $this->data['breadcrumbs'] = array();
  227. $this->data['breadcrumbs'][] = array(
  228. 'text' => $this->language->get('text_home'),
  229. 'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
  230. 'separator' => false
  231. );
  232. $this->data['breadcrumbs'][] = array(
  233. 'text' => $this->language->get('heading_title'),
  234. 'href' => $this->url->link('localisation/geo_zone', 'token=' . $this->session->data['token'] . $url, 'SSL'),
  235. 'separator' => ' :: '
  236. );
  237. if (!isset($this->request->get['geo_zone_id'])) {
  238. $this->data['action'] = $this->url->link('localisation/geo_zone/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
  239. } else {
  240. $this->data['action'] = $this->url->link('localisation/geo_zone/update', 'token=' . $this->session->data['token'] . '&geo_zone_id=' . $this->request->get['geo_zone_id'] . $url, 'SSL');
  241. }
  242. $this->data['cancel'] = $this->url->link('localisation/geo_zone', 'token=' . $this->session->data['token'] . $url, 'SSL');
  243. if (isset($this->request->get['geo_zone_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
  244. $geo_zone_info = $this->model_localisation_geo_zone->getGeoZone($this->request->get['geo_zone_id']);
  245. }
  246. if (isset($this->request->post['name'])) {
  247. $this->data['name'] = $this->request->post['name'];
  248. } elseif (isset($geo_zone_info)) {
  249. $this->data['name'] = $geo_zone_info['name'];
  250. } else {
  251. $this->data['name'] = '';
  252. }
  253. if (isset($this->request->post['description'])) {
  254. $this->data['description'] = $this->request->post['description'];
  255. } elseif (isset($geo_zone_info)) {
  256. $this->data['description'] = $geo_zone_info['description'];
  257. } else {
  258. $this->data['description'] = '';
  259. }
  260. $this->load->model('localisation/country');
  261. $this->data['countries'] = $this->model_localisation_country->getCountries();
  262. if (isset($this->request->post['zone_to_geo_zone'])) {
  263. $this->data['zone_to_geo_zones'] = $this->request->post['zone_to_geo_zone'];
  264. } elseif (isset($this->request->get['geo_zone_id'])) {
  265. $this->data['zone_to_geo_zones'] = $this->model_localisation_geo_zone->getZoneToGeoZones($this->request->get['geo_zone_id']);
  266. } else {
  267. $this->data['zone_to_geo_zones'] = array();
  268. }
  269. $this->template = 'localisation/geo_zone_form.tpl';
  270. $this->children = array(
  271. 'common/header',
  272. 'common/footer',
  273. );
  274. $this->response->setOutput($this->render());
  275. }
  276. private function validateForm() {
  277. if (!$this->user->hasPermission('modify', 'localisation/geo_zone')) {
  278. $this->error['warning'] = $this->language->get('error_permission');
  279. }
  280. if ((strlen(utf8_decode($this->request->post['name'])) < 3) || (strlen(utf8_decode($this->request->post['name'])) > 32)) {
  281. $this->error['name'] = $this->language->get('error_name');
  282. }
  283. if ((strlen(utf8_decode($this->request->post['description'])) < 3) || (strlen(utf8_decode($this->request->post['description'])) > 255)) {
  284. $this->error['description'] = $this->language->get('error_description');
  285. }
  286. if (!$this->error) {
  287. return true;
  288. } else {
  289. return false;
  290. }
  291. }
  292. private function validateDelete() {
  293. if (!$this->user->hasPermission('modify', 'localisation/geo_zone')) {
  294. $this->error['warning'] = $this->language->get('error_permission');
  295. }
  296. $this->load->model('localisation/tax_class');
  297. foreach ($this->request->post['selected'] as $geo_zone_id) {
  298. $tax_rate_total = $this->model_localisation_tax_class->getTotalTaxRatesByGeoZoneId($geo_zone_id);
  299. if ($tax_rate_total) {
  300. $this->error['warning'] = sprintf($this->language->get('error_tax_rate'), $tax_rate_total);
  301. }
  302. }
  303. if (!$this->error) {
  304. return true;
  305. } else {
  306. return false;
  307. }
  308. }
  309. public function zone() {
  310. $output = '<option value="0">' . $this->language->get('text_all_zones') . '</option>';
  311. $this->load->model('localisation/zone');
  312. $results = $this->model_localisation_zone->getZonesByCountryId($this->request->get['country_id']);
  313. foreach ($results as $result) {
  314. $output .= '<option value="' . $result['zone_id'] . '"';
  315. if ($this->request->get['zone_id'] == $result['zone_id']) {
  316. $output .= ' selected="selected"';
  317. }
  318. $output .= '>' . $result['name'] . '</option>';
  319. }
  320. $this->response->setOutput($output);
  321. }
  322. }
  323. ?>