PageRenderTime 57ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/controller/localisation/return_reason.php

https://bitbucket.org/DidenkoDima/opta
PHP | 370 lines | 263 code | 103 blank | 4 comment | 54 complexity | cc880ce312dddd630ac066af539ea3c9 MD5 | raw file
  1. <?php
  2. // * @copyright OPENCART.PRO 2011 - 2016.
  3. // * @forum http://forum.opencart.pro
  4. // * @source See SOURCE.txt for source and other copyright.
  5. // * @license GNU General Public License version 3; see LICENSE.txt
  6. class ControllerLocalisationReturnReason extends Controller {
  7. private $error = array();
  8. public function index() {
  9. $this->load->language('localisation/return_reason');
  10. $this->document->setTitle($this->language->get('heading_title'));
  11. $this->load->model('localisation/return_reason');
  12. $this->getList();
  13. }
  14. public function add() {
  15. $this->load->language('localisation/return_reason');
  16. $this->document->setTitle($this->language->get('heading_title'));
  17. $this->load->model('localisation/return_reason');
  18. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
  19. $this->model_localisation_return_reason->addReturnReason($this->request->post);
  20. $this->session->data['success'] = $this->language->get('text_success');
  21. $url = '';
  22. if (isset($this->request->get['sort'])) {
  23. $url .= '&sort=' . $this->request->get['sort'];
  24. }
  25. if (isset($this->request->get['order'])) {
  26. $url .= '&order=' . $this->request->get['order'];
  27. }
  28. if (isset($this->request->get['page'])) {
  29. $url .= '&page=' . $this->request->get['page'];
  30. }
  31. $this->response->redirect($this->url->link('localisation/return_reason', 'token=' . $this->session->data['token'] . $url, true));
  32. }
  33. $this->getForm();
  34. }
  35. public function edit() {
  36. $this->load->language('localisation/return_reason');
  37. $this->document->setTitle($this->language->get('heading_title'));
  38. $this->load->model('localisation/return_reason');
  39. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
  40. $this->model_localisation_return_reason->editReturnReason($this->request->get['return_reason_id'], $this->request->post);
  41. $this->session->data['success'] = $this->language->get('text_success');
  42. $url = '';
  43. if (isset($this->request->get['sort'])) {
  44. $url .= '&sort=' . $this->request->get['sort'];
  45. }
  46. if (isset($this->request->get['order'])) {
  47. $url .= '&order=' . $this->request->get['order'];
  48. }
  49. if (isset($this->request->get['page'])) {
  50. $url .= '&page=' . $this->request->get['page'];
  51. }
  52. $this->response->redirect($this->url->link('localisation/return_reason', 'token=' . $this->session->data['token'] . $url, true));
  53. }
  54. $this->getForm();
  55. }
  56. public function delete() {
  57. $this->load->language('localisation/return_reason');
  58. $this->document->setTitle($this->language->get('heading_title'));
  59. $this->load->model('localisation/return_reason');
  60. if (isset($this->request->post['selected']) && $this->validateDelete()) {
  61. foreach ($this->request->post['selected'] as $return_reason_id) {
  62. $this->model_localisation_return_reason->deleteReturnReason($return_reason_id);
  63. }
  64. $this->session->data['success'] = $this->language->get('text_success');
  65. $url = '';
  66. if (isset($this->request->get['sort'])) {
  67. $url .= '&sort=' . $this->request->get['sort'];
  68. }
  69. if (isset($this->request->get['order'])) {
  70. $url .= '&order=' . $this->request->get['order'];
  71. }
  72. if (isset($this->request->get['page'])) {
  73. $url .= '&page=' . $this->request->get['page'];
  74. }
  75. $this->response->redirect($this->url->link('localisation/return_reason', 'token=' . $this->session->data['token'] . $url, true));
  76. }
  77. $this->getList();
  78. }
  79. protected function getList() {
  80. if (isset($this->request->get['sort'])) {
  81. $sort = $this->request->get['sort'];
  82. } else {
  83. $sort = 'name';
  84. }
  85. if (isset($this->request->get['order'])) {
  86. $order = $this->request->get['order'];
  87. } else {
  88. $order = 'ASC';
  89. }
  90. if (isset($this->request->get['page'])) {
  91. $page = $this->request->get['page'];
  92. } else {
  93. $page = 1;
  94. }
  95. $url = '';
  96. if (isset($this->request->get['sort'])) {
  97. $url .= '&sort=' . $this->request->get['sort'];
  98. }
  99. if (isset($this->request->get['order'])) {
  100. $url .= '&order=' . $this->request->get['order'];
  101. }
  102. if (isset($this->request->get['page'])) {
  103. $url .= '&page=' . $this->request->get['page'];
  104. }
  105. $data['breadcrumbs'] = array();
  106. $data['breadcrumbs'][] = array(
  107. 'text' => $this->language->get('text_home'),
  108. 'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
  109. );
  110. $data['breadcrumbs'][] = array(
  111. 'text' => $this->language->get('heading_title'),
  112. 'href' => $this->url->link('localisation/return_reason', 'token=' . $this->session->data['token'] . $url, true)
  113. );
  114. $data['add'] = $this->url->link('localisation/return_reason/add', 'token=' . $this->session->data['token'] . $url, true);
  115. $data['delete'] = $this->url->link('localisation/return_reason/delete', 'token=' . $this->session->data['token'] . $url, true);
  116. $data['return_reasons'] = array();
  117. $filter_data = array(
  118. 'sort' => $sort,
  119. 'order' => $order,
  120. 'start' => ($page - 1) * $this->config->get('config_limit_admin'),
  121. 'limit' => $this->config->get('config_limit_admin')
  122. );
  123. $return_reason_total = $this->model_localisation_return_reason->getTotalReturnReasons();
  124. $results = $this->model_localisation_return_reason->getReturnReasons($filter_data);
  125. foreach ($results as $result) {
  126. $data['return_reasons'][] = array(
  127. 'return_reason_id' => $result['return_reason_id'],
  128. 'name' => $result['name'],
  129. 'edit' => $this->url->link('localisation/return_reason/edit', 'token=' . $this->session->data['token'] . '&return_reason_id=' . $result['return_reason_id'] . $url, true)
  130. );
  131. }
  132. $data['heading_title'] = $this->language->get('heading_title');
  133. $data['text_list'] = $this->language->get('text_list');
  134. $data['text_no_results'] = $this->language->get('text_no_results');
  135. $data['text_confirm'] = $this->language->get('text_confirm');
  136. $data['column_name'] = $this->language->get('column_name');
  137. $data['column_action'] = $this->language->get('column_action');
  138. $data['button_add'] = $this->language->get('button_add');
  139. $data['button_edit'] = $this->language->get('button_edit');
  140. $data['button_delete'] = $this->language->get('button_delete');
  141. if (isset($this->error['warning'])) {
  142. $data['error_warning'] = $this->error['warning'];
  143. } else {
  144. $data['error_warning'] = '';
  145. }
  146. if (isset($this->session->data['success'])) {
  147. $data['success'] = $this->session->data['success'];
  148. unset($this->session->data['success']);
  149. } else {
  150. $data['success'] = '';
  151. }
  152. if (isset($this->request->post['selected'])) {
  153. $data['selected'] = (array)$this->request->post['selected'];
  154. } else {
  155. $data['selected'] = array();
  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. $data['sort_name'] = $this->url->link('localisation/return_reason', 'token=' . $this->session->data['token'] . '&sort=name' . $url, true);
  167. $url = '';
  168. if (isset($this->request->get['sort'])) {
  169. $url .= '&sort=' . $this->request->get['sort'];
  170. }
  171. if (isset($this->request->get['order'])) {
  172. $url .= '&order=' . $this->request->get['order'];
  173. }
  174. $pagination = new Pagination();
  175. $pagination->total = $return_reason_total;
  176. $pagination->page = $page;
  177. $pagination->limit = $this->config->get('config_limit_admin');
  178. $pagination->url = $this->url->link('localisation/return_reason', 'token=' . $this->session->data['token'] . $url . '&page={page}', true);
  179. $data['pagination'] = $pagination->render();
  180. $data['results'] = sprintf($this->language->get('text_pagination'), ($return_reason_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($return_reason_total - $this->config->get('config_limit_admin'))) ? $return_reason_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $return_reason_total, ceil($return_reason_total / $this->config->get('config_limit_admin')));
  181. $data['sort'] = $sort;
  182. $data['order'] = $order;
  183. $data['header'] = $this->load->controller('common/header');
  184. $data['column_left'] = $this->load->controller('common/column_left');
  185. $data['footer'] = $this->load->controller('common/footer');
  186. $this->response->setOutput($this->load->view('localisation/return_reason_list', $data));
  187. }
  188. protected function getForm() {
  189. $data['heading_title'] = $this->language->get('heading_title');
  190. $data['text_form'] = !isset($this->request->get['return_reason_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
  191. $data['entry_name'] = $this->language->get('entry_name');
  192. $data['button_save'] = $this->language->get('button_save');
  193. $data['button_cancel'] = $this->language->get('button_cancel');
  194. if (isset($this->error['warning'])) {
  195. $data['error_warning'] = $this->error['warning'];
  196. } else {
  197. $data['error_warning'] = '';
  198. }
  199. if (isset($this->error['name'])) {
  200. $data['error_name'] = $this->error['name'];
  201. } else {
  202. $data['error_name'] = array();
  203. }
  204. $url = '';
  205. if (isset($this->request->get['sort'])) {
  206. $url .= '&sort=' . $this->request->get['sort'];
  207. }
  208. if (isset($this->request->get['order'])) {
  209. $url .= '&order=' . $this->request->get['order'];
  210. }
  211. if (isset($this->request->get['page'])) {
  212. $url .= '&page=' . $this->request->get['page'];
  213. }
  214. $data['breadcrumbs'] = array();
  215. $data['breadcrumbs'][] = array(
  216. 'text' => $this->language->get('text_home'),
  217. 'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
  218. );
  219. $data['breadcrumbs'][] = array(
  220. 'text' => $this->language->get('heading_title'),
  221. 'href' => $this->url->link('localisation/return_reason', 'token=' . $this->session->data['token'] . $url, true)
  222. );
  223. if (!isset($this->request->get['return_reason_id'])) {
  224. $data['action'] = $this->url->link('localisation/return_reason/add', 'token=' . $this->session->data['token'] . $url, true);
  225. } else {
  226. $data['action'] = $this->url->link('localisation/return_reason/edit', 'token=' . $this->session->data['token'] . '&return_reason_id=' . $this->request->get['return_reason_id'] . $url, true);
  227. }
  228. $data['cancel'] = $this->url->link('localisation/return_reason', 'token=' . $this->session->data['token'] . $url, true);
  229. $this->load->model('localisation/language');
  230. $data['languages'] = $this->model_localisation_language->getLanguages();
  231. if (isset($this->request->post['return_reason'])) {
  232. $data['return_reason'] = $this->request->post['return_reason'];
  233. } elseif (isset($this->request->get['return_reason_id'])) {
  234. $data['return_reason'] = $this->model_localisation_return_reason->getReturnReasonDescriptions($this->request->get['return_reason_id']);
  235. } else {
  236. $data['return_reason'] = array();
  237. }
  238. $data['header'] = $this->load->controller('common/header');
  239. $data['column_left'] = $this->load->controller('common/column_left');
  240. $data['footer'] = $this->load->controller('common/footer');
  241. $this->response->setOutput($this->load->view('localisation/return_reason_form', $data));
  242. }
  243. protected function validateForm() {
  244. if (!$this->user->hasPermission('modify', 'localisation/return_reason')) {
  245. $this->error['warning'] = $this->language->get('error_permission');
  246. }
  247. foreach ($this->request->post['return_reason'] as $language_id => $value) {
  248. if ((utf8_strlen($value['name']) < 3) || (utf8_strlen($value['name']) > 128)) {
  249. $this->error['name'][$language_id] = $this->language->get('error_name');
  250. }
  251. }
  252. return !$this->error;
  253. }
  254. protected function validateDelete() {
  255. if (!$this->user->hasPermission('modify', 'localisation/return_reason')) {
  256. $this->error['warning'] = $this->language->get('error_permission');
  257. }
  258. $this->load->model('sale/return');
  259. foreach ($this->request->post['selected'] as $return_reason_id) {
  260. $return_total = $this->model_sale_return->getTotalReturnsByReturnReasonId($return_reason_id);
  261. if ($return_total) {
  262. $this->error['warning'] = sprintf($this->language->get('error_return'), $return_total);
  263. }
  264. }
  265. return !$this->error;
  266. }
  267. }