/admin/controller/design/layout.php

https://github.com/sansanwawa/e-commerse · PHP · 419 lines · 308 code · 111 blank · 0 comment · 67 complexity · 80d801dc349fef44f2d1eb0dabb67b16 MD5 · raw file

  1. <?php
  2. class ControllerDesignLayout extends Controller {
  3. private $error = array();
  4. public function index() {
  5. $this->load->language('design/layout');
  6. $this->document->setTitle($this->language->get('heading_title'));
  7. $this->load->model('design/layout');
  8. $this->getList();
  9. }
  10. public function insert() {
  11. $this->load->language('design/layout');
  12. $this->document->setTitle($this->language->get('heading_title'));
  13. $this->load->model('design/layout');
  14. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
  15. $this->model_design_layout->addLayout($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('design/layout', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  28. }
  29. $this->getForm();
  30. }
  31. public function update() {
  32. $this->load->language('design/layout');
  33. $this->document->setTitle($this->language->get('heading_title'));
  34. $this->load->model('design/layout');
  35. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
  36. $this->model_design_layout->editLayout($this->request->get['layout_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('design/layout', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  49. }
  50. $this->getForm();
  51. }
  52. public function delete() {
  53. $this->load->language('design/layout');
  54. $this->document->setTitle($this->language->get('heading_title'));
  55. $this->load->model('design/layout');
  56. if (isset($this->request->post['selected']) && $this->validateDelete()) {
  57. foreach ($this->request->post['selected'] as $layout_id) {
  58. $this->model_design_layout->deleteLayout($layout_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('design/layout', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  72. }
  73. $this->getList();
  74. }
  75. private 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/home', 'token=' . $this->session->data['token'], 'SSL'),
  105. 'separator' => false
  106. );
  107. $this->data['breadcrumbs'][] = array(
  108. 'text' => $this->language->get('heading_title'),
  109. 'href' => $this->url->link('design/layout', 'token=' . $this->session->data['token'] . $url, 'SSL'),
  110. 'separator' => ' :: '
  111. );
  112. $this->data['insert'] = $this->url->link('design/layout/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
  113. $this->data['delete'] = $this->url->link('design/layout/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');
  114. $this->data['layouts'] = array();
  115. $data = array(
  116. 'sort' => $sort,
  117. 'order' => $order,
  118. 'start' => ($page - 1) * $this->config->get('config_admin_limit'),
  119. 'limit' => $this->config->get('config_admin_limit')
  120. );
  121. $layout_total = $this->model_design_layout->getTotalLayouts();
  122. $results = $this->model_design_layout->getLayouts($data);
  123. foreach ($results as $result) {
  124. $action = array();
  125. $action[] = array(
  126. 'text' => $this->language->get('text_edit'),
  127. 'href' => $this->url->link('design/layout/update', 'token=' . $this->session->data['token'] . '&layout_id=' . $result['layout_id'] . $url, 'SSL')
  128. );
  129. $this->data['layouts'][] = array(
  130. 'layout_id' => $result['layout_id'],
  131. 'name' => $result['name'],
  132. 'selected' => isset($this->request->post['selected']) && in_array($result['layout_id'], $this->request->post['selected']),
  133. 'action' => $action
  134. );
  135. }
  136. $this->data['heading_title'] = $this->language->get('heading_title');
  137. $this->data['text_no_results'] = $this->language->get('text_no_results');
  138. $this->data['column_name'] = $this->language->get('column_name');
  139. $this->data['column_action'] = $this->language->get('column_action');
  140. $this->data['button_insert'] = $this->language->get('button_insert');
  141. $this->data['button_delete'] = $this->language->get('button_delete');
  142. if (isset($this->error['warning'])) {
  143. $this->data['error_warning'] = $this->error['warning'];
  144. } else {
  145. $this->data['error_warning'] = '';
  146. }
  147. if (isset($this->session->data['success'])) {
  148. $this->data['success'] = $this->session->data['success'];
  149. unset($this->session->data['success']);
  150. } else {
  151. $this->data['success'] = '';
  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. $this->data['sort_name'] = $this->url->link('design/layout', 'token=' . $this->session->data['token'] . '&sort=name' . $url, 'SSL');
  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 = $layout_total;
  172. $pagination->page = $page;
  173. $pagination->limit = $this->config->get('config_admin_limit');
  174. $pagination->text = $this->language->get('text_pagination');
  175. $pagination->url = $this->url->link('design/layout', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
  176. $this->data['pagination'] = $pagination->render();
  177. $this->data['sort'] = $sort;
  178. $this->data['order'] = $order;
  179. $this->template = 'design/layout_list.tpl';
  180. $this->children = array(
  181. 'common/header',
  182. 'common/footer',
  183. );
  184. $this->response->setOutput($this->render());
  185. }
  186. private function getForm() {
  187. $this->data['heading_title'] = $this->language->get('heading_title');
  188. $this->data['text_default'] = $this->language->get('text_default');
  189. $this->data['entry_name'] = $this->language->get('entry_name');
  190. $this->data['entry_store'] = $this->language->get('entry_store');
  191. $this->data['entry_route'] = $this->language->get('entry_route');
  192. $this->data['button_save'] = $this->language->get('button_save');
  193. $this->data['button_cancel'] = $this->language->get('button_cancel');
  194. $this->data['button_add_route'] = $this->language->get('button_add_route');
  195. $this->data['button_remove'] = $this->language->get('button_remove');
  196. if (isset($this->error['warning'])) {
  197. $this->data['error_warning'] = $this->error['warning'];
  198. } else {
  199. $this->data['error_warning'] = '';
  200. }
  201. if (isset($this->error['name'])) {
  202. $this->data['error_name'] = $this->error['name'];
  203. } else {
  204. $this->data['error_name'] = '';
  205. }
  206. $url = '';
  207. if (isset($this->request->get['sort'])) {
  208. $url .= '&sort=' . $this->request->get['sort'];
  209. }
  210. if (isset($this->request->get['order'])) {
  211. $url .= '&order=' . $this->request->get['order'];
  212. }
  213. if (isset($this->request->get['page'])) {
  214. $url .= '&page=' . $this->request->get['page'];
  215. }
  216. $this->data['breadcrumbs'] = array();
  217. $this->data['breadcrumbs'][] = array(
  218. 'text' => $this->language->get('text_home'),
  219. 'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
  220. 'separator' => false
  221. );
  222. $this->data['breadcrumbs'][] = array(
  223. 'text' => $this->language->get('heading_title'),
  224. 'href' => $this->url->link('design/layout', 'token=' . $this->session->data['token'] . $url, 'SSL'),
  225. 'separator' => ' :: '
  226. );
  227. if (!isset($this->request->get['layout_id'])) {
  228. $this->data['action'] = $this->url->link('design/layout/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
  229. } else {
  230. $this->data['action'] = $this->url->link('design/layout/update', 'token=' . $this->session->data['token'] . '&layout_id=' . $this->request->get['layout_id'] . $url, 'SSL');
  231. }
  232. $this->data['cancel'] = $this->url->link('design/layout', 'token=' . $this->session->data['token'] . $url, 'SSL');
  233. if (isset($this->request->get['layout_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
  234. $layout_info = $this->model_design_layout->getLayout($this->request->get['layout_id']);
  235. }
  236. if (isset($this->request->post['name'])) {
  237. $this->data['name'] = $this->request->post['name'];
  238. } elseif (isset($layout_info)) {
  239. $this->data['name'] = $layout_info['name'];
  240. } else {
  241. $this->data['name'] = '';
  242. }
  243. $this->load->model('setting/store');
  244. $this->data['stores'] = $this->model_setting_store->getStores();
  245. if (isset($this->request->post['layout_route'])) {
  246. $this->data['layout_routes'] = $this->request->post['layout_route'];
  247. } elseif (isset($layout_info)) {
  248. $this->data['layout_routes'] = $this->model_design_layout->getLayoutRoutes($this->request->get['layout_id']);
  249. } else {
  250. $this->data['layout_routes'] = array();
  251. }
  252. $this->template = 'design/layout_form.tpl';
  253. $this->children = array(
  254. 'common/header',
  255. 'common/footer',
  256. );
  257. $this->response->setOutput($this->render());
  258. }
  259. private function validateForm() {
  260. if (!$this->user->hasPermission('modify', 'design/layout')) {
  261. $this->error['warning'] = $this->language->get('error_permission');
  262. }
  263. if ((strlen(utf8_decode($this->request->post['name'])) < 3) || (strlen(utf8_decode($this->request->post['name'])) > 64)) {
  264. $this->error['name'] = $this->language->get('error_name');
  265. }
  266. if (!$this->error) {
  267. return true;
  268. } else {
  269. return false;
  270. }
  271. }
  272. private function validateDelete() {
  273. if (!$this->user->hasPermission('modify', 'design/layout')) {
  274. $this->error['warning'] = $this->language->get('error_permission');
  275. }
  276. $this->load->model('setting/store');
  277. $this->load->model('catalog/product');
  278. $this->load->model('catalog/category');
  279. $this->load->model('catalog/information');
  280. foreach ($this->request->post['selected'] as $layout_id) {
  281. if ($this->config->get('config_layout_id') == $layout_id) {
  282. $this->error['warning'] = $this->language->get('error_default');
  283. }
  284. $store_total = $this->model_setting_store->getTotalStoresByLayoutId($layout_id);
  285. if ($store_total) {
  286. $this->error['warning'] = sprintf($this->language->get('error_store'), $store_total);
  287. }
  288. $product_total = $this->model_catalog_product->getTotalProductsByLayoutId($layout_id);
  289. if ($product_total) {
  290. $this->error['warning'] = sprintf($this->language->get('error_product'), $product_total);
  291. }
  292. $category_total = $this->model_catalog_category->getTotalCategoriesByLayoutId($layout_id);
  293. if ($category_total) {
  294. $this->error['warning'] = sprintf($this->language->get('error_category'), $category_total);
  295. }
  296. $information_total = $this->model_catalog_information->getTotalInformationsByLayoutId($layout_id);
  297. if ($information_total) {
  298. $this->error['warning'] = sprintf($this->language->get('error_information'), $information_total);
  299. }
  300. }
  301. if (!$this->error) {
  302. return true;
  303. } else {
  304. return false;
  305. }
  306. }
  307. }
  308. ?>