/modules/shop/classes/controller/frontend/categories.php

https://bitbucket.org/seyar/kinda.local · PHP · 237 lines · 192 code · 45 blank · 0 comment · 28 complexity · 52ae6dc1e161929a6c748c63da3b584f MD5 · raw file

  1. <?php
  2. defined('SYSPATH') OR die('No direct access allowed.');
  3. class Controller_Frontend_Categories extends Controller_Content {
  4. public $template = 'shop_category.tpl';
  5. private $_search = false;
  6. public function before() {
  7. parent::before();
  8. }
  9. public function action_index()
  10. {
  11. $category_info = Model_Frontend_Categories::info($this->request->param('id'));
  12. $this->view->assign('category_info', $category_info);
  13. $this->page_info = array(
  14. 'page_title' => arr::get($category_info, 'name', $this->page_info['page_title']),
  15. 'page_keywords' => arr::get($category_info, 'seo_keywords', $this->page_info['page_keywords']),
  16. 'page_description' => arr::get($category_info, 'seo_description', $this->page_info['page_description']),
  17. );
  18. $filterableFields = Model_Frontend_Categories::getFilterableFields($this->request->param('id'));
  19. $this->view->assign('filterableFields', $filterableFields);
  20. $filter = array();
  21. if (Request::$method == 'POST') {
  22. if (isset($_POST['filter'])) {
  23. if (isset($_POST['filter']['price'])) {
  24. $filter['price'] = array_map('floatval', $_POST['filter']['price']);
  25. }
  26. foreach ($filterableFields as $field) {
  27. if (isset($_POST['filter'][$field['alias']]) && !empty($_POST['filter'][$field['alias']])) {
  28. switch ($field['type']) {
  29. case 'int' :
  30. if (is_array($_POST['filter'][$field['alias']]))
  31. $data = array_map('intval', $_POST['filter'][$field['alias']]);
  32. else
  33. $data = intval($_POST['filter'][$field['alias']]);
  34. case 'float' :
  35. if (is_array($_POST['filter'][$field['alias']]))
  36. $data = array_map('floatval', $_POST['filter'][$field['alias']]);
  37. else
  38. $data = floatval($_POST['filter'][$field['alias']]);
  39. default :
  40. $data = $_POST['filter'][$field['alias']];
  41. }
  42. $filter['add_fields'][$field['id']] = array(
  43. 'alias' => $field['alias'],
  44. 'type' => $field['type'],
  45. 'data' => $data
  46. );
  47. }
  48. }
  49. setcookie('shop_f_filter', json_encode($filter), time() + 604800, '/shop/');
  50. $_COOKIE['shop_f_filter'] = json_encode($filter);
  51. }
  52. if (isset($_POST['sortby']) || isset($_POST['sort_asc'])) {
  53. $sortby = $_POST['sortby'];
  54. $sortasc = $_POST['sort_asc'];
  55. setcookie('shop_f_sortby', $sortby, time() + 604800, "/");
  56. setcookie('shop_f_sortasc', $sortasc, time() + 604800, "/");
  57. $_COOKIE['shop_f_sortby'] = $sortby;
  58. $_COOKIE['shop_f_sortasc'] = $sortasc;
  59. }
  60. if (isset($_POST['rowsPerPage'])) {
  61. $rowsPerPage = $_POST['rowsPerPage'];
  62. setcookie('shop_f_rowsPerPage', $rowsPerPage, time() + 604800, "/");
  63. $_COOKIE['shop_f_rowsPerPage'] = $rowsPerPage;
  64. }
  65. if (isset($_POST['brandId'])) {
  66. $brandId = $_POST['brandId'];
  67. setcookie('shop_f_brandId', $brandId, time() + 604800);
  68. $_COOKIE['shop_f_brandId'] = $brandId;
  69. }
  70. }
  71. $filter = $_COOKIE['shop_f_filter'];
  72. $filter = json_decode($filter, true);
  73. if (!is_array($filter)) {
  74. $filter = array();
  75. }
  76. $sortby = !empty($_COOKIE['shop_f_sortby']) ? $_COOKIE['shop_f_sortby'] : null;
  77. $sortasc = !empty($_COOKIE['shop_f_sortasc']) ? $_COOKIE['shop_f_sortasc'] : null;
  78. $rowsPerPage = !empty($_COOKIE['shop_f_rowsPerPage']) ? intval($_COOKIE['shop_f_rowsPerPage']) : Kohana::config('shop')->rows_frontend;
  79. $brandId = !empty($_COOKIE['shop_f_brandId']) ? intval($_COOKIE['shop_f_brandId']) : null;
  80. $this->view->assign('filter', $filter);
  81. $this->view->assign('sortby', $sortby);
  82. $this->view->assign('sortasc', $sortasc);
  83. $this->view->assign('rowsPerPage', $rowsPerPage);
  84. $this->view->assign('brandId', $brandId);
  85. $rows = array();
  86. $page_links = '';
  87. $total_pages = 0;
  88. list($rows, $page_links, $total_pages) = Model_Frontend_Categories::goods($this->request->param('id'), $rowsPerPage, Kohana::config('shop')->default_category, $filter, $sortby, $sortasc, $brandId);
  89. $this->view->assign('goods', $rows);
  90. $this->view->assign('pages_list', $page_links);
  91. $this->view->assign('total_pages', $total_pages);
  92. }
  93. public function action_search() {
  94. $this->page_info = array(
  95. 'page_title' => 'Результаты поиска',
  96. );
  97. if( mb_strlen(trim($_GET['browser']), 'UTF-8') < 3 ) return false;
  98. $srchWhat = isset($_GET['browser']) ? trim($_GET['browser']) : '';
  99. $this->view->assign('srchWhat', $srchWhat);
  100. $this->view->assign('search', true);
  101. $this->_search = true;
  102. $filterableFields = Model_Frontend_Categories::getFilterableFields(0);
  103. $this->view->assign('filterableFields', $filterableFields);
  104. $filter = array();
  105. if (Request::$method == 'POST') {
  106. if (isset($_POST['filter'])) {
  107. if (isset($_POST['filter']['price'])) {
  108. $filter['price'] = array_map('floatval', $_POST['filter']['price']);
  109. }
  110. foreach ($filterableFields as $field) {
  111. if (isset($_POST['filter'][$field['alias']]) && !empty($_POST['filter'][$field['alias']])) {
  112. switch ($field['type']) {
  113. case 'int' :
  114. if (is_array($_POST['filter'][$field['alias']]))
  115. $data = array_map('intval', $_POST['filter'][$field['alias']]);
  116. else
  117. $data = intval($_POST['filter'][$field['alias']]);
  118. case 'float' :
  119. if (is_array($_POST['filter'][$field['alias']]))
  120. $data = array_map('floatval', $_POST['filter'][$field['alias']]);
  121. else
  122. $data = floatval($_POST['filter'][$field['alias']]);
  123. default :
  124. $data = $_POST['filter'][$field['alias']];
  125. }
  126. $filter['add_fields'][$field['id']] = array(
  127. 'alias' => $field['alias'],
  128. 'type' => $field['type'],
  129. 'data' => $data
  130. );
  131. }
  132. }
  133. setcookie('shop_f_filter', json_encode($filter), time() + 604800);
  134. $_COOKIE['shop_f_filter'] = json_encode($filter);
  135. }
  136. if (isset($_POST['sortby'])) {
  137. $sortby = $_POST['sortby'];
  138. $sortasc = $_POST['sort_asc'];
  139. setcookie('shop_f_sortby', $sortby, time() + 604800, "/");
  140. setcookie('shop_f_sortasc', $sortasc, time() + 604800, "/");
  141. $_COOKIE['shop_f_sortby'] = $sortby;
  142. $_COOKIE['shop_f_sortasc'] = $sortasc;
  143. }
  144. if (isset($_POST['rowsPerPage'])) {
  145. $rowsPerPage = $_POST['rowsPerPage'];
  146. setcookie('shop_f_rowsPerPage', $rowsPerPage, time() + 604800, "/");
  147. $_COOKIE['shop_f_rowsPerPage'] = $rowsPerPage;
  148. }
  149. if (isset($_POST['brandId'])) {
  150. $brandId = $_POST['brandId'];
  151. setcookie('shop_f_brandId', $brandId, time() + 604800);
  152. $_COOKIE['shop_f_brandId'] = $brandId;
  153. }
  154. }
  155. $filter = $_COOKIE['shop_f_filter'];
  156. $filter = json_decode($filter, true);
  157. if (!is_array($filter)) {
  158. $filter = array();
  159. }
  160. $sortby = !empty($_COOKIE['shop_f_sortby']) ? $_COOKIE['shop_f_sortby'] : null;
  161. $sortasc = !empty($_COOKIE['shop_f_sortasc']) ? $_COOKIE['shop_f_sortasc'] : null;
  162. $rowsPerPage = !empty($_COOKIE['shop_f_rowsPerPage']) ? intval($_COOKIE['shop_f_rowsPerPage']) : 12;
  163. $brandId = !empty($_COOKIE['shop_f_brandId']) ? intval($_COOKIE['shop_f_brandId']) : null;
  164. $this->view->assign('filter', $filter);
  165. $this->view->assign('sortby', $sortby);
  166. $this->view->assign('sortasc', $sortasc);
  167. $this->view->assign('rowsPerPage', $rowsPerPage);
  168. $this->view->assign('brandId', $brandId);
  169. $rows = array();
  170. $page_links = '';
  171. $total_pages = 0;
  172. list($rows, $page_links, $total_pages) = Model_Frontend_Categories::search($rowsPerPage, $srchWhat, $sortby, $sortasc, $brandId, $filter, CURRENT_LANG_ID);
  173. $this->view->assign('goods', $rows);
  174. $this->view->assign('pages_list', $page_links);
  175. $this->view->assign('total_pages', $total_pages);
  176. }
  177. public function action_childs() {
  178. echo json_encode(Model_Frontend_Categories::show_categories($this->request->param('id')));
  179. exit();
  180. }
  181. public function action_tree() {
  182. echo json_encode(Model_Frontend_Categories::show_categories($this->request->param('id')));
  183. exit();
  184. }
  185. private function filters() {
  186. }
  187. public function action_getLastNewGoods()
  188. {
  189. $res = $this->model->getLastNewGoods();
  190. die(Kohana::debug($res));
  191. }
  192. }