PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/controller/localisation/tax_class.php

https://gitlab.com/shapcy/opencart
PHP | 402 lines | 295 code | 107 blank | 0 comment | 65 complexity | c6ed33d609c5371f6c66773d4265d290 MD5 | raw file
  1. <?php
  2. class ControllerLocalisationTaxClass extends Controller {
  3. private $error = array();
  4. public function index() {
  5. $this->load->language('localisation/tax_class');
  6. $this->document->setTitle($this->language->get('heading_title'));
  7. $this->load->model('localisation/tax_class');
  8. $this->getList();
  9. }
  10. public function add() {
  11. $this->load->language('localisation/tax_class');
  12. $this->document->setTitle($this->language->get('heading_title'));
  13. $this->load->model('localisation/tax_class');
  14. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
  15. $this->model_localisation_tax_class->addTaxClass($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/tax_class', 'token=' . $this->session->data['token'] . $url, true));
  28. }
  29. $this->getForm();
  30. }
  31. public function edit() {
  32. $this->load->language('localisation/tax_class');
  33. $this->document->setTitle($this->language->get('heading_title'));
  34. $this->load->model('localisation/tax_class');
  35. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
  36. $this->model_localisation_tax_class->editTaxClass($this->request->get['tax_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/tax_class', 'token=' . $this->session->data['token'] . $url, true));
  49. }
  50. $this->getForm();
  51. }
  52. public function delete() {
  53. $this->load->language('localisation/tax_class');
  54. $this->document->setTitle($this->language->get('heading_title'));
  55. $this->load->model('localisation/tax_class');
  56. if (isset($this->request->post['selected']) && $this->validateDelete()) {
  57. foreach ($this->request->post['selected'] as $tax_class_id) {
  58. $this->model_localisation_tax_class->deleteTaxClass($tax_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/tax_class', 'token=' . $this->session->data['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 = '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'], true)
  105. );
  106. $data['breadcrumbs'][] = array(
  107. 'text' => $this->language->get('heading_title'),
  108. 'href' => $this->url->link('localisation/tax_class', 'token=' . $this->session->data['token'] . $url, true)
  109. );
  110. $data['add'] = $this->url->link('localisation/tax_class/add', 'token=' . $this->session->data['token'] . $url, true);
  111. $data['delete'] = $this->url->link('localisation/tax_class/delete', 'token=' . $this->session->data['token'] . $url, true);
  112. $data['tax_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. $tax_class_total = $this->model_localisation_tax_class->getTotalTaxClasses();
  120. $results = $this->model_localisation_tax_class->getTaxClasses($filter_data);
  121. foreach ($results as $result) {
  122. $data['tax_classes'][] = array(
  123. 'tax_class_id' => $result['tax_class_id'],
  124. 'title' => $result['title'],
  125. 'edit' => $this->url->link('localisation/tax_class/edit', 'token=' . $this->session->data['token'] . '&tax_class_id=' . $result['tax_class_id'] . $url, true)
  126. );
  127. }
  128. $data['heading_title'] = $this->language->get('heading_title');
  129. $data['text_list'] = $this->language->get('text_list');
  130. $data['text_no_results'] = $this->language->get('text_no_results');
  131. $data['text_confirm'] = $this->language->get('text_confirm');
  132. $data['column_title'] = $this->language->get('column_title');
  133. $data['column_action'] = $this->language->get('column_action');
  134. $data['button_add'] = $this->language->get('button_add');
  135. $data['button_edit'] = $this->language->get('button_edit');
  136. $data['button_delete'] = $this->language->get('button_delete');
  137. if (isset($this->error['warning'])) {
  138. $data['error_warning'] = $this->error['warning'];
  139. } else {
  140. $data['error_warning'] = '';
  141. }
  142. if (isset($this->session->data['success'])) {
  143. $data['success'] = $this->session->data['success'];
  144. unset($this->session->data['success']);
  145. } else {
  146. $data['success'] = '';
  147. }
  148. if (isset($this->request->post['selected'])) {
  149. $data['selected'] = (array)$this->request->post['selected'];
  150. } else {
  151. $data['selected'] = array();
  152. }
  153. $url = '';
  154. if ($order == 'ASC') {
  155. $url .= '&order=DESC';
  156. } else {
  157. $url .= '&order=ASC';
  158. }
  159. if (isset($this->request->get['page'])) {
  160. $url .= '&page=' . $this->request->get['page'];
  161. }
  162. $data['sort_title'] = $this->url->link('localisation/tax_class', 'token=' . $this->session->data['token'] . '&sort=title' . $url, true);
  163. $url = '';
  164. if (isset($this->request->get['sort'])) {
  165. $url .= '&sort=' . $this->request->get['sort'];
  166. }
  167. if (isset($this->request->get['order'])) {
  168. $url .= '&order=' . $this->request->get['order'];
  169. }
  170. $pagination = new Pagination();
  171. $pagination->total = $tax_class_total;
  172. $pagination->page = $page;
  173. $pagination->limit = $this->config->get('config_limit_admin');
  174. $pagination->url = $this->url->link('localisation/tax_class', 'token=' . $this->session->data['token'] . $url . '&page={page}', true);
  175. $data['pagination'] = $pagination->render();
  176. $data['results'] = sprintf($this->language->get('text_pagination'), ($tax_class_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($tax_class_total - $this->config->get('config_limit_admin'))) ? $tax_class_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $tax_class_total, ceil($tax_class_total / $this->config->get('config_limit_admin')));
  177. $data['sort'] = $sort;
  178. $data['order'] = $order;
  179. $data['header'] = $this->load->controller('common/header');
  180. $data['column_left'] = $this->load->controller('common/column_left');
  181. $data['footer'] = $this->load->controller('common/footer');
  182. $this->response->setOutput($this->load->view('localisation/tax_class_list', $data));
  183. }
  184. protected function getForm() {
  185. $data['heading_title'] = $this->language->get('heading_title');
  186. $data['text_form'] = !isset($this->request->get['tax_class_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
  187. $data['text_shipping'] = $this->language->get('text_shipping');
  188. $data['text_payment'] = $this->language->get('text_payment');
  189. $data['text_store'] = $this->language->get('text_store');
  190. $data['entry_title'] = $this->language->get('entry_title');
  191. $data['entry_description'] = $this->language->get('entry_description');
  192. $data['entry_rate'] = $this->language->get('entry_rate');
  193. $data['entry_based'] = $this->language->get('entry_based');
  194. $data['entry_priority'] = $this->language->get('entry_priority');
  195. $data['button_save'] = $this->language->get('button_save');
  196. $data['button_cancel'] = $this->language->get('button_cancel');
  197. $data['button_rule_add'] = $this->language->get('button_rule_add');
  198. $data['button_remove'] = $this->language->get('button_remove');
  199. if (isset($this->error['warning'])) {
  200. $data['error_warning'] = $this->error['warning'];
  201. } else {
  202. $data['error_warning'] = '';
  203. }
  204. if (isset($this->error['title'])) {
  205. $data['error_title'] = $this->error['title'];
  206. } else {
  207. $data['error_title'] = '';
  208. }
  209. if (isset($this->error['description'])) {
  210. $data['error_description'] = $this->error['description'];
  211. } else {
  212. $data['error_description'] = '';
  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. $data['breadcrumbs'] = array();
  225. $data['breadcrumbs'][] = array(
  226. 'text' => $this->language->get('text_home'),
  227. 'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
  228. );
  229. $data['breadcrumbs'][] = array(
  230. 'text' => $this->language->get('heading_title'),
  231. 'href' => $this->url->link('localisation/tax_class', 'token=' . $this->session->data['token'] . $url, true)
  232. );
  233. if (!isset($this->request->get['tax_class_id'])) {
  234. $data['action'] = $this->url->link('localisation/tax_class/add', 'token=' . $this->session->data['token'] . $url, true);
  235. } else {
  236. $data['action'] = $this->url->link('localisation/tax_class/edit', 'token=' . $this->session->data['token'] . '&tax_class_id=' . $this->request->get['tax_class_id'] . $url, true);
  237. }
  238. $data['cancel'] = $this->url->link('localisation/tax_class', 'token=' . $this->session->data['token'] . $url, true);
  239. if (isset($this->request->get['tax_class_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
  240. $tax_class_info = $this->model_localisation_tax_class->getTaxClass($this->request->get['tax_class_id']);
  241. }
  242. if (isset($this->request->post['title'])) {
  243. $data['title'] = $this->request->post['title'];
  244. } elseif (!empty($tax_class_info)) {
  245. $data['title'] = $tax_class_info['title'];
  246. } else {
  247. $data['title'] = '';
  248. }
  249. if (isset($this->request->post['description'])) {
  250. $data['description'] = $this->request->post['description'];
  251. } elseif (!empty($tax_class_info)) {
  252. $data['description'] = $tax_class_info['description'];
  253. } else {
  254. $data['description'] = '';
  255. }
  256. $this->load->model('localisation/tax_rate');
  257. $data['tax_rates'] = $this->model_localisation_tax_rate->getTaxRates();
  258. if (isset($this->request->post['tax_rule'])) {
  259. $data['tax_rules'] = $this->request->post['tax_rule'];
  260. } elseif (isset($this->request->get['tax_class_id'])) {
  261. $data['tax_rules'] = $this->model_localisation_tax_class->getTaxRules($this->request->get['tax_class_id']);
  262. } else {
  263. $data['tax_rules'] = array();
  264. }
  265. $data['header'] = $this->load->controller('common/header');
  266. $data['column_left'] = $this->load->controller('common/column_left');
  267. $data['footer'] = $this->load->controller('common/footer');
  268. $this->response->setOutput($this->load->view('localisation/tax_class_form', $data));
  269. }
  270. protected function validateForm() {
  271. if (!$this->user->hasPermission('modify', 'localisation/tax_class')) {
  272. $this->error['warning'] = $this->language->get('error_permission');
  273. }
  274. if ((utf8_strlen($this->request->post['title']) < 3) || (utf8_strlen($this->request->post['title']) > 32)) {
  275. $this->error['title'] = $this->language->get('error_title');
  276. }
  277. if ((utf8_strlen($this->request->post['description']) < 3) || (utf8_strlen($this->request->post['description']) > 255)) {
  278. $this->error['description'] = $this->language->get('error_description');
  279. }
  280. return !$this->error;
  281. }
  282. protected function validateDelete() {
  283. if (!$this->user->hasPermission('modify', 'localisation/tax_class')) {
  284. $this->error['warning'] = $this->language->get('error_permission');
  285. }
  286. $this->load->model('catalog/product');
  287. foreach ($this->request->post['selected'] as $tax_class_id) {
  288. $product_total = $this->model_catalog_product->getTotalProductsByTaxClassId($tax_class_id);
  289. if ($product_total) {
  290. $this->error['warning'] = sprintf($this->language->get('error_product'), $product_total);
  291. }
  292. }
  293. return !$this->error;
  294. }
  295. }