PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/opencart_v1.4/upload/admin/controller/localisation/weight_class.php

http://coderstalk.googlecode.com/
PHP | 412 lines | 305 code | 107 blank | 0 comment | 68 complexity | 14201c3d1a4c7999fc85172dd05e0145 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, AGPL-3.0
  1. <?php
  2. class ControllerLocalisationWeightClass extends Controller {
  3. private $error = array();
  4. public function index() {
  5. $this->load->language('localisation/weight_class');
  6. $this->document->title = $this->language->get('heading_title');
  7. $this->load->model('localisation/weight_class');
  8. $this->getList();
  9. }
  10. public function insert() {
  11. $this->load->language('localisation/weight_class');
  12. $this->document->title = $this->language->get('heading_title');
  13. $this->load->model('localisation/weight_class');
  14. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
  15. $this->model_localisation_weight_class->addWeightClass($this->request->post);
  16. $this->session->data['success'] = $this->language->get('text_success');
  17. $url = '';
  18. if (isset($this->request->get['page'])) {
  19. $url .= '&page=' . $this->request->get['page'];
  20. }
  21. if (isset($this->request->get['sort'])) {
  22. $url .= '&sort=' . $this->request->get['sort'];
  23. }
  24. if (isset($this->request->get['order'])) {
  25. $url .= '&order=' . $this->request->get['order'];
  26. }
  27. $this->redirect($this->url->https('localisation/weight_class' . $url));
  28. }
  29. $this->getForm();
  30. }
  31. public function update() {
  32. $this->load->language('localisation/weight_class');
  33. $this->document->title = $this->language->get('heading_title');
  34. $this->load->model('localisation/weight_class');
  35. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
  36. $this->model_localisation_weight_class->editWeightClass($this->request->get['weight_class_id'], $this->request->post);
  37. $this->session->data['success'] = $this->language->get('text_success');
  38. $url = '';
  39. if (isset($this->request->get['page'])) {
  40. $url .= '&page=' . $this->request->get['page'];
  41. }
  42. if (isset($this->request->get['sort'])) {
  43. $url .= '&sort=' . $this->request->get['sort'];
  44. }
  45. if (isset($this->request->get['order'])) {
  46. $url .= '&order=' . $this->request->get['order'];
  47. }
  48. $this->redirect($this->url->https('localisation/weight_class' . $url));
  49. }
  50. $this->getForm();
  51. }
  52. public function delete() {
  53. $this->load->language('localisation/weight_class');
  54. $this->document->title = $this->language->get('heading_title');
  55. $this->load->model('localisation/weight_class');
  56. if (isset($this->request->post['selected']) && $this->validateDelete()) {
  57. foreach ($this->request->post['selected'] as $weight_class_id) {
  58. $this->model_localisation_weight_class->deleteWeightClass($weight_class_id);
  59. }
  60. $this->session->data['success'] = $this->language->get('text_success');
  61. $url = '';
  62. if (isset($this->request->get['page'])) {
  63. $url .= '&page=' . $this->request->get['page'];
  64. }
  65. if (isset($this->request->get['sort'])) {
  66. $url .= '&sort=' . $this->request->get['sort'];
  67. }
  68. if (isset($this->request->get['order'])) {
  69. $url .= '&order=' . $this->request->get['order'];
  70. }
  71. $this->redirect($this->url->https('localisation/weight_class' . $url));
  72. }
  73. $this->getList();
  74. }
  75. private function getList() {
  76. if (isset($this->request->get['page'])) {
  77. $page = $this->request->get['page'];
  78. } else {
  79. $page = 1;
  80. }
  81. if (isset($this->request->get['sort'])) {
  82. $sort = $this->request->get['sort'];
  83. } else {
  84. $sort = 'title';
  85. }
  86. if (isset($this->request->get['order'])) {
  87. $order = $this->request->get['order'];
  88. } else {
  89. $order = 'ASC';
  90. }
  91. $url = '';
  92. if (isset($this->request->get['page'])) {
  93. $url .= '&page=' . $this->request->get['page'];
  94. }
  95. if (isset($this->request->get['sort'])) {
  96. $url .= '&sort=' . $this->request->get['sort'];
  97. }
  98. if (isset($this->request->get['order'])) {
  99. $url .= '&order=' . $this->request->get['order'];
  100. }
  101. $this->document->breadcrumbs = array();
  102. $this->document->breadcrumbs[] = array(
  103. 'href' => $this->url->https('common/home'),
  104. 'text' => $this->language->get('text_home'),
  105. 'separator' => FALSE
  106. );
  107. $this->document->breadcrumbs[] = array(
  108. 'href' => $this->url->https('localisation/weight_class' . $url),
  109. 'text' => $this->language->get('heading_title'),
  110. 'separator' => ' :: '
  111. );
  112. $this->data['insert'] = $this->url->https('localisation/weight_class/insert' . $url);
  113. $this->data['delete'] = $this->url->https('localisation/weight_class/delete' . $url);
  114. $this->data['weight_classes'] = array();
  115. $data = array(
  116. 'sort' => $sort,
  117. 'order' => $order,
  118. 'start' => ($page - 1) * 10,
  119. 'limit' => 10
  120. );
  121. $weight_class_total = $this->model_localisation_weight_class->getTotalWeightClasses();
  122. $results = $this->model_localisation_weight_class->getWeightClasses($data);
  123. foreach ($results as $result) {
  124. $action = array();
  125. $action[] = array(
  126. 'text' => $this->language->get('text_edit'),
  127. 'href' => $this->url->https('localisation/weight_class/update&weight_class_id=' . $result['weight_class_id'] . $url)
  128. );
  129. $this->data['weight_classes'][] = array(
  130. 'weight_class_id' => $result['weight_class_id'],
  131. 'title' => $result['title'] . (($result['weight_class_id'] == $this->config->get('config_weight_class_id')) ? $this->language->get('text_default') : NULL),
  132. 'unit' => $result['unit'],
  133. 'selected' => isset($this->request->post['selected']) && in_array($result['weight_class_id'], $this->request->post['selected']),
  134. 'action' => $action
  135. );
  136. }
  137. $this->data['heading_title'] = $this->language->get('heading_title');
  138. $this->data['text_no_results'] = $this->language->get('text_no_results');
  139. $this->data['column_title'] = $this->language->get('column_title');
  140. $this->data['column_unit'] = $this->language->get('column_unit');
  141. $this->data['column_action'] = $this->language->get('column_action');
  142. $this->data['button_insert'] = $this->language->get('button_insert');
  143. $this->data['button_delete'] = $this->language->get('button_delete');
  144. if (isset($this->error['warning'])) {
  145. $this->data['error_warning'] = $this->error['warning'];
  146. } else {
  147. $this->data['error_warning'] = '';
  148. }
  149. if (isset($this->session->data['success'])) {
  150. $this->data['success'] = $this->session->data['success'];
  151. unset($this->session->data['success']);
  152. } else {
  153. $this->data['success'] = '';
  154. }
  155. $url = '';
  156. if ($order == 'ASC') {
  157. $url .= '&order=' . 'DESC';
  158. } else {
  159. $url .= '&order=' . 'ASC';
  160. }
  161. if (isset($this->request->get['page'])) {
  162. $url .= '&page=' . $this->request->get['page'];
  163. }
  164. $this->data['sort_title'] = $this->url->https('localisation/weight_class&sort=title' . $url);
  165. $this->data['sort_unit'] = $this->url->https('localisation/weight_class&sort=unit' . $url);
  166. $url = '';
  167. if (isset($this->request->get['sort'])) {
  168. $url .= '&sort=' . $this->request->get['sort'];
  169. }
  170. if (isset($this->request->get['order'])) {
  171. $url .= '&order=' . $this->request->get['order'];
  172. }
  173. $pagination = new Pagination();
  174. $pagination->total = $weight_class_total;
  175. $pagination->page = $page;
  176. $pagination->limit = 10;
  177. $pagination->text = $this->language->get('text_pagination');
  178. $pagination->url = $this->url->https('localisation/weight_class' . $url . '&page=%s');
  179. $this->data['pagination'] = $pagination->render();
  180. $this->data['sort'] = $sort;
  181. $this->data['order'] = $order;
  182. $this->template = 'localisation/weight_class_list.tpl';
  183. $this->children = array(
  184. 'common/header',
  185. 'common/footer'
  186. );
  187. $this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
  188. }
  189. private function getForm() {
  190. $this->data['heading_title'] = $this->language->get('heading_title');
  191. $this->data['entry_title'] = $this->language->get('entry_title');
  192. $this->data['entry_unit'] = $this->language->get('entry_unit');
  193. $this->data['button_save'] = $this->language->get('button_save');
  194. $this->data['button_cancel'] = $this->language->get('button_cancel');
  195. $this->data['tab_general'] = $this->language->get('tab_general');
  196. if (isset($this->error['warning'])) {
  197. $this->data['error_warning'] = $this->error['warning'];
  198. } else {
  199. $this->data['error_warning'] = '';
  200. }
  201. if (isset($this->error['title'])) {
  202. $this->data['error_title'] = $this->error['title'];
  203. } else {
  204. $this->data['error_title'] = '';
  205. }
  206. if (isset($this->error['unit'])) {
  207. $this->data['error_unit'] = $this->error['unit'];
  208. } else {
  209. $this->data['error_unit'] = '';
  210. }
  211. $url = '';
  212. if (isset($this->request->get['page'])) {
  213. $url .= '&page=' . $this->request->get['page'];
  214. }
  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. $this->document->breadcrumbs = array();
  222. $this->document->breadcrumbs[] = array(
  223. 'href' => $this->url->https('common/home'),
  224. 'text' => $this->language->get('text_home'),
  225. 'separator' => FALSE
  226. );
  227. $this->document->breadcrumbs[] = array(
  228. 'href' => $this->url->https('localisation/weight_class' . $url),
  229. 'text' => $this->language->get('heading_title'),
  230. 'separator' => ' :: '
  231. );
  232. if (!isset($this->request->get['weight_class_id'])) {
  233. $this->data['action'] = $this->url->https('localisation/weight_class/insert' . $url);
  234. } else {
  235. $this->data['action'] = $this->url->https('localisation/weight_class/update&weight_class_id=' . $this->request->get['weight_class_id'] . $url);
  236. }
  237. $this->data['cancel'] = $this->url->https('localisation/weight_class' . $url);
  238. $this->load->model('localisation/language');
  239. $this->data['languages'] = $this->model_localisation_language->getLanguages();
  240. if (isset($this->request->post['weight_class'])) {
  241. $this->data['weight_class'] = $this->request->post['weight_class'];
  242. } elseif (isset($this->request->get['weight_class_id'])) {
  243. $this->data['weight_class'] = $this->model_localisation_weight_class->getWeightClassDescriptions($this->request->get['weight_class_id']);
  244. } else {
  245. $this->data['weight_class'] = array();
  246. }
  247. if (isset($this->request->get['weight_class_id'])) {
  248. $this->data['weight_tos'] = $this->model_localisation_weight_class->getWeightTo($this->request->get['weight_class_id']);
  249. } else {
  250. $this->data['weight_tos'] = $this->model_localisation_weight_class->getWeightTo(0);
  251. }
  252. if (isset($this->request->post['weight_rule'])) {
  253. $this->data['weight_rule'] = $this->request->post['weight_rule'];
  254. } elseif (isset($this->request->get['weight_class_id'])) {
  255. $this->data['weight_rule'] = $this->model_localisation_weight_class->getWeightRules($this->request->get['weight_class_id']);
  256. } else {
  257. $this->data['weight_rule'] = array();
  258. }
  259. $this->template = 'localisation/weight_class_form.tpl';
  260. $this->children = array(
  261. 'common/header',
  262. 'common/footer'
  263. );
  264. $this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
  265. }
  266. private function validateForm() {
  267. if (!$this->user->hasPermission('modify', 'localisation/weight_class')) {
  268. $this->error['warning'] = $this->language->get('error_permission');
  269. }
  270. foreach ($this->request->post['weight_class'] as $language_id => $value) {
  271. if ((strlen(utf8_decode($value['title'])) < 3) || (strlen(utf8_decode($value['title'])) > 32)) {
  272. $this->error['title'][$language_id] = $this->language->get('error_title');
  273. }
  274. if ((!$value['unit']) || (strlen(utf8_decode($value['unit'])) > 4)) {
  275. $this->error['unit'][$language_id] = $this->language->get('error_unit');
  276. }
  277. }
  278. if (!$this->error) {
  279. return TRUE;
  280. } else {
  281. return FALSE;
  282. }
  283. }
  284. private function validateDelete() {
  285. if (!$this->user->hasPermission('modify', 'localisation/weight_class')) {
  286. $this->error['warning'] = $this->language->get('error_permission');
  287. }
  288. $this->load->model('catalog/product');
  289. foreach ($this->request->post['selected'] as $weight_class_id) {
  290. if ($this->config->get('config_weight_class_id') == $weight_class_id) {
  291. $this->error['warning'] = $this->language->get('error_default');
  292. }
  293. $product_total = $this->model_catalog_product->getTotalProductsByWeightClassId($weight_class_id);
  294. if ($product_total) {
  295. $this->error['warning'] = sprintf($this->language->get('error_product'), $product_total);
  296. }
  297. }
  298. if (!$this->error) {
  299. return TRUE;
  300. } else {
  301. return FALSE;
  302. }
  303. }
  304. }
  305. ?>