PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/controller/localisation/location.php

https://bitbucket.org/mmahdy/opencart-modules-app
PHP | 427 lines | 320 code | 107 blank | 0 comment | 82 complexity | f15dc7335cb156f4820994b500bb4984 MD5 | raw file
  1. <?php
  2. class ControllerLocalisationLocation extends Controller {
  3. private $error = array();
  4. public function index() {
  5. $this->load->language('localisation/location');
  6. $this->document->setTitle($this->language->get('heading_title'));
  7. $this->load->model('localisation/location');
  8. $this->getList();
  9. }
  10. public function add() {
  11. $this->load->language('localisation/location');
  12. $this->document->setTitle($this->language->get('heading_title'));
  13. $this->load->model('localisation/location');
  14. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
  15. $this->model_localisation_location->addLocation($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/location', 'user_token=' . $this->session->data['user_token'] . $url, true));
  28. }
  29. $this->getForm();
  30. }
  31. public function edit() {
  32. $this->load->language('localisation/location');
  33. $this->document->setTitle($this->language->get('heading_title'));
  34. $this->load->model('localisation/location');
  35. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
  36. $this->model_localisation_location->editLocation($this->request->get['location_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/location', 'user_token=' . $this->session->data['user_token'] . $url, true));
  49. }
  50. $this->getForm();
  51. }
  52. public function delete() {
  53. $this->load->language('localisation/location');
  54. $this->document->setTitle($this->language->get('heading_title'));
  55. $this->load->model('localisation/location');
  56. if (isset($this->request->post['selected']) && $this->validateDelete()) {
  57. foreach ($this->request->post['selected'] as $location_id) {
  58. $this->model_localisation_location->deleteLocation($location_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/location', 'user_token=' . $this->session->data['user_token'] . $url, true));
  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. $data['breadcrumbs'] = array();
  102. $data['breadcrumbs'][] = array(
  103. 'text' => $this->language->get('text_home'),
  104. 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
  105. );
  106. $data['breadcrumbs'][] = array(
  107. 'text' => $this->language->get('heading_title'),
  108. 'href' => $this->url->link('localisation/location', 'user_token=' . $this->session->data['user_token'] . $url, true)
  109. );
  110. $data['add'] = $this->url->link('localisation/location/add', 'user_token=' . $this->session->data['user_token'] . $url, true);
  111. $data['delete'] = $this->url->link('localisation/location/delete', 'user_token=' . $this->session->data['user_token'] . $url, true);
  112. $data['location'] = 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. $location_total = $this->model_localisation_location->getTotalLocations();
  120. $results = $this->model_localisation_location->getLocations($filter_data);
  121. foreach ($results as $result) {
  122. $data['location'][] = array(
  123. 'location_id' => $result['location_id'],
  124. 'name' => $result['name'],
  125. 'address' => $result['address'],
  126. 'edit' => $this->url->link('localisation/location/edit', 'user_token=' . $this->session->data['user_token'] . '&location_id=' . $result['location_id'] . $url, true)
  127. );
  128. }
  129. if (isset($this->error['warning'])) {
  130. $data['error_warning'] = $this->error['warning'];
  131. } else {
  132. $data['error_warning'] = '';
  133. }
  134. if (isset($this->session->data['success'])) {
  135. $data['success'] = $this->session->data['success'];
  136. unset($this->session->data['success']);
  137. } else {
  138. $data['success'] = '';
  139. }
  140. if (isset($this->request->post['selected'])) {
  141. $data['selected'] = (array)$this->request->post['selected'];
  142. } else {
  143. $data['selected'] = array();
  144. }
  145. $url = '';
  146. if ($order == 'ASC') {
  147. $url .= '&order=DESC';
  148. } else {
  149. $url .= '&order=ASC';
  150. }
  151. if (isset($this->request->get['page'])) {
  152. $url .= '&page=' . $this->request->get['page'];
  153. }
  154. $data['sort_name'] = $this->url->link('localisation/location', 'user_token=' . $this->session->data['user_token'] . '&sort=name' . $url, true);
  155. $data['sort_address'] = $this->url->link('localisation/location', 'user_token=' . $this->session->data['user_token'] . '&sort=address' . $url, true);
  156. $url = '';
  157. if (isset($this->request->get['sort'])) {
  158. $url .= '&sort=' . $this->request->get['sort'];
  159. }
  160. if (isset($this->request->get['order'])) {
  161. $url .= '&order=' . $this->request->get['order'];
  162. }
  163. $pagination = new Pagination();
  164. $pagination->total = $location_total;
  165. $pagination->page = $page;
  166. $pagination->limit = $this->config->get('config_limit_admin');
  167. $pagination->url = $this->url->link('localisation/location', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}', true);
  168. $data['pagination'] = $pagination->render();
  169. $data['results'] = sprintf($this->language->get('text_pagination'), ($location_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($location_total - $this->config->get('config_limit_admin'))) ? $location_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $location_total, ceil($location_total / $this->config->get('config_limit_admin')));
  170. $data['sort'] = $sort;
  171. $data['order'] = $order;
  172. $data['header'] = $this->load->controller('common/header');
  173. $data['column_left'] = $this->load->controller('common/column_left');
  174. $data['footer'] = $this->load->controller('common/footer');
  175. $this->response->setOutput($this->load->view('localisation/location_list', $data));
  176. }
  177. protected function getForm() {
  178. $data['text_form'] = !isset($this->request->get['location_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
  179. if (isset($this->error['warning'])) {
  180. $data['error_warning'] = $this->error['warning'];
  181. } else {
  182. $data['error_warning'] = '';
  183. }
  184. if (isset($this->error['name'])) {
  185. $data['error_name'] = $this->error['name'];
  186. } else {
  187. $data['error_name'] = '';
  188. }
  189. if (isset($this->error['address'])) {
  190. $data['error_address'] = $this->error['address'];
  191. } else {
  192. $data['error_address'] = '';
  193. }
  194. if (isset($this->error['telephone'])) {
  195. $data['error_telephone'] = $this->error['telephone'];
  196. } else {
  197. $data['error_telephone'] = '';
  198. }
  199. $url = '';
  200. if (isset($this->request->get['sort'])) {
  201. $url .= '&sort=' . $this->request->get['sort'];
  202. }
  203. if (isset($this->request->get['order'])) {
  204. $url .= '&order=' . $this->request->get['order'];
  205. }
  206. if (isset($this->request->get['page'])) {
  207. $url .= '&page=' . $this->request->get['page'];
  208. }
  209. $data['breadcrumbs'] = array();
  210. $data['breadcrumbs'][] = array(
  211. 'text' => $this->language->get('text_home'),
  212. 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
  213. );
  214. $data['breadcrumbs'][] = array(
  215. 'text' => $this->language->get('heading_title'),
  216. 'href' => $this->url->link('localisation/location', 'user_token=' . $this->session->data['user_token'] . $url, true)
  217. );
  218. if (!isset($this->request->get['location_id'])) {
  219. $data['action'] = $this->url->link('localisation/location/add', 'user_token=' . $this->session->data['user_token'] . $url, true);
  220. } else {
  221. $data['action'] = $this->url->link('localisation/location/edit', 'user_token=' . $this->session->data['user_token'] . '&location_id=' . $this->request->get['location_id'] . $url, true);
  222. }
  223. $data['cancel'] = $this->url->link('localisation/location', 'user_token=' . $this->session->data['user_token'] . $url, true);
  224. if (isset($this->request->get['location_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
  225. $location_info = $this->model_localisation_location->getLocation($this->request->get['location_id']);
  226. }
  227. $data['user_token'] = $this->session->data['user_token'];
  228. $this->load->model('setting/store');
  229. if (isset($this->request->post['name'])) {
  230. $data['name'] = $this->request->post['name'];
  231. } elseif (!empty($location_info)) {
  232. $data['name'] = $location_info['name'];
  233. } else {
  234. $data['name'] = '';
  235. }
  236. if (isset($this->request->post['address'])) {
  237. $data['address'] = $this->request->post['address'];
  238. } elseif (!empty($location_info)) {
  239. $data['address'] = $location_info['address'];
  240. } else {
  241. $data['address'] = '';
  242. }
  243. if (isset($this->request->post['geocode'])) {
  244. $data['geocode'] = $this->request->post['geocode'];
  245. } elseif (!empty($location_info)) {
  246. $data['geocode'] = $location_info['geocode'];
  247. } else {
  248. $data['geocode'] = '';
  249. }
  250. if (isset($this->request->post['telephone'])) {
  251. $data['telephone'] = $this->request->post['telephone'];
  252. } elseif (!empty($location_info)) {
  253. $data['telephone'] = $location_info['telephone'];
  254. } else {
  255. $data['telephone'] = '';
  256. }
  257. if (isset($this->request->post['fax'])) {
  258. $data['fax'] = $this->request->post['fax'];
  259. } elseif (!empty($location_info)) {
  260. $data['fax'] = $location_info['fax'];
  261. } else {
  262. $data['fax'] = '';
  263. }
  264. if (isset($this->request->post['image'])) {
  265. $data['image'] = $this->request->post['image'];
  266. } elseif (!empty($location_info)) {
  267. $data['image'] = $location_info['image'];
  268. } else {
  269. $data['image'] = '';
  270. }
  271. $this->load->model('tool/image');
  272. if (isset($this->request->post['image']) && is_file(DIR_IMAGE . $this->request->post['image'])) {
  273. $data['thumb'] = $this->model_tool_image->resize($this->request->post['image'], 100, 100);
  274. } elseif (!empty($location_info) && is_file(DIR_IMAGE . $location_info['image'])) {
  275. $data['thumb'] = $this->model_tool_image->resize($location_info['image'], 100, 100);
  276. } else {
  277. $data['thumb'] = $this->model_tool_image->resize('no_image.png', 100, 100);
  278. }
  279. $data['placeholder'] = $this->model_tool_image->resize('no_image.png', 100, 100);
  280. if (isset($this->request->post['open'])) {
  281. $data['open'] = $this->request->post['open'];
  282. } elseif (!empty($location_info)) {
  283. $data['open'] = $location_info['open'];
  284. } else {
  285. $data['open'] = '';
  286. }
  287. if (isset($this->request->post['comment'])) {
  288. $data['comment'] = $this->request->post['comment'];
  289. } elseif (!empty($location_info)) {
  290. $data['comment'] = $location_info['comment'];
  291. } else {
  292. $data['comment'] = '';
  293. }
  294. $data['header'] = $this->load->controller('common/header');
  295. $data['column_left'] = $this->load->controller('common/column_left');
  296. $data['footer'] = $this->load->controller('common/footer');
  297. $this->response->setOutput($this->load->view('localisation/location_form', $data));
  298. }
  299. protected function validateForm() {
  300. if (!$this->user->hasPermission('modify', 'localisation/location')) {
  301. $this->error['warning'] = $this->language->get('error_permission');
  302. }
  303. if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 32)) {
  304. $this->error['name'] = $this->language->get('error_name');
  305. }
  306. if ((utf8_strlen($this->request->post['address']) < 3) || (utf8_strlen($this->request->post['address']) > 128)) {
  307. $this->error['address'] = $this->language->get('error_address');
  308. }
  309. if ((utf8_strlen($this->request->post['telephone']) < 3) || (utf8_strlen($this->request->post['telephone']) > 32)) {
  310. $this->error['telephone'] = $this->language->get('error_telephone');
  311. }
  312. return !$this->error;
  313. }
  314. protected function validateDelete() {
  315. if (!$this->user->hasPermission('modify', 'localisation/location')) {
  316. $this->error['warning'] = $this->language->get('error_permission');
  317. }
  318. return !$this->error;
  319. }
  320. }