/upload/admin/controller/localisation/tax_class.php

https://bitbucket.org/mjalajel/opencart · PHP · 413 lines · 305 code · 108 blank · 0 comment · 68 complexity · c4b9026ceffd7dad89db3bd24cc44e9a MD5 · raw file

  1. <?php
  2. class ControllerLocalisationTaxClass extends Controller {
  3. private $error = array();
  4. public function index() {
  5. $this->language->load('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 insert() {
  11. $this->language->load('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->redirect($this->url->link('localisation/tax_class', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  28. }
  29. $this->getForm();
  30. }
  31. public function update() {
  32. $this->language->load('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->redirect($this->url->link('localisation/tax_class', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  49. }
  50. $this->getForm();
  51. }
  52. public function delete() {
  53. $this->language->load('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->redirect($this->url->link('localisation/tax_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/dashboard', 'token=' . $this->session->data['token'], 'SSL')
  105. );
  106. $this->data['breadcrumbs'][] = array(
  107. 'text' => $this->language->get('heading_title'),
  108. 'href' => $this->url->link('localisation/tax_class', 'token=' . $this->session->data['token'] . $url, 'SSL')
  109. );
  110. $this->data['insert'] = $this->url->link('localisation/tax_class/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
  111. $this->data['delete'] = $this->url->link('localisation/tax_class/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');
  112. $this->data['tax_classes'] = array();
  113. $data = array(
  114. 'sort' => $sort,
  115. 'order' => $order,
  116. 'start' => ($page - 1) * $this->config->get('config_admin_limit'),
  117. 'limit' => $this->config->get('config_admin_limit')
  118. );
  119. $tax_class_total = $this->model_localisation_tax_class->getTotalTaxClasses();
  120. $results = $this->model_localisation_tax_class->getTaxClasses($data);
  121. foreach ($results as $result) {
  122. $action = array();
  123. $action[] = array(
  124. 'text' => $this->language->get('text_edit'),
  125. 'href' => $this->url->link('localisation/tax_class/update', 'token=' . $this->session->data['token'] . '&tax_class_id=' . $result['tax_class_id'] . $url, 'SSL')
  126. );
  127. $this->data['tax_classes'][] = array(
  128. 'tax_class_id' => $result['tax_class_id'],
  129. 'title' => $result['title'],
  130. 'selected' => isset($this->request->post['selected']) && in_array($result['tax_class_id'], $this->request->post['selected']),
  131. 'action' => $action
  132. );
  133. }
  134. $this->data['heading_title'] = $this->language->get('heading_title');
  135. $this->data['text_no_results'] = $this->language->get('text_no_results');
  136. $this->data['column_title'] = $this->language->get('column_title');
  137. $this->data['column_action'] = $this->language->get('column_action');
  138. $this->data['button_insert'] = $this->language->get('button_insert');
  139. $this->data['button_delete'] = $this->language->get('button_delete');
  140. if (isset($this->error['warning'])) {
  141. $this->data['error_warning'] = $this->error['warning'];
  142. } else {
  143. $this->data['error_warning'] = '';
  144. }
  145. if (isset($this->session->data['success'])) {
  146. $this->data['success'] = $this->session->data['success'];
  147. unset($this->session->data['success']);
  148. } else {
  149. $this->data['success'] = '';
  150. }
  151. $url = '';
  152. if ($order == 'ASC') {
  153. $url .= '&order=DESC';
  154. } else {
  155. $url .= '&order=ASC';
  156. }
  157. if (isset($this->request->get['page'])) {
  158. $url .= '&page=' . $this->request->get['page'];
  159. }
  160. $this->data['sort_title'] = $this->url->link('localisation/tax_class', 'token=' . $this->session->data['token'] . '&sort=title' . $url, 'SSL');
  161. $url = '';
  162. if (isset($this->request->get['sort'])) {
  163. $url .= '&sort=' . $this->request->get['sort'];
  164. }
  165. if (isset($this->request->get['order'])) {
  166. $url .= '&order=' . $this->request->get['order'];
  167. }
  168. $pagination = new Pagination();
  169. $pagination->total = $tax_class_total;
  170. $pagination->page = $page;
  171. $pagination->limit = $this->config->get('config_admin_limit');
  172. $pagination->url = $this->url->link('localisation/tax_class', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
  173. $this->data['pagination'] = $pagination->render();
  174. $this->data['results'] = sprintf($this->language->get('text_pagination'), ($tax_class_total) ? (($page - 1) * $this->config->get('config_admin_limit')) + 1 : 0, ((($page - 1) * $this->config->get('config_admin_limit')) > ($tax_class_total - $this->config->get('config_admin_limit'))) ? $tax_class_total : ((($page - 1) * $this->config->get('config_admin_limit')) + $this->config->get('config_admin_limit')), $tax_class_total, ceil($tax_class_total / $this->config->get('config_admin_limit')));
  175. $this->data['sort'] = $sort;
  176. $this->data['order'] = $order;
  177. $this->template = 'localisation/tax_class_list.tpl';
  178. $this->children = array(
  179. 'common/header',
  180. 'common/footer',
  181. );
  182. $this->response->setOutput($this->render());
  183. }
  184. protected function getForm() {
  185. $this->data['heading_title'] = $this->language->get('heading_title');
  186. $this->data['text_shipping'] = $this->language->get('text_shipping');
  187. $this->data['text_payment'] = $this->language->get('text_payment');
  188. $this->data['text_store'] = $this->language->get('text_store');
  189. $this->data['entry_title'] = $this->language->get('entry_title');
  190. $this->data['entry_description'] = $this->language->get('entry_description');
  191. $this->data['entry_rate'] = $this->language->get('entry_rate');
  192. $this->data['entry_based'] = $this->language->get('entry_based');
  193. $this->data['entry_priority'] = $this->language->get('entry_priority');
  194. $this->data['button_save'] = $this->language->get('button_save');
  195. $this->data['button_cancel'] = $this->language->get('button_cancel');
  196. $this->data['button_add_rule'] = $this->language->get('button_add_rule');
  197. $this->data['button_remove'] = $this->language->get('button_remove');
  198. if (isset($this->error['warning'])) {
  199. $this->data['error_warning'] = $this->error['warning'];
  200. } else {
  201. $this->data['error_warning'] = '';
  202. }
  203. if (isset($this->error['title'])) {
  204. $this->data['error_title'] = $this->error['title'];
  205. } else {
  206. $this->data['error_title'] = '';
  207. }
  208. if (isset($this->error['description'])) {
  209. $this->data['error_description'] = $this->error['description'];
  210. } else {
  211. $this->data['error_description'] = '';
  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. $this->data['breadcrumbs'] = array();
  224. $this->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. $this->data['breadcrumbs'][] = array(
  229. 'text' => $this->language->get('heading_title'),
  230. 'href' => $this->url->link('localisation/tax_class', 'token=' . $this->session->data['token'] . $url, 'SSL')
  231. );
  232. if (!isset($this->request->get['tax_class_id'])) {
  233. $this->data['action'] = $this->url->link('localisation/tax_class/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
  234. } else {
  235. $this->data['action'] = $this->url->link('localisation/tax_class/update', 'token=' . $this->session->data['token'] . '&tax_class_id=' . $this->request->get['tax_class_id'] . $url, 'SSL');
  236. }
  237. $this->data['cancel'] = $this->url->link('localisation/tax_class', 'token=' . $this->session->data['token'] . $url, 'SSL');
  238. if (isset($this->request->get['tax_class_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
  239. $tax_class_info = $this->model_localisation_tax_class->getTaxClass($this->request->get['tax_class_id']);
  240. }
  241. if (isset($this->request->post['title'])) {
  242. $this->data['title'] = $this->request->post['title'];
  243. } elseif (!empty($tax_class_info)) {
  244. $this->data['title'] = $tax_class_info['title'];
  245. } else {
  246. $this->data['title'] = '';
  247. }
  248. if (isset($this->request->post['description'])) {
  249. $this->data['description'] = $this->request->post['description'];
  250. } elseif (!empty($tax_class_info)) {
  251. $this->data['description'] = $tax_class_info['description'];
  252. } else {
  253. $this->data['description'] = '';
  254. }
  255. $this->load->model('localisation/tax_rate');
  256. $this->data['tax_rates'] = $this->model_localisation_tax_rate->getTaxRates();
  257. if (isset($this->request->post['tax_rule'])) {
  258. $this->data['tax_rules'] = $this->request->post['tax_rule'];
  259. } elseif (isset($this->request->get['tax_class_id'])) {
  260. $this->data['tax_rules'] = $this->model_localisation_tax_class->getTaxRules($this->request->get['tax_class_id']);
  261. } else {
  262. $this->data['tax_rules'] = array();
  263. }
  264. $this->template = 'localisation/tax_class_form.tpl';
  265. $this->children = array(
  266. 'common/header',
  267. 'common/footer',
  268. );
  269. $this->response->setOutput($this->render());
  270. }
  271. protected function validateForm() {
  272. if (!$this->user->hasPermission('modify', 'localisation/tax_class')) {
  273. $this->error['warning'] = $this->language->get('error_permission');
  274. }
  275. if ((utf8_strlen($this->request->post['title']) < 3) || (utf8_strlen($this->request->post['title']) > 32)) {
  276. $this->error['title'] = $this->language->get('error_title');
  277. }
  278. if ((utf8_strlen($this->request->post['description']) < 3) || (utf8_strlen($this->request->post['description']) > 255)) {
  279. $this->error['description'] = $this->language->get('error_description');
  280. }
  281. if (!$this->error) {
  282. return true;
  283. } else {
  284. return false;
  285. }
  286. }
  287. protected function validateDelete() {
  288. if (!$this->user->hasPermission('modify', 'localisation/tax_class')) {
  289. $this->error['warning'] = $this->language->get('error_permission');
  290. }
  291. $this->load->model('catalog/product');
  292. foreach ($this->request->post['selected'] as $tax_class_id) {
  293. $product_total = $this->model_catalog_product->getTotalProductsByTaxClassId($tax_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. ?>