Srv Bebeboutik 6c0978166c add modules
2016-01-04 12:49:26 +01:00

200 lines
7.8 KiB
PHP
Executable File

<?php
include(dirname(__FILE__).'/../../config/config.inc.php');
include(dirname(__FILE__).'/../../init.php');
$order = array(
'name',
'price',
'date_add',
'date_upd',
'position',
'manufacturer',
'quantity',
);
$id_category = (int) Tools::getValue('c');
$p = (int) Tools::getValue('p');
$f = Tools::getValue('f');
$result = array(
'error' => FALSE,
'products' => array(),
'endreached' => FALSE,
);
if($id_category && $p && $id_category > 1) {
if(Module::isInstalled('privatesales')) {
include(dirname(__FILE__).'/../privatesales/Sale.php');
$sale = Sale::getSaleFromCategory($id_category);
if($cookie->isLogged() || !$cookie->isLogged() && $sale->pub == 1) {
$now = mktime();
$category = new Category($id_category, $cookie->id_lang);
$customer = new Customer((int) $cookie->id_customer);
if( ($sale !== NULL
&& $sale->enabled
&& strtotime($sale->date_start) < $now
&& strtotime($sale->date_end) > $now
&& count(array_intersect(($cookie->isLogged()? Customer::getGroupsStatic((int) $cookie->id_customer): array(1)), $sale->groups) > 0)
&& $category->active) || ($customer->isMemberOfGroup(2) == 1) ) {
$n = (int) Configuration::get('PS_PRODUCTS_PER_PAGE', 10);
$orderBy = $order[Configuration::get('PS_PRODUCTS_ORDER_BY', 1)];
$orderWay = (Configuration::get('PS_PRODUCTS_ORDER_WAY', 0) == 0? 'ASC': 'DESC');
if (!empty($f)){
$nbProducts = count($category->getProductsByAttributes($cookie->id_lang, $f, false, false));
}
else{
$nbProducts = $category->getProducts(NULL, NULL, NULL, $orderBy, $orderWay, true);
}
// if($p >= $nbProducts / $n) {
if($p >= $nbProducts / $n && !Tools::getValue('init')) {
$result['endreached'] = TRUE;
} else {
if (!empty($f)){
if (Tools::getValue('init')){
$cat_products = $category->getProductsByAttributes($cookie->id_lang, $f, $p, $n, $orderBy, $orderWay);
}
else{
$cat_products = $category->getProductsByAttributes($cookie->id_lang, $f, $p+1, $n, $orderBy, $orderWay);
}
}
else{
if (Tools::getValue('init')){
$cat_products = $category->getProducts($cookie->id_lang, $p, $n, $orderBy, $orderWay);
}
else{
$cat_products = $category->getProducts($cookie->id_lang, $p + 1, $n, $orderBy, $orderWay);
}
// $cat_products = $category->getProducts($cookie->id_lang, $p + 1, $n, $orderBy, $orderWay);
}
$smarty->assign('products', $cat_products);
$id_products = array();
foreach($cat_products as $p) {
$id_products[] = $p['id_product'];
}
$attributes = array();
$id_attributes = array();
foreach(Db::getInstance()->ExecuteS('
SELECT pa.`id_product`, pa.`quantity`, ag.`public_name` as `group`, al.`name`, al.`id_attribute`
FROM `'._DB_PREFIX_.'product_attribute` pa, `'._DB_PREFIX_.'product_attribute_combination` ac, `'._DB_PREFIX_.'attribute` a, `'._DB_PREFIX_.'attribute_lang` al, `'._DB_PREFIX_.'attribute_group_lang` ag
WHERE pa.`id_product` IN ('.implode(', ', $id_products).')
AND ac.`id_product_attribute` = pa.`id_product_attribute`
AND al.`id_attribute` = ac.`id_attribute`
AND al.`id_lang` = '.(int) $cookie->id_lang.'
AND a.`id_attribute` = al.`id_attribute`
AND ag.`id_attribute_group` = a.`id_attribute_group`
AND ag.`id_lang` = '.(int) $cookie->id_lang.'
AND pa.`quantity` > 0
AND ag.`id_attribute_group` IN(75, 9, 272, 172)
ORDER BY pa.`id_product` ASC, ag.`public_name` ASC, al.`name` ASC
') as $attr) {
if(!isset($attributes[$attr['id_product']])) {
$attributes[$attr['id_product']] = array();
}
if(!isset($attributes[$attr['id_product']][$attr['group']])) {
$attributes[$attr['id_product']][$attr['group']] = array();
}
if(!isset($id_attributes[$attr['id_product']])) {
$id_attributes[$attr['id_product']] = array();
}
if(!in_array($attr['id_attribute'], $id_attributes[$attr['id_product']])) {
$attributes[$attr['id_product']][$attr['group']][] = array(
'name' => $attr['name'],
'quantity' => $attr['quantity'],
);
$id_attributes[$attr['id_product']][] = $attr['id_attribute'];
}
}
// $images = array();
// foreach ($id_products as $key => $id_product) {
// $product = new Product($id_product);
// $images_product = $product->getImages((int) $cookie->id_lang);
// $images[(int)$id_product] = $images_product;
// }
$images = array();
foreach(Db::getInstance()->ExecuteS('
SELECT i.`id_product`, i.`cover`, i.`id_image`, il.`legend`, i.`position`
FROM `'._DB_PREFIX_.'image` i
LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)($cookie->id_lang).')
WHERE i.`id_product` IN ('.implode(', ', $id_products).')
ORDER BY `position`
') as $img) {
if(!isset($images[$img['id_product']])) {
$images[$img['id_product']] = array();
}
$images[$img['id_product']][] = $img;
}
$smarty->assign('images', $images);
$smarty->assign('categoryProductAttributes', $attributes);
$result['products'] = $smarty->fetch(file_exists(_PS_THEME_DIR_.'modules/categoryscroll/product-list.tpl')? '../../themes/'._THEME_NAME_.'/modules/categoryscroll/product-list.tpl': 'product-list.tpl', __FILE__);
}
} else {
$result['error'] = 'privatesale_inactive';
}
} else {
$result['error'] = 'permission_denied';
}
} else {
$n = (int) Configuration::get('PS_PRODUCTS_PER_PAGE', 10);
$orderBy = $order[Configuration::get('PS_PRODUCTS_ORDER_BY', 1)];
$orderWay = (Configuration::get('PS_PRODUCTS_ORDER_WAY', 0) == 0? 'ASC': 'DESC');
$category = new Category($id_category, $cookie->id_lang);
if($category->active) {
$nbProducts = $category->getProducts(NULL, NULL, NULL, $orderBy, $orderWay, true);
if($p >= $nbProducts / $n) {
$result['endreached'] = TRUE;
} else {
$cat_products = $category->getProducts($cookie->id_lang, $p + 1, $n, $orderBy, $orderWay);
$smarty->assign('products', $cat_products);
$id_products = array();
foreach($cat_products as $p) {
$id_products[] = $p['id_product'];
}
$attributes = array();
foreach(Db::getInstance()->ExecuteS('
SELECT pa.`id_product`, pa.`quantity`, ag.`public_name` as `group`, al.`name`
FROM `'._DB_PREFIX_.'product_attribute` pa, `'._DB_PREFIX_.'product_attribute_combination` ac, `'._DB_PREFIX_.'attribute` a, `'._DB_PREFIX_.'attribute_lang` al, `'._DB_PREFIX_.'attribute_group_lang` ag
WHERE pa.`id_product` IN ('.implode(', ', $id_products).')
AND ac.`id_product_attribute` = pa.`id_product_attribute`
AND al.`id_attribute` = ac.`id_attribute`
AND al.`id_lang` = '.(int) $cookie->id_lang.'
AND a.`id_attribute` = al.`id_attribute`
AND ag.`id_attribute_group` = a.`id_attribute_group`
AND ag.`id_lang` = '.(int) $cookie->id_lang.'
AND pa.`quantity` > 0
ORDER BY pa.`id_product` ASC, ag.`public_name` ASC, al.`name` ASC
') as $attr) {
if(!isset($attributes[$attr['id_product']])) {
$attributes[$attr['id_product']] = array();
}
if(!isset($attributes[$attr['id_product']][$attr['group']])) {
$attributes[$attr['id_product']][$attr['group']] = array();
}
$attributes[$attr['id_product']][$attr['group']][] = array(
'name' => $attr['name'],
'quantity' => $attr['quantity'],
);
}
$smarty->assign('categoryProductAttributes', $attributes);
$result['products'] = $smarty->fetch(file_exists(_PS_THEME_DIR_.'modules/categoryscroll/product-list.tpl')? '../../themes/'._THEME_NAME_.'/modules/categoryscroll/product-list.tpl': 'product-list.tpl', __FILE__);
}
} else {
$result['error'] = 'inactive_category';
}
}
} else {
$result['error'] = 'invalid_request';
}
echo json_encode(array($result));