PageRenderTime 30ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/controller/localisation/length_class.php

https://gitlab.com/reclamare/mao
PHP | 399 lines | 292 code | 107 blank | 0 comment | 66 complexity | 336afe0d63da23c38957dd8244bd8267 MD5 | raw file
  1. <?php
  2. class ControllerLocalisationLengthClass extends Controller {
  3. private $error = array();
  4. public function index() {
  5. $this->load->language('localisation/length_class');
  6. $this->document->setTitle($this->language->get('heading_title'));
  7. $this->load->model('localisation/length_class');
  8. $this->getList();
  9. }
  10. public function add() {
  11. $this->load->language('localisation/length_class');
  12. $this->document->setTitle($this->language->get('heading_title'));
  13. $this->load->model('localisation/length_class');
  14. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
  15. $this->model_localisation_length_class->addLengthClass($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/length_class', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  28. }
  29. $this->getForm();
  30. }
  31. public function edit() {
  32. $this->load->language('localisation/length_class');
  33. $this->document->setTitle($this->language->get('heading_title'));
  34. $this->load->model('localisation/length_class');
  35. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
  36. $this->model_localisation_length_class->editLengthClass($this->request->get['length_class_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/length_class', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  49. }
  50. $this->getForm();
  51. }
  52. public function delete() {
  53. $this->load->language('localisation/length_class');
  54. $this->document->setTitle($this->language->get('heading_title'));
  55. $this->load->model('localisation/length_class');
  56. if (isset($this->request->post['selected']) && $this->validateDelete()) {
  57. foreach ($this->request->post['selected'] as $length_class_id) {
  58. $this->model_localisation_length_class->deleteLengthClass($length_class_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/length_class', '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 = 'title';
  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/length_class', 'token=' . $this->session->data['token'] . $url, 'SSL')
  109. );
  110. $data['add'] = $this->url->link('localisation/length_class/add', 'token=' . $this->session->data['token'] . $url, 'SSL');
  111. $data['delete'] = $this->url->link('localisation/length_class/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');
  112. $data['length_classes'] = 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. $length_class_total = $this->model_localisation_length_class->getTotalLengthClasses();
  120. $results = $this->model_localisation_length_class->getLengthClasses($filter_data);
  121. foreach ($results as $result) {
  122. $data['length_classes'][] = array(
  123. 'length_class_id' => $result['length_class_id'],
  124. 'title' => $result['title'] . (($result['length_class_id'] == $this->config->get('config_length_class_id')) ? $this->language->get('text_default') : null),
  125. 'unit' => $result['unit'],
  126. 'value' => $result['value'],
  127. 'edit' => $this->url->link('localisation/length_class/edit', 'token=' . $this->session->data['token'] . '&length_class_id=' . $result['length_class_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_title'] = $this->language->get('column_title');
  135. $data['column_unit'] = $this->language->get('column_unit');
  136. $data['column_value'] = $this->language->get('column_value');
  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_title'] = $this->url->link('localisation/length_class', 'token=' . $this->session->data['token'] . '&sort=title' . $url, 'SSL');
  167. $data['sort_unit'] = $this->url->link('localisation/length_class', 'token=' . $this->session->data['token'] . '&sort=unit' . $url, 'SSL');
  168. $data['sort_value'] = $this->url->link('localisation/length_class', 'token=' . $this->session->data['token'] . '&sort=value' . $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 = $length_class_total;
  178. $pagination->page = $page;
  179. $pagination->limit = $this->config->get('config_limit_admin');
  180. $pagination->url = $this->url->link('localisation/length_class', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
  181. $data['pagination'] = $pagination->render();
  182. $data['results'] = sprintf($this->language->get('text_pagination'), ($length_class_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($length_class_total - $this->config->get('config_limit_admin'))) ? $length_class_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $length_class_total, ceil($length_class_total / $this->config->get('config_limit_admin')));
  183. $data['sort'] = $sort;
  184. $data['order'] = $order; $data['header'] = $this->load->controller('common/header');
  185. $data['column_left'] = $this->load->controller('common/column_left');
  186. $data['footer'] = $this->load->controller('common/footer');
  187. $this->response->setOutput($this->load->view('localisation/length_class_list.tpl', $data));
  188. }
  189. protected function getForm() {
  190. $data['heading_title'] = $this->language->get('heading_title');
  191. $data['text_form'] = !isset($this->request->get['length_class_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
  192. $data['entry_title'] = $this->language->get('entry_title');
  193. $data['entry_unit'] = $this->language->get('entry_unit');
  194. $data['entry_value'] = $this->language->get('entry_value');
  195. $data['help_value'] = $this->language->get('help_value');
  196. $data['button_save'] = $this->language->get('button_save');
  197. $data['button_cancel'] = $this->language->get('button_cancel');
  198. if (isset($this->error['warning'])) {
  199. $data['error_warning'] = $this->error['warning'];
  200. } else {
  201. $data['error_warning'] = '';
  202. }
  203. if (isset($this->error['title'])) {
  204. $data['error_title'] = $this->error['title'];
  205. } else {
  206. $data['error_title'] = array();
  207. }
  208. if (isset($this->error['unit'])) {
  209. $data['error_unit'] = $this->error['unit'];
  210. } else {
  211. $data['error_unit'] = array();
  212. }
  213. $url = '';
  214. if (isset($this->request->get['sort'])) {
  215. $url .= '&sort=' . $this->request->get['sort'];
  216. }
  217. if (isset($this->request->get['order'])) {
  218. $url .= '&order=' . $this->request->get['order'];
  219. }
  220. if (isset($this->request->get['page'])) {
  221. $url .= '&page=' . $this->request->get['page'];
  222. }
  223. $data['breadcrumbs'] = array();
  224. $data['breadcrumbs'][] = array(
  225. 'text' => $this->language->get('text_home'),
  226. 'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
  227. );
  228. $data['breadcrumbs'][] = array(
  229. 'text' => $this->language->get('heading_title'),
  230. 'href' => $this->url->link('localisation/length_class', 'token=' . $this->session->data['token'] . $url, 'SSL')
  231. );
  232. if (!isset($this->request->get['length_class_id'])) {
  233. $data['action'] = $this->url->link('localisation/length_class/add', 'token=' . $this->session->data['token'] . $url, 'SSL');
  234. } else {
  235. $data['action'] = $this->url->link('localisation/length_class/edit', 'token=' . $this->session->data['token'] . '&length_class_id=' . $this->request->get['length_class_id'] . $url, 'SSL');
  236. }
  237. $data['cancel'] = $this->url->link('localisation/length_class', 'token=' . $this->session->data['token'] . $url, 'SSL');
  238. if (isset($this->request->get['length_class_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
  239. $length_class_info = $this->model_localisation_length_class->getLengthClass($this->request->get['length_class_id']);
  240. }
  241. $this->load->model('localisation/language');
  242. $data['languages'] = $this->model_localisation_language->getLanguages();
  243. if (isset($this->request->post['length_class_description'])) {
  244. $data['length_class_description'] = $this->request->post['length_class_description'];
  245. } elseif (isset($this->request->get['length_class_id'])) {
  246. $data['length_class_description'] = $this->model_localisation_length_class->getLengthClassDescriptions($this->request->get['length_class_id']);
  247. } else {
  248. $data['length_class_description'] = array();
  249. }
  250. if (isset($this->request->post['value'])) {
  251. $data['value'] = $this->request->post['value'];
  252. } elseif (!empty($length_class_info)) {
  253. $data['value'] = $length_class_info['value'];
  254. } else {
  255. $data['value'] = '';
  256. }
  257. $data['header'] = $this->load->controller('common/header');
  258. $data['column_left'] = $this->load->controller('common/column_left');
  259. $data['footer'] = $this->load->controller('common/footer');
  260. $this->response->setOutput($this->load->view('localisation/length_class_form.tpl', $data));
  261. }
  262. protected function validateForm() {
  263. if (!$this->user->hasPermission('modify', 'localisation/length_class')) {
  264. $this->error['warning'] = $this->language->get('error_permission');
  265. }
  266. foreach ($this->request->post['length_class_description'] as $language_id => $value) {
  267. if ((utf8_strlen($value['title']) < 3) || (utf8_strlen($value['title']) > 32)) {
  268. $this->error['title'][$language_id] = $this->language->get('error_title');
  269. }
  270. if (!$value['unit'] || (utf8_strlen($value['unit']) > 4)) {
  271. $this->error['unit'][$language_id] = $this->language->get('error_unit');
  272. }
  273. }
  274. return !$this->error;
  275. }
  276. protected function validateDelete() {
  277. if (!$this->user->hasPermission('modify', 'localisation/length_class')) {
  278. $this->error['warning'] = $this->language->get('error_permission');
  279. }
  280. $this->load->model('catalog/product');
  281. foreach ($this->request->post['selected'] as $length_class_id) {
  282. if ($this->config->get('config_length_class_id') == $length_class_id) {
  283. $this->error['warning'] = $this->language->get('error_default');
  284. }
  285. $product_total = $this->model_catalog_product->getTotalProductsByLengthClassId($length_class_id);
  286. if ($product_total) {
  287. $this->error['warning'] = sprintf($this->language->get('error_product'), $product_total);
  288. }
  289. }
  290. return !$this->error;
  291. }
  292. }