/upload/catalog/controller/product/compare.php

https://bitbucket.org/elena_dyavolova/omf · PHP · 204 lines · 159 code · 45 blank · 0 comment · 25 complexity · ab44c333b669c65dcb86e5e7cf017871 MD5 · raw file

  1. <?php
  2. class ControllerProductCompare extends Controller {
  3. public function index() {
  4. $this->language->load('product/compare');
  5. $this->load->model('catalog/product');
  6. $this->load->model('tool/image');
  7. if (!isset($this->session->data['compare'])) {
  8. $this->session->data['compare'] = array();
  9. }
  10. if (isset($this->request->get['remove'])) {
  11. $key = array_search($this->request->get['remove'], $this->session->data['compare']);
  12. if ($key !== false) {
  13. unset($this->session->data['compare'][$key]);
  14. }
  15. $this->session->data['success'] = $this->language->get('text_remove');
  16. $this->redirect($this->url->link('product/compare'));
  17. }
  18. $this->document->setTitle($this->language->get('heading_title'));
  19. $this->data['breadcrumbs'] = array();
  20. $this->data['breadcrumbs'][] = array(
  21. 'text' => $this->language->get('text_home'),
  22. 'href' => $this->url->link('common/home'),
  23. 'separator' => false
  24. );
  25. $this->data['breadcrumbs'][] = array(
  26. 'text' => $this->language->get('heading_title'),
  27. 'href' => $this->url->link('product/compare'),
  28. 'separator' => $this->language->get('text_separator')
  29. );
  30. $this->data['heading_title'] = $this->language->get('heading_title');
  31. $this->data['text_product'] = $this->language->get('text_product');
  32. $this->data['text_name'] = $this->language->get('text_name');
  33. $this->data['text_image'] = $this->language->get('text_image');
  34. $this->data['text_price'] = $this->language->get('text_price');
  35. $this->data['text_model'] = $this->language->get('text_model');
  36. $this->data['text_manufacturer'] = $this->language->get('text_manufacturer');
  37. $this->data['text_availability'] = $this->language->get('text_availability');
  38. $this->data['text_rating'] = $this->language->get('text_rating');
  39. $this->data['text_summary'] = $this->language->get('text_summary');
  40. $this->data['text_weight'] = $this->language->get('text_weight');
  41. $this->data['text_dimension'] = $this->language->get('text_dimension');
  42. $this->data['text_empty'] = $this->language->get('text_empty');
  43. $this->data['button_continue'] = $this->language->get('button_continue');
  44. $this->data['button_cart'] = $this->language->get('button_cart');
  45. $this->data['button_remove'] = $this->language->get('button_remove');
  46. if (isset($this->session->data['success'])) {
  47. $this->data['success'] = $this->session->data['success'];
  48. unset($this->session->data['success']);
  49. } else {
  50. $this->data['success'] = '';
  51. }
  52. $this->data['review_status'] = $this->config->get('config_review_status');
  53. $this->data['products'] = array();
  54. $this->data['attribute_groups'] = array();
  55. foreach ($this->session->data['compare'] as $key => $product_id) {
  56. $product_info = $this->model_catalog_product->getProduct($product_id);
  57. if ($product_info) {
  58. if ($product_info['image']) {
  59. $image = $this->model_tool_image->resize($product_info['image'], $this->config->get('config_image_compare_width'), $this->config->get('config_image_compare_height'));
  60. } else {
  61. $image = false;
  62. }
  63. if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
  64. $price = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
  65. } else {
  66. $price = false;
  67. }
  68. if ((float)$product_info['special']) {
  69. $special = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')));
  70. } else {
  71. $special = false;
  72. }
  73. if ($product_info['quantity'] <= 0) {
  74. $availability = $product_info['stock_status'];
  75. } elseif ($this->config->get('config_stock_display')) {
  76. $availability = $product_info['quantity'];
  77. } else {
  78. $availability = $this->language->get('text_instock');
  79. }
  80. $attribute_data = array();
  81. $attribute_groups = $this->model_catalog_product->getProductAttributes($product_id);
  82. foreach ($attribute_groups as $attribute_group) {
  83. foreach ($attribute_group['attribute'] as $attribute) {
  84. $attribute_data[$attribute['attribute_id']] = $attribute['text'];
  85. }
  86. }
  87. $this->data['products'][$product_id] = array(
  88. 'product_id' => $product_info['product_id'],
  89. 'name' => $product_info['name'],
  90. 'thumb' => $image,
  91. 'price' => $price,
  92. 'special' => $special,
  93. 'description' => utf8_substr(strip_tags(html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8')), 0, 200) . '..',
  94. 'model' => $product_info['model'],
  95. 'manufacturer' => $product_info['manufacturer'],
  96. 'availability' => $availability,
  97. 'rating' => (int)$product_info['rating'],
  98. 'reviews' => sprintf($this->language->get('text_reviews'), (int)$product_info['reviews']),
  99. 'weight' => $this->weight->format($product_info['weight'], $product_info['weight_class_id']),
  100. 'length' => $this->length->format($product_info['length'], $product_info['length_class_id']),
  101. 'width' => $this->length->format($product_info['width'], $product_info['length_class_id']),
  102. 'height' => $this->length->format($product_info['height'], $product_info['length_class_id']),
  103. 'attribute' => $attribute_data,
  104. 'href' => $this->url->link('product/product', 'product_id=' . $product_id),
  105. 'remove' => $this->url->link('product/compare', 'remove=' . $product_id)
  106. );
  107. foreach ($attribute_groups as $attribute_group) {
  108. $this->data['attribute_groups'][$attribute_group['attribute_group_id']]['name'] = $attribute_group['name'];
  109. foreach ($attribute_group['attribute'] as $attribute) {
  110. $this->data['attribute_groups'][$attribute_group['attribute_group_id']]['attribute'][$attribute['attribute_id']]['name'] = $attribute['name'];
  111. }
  112. }
  113. } else {
  114. unset($this->session->data['compare'][$key]);
  115. }
  116. }
  117. $this->data['continue'] = $this->url->link('common/home');
  118. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/compare.tpl')) {
  119. $this->template = $this->config->get('config_template') . '/template/product/compare.tpl';
  120. } else {
  121. $this->template = 'default/template/product/compare.tpl';
  122. }
  123. $this->children = array(
  124. 'common/column_left',
  125. 'common/column_right',
  126. 'common/content_top',
  127. 'common/content_bottom',
  128. 'common/footer',
  129. 'common/header'
  130. );
  131. $this->response->setOutput($this->render());
  132. }
  133. public function add() {
  134. $this->language->load('product/compare');
  135. $json = array();
  136. if (!isset($this->session->data['compare'])) {
  137. $this->session->data['compare'] = array();
  138. }
  139. if (isset($this->request->post['product_id'])) {
  140. $product_id = $this->request->post['product_id'];
  141. } else {
  142. $product_id = 0;
  143. }
  144. $this->load->model('catalog/product');
  145. $product_info = $this->model_catalog_product->getProduct($product_id);
  146. if ($product_info) {
  147. if (!in_array($this->request->post['product_id'], $this->session->data['compare'])) {
  148. if (count($this->session->data['compare']) >= 4) {
  149. array_shift($this->session->data['compare']);
  150. }
  151. $this->session->data['compare'][] = $this->request->post['product_id'];
  152. }
  153. $json['success'] = sprintf($this->language->get('text_success'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('product/compare'));
  154. $json['total'] = sprintf($this->language->get('text_compare'), (isset($this->session->data['compare']) ? count($this->session->data['compare']) : 0));
  155. }
  156. $this->response->setOutput(json_encode($json));
  157. }
  158. }
  159. ?>