bebeboutik/override/classes/Category.php

407 lines
16 KiB
PHP
Raw Normal View History

2016-01-04 12:48:08 +01:00
<?php
class Category extends CategoryCore {
public function getProducts($id_lang, $p, $n, $orderBy = NULL, $orderWay = NULL, $getTotal = false, $active = true, $random = false, $randomNumberProducts = 1, $checkAccess = true)
{
global $cookie;
if (!$checkAccess OR !$this->checkAccess($cookie->id_customer))
return false;
if ($p < 1) $p = 1;
if (empty($orderBy))
$orderBy = 'position';
else
/* Fix for all modules which are now using lowercase values for 'orderBy' parameter */
$orderBy = strtolower($orderBy);
if (empty($orderWay))
$orderWay = 'ASC';
if ($orderBy == 'id_product' OR $orderBy == 'date_add')
$orderByPrefix = 'p';
elseif ($orderBy == 'name')
$orderByPrefix = 'pl';
elseif ($orderBy == 'manufacturer')
{
$orderByPrefix = 'm';
$orderBy = 'name';
}
elseif ($orderBy == 'position')
$orderByPrefix = 'cp';
if ($orderBy == 'price')
$orderBy = 'orderprice';
if (!Validate::isBool($active) OR !Validate::isOrderBy($orderBy) OR !Validate::isOrderWay($orderWay))
die (Tools::displayError());
$id_supplier = (int)(Tools::getValue('id_supplier'));
/* Return only the number of products */
if ($getTotal)
{
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
SELECT COUNT(cp.`id_product`) AS total
FROM `'._DB_PREFIX_.'product` p
LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON p.`id_product` = cp.`id_product`
WHERE cp.`id_category` = '.(int)($this->id).($active ? ' AND p.`active` = 1' : '').'
'.($id_supplier ? 'AND p.id_supplier = '.(int)($id_supplier) : ''));
return isset($result) ? $result['total'] : 0;
}
$sql = '
SELECT p.*, pa.`id_product_attribute`, pl.`description`, pl.`description_short`, pl.`available_now`, pl.`available_later`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, i.`id_image`, il.`legend`, m.`name` AS manufacturer_name, tl.`name` AS tax_name, t.`rate`, cl.`name` AS category_default, DATEDIFF(p.`date_add`, DATE_SUB(NOW(), INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).' DAY)) > 0 AS new,
(p.`price` * IF(t.`rate`,((100 + (t.`rate`))/100),1)) AS orderprice
FROM `'._DB_PREFIX_.'category_product` cp
LEFT JOIN `'._DB_PREFIX_.'product` p ON p.`id_product` = cp.`id_product`
LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON (p.`id_product` = pa.`id_product` AND default_on = 1)
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (p.`id_category_default` = cl.`id_category` AND cl.`id_lang` = '.(int)($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group`
AND tr.`id_country` = '.(int)Country::getDefaultCountryId().'
AND tr.`id_state` = 0)
LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`)
LEFT JOIN `'._DB_PREFIX_.'tax_lang` tl ON (t.`id_tax` = tl.`id_tax` AND tl.`id_lang` = '.(int)($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON m.`id_manufacturer` = p.`id_manufacturer`
WHERE cp.`id_category` = '.(int)($this->id).($active ? ' AND p.`active` = 1' : '').'
'.($id_supplier ? 'AND p.id_supplier = '.(int)$id_supplier : '');
if ($random === true)
{
$sql .= ' ORDER BY RAND()';
$sql .= ' LIMIT 0, '.(int)($randomNumberProducts);
}
else
{
$sql .= ' ORDER BY IF(p.`quantity` > 0, 1, 0) DESC, '.(isset($orderByPrefix) ? $orderByPrefix.'.' : '').'`'.pSQL($orderBy).'` '.pSQL($orderWay).'
LIMIT '.(((int)($p) - 1) * (int)($n)).','.(int)($n);
}
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql);
if ($orderBy == 'orderprice')
Tools::orderbyPrice($result, $orderWay);
if (!$result)
return false;
/* Modify SQL result */
return Product::getProductsProperties($id_lang, $result);
}
public function getProductsStats($id_lang, $p, $n, $orderBy = NULL, $orderWay = NULL, $getTotal = false, $active = true, $random = false, $randomNumberProducts = 1, $checkAccess = true)
{
global $cookie;
if (!$checkAccess OR !$this->checkAccess($cookie->id_customer))
return false;
if ($p < 1) $p = 1;
if (empty($orderBy))
$orderBy = 'position';
else
/* Fix for all modules which are now using lowercase values for 'orderBy' parameter */
$orderBy = strtolower($orderBy);
if (empty($orderWay))
$orderWay = 'ASC';
if ($orderBy == 'id_product' OR $orderBy == 'date_add')
$orderByPrefix = 'p';
elseif ($orderBy == 'name')
$orderByPrefix = 'pl';
elseif ($orderBy == 'manufacturer')
{
$orderByPrefix = 'm';
$orderBy = 'name';
}
elseif ($orderBy == 'position')
$orderByPrefix = 'cp';
if ($orderBy == 'price')
$orderBy = 'orderprice';
if (!Validate::isBool($active) OR !Validate::isOrderBy($orderBy) OR !Validate::isOrderWay($orderWay))
die (Tools::displayError());
$id_supplier = (int)(Tools::getValue('id_supplier'));
/* Return only the number of products */
if ($getTotal)
{
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
SELECT COUNT(cp.`id_product`) AS total
FROM `'._DB_PREFIX_.'product` p
LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON p.`id_product` = cp.`id_product`
WHERE cp.`id_category` = '.(int)($this->id).($active ? ' AND p.`active` = 1' : '').'
'.($id_supplier ? 'AND p.id_supplier = '.(int)($id_supplier) : ''));
return isset($result) ? $result['total'] : 0;
}
$categories = array();
foreach (Db::getInstance()->ExecuteS('
SELECT
`id_category`
FROM `'._DB_PREFIX_.'category`
WHERE `id_parent` = ' . $this->id
) as $row ) {
$categories[] = $row['id_category'];
}
$categories[] = $this->id;
$sql = '
SELECT
p.*,
pa.`id_product_attribute`,
pl.`description`,
pl.`description_short`,
pl.`available_now`,
pl.`available_later`,
pl.`link_rewrite`,
pl.`meta_description`,
pl.`meta_keywords`,
pl.`meta_title`,
pl.`name`,
i.`id_image`,
il.`legend`,
m.`name` AS manufacturer_name,
tl.`name` AS tax_name,
t.`rate`,
cl.`name` AS category_default,
DATEDIFF(p.`date_add`, DATE_SUB(NOW(),
INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).' DAY)) > 0 AS new,
(p.`price` * IF(t.`rate`,((100 + (t.`rate`))/100),1)) AS orderprice
FROM `'._DB_PREFIX_.'category_product` cp
LEFT JOIN `'._DB_PREFIX_.'product` p ON p.`id_product` = cp.`id_product`
LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON (p.`id_product` = pa.`id_product` AND default_on = 1)
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (p.`id_category_default` = cl.`id_category` AND cl.`id_lang` = '.(int)($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group`
AND tr.`id_country` = '.(int)Country::getDefaultCountryId().'
AND tr.`id_state` = 0)
LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`)
LEFT JOIN `'._DB_PREFIX_.'tax_lang` tl ON (t.`id_tax` = tl.`id_tax` AND tl.`id_lang` = '.(int)($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON m.`id_manufacturer` = p.`id_manufacturer`
WHERE cp.`id_category` IN ('.implode($categories, ',').')'.($active ? ' AND p.`active` = 1' : '').'
'.($id_supplier ? 'AND p.id_supplier = '.(int)$id_supplier : '');
// if ($random === true)
// {
// $sql .= ' ORDER BY RAND()';
// $sql .= ' LIMIT 0, '.(int)($randomNumberProducts);
// }
// else
// {
// $sql .= ' ORDER BY IF(p.`quantity` > 0, 1, 0) DESC, '.(isset($orderByPrefix) ? $orderByPrefix.'.' : '').'`'.pSQL($orderBy).'` '.pSQL($orderWay).'
// LIMIT '.(((int)($p) - 1) * (int)($n)).','.(int)($n);
// }
$sql .= " GROUP BY p.`id_product`, pa.`id_product_attribute`
ORDER BY CAST( SPLIT_STRING(
'-', p.`reference` , 2
) AS unsigned )";
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql);
// if ($orderBy == 'orderprice')
// Tools::orderbyPrice($result, $orderWay);
if (!$result)
return false;
/* Modify SQL result */
return Product::getProductsProperties($id_lang, $result);
}
public static function getIdParent($id_category){
$sql = 'SELECT c.id_parent
FROM '._DB_PREFIX_.'category c
WHERE id_category ='.$id_category;
return $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);
}
/**
* Return current category products
*
* @param integer $id_lang Language ID
* @param integer $p Page number
* @param integer $n Number of products per page
* @param boolean $getTotal return the number of results instead of the results themself
* @param boolean $active return only active products
* @param boolean $random active a random filter for returned products
* @param int $randomNumberProducts number of products to return if random is activated
* @param boolean $checkAccess set to false to return all products (even if customer hasn't access)
* @return mixed Products or number of products
*/
public function getProductsByAttributes($id_lang, $attributes=false, $p=false, $n=false, $orderBy = NULL, $orderWay = NULL, $getTotal = false, $active = true, $random = false, $randomNumberProducts = 1, $checkAccess = true)
{
global $cookie;
if (!$checkAccess OR !$this->checkAccess($cookie->id_customer))
return false;
if ($p < 1) $p = 1;
if (empty($orderBy))
$orderBy = 'position';
else
/* Fix for all modules which are now using lowercase values for 'orderBy' parameter */
$orderBy = strtolower($orderBy);
if (empty($orderWay))
$orderWay = 'ASC';
if ($orderBy == 'id_product' OR $orderBy == 'date_add')
$orderByPrefix = 'p';
elseif ($orderBy == 'name')
$orderByPrefix = 'pl';
elseif ($orderBy == 'manufacturer')
{
$orderByPrefix = 'm';
$orderBy = 'name';
}
elseif ($orderBy == 'position')
$orderByPrefix = 'cp';
if ($orderBy == 'price')
$orderBy = 'orderprice';
if (!Validate::isBool($active) OR !Validate::isOrderBy($orderBy) OR !Validate::isOrderWay($orderWay))
die (Tools::displayError());
$id_supplier = (int)(Tools::getValue('id_supplier'));
/* Return only the number of products */
if ($getTotal)
{
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
SELECT COUNT(cp.`id_product`) AS total
FROM `'._DB_PREFIX_.'product` p
LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON p.`id_product` = cp.`id_product`
WHERE cp.`id_category` = '.(int)($this->id).($active ? ' AND p.`active` = 1' : '').'
'.($id_supplier ? 'AND p.id_supplier = '.(int)($id_supplier) : ''));
return isset($result) ? $result['total'] : 0;
}
$sql = '
SELECT DISTINCT p.*, pl.`description`, pl.`description_short`, pl.`available_now`, pl.`available_later`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, i.`id_image`, il.`legend`, m.`name` AS manufacturer_name, tl.`name` AS tax_name, t.`rate`, cl.`name` AS category_default, DATEDIFF(p.`date_add`, DATE_SUB(NOW(), INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).' DAY)) > 0 AS new,
(p.`price` * IF(t.`rate`,((100 + (t.`rate`))/100),1)) AS orderprice
FROM `'._DB_PREFIX_.'category_product` cp
LEFT JOIN `'._DB_PREFIX_.'product` p ON p.`id_product` = cp.`id_product`
LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON (p.`id_product` = pa.`id_product` )
LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON (pac.`id_product_attribute` = pa.`id_product_attribute`)
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (p.`id_category_default` = cl.`id_category` AND cl.`id_lang` = '.(int)($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group`
AND tr.`id_country` = '.(int)Country::getDefaultCountryId().'
AND tr.`id_state` = 0)
LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`)
LEFT JOIN `'._DB_PREFIX_.'tax_lang` tl ON (t.`id_tax` = tl.`id_tax` AND tl.`id_lang` = '.(int)($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON m.`id_manufacturer` = p.`id_manufacturer`
WHERE cp.`id_category` = '.(int)($this->id).($active ? ' AND p.`active` = 1' : '').'
'.($id_supplier ? 'AND p.id_supplier = '.(int)$id_supplier : '').'
AND p.`quantity` > 0';
if ( $attributes ){
$sql .= ' AND pac.`id_attribute` IN ('.$attributes.')
AND pa.`quantity` > 0';
}
if ($random === true)
{
$sql .= ' ORDER BY RAND()';
$sql .= ' LIMIT 0, '.(int)($randomNumberProducts);
}
else
{
$sql .= ' ORDER BY '.(isset($orderByPrefix) ? $orderByPrefix.'.' : '').'`'.pSQL($orderBy).'` '.pSQL($orderWay);
if ( $n && $p ){
$sql.=' LIMIT '.(((int)($p) - 1) * (int)($n)).','.(int)($n);
}
}
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql);
if ($orderBy == 'orderprice')
Tools::orderbyPrice($result, $orderWay);
if (!$result)
return false;
/* Modify SQL result */
return $data = Product::getProductsProperties($id_lang, $result);
return Product::getProductsProperties($id_lang, $result);
}
public static function getMaxId()
{
return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
SELECT MAX(`id_category`) as max
FROM `'._DB_PREFIX_.'category`
');
}
public static function getCategoriesSameVP($id_category, $id_lang) {
return $categories = Db::getInstance()->ExecuteS('
SELECT pc.`id_category`, cl.`name`, c.`id_parent`
FROM `'._DB_PREFIX_.'privatesale_category` pc
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON pc.`id_category` = cl.`id_category`
LEFT JOIN `'._DB_PREFIX_.'category` c ON c.`id_category` = cl.`id_category`
WHERE pc.`id_sale` = (SELECT `id_sale` FROM `'._DB_PREFIX_.'privatesale_category` WHERE `id_category` = '. (int)$id_category .')
AND cl.`id_lang` = '. (int)$id_lang .'');
}
public static function getCategoriesVP($id_lang){
return $categories = Db::getInstance()->ExecuteS('
SELECT c.`id_category`, l.`name`, l.`link_rewrite`
FROM `'._DB_PREFIX_.'category` c, `'._DB_PREFIX_.'category_lang` l
WHERE c.`id_category` = l.`id_category` AND l.`id_lang` = '.intval($id_lang).' AND c.`id_parent` = '.Configuration::get('PRIVATESALES_ROOT').'
ORDER BY c.`id_category` DESC
');
}
public static function getSalesInfos($ids)
{
if (!class_exists('PrivateSales_ExtraFields')) {
require_once dirname(__FILE__).'/../../modules/privatesales_extrafields/privatesales_extrafields.php';
}
$results = array();
$id_sales = array();
foreach (Db::getInstance()->executeS('SELECT `id_sale`, `id_category`
FROM `'._DB_PREFIX_.'privatesale_category`
WHERE id_category IN ('.implode(', ', $ids).')
') as $key => $value) {
if(!isset($results[$value['id_category']])) {
$results[$value['id_category']] = array();
}
$results[$value['id_category']]['id_sale'] = $value['id_sale'];
$id_sales[] = $value['id_sale'];
}
array_unique($id_sales);
$extrafields = PrivateSales_ExtraFields::getFieldsForSale($id_sales);
foreach ($results as $key => $result) {
$results[$key]['sales'] = $extrafields[$result['id_sale']];
}
return $results;
}
}