/upload/admin/controller/localisation/geo_zone.php

https://bitbucket.org/deringer/opencart · PHP · 436 lines · 322 code · 114 blank · 0 comment · 70 complexity · 2d762d56af73b0fa68091a089c6854f5 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. protected 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. protected 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. if (isset($this->error['warning'])) {
  200. $this->data['error_warning'] = $this->error['warning'];
  201. } else {
  202. $this->data['error_warning'] = '';
  203. }
  204. if (isset($this->error['name'])) {
  205. $this->data['error_name'] = $this->error['name'];
  206. } else {
  207. $this->data['error_name'] = '';
  208. }
  209. if (isset($this->error['description'])) {
  210. $this->data['error_description'] = $this->error['description'];
  211. } else {
  212. $this->data['error_description'] = '';
  213. }
  214. $url = '';
  215. if (isset($this->request->get['sort'])) {
  216. $url .= '&sort=' . $this->request->get['sort'];
  217. }
  218. if (isset($this->request->get['order'])) {
  219. $url .= '&order=' . $this->request->get['order'];
  220. }
  221. if (isset($this->request->get['page'])) {
  222. $url .= '&page=' . $this->request->get['page'];
  223. }
  224. $this->data['breadcrumbs'] = array();
  225. $this->data['breadcrumbs'][] = array(
  226. 'text' => $this->language->get('text_home'),
  227. 'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
  228. 'separator' => false
  229. );
  230. $this->data['breadcrumbs'][] = array(
  231. 'text' => $this->language->get('heading_title'),
  232. 'href' => $this->url->link('localisation/geo_zone', 'token=' . $this->session->data['token'] . $url, 'SSL'),
  233. 'separator' => ' :: '
  234. );
  235. if (!isset($this->request->get['geo_zone_id'])) {
  236. $this->data['action'] = $this->url->link('localisation/geo_zone/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
  237. } else {
  238. $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');
  239. }
  240. $this->data['cancel'] = $this->url->link('localisation/geo_zone', 'token=' . $this->session->data['token'] . $url, 'SSL');
  241. if (isset($this->request->get['geo_zone_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
  242. $geo_zone_info = $this->model_localisation_geo_zone->getGeoZone($this->request->get['geo_zone_id']);
  243. }
  244. $this->data['token'] = $this->session->data['token'];
  245. if (isset($this->request->post['name'])) {
  246. $this->data['name'] = $this->request->post['name'];
  247. } elseif (!empty($geo_zone_info)) {
  248. $this->data['name'] = $geo_zone_info['name'];
  249. } else {
  250. $this->data['name'] = '';
  251. }
  252. if (isset($this->request->post['description'])) {
  253. $this->data['description'] = $this->request->post['description'];
  254. } elseif (!empty($geo_zone_info)) {
  255. $this->data['description'] = $geo_zone_info['description'];
  256. } else {
  257. $this->data['description'] = '';
  258. }
  259. $this->load->model('localisation/country');
  260. $this->data['countries'] = $this->model_localisation_country->getCountries();
  261. if (isset($this->request->post['zone_to_geo_zone'])) {
  262. $this->data['zone_to_geo_zones'] = $this->request->post['zone_to_geo_zone'];
  263. } elseif (isset($this->request->get['geo_zone_id'])) {
  264. $this->data['zone_to_geo_zones'] = $this->model_localisation_geo_zone->getZoneToGeoZones($this->request->get['geo_zone_id']);
  265. } else {
  266. $this->data['zone_to_geo_zones'] = array();
  267. }
  268. $this->template = 'localisation/geo_zone_form.tpl';
  269. $this->children = array(
  270. 'common/header',
  271. 'common/footer'
  272. );
  273. $this->response->setOutput($this->render());
  274. }
  275. protected function validateForm() {
  276. if (!$this->user->hasPermission('modify', 'localisation/geo_zone')) {
  277. $this->error['warning'] = $this->language->get('error_permission');
  278. }
  279. if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 32)) {
  280. $this->error['name'] = $this->language->get('error_name');
  281. }
  282. if ((utf8_strlen($this->request->post['description']) < 3) || (utf8_strlen($this->request->post['description']) > 255)) {
  283. $this->error['description'] = $this->language->get('error_description');
  284. }
  285. if (!$this->error) {
  286. return true;
  287. } else {
  288. return false;
  289. }
  290. }
  291. protected function validateDelete() {
  292. if (!$this->user->hasPermission('modify', 'localisation/geo_zone')) {
  293. $this->error['warning'] = $this->language->get('error_permission');
  294. }
  295. $this->load->model('localisation/tax_rate');
  296. foreach ($this->request->post['selected'] as $geo_zone_id) {
  297. $tax_rate_total = $this->model_localisation_tax_rate->getTotalTaxRatesByGeoZoneId($geo_zone_id);
  298. if ($tax_rate_total) {
  299. $this->error['warning'] = sprintf($this->language->get('error_tax_rate'), $tax_rate_total);
  300. }
  301. }
  302. if (!$this->error) {
  303. return true;
  304. } else {
  305. return false;
  306. }
  307. }
  308. public function zone() {
  309. $output = '<option value="0">' . $this->language->get('text_all_zones') . '</option>';
  310. $this->load->model('localisation/zone');
  311. $results = $this->model_localisation_zone->getZonesByCountryId($this->request->get['country_id']);
  312. foreach ($results as $result) {
  313. $output .= '<option value="' . $result['zone_id'] . '"';
  314. if ($this->request->get['zone_id'] == $result['zone_id']) {
  315. $output .= ' selected="selected"';
  316. }
  317. $output .= '>' . $result['name'] . '</option>';
  318. }
  319. $this->response->setOutput($output);
  320. }
  321. }
  322. ?>