PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/controller/localisation/zone.php

https://bitbucket.org/sandeepbhaskar/inspiredliving
PHP | 441 lines | 328 code | 113 blank | 0 comment | 72 complexity | 287a0cf3e6e1bdb239be7690552f073b 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 insert() {
  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->redirect($this->url->link('localisation/zone', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  28. }
  29. $this->getForm();
  30. }
  31. public function update() {
  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->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->redirect($this->url->link('localisation/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 = '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. $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/zone', 'token=' . $this->session->data['token'] . $url, 'SSL'),
  110. 'separator' => ' :: '
  111. );
  112. $this->data['insert'] = $this->url->link('localisation/zone/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
  113. $this->data['delete'] = $this->url->link('localisation/zone/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');
  114. $this->data['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. $zone_total = $this->model_localisation_zone->getTotalZones();
  122. $results = $this->model_localisation_zone->getZones($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/zone/update', 'token=' . $this->session->data['token'] . '&zone_id=' . $result['zone_id'] . $url, 'SSL')
  128. );
  129. $this->data['zones'][] = array(
  130. 'zone_id' => $result['zone_id'],
  131. 'country' => $result['country'],
  132. 'name' => $result['name'] . (($result['zone_id'] == $this->config->get('config_zone_id')) ? $this->language->get('text_default') : null),
  133. 'code' => $result['code'],
  134. 'selected' => isset($this->request->post['selected']) && in_array($result['zone_id'], $this->request->post['selected']),
  135. 'action' => $action
  136. );
  137. }
  138. $this->data['heading_title'] = $this->language->get('heading_title');
  139. $this->data['text_no_results'] = $this->language->get('text_no_results');
  140. $this->data['column_country'] = $this->language->get('column_country');
  141. $this->data['column_name'] = $this->language->get('column_name');
  142. $this->data['column_code'] = $this->language->get('column_code');
  143. $this->data['column_action'] = $this->language->get('column_action');
  144. $this->data['button_insert'] = $this->language->get('button_insert');
  145. $this->data['button_delete'] = $this->language->get('button_delete');
  146. if (isset($this->error['warning'])) {
  147. $this->data['error_warning'] = $this->error['warning'];
  148. } else {
  149. $this->data['error_warning'] = '';
  150. }
  151. if (isset($this->session->data['success'])) {
  152. $this->data['success'] = $this->session->data['success'];
  153. unset($this->session->data['success']);
  154. } else {
  155. $this->data['success'] = '';
  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. $this->data['sort_country'] = $this->url->link('localisation/zone', 'token=' . $this->session->data['token'] . '&sort=c.name' . $url, 'SSL');
  167. $this->data['sort_name'] = $this->url->link('localisation/zone', 'token=' . $this->session->data['token'] . '&sort=z.name' . $url, 'SSL');
  168. $this->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_admin_limit');
  180. $pagination->text = $this->language->get('text_pagination');
  181. $pagination->url = $this->url->link('localisation/zone', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
  182. $this->data['pagination'] = $pagination->render();
  183. $this->data['sort'] = $sort;
  184. $this->data['order'] = $order;
  185. $this->template = 'localisation/zone_list.tpl';
  186. $this->children = array(
  187. 'common/header',
  188. 'common/footer'
  189. );
  190. $this->response->setOutput($this->render());
  191. }
  192. private function getForm() {
  193. $this->data['heading_title'] = $this->language->get('heading_title');
  194. $this->data['entry_status'] = $this->language->get('entry_status');
  195. $this->data['entry_name'] = $this->language->get('entry_name');
  196. $this->data['entry_code'] = $this->language->get('entry_code');
  197. $this->data['entry_country'] = $this->language->get('entry_country');
  198. $this->data['text_enabled'] = $this->language->get('text_enabled');
  199. $this->data['text_disabled'] = $this->language->get('text_disabled');
  200. $this->data['button_save'] = $this->language->get('button_save');
  201. $this->data['button_cancel'] = $this->language->get('button_cancel');
  202. if (isset($this->error['warning'])) {
  203. $this->data['error_warning'] = $this->error['warning'];
  204. } else {
  205. $this->data['error_warning'] = '';
  206. }
  207. if (isset($this->error['name'])) {
  208. $this->data['error_name'] = $this->error['name'];
  209. } else {
  210. $this->data['error_name'] = '';
  211. }
  212. $url = '';
  213. if (isset($this->request->get['sort'])) {
  214. $url .= '&sort=' . $this->request->get['sort'];
  215. }
  216. if (isset($this->request->get['order'])) {
  217. $url .= '&order=' . $this->request->get['order'];
  218. }
  219. if (isset($this->request->get['page'])) {
  220. $url .= '&page=' . $this->request->get['page'];
  221. }
  222. $this->data['breadcrumbs'] = array();
  223. $this->data['breadcrumbs'][] = array(
  224. 'text' => $this->language->get('text_home'),
  225. 'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
  226. 'separator' => false
  227. );
  228. $this->data['breadcrumbs'][] = array(
  229. 'text' => $this->language->get('heading_title'),
  230. 'href' => $this->url->link('localisation/zone', 'token=' . $this->session->data['token'] . $url, 'SSL'),
  231. 'separator' => ' :: '
  232. );
  233. if (!isset($this->request->get['zone_id'])) {
  234. $this->data['action'] = $this->url->link('localisation/zone/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
  235. } else {
  236. $this->data['action'] = $this->url->link('localisation/zone/update', 'token=' . $this->session->data['token'] . '&zone_id=' . $this->request->get['zone_id'] . $url, 'SSL');
  237. }
  238. $this->data['cancel'] = $this->url->link('localisation/zone', 'token=' . $this->session->data['token'] . $url, 'SSL');
  239. if (isset($this->request->get['zone_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
  240. $zone_info = $this->model_localisation_zone->getZone($this->request->get['zone_id']);
  241. }
  242. if (isset($this->request->post['status'])) {
  243. $this->data['status'] = $this->request->post['status'];
  244. } elseif (!empty($zone_info)) {
  245. $this->data['status'] = $zone_info['status'];
  246. } else {
  247. $this->data['status'] = '1';
  248. }
  249. if (isset($this->request->post['name'])) {
  250. $this->data['name'] = $this->request->post['name'];
  251. } elseif (!empty($zone_info)) {
  252. $this->data['name'] = $zone_info['name'];
  253. } else {
  254. $this->data['name'] = '';
  255. }
  256. if (isset($this->request->post['code'])) {
  257. $this->data['code'] = $this->request->post['code'];
  258. } elseif (!empty($zone_info)) {
  259. $this->data['code'] = $zone_info['code'];
  260. } else {
  261. $this->data['code'] = '';
  262. }
  263. if (isset($this->request->post['country_id'])) {
  264. $this->data['country_id'] = $this->request->post['country_id'];
  265. } elseif (!empty($zone_info)) {
  266. $this->data['country_id'] = $zone_info['country_id'];
  267. } else {
  268. $this->data['country_id'] = '';
  269. }
  270. $this->load->model('localisation/country');
  271. $this->data['countries'] = $this->model_localisation_country->getCountries();
  272. $this->template = 'localisation/zone_form.tpl';
  273. $this->children = array(
  274. 'common/header',
  275. 'common/footer'
  276. );
  277. $this->response->setOutput($this->render());
  278. }
  279. private function validateForm() {
  280. if (!$this->user->hasPermission('modify', 'localisation/zone')) {
  281. $this->error['warning'] = $this->language->get('error_permission');
  282. }
  283. if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 64)) {
  284. $this->error['name'] = $this->language->get('error_name');
  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/zone')) {
  294. $this->error['warning'] = $this->language->get('error_permission');
  295. }
  296. $this->load->model('setting/store');
  297. $this->load->model('sale/customer');
  298. $this->load->model('sale/affiliate');
  299. $this->load->model('localisation/geo_zone');
  300. foreach ($this->request->post['selected'] as $zone_id) {
  301. if ($this->config->get('config_zone_id') == $zone_id) {
  302. $this->error['warning'] = $this->language->get('error_default');
  303. }
  304. $store_total = $this->model_setting_store->getTotalStoresByZoneId($zone_id);
  305. if ($store_total) {
  306. $this->error['warning'] = sprintf($this->language->get('error_store'), $store_total);
  307. }
  308. $address_total = $this->model_sale_customer->getTotalAddressesByZoneId($zone_id);
  309. if ($address_total) {
  310. $this->error['warning'] = sprintf($this->language->get('error_address'), $address_total);
  311. }
  312. $affiliate_total = $this->model_sale_affiliate->getTotalAffiliatesByZoneId($zone_id);
  313. if ($affiliate_total) {
  314. $this->error['warning'] = sprintf($this->language->get('error_affiliate'), $affiliate_total);
  315. }
  316. $zone_to_geo_zone_total = $this->model_localisation_geo_zone->getTotalZoneToGeoZoneByZoneId($zone_id);
  317. if ($zone_to_geo_zone_total) {
  318. $this->error['warning'] = sprintf($this->language->get('error_zone_to_geo_zone'), $zone_to_geo_zone_total);
  319. }
  320. }
  321. if (!$this->error) {
  322. return true;
  323. } else {
  324. return false;
  325. }
  326. }
  327. }
  328. ?>