/upload/admin/controller/localisation/stock_status.php

https://bitbucket.org/mjalajel/opencart · PHP · 381 lines · 278 code · 103 blank · 0 comment · 60 complexity · 5100800b2f67256cdfabe26ce2672005 MD5 · raw file

  1. <?php
  2. class ControllerLocalisationStockStatus extends Controller {
  3. private $error = array();
  4. public function index() {
  5. $this->language->load('localisation/stock_status');
  6. $this->document->setTitle($this->language->get('heading_title'));
  7. $this->load->model('localisation/stock_status');
  8. $this->getList();
  9. }
  10. public function insert() {
  11. $this->language->load('localisation/stock_status');
  12. $this->document->setTitle($this->language->get('heading_title'));
  13. $this->load->model('localisation/stock_status');
  14. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
  15. $this->model_localisation_stock_status->addStockStatus($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/stock_status', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  28. }
  29. $this->getForm();
  30. }
  31. public function update() {
  32. $this->language->load('localisation/stock_status');
  33. $this->document->setTitle($this->language->get('heading_title'));
  34. $this->load->model('localisation/stock_status');
  35. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
  36. $this->model_localisation_stock_status->editStockStatus($this->request->get['stock_status_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/stock_status', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  49. }
  50. $this->getForm();
  51. }
  52. public function delete() {
  53. $this->language->load('localisation/stock_status');
  54. $this->document->setTitle($this->language->get('heading_title'));
  55. $this->load->model('localisation/stock_status');
  56. if (isset($this->request->post['selected']) && $this->validateDelete()) {
  57. foreach ($this->request->post['selected'] as $stock_status_id) {
  58. $this->model_localisation_stock_status->deleteStockStatus($stock_status_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/stock_status', '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 = 'name';
  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/stock_status', 'token=' . $this->session->data['token'] . $url, 'SSL')
  109. );
  110. $this->data['insert'] = $this->url->link('localisation/stock_status/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
  111. $this->data['delete'] = $this->url->link('localisation/stock_status/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');
  112. $this->data['stock_statuses'] = 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. $stock_status_total = $this->model_localisation_stock_status->getTotalStockStatuses();
  120. $results = $this->model_localisation_stock_status->getStockStatuses($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/stock_status/update', 'token=' . $this->session->data['token'] . '&stock_status_id=' . $result['stock_status_id'] . $url, 'SSL')
  126. );
  127. $this->data['stock_statuses'][] = array(
  128. 'stock_status_id' => $result['stock_status_id'],
  129. 'name' => $result['name'] . (($result['stock_status_id'] == $this->config->get('config_stock_status_id')) ? $this->language->get('text_default') : null),
  130. 'selected' => isset($this->request->post['selected']) && in_array($result['stock_status_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_name'] = $this->language->get('column_name');
  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_name'] = $this->url->link('localisation/stock_status', 'token=' . $this->session->data['token'] . '&sort=name' . $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 = $stock_status_total;
  170. $pagination->page = $page;
  171. $pagination->limit = $this->config->get('config_admin_limit');
  172. $pagination->url = $this->url->link('localisation/stock_status', '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'), ($stock_status_total) ? (($page - 1) * $this->config->get('config_admin_limit')) + 1 : 0, ((($page - 1) * $this->config->get('config_admin_limit')) > ($stock_status_total - $this->config->get('config_admin_limit'))) ? $stock_status_total : ((($page - 1) * $this->config->get('config_admin_limit')) + $this->config->get('config_admin_limit')), $stock_status_total, ceil($stock_status_total / $this->config->get('config_admin_limit')));
  175. $this->data['sort'] = $sort;
  176. $this->data['order'] = $order;
  177. $this->template = 'localisation/stock_status_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['entry_name'] = $this->language->get('entry_name');
  187. $this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
  188. $this->data['button_save'] = $this->language->get('button_save');
  189. $this->data['button_cancel'] = $this->language->get('button_cancel');
  190. if (isset($this->error['warning'])) {
  191. $this->data['error_warning'] = $this->error['warning'];
  192. } else {
  193. $this->data['error_warning'] = '';
  194. }
  195. if (isset($this->error['name'])) {
  196. $this->data['error_name'] = $this->error['name'];
  197. } else {
  198. $this->data['error_name'] = array();
  199. }
  200. $url = '';
  201. if (isset($this->request->get['sort'])) {
  202. $url .= '&sort=' . $this->request->get['sort'];
  203. }
  204. if (isset($this->request->get['order'])) {
  205. $url .= '&order=' . $this->request->get['order'];
  206. }
  207. if (isset($this->request->get['page'])) {
  208. $url .= '&page=' . $this->request->get['page'];
  209. }
  210. $this->data['breadcrumbs'] = array();
  211. $this->data['breadcrumbs'][] = array(
  212. 'text' => $this->language->get('text_home'),
  213. 'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
  214. );
  215. $this->data['breadcrumbs'][] = array(
  216. 'text' => $this->language->get('heading_title'),
  217. 'href' => $this->url->link('localisation/stock_status', 'token=' . $this->session->data['token'] . $url, 'SSL')
  218. );
  219. if (!isset($this->request->get['stock_status_id'])) {
  220. $this->data['action'] = $this->url->link('localisation/stock_status/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
  221. } else {
  222. $this->data['action'] = $this->url->link('localisation/stock_status/update', 'token=' . $this->session->data['token'] . '&stock_status_id=' . $this->request->get['stock_status_id'] . $url, 'SSL');
  223. }
  224. $this->data['cancel'] = $this->url->link('localisation/stock_status', 'token=' . $this->session->data['token'] . $url, 'SSL');
  225. $this->load->model('localisation/language');
  226. $this->data['languages'] = $this->model_localisation_language->getLanguages();
  227. if (isset($this->request->post['stock_status'])) {
  228. $this->data['stock_status'] = $this->request->post['stock_status'];
  229. } elseif (isset($this->request->get['stock_status_id'])) {
  230. $this->data['stock_status'] = $this->model_localisation_stock_status->getStockStatusDescriptions($this->request->get['stock_status_id']);
  231. } else {
  232. $this->data['stock_status'] = array();
  233. }
  234. $this->template = 'localisation/stock_status_form.tpl';
  235. $this->children = array(
  236. 'common/header',
  237. 'common/footer'
  238. );
  239. $this->response->setOutput($this->render());
  240. }
  241. protected function validateForm() {
  242. if (!$this->user->hasPermission('modify', 'localisation/stock_status')) {
  243. $this->error['warning'] = $this->language->get('error_permission');
  244. }
  245. foreach ($this->request->post['stock_status'] as $language_id => $value) {
  246. if ((utf8_strlen($value['name']) < 3) || (utf8_strlen($value['name']) > 32)) {
  247. $this->error['name'][$language_id] = $this->language->get('error_name');
  248. }
  249. }
  250. if (!$this->error) {
  251. return true;
  252. } else {
  253. return false;
  254. }
  255. }
  256. protected function validateDelete() {
  257. if (!$this->user->hasPermission('modify', 'localisation/stock_status')) {
  258. $this->error['warning'] = $this->language->get('error_permission');
  259. }
  260. $this->load->model('setting/store');
  261. $this->load->model('catalog/product');
  262. foreach ($this->request->post['selected'] as $stock_status_id) {
  263. if ($this->config->get('config_stock_status_id') == $stock_status_id) {
  264. $this->error['warning'] = $this->language->get('error_default');
  265. }
  266. $product_total = $this->model_catalog_product->getTotalProductsByStockStatusId($stock_status_id);
  267. if ($product_total) {
  268. $this->error['warning'] = sprintf($this->language->get('error_product'), $product_total);
  269. }
  270. }
  271. if (!$this->error) {
  272. return true;
  273. } else {
  274. return false;
  275. }
  276. }
  277. }
  278. ?>