Guest User

Model file

a guest
Dec 15th, 2016
2,305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.39 KB | None | 0 0
  1. <?php
  2. class ModelCatalogAllProduct extends Model {
  3. public function getAllProduct($data = array()) {
  4. $sql = "SELECT p.product_id, (SELECT AVG(rating) AS total FROM " . DB_PREFIX . "review r1 WHERE r1.product_id = p.product_id AND r1.status = '1' GROUP BY r1.product_id) AS rating, (SELECT price FROM " . DB_PREFIX . "product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1) AS discount, (SELECT price FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1) AS special";
  5.  
  6. if (!empty($data['filter_category_id'])) {
  7. if (!empty($data['filter_sub_category'])) {
  8. $sql .= " FROM " . DB_PREFIX . "category_path cp LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (cp.category_id = p2c.category_id)";
  9. } else {
  10. $sql .= " FROM " . DB_PREFIX . "product_to_category p2c";
  11. }
  12.  
  13. if (!empty($data['filter_filter'])) {
  14. $sql .= " LEFT JOIN " . DB_PREFIX . "product_filter pf ON (p2c.product_id = pf.product_id) LEFT JOIN " . DB_PREFIX . "product p ON (pf.product_id = p.product_id)";
  15. } else {
  16. $sql .= " LEFT JOIN " . DB_PREFIX . "product p ON (p2c.product_id = p.product_id)";
  17. }
  18. } else {
  19. $sql .= " FROM " . DB_PREFIX . "product p";
  20. }
  21.  
  22. $sql .= " LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'";
  23.  
  24. if (!empty($data['filter_category_id'])) {
  25. if (!empty($data['filter_sub_category'])) {
  26. $sql .= " AND cp.path_id = '" . (int)$data['filter_category_id'] . "'";
  27. } else {
  28. $sql .= " AND p2c.category_id = '" . (int)$data['filter_category_id'] . "'";
  29. }
  30.  
  31. if (!empty($data['filter_filter'])) {
  32. $implode = array();
  33.  
  34. $filters = explode(',', $data['filter_filter']);
  35.  
  36. foreach ($filters as $filter_id) {
  37. $implode[] = (int)$filter_id;
  38. }
  39.  
  40. $sql .= " AND pf.filter_id IN (" . implode(',', $implode) . ")";
  41. }
  42. }
  43.  
  44. if (!empty($data['filter_name']) || !empty($data['filter_tag'])) {
  45. $sql .= " AND (";
  46.  
  47. if (!empty($data['filter_name'])) {
  48. $implode = array();
  49.  
  50. $words = explode(' ', trim(preg_replace('/\s+/', ' ', $data['filter_name'])));
  51.  
  52. foreach ($words as $word) {
  53. $implode[] = "pd.name LIKE '%" . $this->db->escape($word) . "%'";
  54. }
  55.  
  56. if ($implode) {
  57. $sql .= " " . implode(" AND ", $implode) . "";
  58. }
  59.  
  60. if (!empty($data['filter_description'])) {
  61. $sql .= " OR pd.description LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
  62. }
  63. }
  64.  
  65. if (!empty($data['filter_name']) && !empty($data['filter_tag'])) {
  66. $sql .= " OR ";
  67. }
  68.  
  69. if (!empty($data['filter_tag'])) {
  70. $sql .= "pd.tag LIKE '%" . $this->db->escape($data['filter_tag']) . "%'";
  71. }
  72.  
  73. if (!empty($data['filter_name'])) {
  74. $sql .= " OR LCASE(p.model) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
  75. $sql .= " OR LCASE(p.sku) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
  76. $sql .= " OR LCASE(p.upc) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
  77. $sql .= " OR LCASE(p.ean) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
  78. $sql .= " OR LCASE(p.jan) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
  79. $sql .= " OR LCASE(p.isbn) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
  80. $sql .= " OR LCASE(p.mpn) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
  81. }
  82.  
  83. $sql .= ")";
  84. }
  85.  
  86. if (!empty($data['filter_manufacturer_id'])) {
  87. $sql .= " AND p.manufacturer_id = '" . (int)$data['filter_manufacturer_id'] . "'";
  88. }
  89.  
  90. $sql .= " GROUP BY p.product_id";
  91.  
  92. $sort_data = array(
  93. 'pd.name',
  94. 'p.model',
  95. 'p.quantity',
  96. 'p.price',
  97. 'rating',
  98. 'p.sort_order',
  99. 'p.date_added'
  100. );
  101.  
  102. if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
  103. if ($data['sort'] == 'pd.name' || $data['sort'] == 'p.model') {
  104. $sql .= " ORDER BY LCASE(" . $data['sort'] . ")";
  105. } elseif ($data['sort'] == 'p.price') {
  106. $sql .= " ORDER BY (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END)";
  107. } else {
  108. $sql .= " ORDER BY " . $data['sort'];
  109. }
  110. } else {
  111. $sql .= " ORDER BY p.sort_order";
  112. }
  113.  
  114. if (isset($data['order']) && ($data['order'] == 'DESC')) {
  115. $sql .= " DESC, LCASE(pd.name) DESC";
  116. } else {
  117. $sql .= " ASC, LCASE(pd.name) ASC";
  118. }
  119.  
  120. if (isset($data['start']) || isset($data['limit'])) {
  121. if ($data['start'] < 0) {
  122. $data['start'] = 0;
  123. }
  124.  
  125. if ($data['limit'] < 1) {
  126. $data['limit'] = 20;
  127. }
  128.  
  129. $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  130. }
  131.  
  132. $product_data = array();
  133.  
  134. $query = $this->db->query($sql);
  135.  
  136. foreach ($query->rows as $result) {
  137. $product_data[$result['product_id']] = $this->getProduct($result['product_id']);
  138. }
  139.  
  140. return $product_data;
  141. }
  142.  
  143. public function getProduct($product_id) {
  144. $query = $this->db->query("SELECT DISTINCT *, pd.name AS name, p.image, m.name AS manufacturer, (SELECT price FROM " . DB_PREFIX . "product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1) AS discount, (SELECT price FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1) AS special, (SELECT points FROM " . DB_PREFIX . "product_reward pr WHERE pr.product_id = p.product_id AND customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "') AS reward, (SELECT ss.name FROM " . DB_PREFIX . "stock_status ss WHERE ss.stock_status_id = p.stock_status_id AND ss.language_id = '" . (int)$this->config->get('config_language_id') . "') AS stock_status, (SELECT wcd.unit FROM " . DB_PREFIX . "weight_class_description wcd WHERE p.weight_class_id = wcd.weight_class_id AND wcd.language_id = '" . (int)$this->config->get('config_language_id') . "') AS weight_class, (SELECT lcd.unit FROM " . DB_PREFIX . "length_class_description lcd WHERE p.length_class_id = lcd.length_class_id AND lcd.language_id = '" . (int)$this->config->get('config_language_id') . "') AS length_class, (SELECT AVG(rating) AS total FROM " . DB_PREFIX . "review r1 WHERE r1.product_id = p.product_id AND r1.status = '1' GROUP BY r1.product_id) AS rating, (SELECT COUNT(*) AS total FROM " . DB_PREFIX . "review r2 WHERE r2.product_id = p.product_id AND r2.status = '1' GROUP BY r2.product_id) AS reviews, p.sort_order FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) LEFT JOIN " . DB_PREFIX . "manufacturer m ON (p.manufacturer_id = m.manufacturer_id) WHERE p.product_id = '" . (int)$product_id . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'");
  145.  
  146. if ($query->num_rows) {
  147. return array(
  148. 'product_id' => $query->row['product_id'],
  149. 'name' => $query->row['name'],
  150. 'description' => $query->row['description'],
  151. 'meta_title' => $query->row['meta_title'],
  152. 'meta_description' => $query->row['meta_description'],
  153. 'meta_keyword' => $query->row['meta_keyword'],
  154. 'tag' => $query->row['tag'],
  155. 'model' => $query->row['model'],
  156. 'sku' => $query->row['sku'],
  157. 'upc' => $query->row['upc'],
  158. 'ean' => $query->row['ean'],
  159. 'jan' => $query->row['jan'],
  160. 'isbn' => $query->row['isbn'],
  161. 'mpn' => $query->row['mpn'],
  162. 'location' => $query->row['location'],
  163. 'quantity' => $query->row['quantity'],
  164. 'stock_status' => $query->row['stock_status'],
  165. 'image' => $query->row['image'],
  166. 'manufacturer_id' => $query->row['manufacturer_id'],
  167. 'manufacturer' => $query->row['manufacturer'],
  168. 'price' => ($query->row['discount'] ? $query->row['discount'] : $query->row['price']),
  169. 'special' => $query->row['special'],
  170. 'reward' => $query->row['reward'],
  171. 'points' => $query->row['points'],
  172. 'tax_class_id' => $query->row['tax_class_id'],
  173. 'date_available' => $query->row['date_available'],
  174. 'weight' => $query->row['weight'],
  175. 'weight_class_id' => $query->row['weight_class_id'],
  176. 'length' => $query->row['length'],
  177. 'width' => $query->row['width'],
  178. 'height' => $query->row['height'],
  179. 'length_class_id' => $query->row['length_class_id'],
  180. 'subtract' => $query->row['subtract'],
  181. 'rating' => round($query->row['rating']),
  182. 'reviews' => $query->row['reviews'] ? $query->row['reviews'] : 0,
  183. 'minimum' => $query->row['minimum'],
  184. 'sort_order' => $query->row['sort_order'],
  185. 'status' => $query->row['status'],
  186. 'date_added' => $query->row['date_added'],
  187. 'date_modified' => $query->row['date_modified'],
  188. 'viewed' => $query->row['viewed']
  189. );
  190. } else {
  191. return false;
  192. }
  193. }
  194. }
Add Comment
Please, Sign In to add comment