PageRenderTime 27ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/catalog/controller/product/category.php

https://bitbucket.org/zirrow/ecoelka.com
PHP | 412 lines | 316 code | 94 blank | 2 comment | 59 complexity | 417f114d1d0d31c8ea21ebf8924729fb MD5 | raw file
  1. <?php
  2. class ControllerProductCategory extends Controller {
  3. public function index() {
  4. $this->load->language('product/category');
  5. $this->load->model('catalog/category');
  6. $this->load->model('catalog/product');
  7. $this->load->model('tool/image');
  8. if (isset($this->request->get['filter'])) {
  9. $filter = $this->request->get['filter'];
  10. } else {
  11. $filter = '';
  12. }
  13. if (isset($this->request->get['sort'])) {
  14. $sort = $this->request->get['sort'];
  15. } else {
  16. $sort = 'p.sort_order';
  17. }
  18. if (isset($this->request->get['order'])) {
  19. $order = $this->request->get['order'];
  20. } else {
  21. $order = 'ASC';
  22. }
  23. if (isset($this->request->get['page'])) {
  24. $page = $this->request->get['page'];
  25. } else {
  26. $page = 1;
  27. }
  28. if (isset($this->request->get['limit'])) {
  29. $limit = (int)$this->request->get['limit'];
  30. } else {
  31. $limit = $this->config->get('theme_' . $this->config->get('config_theme') . '_product_limit');
  32. }
  33. $data['breadcrumbs'] = array();
  34. $data['breadcrumbs'][] = array(
  35. 'text' => $this->language->get('text_home'),
  36. 'href' => $this->url->link('common/home')
  37. );
  38. if (isset($this->request->get['path'])) {
  39. $url = '';
  40. if (isset($this->request->get['sort'])) {
  41. $url .= '&sort=' . $this->request->get['sort'];
  42. }
  43. if (isset($this->request->get['order'])) {
  44. $url .= '&order=' . $this->request->get['order'];
  45. }
  46. if (isset($this->request->get['limit'])) {
  47. $url .= '&limit=' . $this->request->get['limit'];
  48. }
  49. $path = '';
  50. $parts = explode('_', (string)$this->request->get['path']);
  51. $category_id = (int)array_pop($parts);
  52. foreach ($parts as $path_id) {
  53. if (!$path) {
  54. $path = (int)$path_id;
  55. } else {
  56. $path .= '_' . (int)$path_id;
  57. }
  58. $category_info = $this->model_catalog_category->getCategory($path_id);
  59. if ($category_info) {
  60. $data['breadcrumbs'][] = array(
  61. 'text' => $category_info['name'],
  62. 'href' => $this->url->link('product/category', 'path=' . $path . $url)
  63. );
  64. }
  65. }
  66. } else {
  67. $category_id = 0;
  68. }
  69. $category_info = $this->model_catalog_category->getCategory($category_id);
  70. if ($category_info) {
  71. $this->document->setTitle($category_info['meta_title']);
  72. $this->document->setDescription($category_info['meta_description']);
  73. $this->document->setKeywords($category_info['meta_keyword']);
  74. $data['heading_title'] = $category_info['name'];
  75. $data['text_compare'] = sprintf($this->language->get('text_compare'), (isset($this->session->data['compare']) ? count($this->session->data['compare']) : 0));
  76. // Set the last category breadcrumb
  77. $data['breadcrumbs'][] = array(
  78. 'text' => $category_info['name'],
  79. 'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'])
  80. );
  81. if ($category_info['image']) {
  82. $data['thumb'] = $this->model_tool_image->resize($category_info['image'], $this->config->get('theme_' . $this->config->get('config_theme') . '_image_category_width'), $this->config->get('theme_' . $this->config->get('config_theme') . '_image_category_height'));
  83. } else {
  84. $data['thumb'] = '';
  85. }
  86. $data['description'] = html_entity_decode($category_info['description'], ENT_QUOTES, 'UTF-8');
  87. $data['compare'] = $this->url->link('product/compare');
  88. $url = '';
  89. if (isset($this->request->get['filter'])) {
  90. $url .= '&filter=' . $this->request->get['filter'];
  91. }
  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['limit'])) {
  99. $url .= '&limit=' . $this->request->get['limit'];
  100. }
  101. $data['categories'] = array();
  102. $results = $this->model_catalog_category->getCategories($category_id);
  103. foreach ($results as $result) {
  104. $filter_data = array(
  105. 'filter_category_id' => $result['category_id'],
  106. 'filter_sub_category' => true
  107. );
  108. $data['categories'][] = array(
  109. 'name' => $result['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
  110. 'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '_' . $result['category_id'] . $url)
  111. );
  112. }
  113. $data['products'] = array();
  114. $filter_data = array(
  115. 'filter_category_id' => $category_id,
  116. 'filter_sub_category'=> true,
  117. 'filter_filter' => $filter,
  118. 'sort' => $sort,
  119. 'order' => $order,
  120. 'start' => ($page - 1) * $limit,
  121. 'limit' => $limit
  122. );
  123. $product_total = $this->model_catalog_product->getTotalProducts($filter_data);
  124. $results = $this->model_catalog_product->getProducts($filter_data);
  125. foreach ($results as $result) {
  126. if ($result['image']) {
  127. $image = $this->model_tool_image->resize($result['image'], $this->config->get('theme_' . $this->config->get('config_theme') . '_image_product_width'), $this->config->get('theme_' . $this->config->get('config_theme') . '_image_product_height'));
  128. } else {
  129. $image = $this->model_tool_image->resize('placeholder.png', $this->config->get('theme_' . $this->config->get('config_theme') . '_image_product_width'), $this->config->get('theme_' . $this->config->get('config_theme') . '_image_product_height'));
  130. }
  131. if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
  132. $price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
  133. } else {
  134. $price = false;
  135. }
  136. if ((float)$result['special']) {
  137. $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
  138. } else {
  139. $special = false;
  140. }
  141. if ($this->config->get('config_tax')) {
  142. $tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price'], $this->session->data['currency']);
  143. } else {
  144. $tax = false;
  145. }
  146. if ($this->config->get('config_review_status')) {
  147. $rating = (int)$result['rating'];
  148. } else {
  149. $rating = false;
  150. }
  151. $data['products'][] = array(
  152. 'product_id' => $result['product_id'],
  153. 'thumb' => $image,
  154. 'name' => $result['name'],
  155. 'description' => utf8_substr(trim(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'))), 0, $this->config->get('theme_' . $this->config->get('config_theme') . '_product_description_length')) . '..',
  156. 'price' => $price,
  157. 'special' => $special,
  158. 'tax' => $tax,
  159. 'minimum' => $result['minimum'] > 0 ? $result['minimum'] : 1,
  160. 'rating' => $result['rating'],
  161. 'href' => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'] . $url)
  162. );
  163. }
  164. $url = '';
  165. if (isset($this->request->get['filter'])) {
  166. $url .= '&filter=' . $this->request->get['filter'];
  167. }
  168. if (isset($this->request->get['limit'])) {
  169. $url .= '&limit=' . $this->request->get['limit'];
  170. }
  171. $data['sorts'] = array();
  172. $data['sorts'][] = array(
  173. 'text' => $this->language->get('text_default'),
  174. 'value' => 'p.sort_order-ASC',
  175. 'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '&sort=p.sort_order&order=ASC' . $url)
  176. );
  177. $data['sorts'][] = array(
  178. 'text' => $this->language->get('text_name_asc'),
  179. 'value' => 'pd.name-ASC',
  180. 'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '&sort=pd.name&order=ASC' . $url)
  181. );
  182. $data['sorts'][] = array(
  183. 'text' => $this->language->get('text_name_desc'),
  184. 'value' => 'pd.name-DESC',
  185. 'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '&sort=pd.name&order=DESC' . $url)
  186. );
  187. $data['sorts'][] = array(
  188. 'text' => $this->language->get('text_price_asc'),
  189. 'value' => 'p.price-ASC',
  190. 'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '&sort=p.price&order=ASC' . $url)
  191. );
  192. $data['sorts'][] = array(
  193. 'text' => $this->language->get('text_price_desc'),
  194. 'value' => 'p.price-DESC',
  195. 'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '&sort=p.price&order=DESC' . $url)
  196. );
  197. if ($this->config->get('config_review_status')) {
  198. $data['sorts'][] = array(
  199. 'text' => $this->language->get('text_rating_desc'),
  200. 'value' => 'rating-DESC',
  201. 'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '&sort=rating&order=DESC' . $url)
  202. );
  203. $data['sorts'][] = array(
  204. 'text' => $this->language->get('text_rating_asc'),
  205. 'value' => 'rating-ASC',
  206. 'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '&sort=rating&order=ASC' . $url)
  207. );
  208. }
  209. $data['sorts'][] = array(
  210. 'text' => $this->language->get('text_model_asc'),
  211. 'value' => 'p.model-ASC',
  212. 'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '&sort=p.model&order=ASC' . $url)
  213. );
  214. $data['sorts'][] = array(
  215. 'text' => $this->language->get('text_model_desc'),
  216. 'value' => 'p.model-DESC',
  217. 'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '&sort=p.model&order=DESC' . $url)
  218. );
  219. $url = '';
  220. if (isset($this->request->get['filter'])) {
  221. $url .= '&filter=' . $this->request->get['filter'];
  222. }
  223. if (isset($this->request->get['sort'])) {
  224. $url .= '&sort=' . $this->request->get['sort'];
  225. }
  226. if (isset($this->request->get['order'])) {
  227. $url .= '&order=' . $this->request->get['order'];
  228. }
  229. $data['limits'] = array();
  230. $limits = array_unique(array($this->config->get('theme_' . $this->config->get('config_theme') . '_product_limit'), 25, 50, 75, 100));
  231. sort($limits);
  232. foreach($limits as $value) {
  233. $data['limits'][] = array(
  234. 'text' => $value,
  235. 'value' => $value,
  236. 'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . $url . '&limit=' . $value)
  237. );
  238. }
  239. $url = '';
  240. if (isset($this->request->get['filter'])) {
  241. $url .= '&filter=' . $this->request->get['filter'];
  242. }
  243. if (isset($this->request->get['sort'])) {
  244. $url .= '&sort=' . $this->request->get['sort'];
  245. }
  246. if (isset($this->request->get['order'])) {
  247. $url .= '&order=' . $this->request->get['order'];
  248. }
  249. if (isset($this->request->get['limit'])) {
  250. $url .= '&limit=' . $this->request->get['limit'];
  251. }
  252. $pagination = new Pagination();
  253. $pagination->total = $product_total;
  254. $pagination->page = $page;
  255. $pagination->limit = $limit;
  256. $pagination->url = $this->url->link('product/category', 'path=' . $this->request->get['path'] . $url . '&page={page}');
  257. $data['pagination'] = $pagination->render();
  258. $data['results'] = sprintf($this->language->get('text_pagination'), ($product_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($product_total - $limit)) ? $product_total : ((($page - 1) * $limit) + $limit), $product_total, ceil($product_total / $limit));
  259. // http://googlewebmastercentral.blogspot.com/2011/09/pagination-with-relnext-and-relprev.html
  260. if ($page == 1) {
  261. $this->document->addLink($this->url->link('product/category', 'path=' . $category_info['category_id']), 'canonical');
  262. } else {
  263. $this->document->addLink($this->url->link('product/category', 'path=' . $category_info['category_id'] . '&page='. $page), 'canonical');
  264. }
  265. if ($page > 1) {
  266. $this->document->addLink($this->url->link('product/category', 'path=' . $category_info['category_id'] . (($page - 2) ? '&page='. ($page - 1) : '')), 'prev');
  267. }
  268. if ($limit && ceil($product_total / $limit) > $page) {
  269. $this->document->addLink($this->url->link('product/category', 'path=' . $category_info['category_id'] . '&page='. ($page + 1)), 'next');
  270. }
  271. $data['sort'] = $sort;
  272. $data['order'] = $order;
  273. $data['limit'] = $limit;
  274. $data['continue'] = $this->url->link('common/home');
  275. $data['column_left'] = $this->load->controller('common/column_left');
  276. $data['column_right'] = $this->load->controller('common/column_right');
  277. $data['content_top'] = $this->load->controller('common/content_top');
  278. $data['content_bottom'] = $this->load->controller('common/content_bottom');
  279. $data['footer'] = $this->load->controller('common/footer');
  280. $data['header'] = $this->load->controller('common/header');
  281. $this->response->setOutput($this->load->view('product/category', $data));
  282. } else {
  283. $url = '';
  284. if (isset($this->request->get['path'])) {
  285. $url .= '&path=' . $this->request->get['path'];
  286. }
  287. if (isset($this->request->get['filter'])) {
  288. $url .= '&filter=' . $this->request->get['filter'];
  289. }
  290. if (isset($this->request->get['sort'])) {
  291. $url .= '&sort=' . $this->request->get['sort'];
  292. }
  293. if (isset($this->request->get['order'])) {
  294. $url .= '&order=' . $this->request->get['order'];
  295. }
  296. if (isset($this->request->get['page'])) {
  297. $url .= '&page=' . $this->request->get['page'];
  298. }
  299. if (isset($this->request->get['limit'])) {
  300. $url .= '&limit=' . $this->request->get['limit'];
  301. }
  302. $data['breadcrumbs'][] = array(
  303. 'text' => $this->language->get('text_error'),
  304. 'href' => $this->url->link('product/category', $url)
  305. );
  306. $this->document->setTitle($this->language->get('text_error'));
  307. $data['continue'] = $this->url->link('common/home');
  308. $this->response->addHeader($this->request->server['SERVER_PROTOCOL'] . ' 404 Not Found');
  309. $data['column_left'] = $this->load->controller('common/column_left');
  310. $data['column_right'] = $this->load->controller('common/column_right');
  311. $data['content_top'] = $this->load->controller('common/content_top');
  312. $data['content_bottom'] = $this->load->controller('common/content_bottom');
  313. $data['footer'] = $this->load->controller('common/footer');
  314. $data['header'] = $this->load->controller('common/header');
  315. $this->response->setOutput($this->load->view('error/not_found', $data));
  316. }
  317. }
  318. }