PageRenderTime 31ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/zirrow/ecoelka.com
PHP | 373 lines | 273 code | 100 blank | 0 comment | 65 complexity | 143220e5d058908ea01857aa87a4c69d 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', 'user_token=' . $this->session->data['user_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', 'user_token=' . $this->session->data['user_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', 'user_token=' . $this->session->data['user_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', 'user_token=' . $this->session->data['user_token'], true)
  105. );
  106. $data['breadcrumbs'][] = array(
  107. 'text' => $this->language->get('heading_title'),
  108. 'href' => $this->url->link('localisation/tax_class', 'user_token=' . $this->session->data['user_token'] . $url, true)
  109. );
  110. $data['add'] = $this->url->link('localisation/tax_class/add', 'user_token=' . $this->session->data['user_token'] . $url, true);
  111. $data['delete'] = $this->url->link('localisation/tax_class/delete', 'user_token=' . $this->session->data['user_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', 'user_token=' . $this->session->data['user_token'] . '&tax_class_id=' . $result['tax_class_id'] . $url, true)
  126. );
  127. }
  128. if (isset($this->error['warning'])) {
  129. $data['error_warning'] = $this->error['warning'];
  130. } else {
  131. $data['error_warning'] = '';
  132. }
  133. if (isset($this->session->data['success'])) {
  134. $data['success'] = $this->session->data['success'];
  135. unset($this->session->data['success']);
  136. } else {
  137. $data['success'] = '';
  138. }
  139. if (isset($this->request->post['selected'])) {
  140. $data['selected'] = (array)$this->request->post['selected'];
  141. } else {
  142. $data['selected'] = array();
  143. }
  144. $url = '';
  145. if ($order == 'ASC') {
  146. $url .= '&order=DESC';
  147. } else {
  148. $url .= '&order=ASC';
  149. }
  150. if (isset($this->request->get['page'])) {
  151. $url .= '&page=' . $this->request->get['page'];
  152. }
  153. $data['sort_title'] = $this->url->link('localisation/tax_class', 'user_token=' . $this->session->data['user_token'] . '&sort=title' . $url, true);
  154. $url = '';
  155. if (isset($this->request->get['sort'])) {
  156. $url .= '&sort=' . $this->request->get['sort'];
  157. }
  158. if (isset($this->request->get['order'])) {
  159. $url .= '&order=' . $this->request->get['order'];
  160. }
  161. $pagination = new Pagination();
  162. $pagination->total = $tax_class_total;
  163. $pagination->page = $page;
  164. $pagination->limit = $this->config->get('config_limit_admin');
  165. $pagination->url = $this->url->link('localisation/tax_class', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}', true);
  166. $data['pagination'] = $pagination->render();
  167. $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')));
  168. $data['sort'] = $sort;
  169. $data['order'] = $order;
  170. $data['header'] = $this->load->controller('common/header');
  171. $data['column_left'] = $this->load->controller('common/column_left');
  172. $data['footer'] = $this->load->controller('common/footer');
  173. $this->response->setOutput($this->load->view('localisation/tax_class_list', $data));
  174. }
  175. protected function getForm() {
  176. $data['text_form'] = !isset($this->request->get['tax_class_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
  177. if (isset($this->error['warning'])) {
  178. $data['error_warning'] = $this->error['warning'];
  179. } else {
  180. $data['error_warning'] = '';
  181. }
  182. if (isset($this->error['title'])) {
  183. $data['error_title'] = $this->error['title'];
  184. } else {
  185. $data['error_title'] = '';
  186. }
  187. if (isset($this->error['description'])) {
  188. $data['error_description'] = $this->error['description'];
  189. } else {
  190. $data['error_description'] = '';
  191. }
  192. $url = '';
  193. if (isset($this->request->get['sort'])) {
  194. $url .= '&sort=' . $this->request->get['sort'];
  195. }
  196. if (isset($this->request->get['order'])) {
  197. $url .= '&order=' . $this->request->get['order'];
  198. }
  199. if (isset($this->request->get['page'])) {
  200. $url .= '&page=' . $this->request->get['page'];
  201. }
  202. $data['breadcrumbs'] = array();
  203. $data['breadcrumbs'][] = array(
  204. 'text' => $this->language->get('text_home'),
  205. 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
  206. );
  207. $data['breadcrumbs'][] = array(
  208. 'text' => $this->language->get('heading_title'),
  209. 'href' => $this->url->link('localisation/tax_class', 'user_token=' . $this->session->data['user_token'] . $url, true)
  210. );
  211. if (!isset($this->request->get['tax_class_id'])) {
  212. $data['action'] = $this->url->link('localisation/tax_class/add', 'user_token=' . $this->session->data['user_token'] . $url, true);
  213. } else {
  214. $data['action'] = $this->url->link('localisation/tax_class/edit', 'user_token=' . $this->session->data['user_token'] . '&tax_class_id=' . $this->request->get['tax_class_id'] . $url, true);
  215. }
  216. $data['cancel'] = $this->url->link('localisation/tax_class', 'user_token=' . $this->session->data['user_token'] . $url, true);
  217. if (isset($this->request->get['tax_class_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
  218. $tax_class_info = $this->model_localisation_tax_class->getTaxClass($this->request->get['tax_class_id']);
  219. }
  220. if (isset($this->request->post['title'])) {
  221. $data['title'] = $this->request->post['title'];
  222. } elseif (!empty($tax_class_info)) {
  223. $data['title'] = $tax_class_info['title'];
  224. } else {
  225. $data['title'] = '';
  226. }
  227. if (isset($this->request->post['description'])) {
  228. $data['description'] = $this->request->post['description'];
  229. } elseif (!empty($tax_class_info)) {
  230. $data['description'] = $tax_class_info['description'];
  231. } else {
  232. $data['description'] = '';
  233. }
  234. $this->load->model('localisation/tax_rate');
  235. $data['tax_rates'] = $this->model_localisation_tax_rate->getTaxRates();
  236. if (isset($this->request->post['tax_rule'])) {
  237. $data['tax_rules'] = $this->request->post['tax_rule'];
  238. } elseif (isset($this->request->get['tax_class_id'])) {
  239. $data['tax_rules'] = $this->model_localisation_tax_class->getTaxRules($this->request->get['tax_class_id']);
  240. } else {
  241. $data['tax_rules'] = array();
  242. }
  243. $data['header'] = $this->load->controller('common/header');
  244. $data['column_left'] = $this->load->controller('common/column_left');
  245. $data['footer'] = $this->load->controller('common/footer');
  246. $this->response->setOutput($this->load->view('localisation/tax_class_form', $data));
  247. }
  248. protected function validateForm() {
  249. if (!$this->user->hasPermission('modify', 'localisation/tax_class')) {
  250. $this->error['warning'] = $this->language->get('error_permission');
  251. }
  252. if ((utf8_strlen($this->request->post['title']) < 3) || (utf8_strlen($this->request->post['title']) > 32)) {
  253. $this->error['title'] = $this->language->get('error_title');
  254. }
  255. if ((utf8_strlen($this->request->post['description']) < 3) || (utf8_strlen($this->request->post['description']) > 255)) {
  256. $this->error['description'] = $this->language->get('error_description');
  257. }
  258. return !$this->error;
  259. }
  260. protected function validateDelete() {
  261. if (!$this->user->hasPermission('modify', 'localisation/tax_class')) {
  262. $this->error['warning'] = $this->language->get('error_permission');
  263. }
  264. $this->load->model('catalog/product');
  265. foreach ($this->request->post['selected'] as $tax_class_id) {
  266. $product_total = $this->model_catalog_product->getTotalProductsByTaxClassId($tax_class_id);
  267. if ($product_total) {
  268. $this->error['warning'] = sprintf($this->language->get('error_product'), $product_total);
  269. }
  270. }
  271. return !$this->error;
  272. }
  273. }