PageRenderTime 25ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/controller/localisation/zone.php

https://gitlab.com/hazelnuts23/unitedfoodstuff
PHP | 428 lines | 314 code | 114 blank | 0 comment | 69 complexity | 8cd7a0b941cf5c23fffd47af4eb50cf0 MD5 | raw file
  1. <?php
  2. class ControllerLocalisationZone extends Controller {
  3. private $error = array();
  4. public function index() {
  5. $this->load->language('localisation/zone');
  6. $this->document->setTitle($this->language->get('heading_title'));
  7. $this->load->model('localisation/zone');
  8. $this->getList();
  9. }
  10. public function add() {
  11. $this->load->language('localisation/zone');
  12. $this->document->setTitle($this->language->get('heading_title'));
  13. $this->load->model('localisation/zone');
  14. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
  15. $this->model_localisation_zone->addZone($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->response->redirect($this->url->link('localisation/zone', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  28. }
  29. $this->getForm();
  30. }
  31. public function edit() {
  32. $this->load->language('localisation/zone');
  33. $this->document->setTitle($this->language->get('heading_title'));
  34. $this->load->model('localisation/zone');
  35. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
  36. $this->model_localisation_zone->editZone($this->request->get['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->response->redirect($this->url->link('localisation/zone', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  49. }
  50. $this->getForm();
  51. }
  52. public function delete() {
  53. $this->load->language('localisation/zone');
  54. $this->document->setTitle($this->language->get('heading_title'));
  55. $this->load->model('localisation/zone');
  56. if (isset($this->request->post['selected']) && $this->validateDelete()) {
  57. foreach ($this->request->post['selected'] as $zone_id) {
  58. $this->model_localisation_zone->deleteZone($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->response->redirect($this->url->link('localisation/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 = 'c.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. $data['breadcrumbs'] = array();
  102. $data['breadcrumbs'][] = array(
  103. 'text' => $this->language->get('text_home'),
  104. 'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
  105. );
  106. $data['breadcrumbs'][] = array(
  107. 'text' => $this->language->get('heading_title'),
  108. 'href' => $this->url->link('localisation/zone', 'token=' . $this->session->data['token'] . $url, 'SSL')
  109. );
  110. $data['add'] = $this->url->link('localisation/zone/add', 'token=' . $this->session->data['token'] . $url, 'SSL');
  111. $data['delete'] = $this->url->link('localisation/zone/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');
  112. $data['zones'] = array();
  113. $filter_data = array(
  114. 'sort' => $sort,
  115. 'order' => $order,
  116. 'start' => ($page - 1) * $this->config->get('config_limit_admin'),
  117. 'limit' => $this->config->get('config_limit_admin')
  118. );
  119. $zone_total = $this->model_localisation_zone->getTotalZones();
  120. $results = $this->model_localisation_zone->getZones($filter_data);
  121. foreach ($results as $result) {
  122. $data['zones'][] = array(
  123. 'zone_id' => $result['zone_id'],
  124. 'country' => $result['country'],
  125. 'name' => $result['name'] . (($result['zone_id'] == $this->config->get('config_zone_id')) ? $this->language->get('text_default') : null),
  126. 'code' => $result['code'],
  127. 'edit' => $this->url->link('localisation/zone/edit', 'token=' . $this->session->data['token'] . '&zone_id=' . $result['zone_id'] . $url, 'SSL')
  128. );
  129. }
  130. $data['heading_title'] = $this->language->get('heading_title');
  131. $data['text_list'] = $this->language->get('text_list');
  132. $data['text_no_results'] = $this->language->get('text_no_results');
  133. $data['text_confirm'] = $this->language->get('text_confirm');
  134. $data['column_country'] = $this->language->get('column_country');
  135. $data['column_name'] = $this->language->get('column_name');
  136. $data['column_code'] = $this->language->get('column_code');
  137. $data['column_action'] = $this->language->get('column_action');
  138. $data['button_add'] = $this->language->get('button_add');
  139. $data['button_edit'] = $this->language->get('button_edit');
  140. $data['button_delete'] = $this->language->get('button_delete');
  141. if (isset($this->error['warning'])) {
  142. $data['error_warning'] = $this->error['warning'];
  143. } else {
  144. $data['error_warning'] = '';
  145. }
  146. if (isset($this->session->data['success'])) {
  147. $data['success'] = $this->session->data['success'];
  148. unset($this->session->data['success']);
  149. } else {
  150. $data['success'] = '';
  151. }
  152. if (isset($this->request->post['selected'])) {
  153. $data['selected'] = (array)$this->request->post['selected'];
  154. } else {
  155. $data['selected'] = array();
  156. }
  157. $url = '';
  158. if ($order == 'ASC') {
  159. $url .= '&order=DESC';
  160. } else {
  161. $url .= '&order=ASC';
  162. }
  163. if (isset($this->request->get['page'])) {
  164. $url .= '&page=' . $this->request->get['page'];
  165. }
  166. $data['sort_country'] = $this->url->link('localisation/zone', 'token=' . $this->session->data['token'] . '&sort=c.name' . $url, 'SSL');
  167. $data['sort_name'] = $this->url->link('localisation/zone', 'token=' . $this->session->data['token'] . '&sort=z.name' . $url, 'SSL');
  168. $data['sort_code'] = $this->url->link('localisation/zone', 'token=' . $this->session->data['token'] . '&sort=z.code' . $url, 'SSL');
  169. $url = '';
  170. if (isset($this->request->get['sort'])) {
  171. $url .= '&sort=' . $this->request->get['sort'];
  172. }
  173. if (isset($this->request->get['order'])) {
  174. $url .= '&order=' . $this->request->get['order'];
  175. }
  176. $pagination = new Pagination();
  177. $pagination->total = $zone_total;
  178. $pagination->page = $page;
  179. $pagination->limit = $this->config->get('config_limit_admin');
  180. $pagination->url = $this->url->link('localisation/zone', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
  181. $data['pagination'] = $pagination->render();
  182. $data['results'] = sprintf($this->language->get('text_pagination'), ($zone_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($zone_total - $this->config->get('config_limit_admin'))) ? $zone_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $zone_total, ceil($zone_total / $this->config->get('config_limit_admin')));
  183. $data['sort'] = $sort;
  184. $data['order'] = $order;
  185. $data['header'] = $this->load->controller('common/header');
  186. $data['column_left'] = $this->load->controller('common/column_left');
  187. $data['footer'] = $this->load->controller('common/footer');
  188. $this->response->setOutput($this->load->view('localisation/zone_list.tpl', $data));
  189. }
  190. protected function getForm() {
  191. $data['heading_title'] = $this->language->get('heading_title');
  192. $data['text_form'] = !isset($this->request->get['zone_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
  193. $data['entry_status'] = $this->language->get('entry_status');
  194. $data['entry_name'] = $this->language->get('entry_name');
  195. $data['entry_code'] = $this->language->get('entry_code');
  196. $data['entry_country'] = $this->language->get('entry_country');
  197. $data['text_enabled'] = $this->language->get('text_enabled');
  198. $data['text_disabled'] = $this->language->get('text_disabled');
  199. $data['button_save'] = $this->language->get('button_save');
  200. $data['button_cancel'] = $this->language->get('button_cancel');
  201. if (isset($this->error['warning'])) {
  202. $data['error_warning'] = $this->error['warning'];
  203. } else {
  204. $data['error_warning'] = '';
  205. }
  206. if (isset($this->error['name'])) {
  207. $data['error_name'] = $this->error['name'];
  208. } else {
  209. $data['error_name'] = '';
  210. }
  211. $url = '';
  212. if (isset($this->request->get['sort'])) {
  213. $url .= '&sort=' . $this->request->get['sort'];
  214. }
  215. if (isset($this->request->get['order'])) {
  216. $url .= '&order=' . $this->request->get['order'];
  217. }
  218. if (isset($this->request->get['page'])) {
  219. $url .= '&page=' . $this->request->get['page'];
  220. }
  221. $data['breadcrumbs'] = array();
  222. $data['breadcrumbs'][] = array(
  223. 'text' => $this->language->get('text_home'),
  224. 'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
  225. );
  226. $data['breadcrumbs'][] = array(
  227. 'text' => $this->language->get('heading_title'),
  228. 'href' => $this->url->link('localisation/zone', 'token=' . $this->session->data['token'] . $url, 'SSL')
  229. );
  230. if (!isset($this->request->get['zone_id'])) {
  231. $data['action'] = $this->url->link('localisation/zone/add', 'token=' . $this->session->data['token'] . $url, 'SSL');
  232. } else {
  233. $data['action'] = $this->url->link('localisation/zone/edit', 'token=' . $this->session->data['token'] . '&zone_id=' . $this->request->get['zone_id'] . $url, 'SSL');
  234. }
  235. $data['cancel'] = $this->url->link('localisation/zone', 'token=' . $this->session->data['token'] . $url, 'SSL');
  236. if (isset($this->request->get['zone_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
  237. $zone_info = $this->model_localisation_zone->getZone($this->request->get['zone_id']);
  238. }
  239. if (isset($this->request->post['status'])) {
  240. $data['status'] = $this->request->post['status'];
  241. } elseif (!empty($zone_info)) {
  242. $data['status'] = $zone_info['status'];
  243. } else {
  244. $data['status'] = '1';
  245. }
  246. if (isset($this->request->post['name'])) {
  247. $data['name'] = $this->request->post['name'];
  248. } elseif (!empty($zone_info)) {
  249. $data['name'] = $zone_info['name'];
  250. } else {
  251. $data['name'] = '';
  252. }
  253. if (isset($this->request->post['code'])) {
  254. $data['code'] = $this->request->post['code'];
  255. } elseif (!empty($zone_info)) {
  256. $data['code'] = $zone_info['code'];
  257. } else {
  258. $data['code'] = '';
  259. }
  260. if (isset($this->request->post['country_id'])) {
  261. $data['country_id'] = $this->request->post['country_id'];
  262. } elseif (!empty($zone_info)) {
  263. $data['country_id'] = $zone_info['country_id'];
  264. } else {
  265. $data['country_id'] = '';
  266. }
  267. $this->load->model('localisation/country');
  268. $data['countries'] = $this->model_localisation_country->getCountries();
  269. $data['header'] = $this->load->controller('common/header');
  270. $data['column_left'] = $this->load->controller('common/column_left');
  271. $data['footer'] = $this->load->controller('common/footer');
  272. $this->response->setOutput($this->load->view('localisation/zone_form.tpl', $data));
  273. }
  274. protected function validateForm() {
  275. if (!$this->user->hasPermission('modify', 'localisation/zone')) {
  276. $this->error['warning'] = $this->language->get('error_permission');
  277. }
  278. if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 64)) {
  279. $this->error['name'] = $this->language->get('error_name');
  280. }
  281. return !$this->error;
  282. }
  283. protected function validateDelete() {
  284. if (!$this->user->hasPermission('modify', 'localisation/zone')) {
  285. $this->error['warning'] = $this->language->get('error_permission');
  286. }
  287. $this->load->model('setting/store');
  288. $this->load->model('sale/customer');
  289. $this->load->model('marketing/affiliate');
  290. $this->load->model('localisation/geo_zone');
  291. foreach ($this->request->post['selected'] as $zone_id) {
  292. if ($this->config->get('config_zone_id') == $zone_id) {
  293. $this->error['warning'] = $this->language->get('error_default');
  294. }
  295. $store_total = $this->model_setting_store->getTotalStoresByZoneId($zone_id);
  296. if ($store_total) {
  297. $this->error['warning'] = sprintf($this->language->get('error_store'), $store_total);
  298. }
  299. $address_total = $this->model_sale_customer->getTotalAddressesByZoneId($zone_id);
  300. if ($address_total) {
  301. $this->error['warning'] = sprintf($this->language->get('error_address'), $address_total);
  302. }
  303. $affiliate_total = $this->model_marketing_affiliate->getTotalAffiliatesByZoneId($zone_id);
  304. if ($affiliate_total) {
  305. $this->error['warning'] = sprintf($this->language->get('error_affiliate'), $affiliate_total);
  306. }
  307. $zone_to_geo_zone_total = $this->model_localisation_geo_zone->getTotalZoneToGeoZoneByZoneId($zone_id);
  308. if ($zone_to_geo_zone_total) {
  309. $this->error['warning'] = sprintf($this->language->get('error_zone_to_geo_zone'), $zone_to_geo_zone_total);
  310. }
  311. }
  312. return !$this->error;
  313. }
  314. }