Advertisement
Guest User

Untitled

a guest
Jun 8th, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. public function getProducts($id_lang, $p, $n, $order_by = null, $order_way = null, $get_total = false, $active = true, $random = false, $random_number_products = 1, $check_access = true, Context $context = null)
  2. {
  3. if (!$context)
  4. $context = Context::getContext();
  5. if ($check_access && !$this->checkAccess($context->customer->id))
  6. return false;
  7.  
  8. $front = true;
  9. if (!in_array($context->controller->controller_type, array('front', 'modulefront')))
  10. $front = false;
  11.  
  12. if ($p < 1) $p = 1;
  13.  
  14. if (empty($order_by))
  15. $order_by = 'position';
  16. else
  17. /* Fix for all modules which are now using lowercase values for 'orderBy' parameter */
  18. $order_by = strtolower($order_by);
  19.  
  20. if (empty($order_way))
  21. $order_way = 'ASC';
  22. if ($order_by == 'id_product' || $order_by == 'date_add' || $order_by == 'date_upd')
  23. $order_by_prefix = 'p';
  24. elseif ($order_by == 'name')
  25. $order_by_prefix = 'pl';
  26. elseif ($order_by == 'manufacturer')
  27. {
  28. $order_by_prefix = 'm';
  29. $order_by = 'name';
  30. }
  31. elseif ($order_by == 'position')
  32. $order_by_prefix = 'cp';
  33.  
  34. if ($order_by == 'price')
  35. $order_by = 'orderprice';
  36.  
  37. if (!Validate::isBool($active) || !Validate::isOrderBy($order_by) || !Validate::isOrderWay($order_way))
  38. die (Tools::displayError());
  39.  
  40. $id_supplier = (int)Tools::getValue('id_supplier');
  41.  
  42. /* Return only the number of products */
  43. if ($get_total)
  44. {
  45. $sql = 'SELECT DISTINCT COUNT(cp.`id_product`) AS total
  46. FROM `'._DB_PREFIX_.'product` p
  47. '.Shop::addSqlAssociation('product', 'p').'
  48. LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON p.`id_product` = cp.`id_product`
  49. LEFT JOIN `'._DB_PREFIX_.'category` c ON c.`id_category` = cp.`id_category`
  50. WHERE c.`nleft` >= '.(int)$this->nleft.
  51. ' AND c.`nright` <= '.(int)$this->nright.
  52. ($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '').
  53. ($active ? ' AND product_shop.`active` = 1' : '').
  54. ($id_supplier ? 'AND p.id_supplier = '.(int)$id_supplier : '');
  55.  
  56. return (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);
  57. }
  58.  
  59. $sql = 'SELECT DISTINCT p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity, MAX(product_attribute_shop.id_product_attribute) id_product_attribute, product_attribute_shop.minimal_quantity AS product_attribute_minimal_quantity, pl.`description`, pl.`description_short`, pl.`available_now`,
  60. pl.`available_later`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, MAX(image_shop.`id_image`) id_image,
  61. il.`legend`, m.`name` AS manufacturer_name, cl.`name` AS category_default,
  62. DATEDIFF(product_shop.`date_add`, DATE_SUB(NOW(),
  63. INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).'
  64. DAY)) > 0 AS new, product_shop.price AS orderprice
  65. FROM `'._DB_PREFIX_.'category_product` cp
  66. LEFT JOIN `'._DB_PREFIX_.'product` p
  67. ON p.`id_product` = cp.`id_product`
  68. '.Shop::addSqlAssociation('product', 'p').'
  69. LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa
  70. ON (p.`id_product` = pa.`id_product`)
  71. '.Shop::addSqlAssociation('product_attribute', 'pa', false, 'product_attribute_shop.`default_on` = 1').'
  72. '.Product::sqlStock('p', 'product_attribute_shop', false, $context->shop).'
  73. LEFT JOIN `'._DB_PREFIX_.'category_lang` cl
  74. ON (product_shop.`id_category_default` = cl.`id_category`
  75. AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').')
  76. LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
  77. ON (p.`id_product` = pl.`id_product`
  78. AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').')
  79. LEFT JOIN `'._DB_PREFIX_.'image` i
  80. ON (i.`id_product` = p.`id_product`)'.
  81. Shop::addSqlAssociation('image', 'i', false, 'image_shop.cover=1').'
  82. LEFT JOIN `'._DB_PREFIX_.'image_lang` il
  83. ON (image_shop.`id_image` = il.`id_image`
  84. AND il.`id_lang` = '.(int)$id_lang.')
  85. LEFT JOIN `'._DB_PREFIX_.'manufacturer` m
  86. ON m.`id_manufacturer` = p.`id_manufacturer`
  87. LEFT JOIN `'._DB_PREFIX_.'category` c ON c.`id_category` = cp.`id_category`
  88.  
  89. WHERE product_shop.`id_shop` = '.(int)$context->shop->id.'
  90. AND c.`nleft` >= '.(int)$this->nleft.
  91. ' AND c.`nright` <= '.(int)$this->nright
  92. .($active ? ' AND product_shop.`active` = 1' : '')
  93. .($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '')
  94. .($id_supplier ? ' AND p.id_supplier = '.(int)$id_supplier : '')
  95. .' GROUP BY product_shop.id_product';
  96.  
  97. if ($random === true)
  98. {
  99. $sql .= ' ORDER BY RAND()';
  100. $sql .= ' LIMIT 0, '.(int)$random_number_products;
  101. }
  102. else
  103. $sql .= ' ORDER BY '.(isset($order_by_prefix) ? $order_by_prefix.'.' : '').'`'.pSQL($order_by).'` '.pSQL($order_way).'
  104. LIMIT '.(((int)$p - 1) * (int)$n).','.(int)$n;
  105.  
  106. $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
  107. if ($order_by == 'orderprice')
  108. Tools::orderbyPrice($result, $order_way);
  109.  
  110. if (!$result)
  111. return array();
  112.  
  113. /* Modify SQL result */
  114. return Product::getProductsProperties($id_lang, $result);
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement