PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/mall/upload/admin/controller/localisation/length_class.php

https://bitbucket.org/allanxyh/uniquemall
PHP | 412 lines | 306 code | 106 blank | 0 comment | 69 complexity | 3f769e6e4194535e508e924e0ed10b5e MD5 | raw file
  1. <?php
  2. class ControllerLocalisationLengthClass extends Controller {
  3. private $error = array();
  4. public function index() {
  5. $this->language->load('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 insert() {
  11. $this->language->load('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->redirect($this->url->link('localisation/length_class', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  28. }
  29. $this->getForm();
  30. }
  31. public function update() {
  32. $this->language->load('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->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->language->load('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->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. $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/length_class', 'token=' . $this->session->data['token'] . $url, 'SSL'),
  110. 'separator' => ' :: '
  111. );
  112. $this->data['insert'] = $this->url->link('localisation/length_class/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
  113. $this->data['delete'] = $this->url->link('localisation/length_class/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');
  114. $this->data['length_classes'] = 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. $length_class_total = $this->model_localisation_length_class->getTotalLengthClasses();
  122. $results = $this->model_localisation_length_class->getLengthClasses($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/length_class/update', 'token=' . $this->session->data['token'] . '&length_class_id=' . $result['length_class_id'] . $url, 'SSL')
  128. );
  129. $this->data['length_classes'][] = array(
  130. 'length_class_id' => $result['length_class_id'],
  131. 'title' => $result['title'] . (($result['unit'] == $this->config->get('config_length_class')) ? $this->language->get('text_default') : null),
  132. 'unit' => $result['unit'],
  133. 'value' => $result['value'],
  134. 'selected' => isset($this->request->post['selected']) && in_array($result['length_class_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_title'] = $this->language->get('column_title');
  141. $this->data['column_unit'] = $this->language->get('column_unit');
  142. $this->data['column_value'] = $this->language->get('column_value');
  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_title'] = $this->url->link('localisation/length_class', 'token=' . $this->session->data['token'] . '&sort=title' . $url, 'SSL');
  167. $this->data['sort_unit'] = $this->url->link('localisation/length_class', 'token=' . $this->session->data['token'] . '&sort=unit' . $url, 'SSL');
  168. $this->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_admin_limit');
  180. $pagination->text = $this->language->get('text_pagination');
  181. $pagination->url = $this->url->link('localisation/length_class', '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/length_class_list.tpl';
  186. $this->children = array(
  187. 'common/header',
  188. 'common/footer'
  189. );
  190. $this->response->setOutput($this->render());
  191. }
  192. protected function getForm() {
  193. $this->data['heading_title'] = $this->language->get('heading_title');
  194. $this->data['entry_title'] = $this->language->get('entry_title');
  195. $this->data['entry_unit'] = $this->language->get('entry_unit');
  196. $this->data['entry_value'] = $this->language->get('entry_value');
  197. $this->data['button_save'] = $this->language->get('button_save');
  198. $this->data['button_cancel'] = $this->language->get('button_cancel');
  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['title'])) {
  205. $this->data['error_title'] = $this->error['title'];
  206. } else {
  207. $this->data['error_title'] = array();
  208. }
  209. if (isset($this->error['unit'])) {
  210. $this->data['error_unit'] = $this->error['unit'];
  211. } else {
  212. $this->data['error_unit'] = array();
  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/length_class', 'token=' . $this->session->data['token'] . $url, 'SSL'),
  233. 'separator' => ' :: '
  234. );
  235. if (!isset($this->request->get['length_class_id'])) {
  236. $this->data['action'] = $this->url->link('localisation/length_class/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
  237. } else {
  238. $this->data['action'] = $this->url->link('localisation/length_class/update', 'token=' . $this->session->data['token'] . '&length_class_id=' . $this->request->get['length_class_id'] . $url, 'SSL');
  239. }
  240. $this->data['cancel'] = $this->url->link('localisation/length_class', 'token=' . $this->session->data['token'] . $url, 'SSL');
  241. if (isset($this->request->get['length_class_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
  242. $length_class_info = $this->model_localisation_length_class->getLengthClass($this->request->get['length_class_id']);
  243. }
  244. $this->load->model('localisation/language');
  245. $this->data['languages'] = $this->model_localisation_language->getLanguages();
  246. if (isset($this->request->post['length_class_description'])) {
  247. $this->data['length_class_description'] = $this->request->post['length_class_description'];
  248. } elseif (isset($this->request->get['length_class_id'])) {
  249. $this->data['length_class_description'] = $this->model_localisation_length_class->getLengthClassDescriptions($this->request->get['length_class_id']);
  250. } else {
  251. $this->data['length_class_description'] = array();
  252. }
  253. if (isset($this->request->post['value'])) {
  254. $this->data['value'] = $this->request->post['value'];
  255. } elseif (!empty($length_class_info)) {
  256. $this->data['value'] = $length_class_info['value'];
  257. } else {
  258. $this->data['value'] = '';
  259. }
  260. $this->template = 'localisation/length_class_form.tpl';
  261. $this->children = array(
  262. 'common/header',
  263. 'common/footer'
  264. );
  265. $this->response->setOutput($this->render());
  266. }
  267. protected function validateForm() {
  268. if (!$this->user->hasPermission('modify', 'localisation/length_class')) {
  269. $this->error['warning'] = $this->language->get('error_permission');
  270. }
  271. foreach ($this->request->post['length_class_description'] as $language_id => $value) {
  272. if ((utf8_strlen($value['title']) < 3) || (utf8_strlen($value['title']) > 32)) {
  273. $this->error['title'][$language_id] = $this->language->get('error_title');
  274. }
  275. if (!$value['unit'] || (utf8_strlen($value['unit']) > 4)) {
  276. $this->error['unit'][$language_id] = $this->language->get('error_unit');
  277. }
  278. }
  279. if (!$this->error) {
  280. return true;
  281. } else {
  282. return false;
  283. }
  284. }
  285. protected function validateDelete() {
  286. if (!$this->user->hasPermission('modify', 'localisation/length_class')) {
  287. $this->error['warning'] = $this->language->get('error_permission');
  288. }
  289. $this->load->model('catalog/product');
  290. foreach ($this->request->post['selected'] as $length_class_id) {
  291. if ($this->config->get('config_length_class_id') == $length_class_id) {
  292. $this->error['warning'] = $this->language->get('error_default');
  293. }
  294. $product_total = $this->model_catalog_product->getTotalProductsByLengthClassId($length_class_id);
  295. if ($product_total) {
  296. $this->error['warning'] = sprintf($this->language->get('error_product'), $product_total);
  297. }
  298. }
  299. if (!$this->error) {
  300. return true;
  301. } else {
  302. return false;
  303. }
  304. }
  305. }
  306. ?>