Add filter value in cookie

This commit is contained in:
Michael RICOIS 2018-03-15 17:37:01 +01:00
parent 4691d8bb0f
commit 9803b2424c
2 changed files with 88 additions and 62 deletions

View File

@ -12,8 +12,13 @@ $order = array(
'quantity',
);
// Category
$id_category = (int) Tools::getValue('c');
// Page
$p = (int) Tools::getValue('p');
// Filters value - attribute
$f = Tools::getValue('f');
$result = array(
@ -22,56 +27,70 @@ $result = array(
'endreached' => FALSE,
);
if($id_category && $p && $id_category > 1) {
if(Module::isInstalled('privatesales')) {
include(dirname(__FILE__).'/../privatesales/Sale.php');
if ($id_category && $p && $id_category > 1) {
// Private Sales
if (Module::isInstalled('privatesales')) {
include dirname(__FILE__).'/../privatesales/Sale.php';
$sale = Sale::getSaleFromCategory($id_category);
if($cookie->isLogged() || !$cookie->isLogged() && $sale->pub == 1) {
if ($cookie->isLogged() || !$cookie->isLogged() && $sale->pub == 1) {
// Reset filters
if (empty($f)) {
$cookie->filters_category = null;
$cookie->filters_value = null;
}
// Add filters
else {
$cookie->filters_category = $id_category;
$cookie->filters_value = $f;
}
$cookie->write();
$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) ) {
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));
if (!empty($cookie->filters_value)) {
$nbProducts = count($category->getProductsByAttributes($cookie->id_lang, $cookie->filters_value, false, false));
}
else{
else {
$nbProducts = $category->getProducts(NULL, NULL, NULL, $orderBy, $orderWay, true);
}
// if($p >= $nbProducts / $n) {
if($p >= $nbProducts / $n && !Tools::getValue('init')) {
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);
if (!empty($cookie->filters_value)) {
if (Tools::getValue('init')) {
$cat_products = $category->getProductsByAttributes($cookie->id_lang, $cookie->filters_value, $p, $n, $orderBy, $orderWay);
}
else{
$cat_products = $category->getProductsByAttributes($cookie->id_lang, $f, $p+1, $n, $orderBy, $orderWay);
else {
$cat_products = $category->getProductsByAttributes($cookie->id_lang, $cookie->filters_value, $p+1, $n, $orderBy, $orderWay);
}
}
else{
if (Tools::getValue('init')){
else {
if (Tools::getValue('init')) {
$cat_products = $category->getProducts($cookie->id_lang, $p, $n, $orderBy, $orderWay);
}
else{
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);
}
if(!$cat_products) {
if (!$cat_products) {
$smarty->assign('products', array());
} else {
$smarty->assign('products', $cat_products);
@ -81,7 +100,7 @@ if($id_category && $p && $id_category > 1) {
}
$attributes = array();
$id_attributes = array();
foreach(Db::getInstance()->ExecuteS('
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).')
@ -95,17 +114,17 @@ if($id_category && $p && $id_category > 1) {
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']])) {
if (!isset($attributes[$attr['id_product']])) {
$attributes[$attr['id_product']] = array();
}
if(!isset($attributes[$attr['id_product']][$attr['group']])) {
if (!isset($attributes[$attr['id_product']][$attr['group']])) {
$attributes[$attr['id_product']][$attr['group']] = array();
}
if(!isset($id_attributes[$attr['id_product']])) {
if (!isset($id_attributes[$attr['id_product']])) {
$id_attributes[$attr['id_product']] = array();
}
if(!in_array($attr['id_attribute'], $id_attributes[$attr['id_product']])) {
if (!in_array($attr['id_attribute'], $id_attributes[$attr['id_product']])) {
$attributes[$attr['id_product']][$attr['group']][] = array(
'name' => $attr['name'],
'quantity' => $attr['quantity'],
@ -147,14 +166,16 @@ if($id_category && $p && $id_category > 1) {
} else {
$result['error'] = 'permission_denied';
}
} else {
}
// No private sales
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) {
if ($category->active) {
$nbProducts = $category->getProducts(NULL, NULL, NULL, $orderBy, $orderWay, true);
if($p >= $nbProducts / $n) {
if ($p >= $nbProducts / $n) {
$result['endreached'] = TRUE;
} else {
$cat_products = $category->getProducts($cookie->id_lang, $p + 1, $n, $orderBy, $orderWay);
@ -165,7 +186,7 @@ if($id_category && $p && $id_category > 1) {
$id_products[] = $p['id_product'];
}
$attributes = array();
foreach(Db::getInstance()->ExecuteS('
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).')
@ -178,10 +199,10 @@ if($id_category && $p && $id_category > 1) {
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']])) {
if (!isset($attributes[$attr['id_product']])) {
$attributes[$attr['id_product']] = array();
}
if(!isset($attributes[$attr['id_product']][$attr['group']])) {
if (!isset($attributes[$attr['id_product']][$attr['group']])) {
$attributes[$attr['id_product']][$attr['group']] = array();
}
$attributes[$attr['id_product']][$attr['group']][] = array(
@ -200,4 +221,5 @@ if($id_category && $p && $id_category > 1) {
} else {
$result['error'] = 'invalid_request';
}
echo json_encode(array($result));

View File

@ -1,18 +1,20 @@
<?php
class CategoryController extends CategoryControllerCore {
public function preProcess() {
class CategoryController extends CategoryControllerCore
{
public function preProcess()
{
global $site_version_front;
global $cookie;
parent::preProcess();
if(Module::isInstalled('privatesales')) {
if (Module::isInstalled('privatesales')) {
include_once dirname(__FILE__).'/../../modules/privatesales/Sale.php';
$id_category = Tools::getValue('id_category');
$sale = Sale::getSaleFromCategory($id_category);
$other_sales = array();
if(!empty($sale)){
if (!empty($sale)){
$other_sales = $sale->getOthersSales(true, null, null, 'current', false, Configuration::get('PRIVATES_SHOW_PUBLIC'), '`date_start` desc', $site_version_front);
foreach ($other_sales as $key => &$s) {
$s['is_thumb_img_lang'] = false;
@ -23,7 +25,7 @@ class CategoryController extends CategoryControllerCore {
$s['is_thumb_img'] = true;
}
}
if(file_exists(_PS_ROOT_DIR_.'/modules/privatesales/img/'.$sale->id.'/banner_mobile_'.$cookie->id_lang.'.jpg')) {
if (file_exists(_PS_ROOT_DIR_.'/modules/privatesales/img/'.$sale->id.'/banner_mobile_'.$cookie->id_lang.'.jpg')) {
$banner_mobile = true;
}
}
@ -38,7 +40,7 @@ class CategoryController extends CategoryControllerCore {
'category' => $this->category,
'HOOK_PRIVATESALES_CATEGORY' => Module::hookExec('privatesales_category', array('sale' => $sale)),
'HOOK_LEFT_COLUMN_VP' => Module::hookExec('displayleftVP', array('id_category' => $id_category)),
'is_sale_home' => ($sale? $sale->id_category == $id_category: FALSE),
'is_sale_home' => ($sale ? $sale->id_category == $id_category : false),
'other_sales' => $other_sales,
'last_qties' => (int)Configuration::get('PS_LAST_QTIES'),
'HOOK_PRIVATESALES_LISTING' => Module::hookExec('privatesales_listing', array('other_sales_category' => $other_sales, 'futuresales' => array())),
@ -46,7 +48,8 @@ class CategoryController extends CategoryControllerCore {
}
}
public function setMedia() {
public function setMedia()
{
parent::setMedia();
global $css_files;
@ -58,12 +61,14 @@ class CategoryController extends CategoryControllerCore {
$js_files[] = _PS_JS_DIR_.'/jquery.flexslider-min.js';
}
public function productListAssign() {
public function productListAssign()
{
global $cookie;
$hookExecuted = false;
Module::hookExec('productListAssign', array('nbProducts' => &$this->nbProducts, 'catProducts' => &$this->cat_products, 'hookExecuted' => &$hookExecuted));
if(!$hookExecuted) { // The hook was not executed, standard working
if (!$hookExecuted) { // The hook was not executed, standard working
self::$smarty->assign('categoryNameComplement', '');
$this->nbProducts = $this->category->getProducts(NULL, NULL, NULL, $this->orderBy, $this->orderWay, TRUE, TRUE, FALSE, 1, TRUE/*, ((($sale = self::$smarty->getTemplateVars('sale')) !== NULL) && (int) $sale->id > 0 && $sale->avail_only == TRUE? TRUE: FALSE)*/);
$this->pagination((int)$this->nbProducts); // Pagination must be call after "getProducts"
@ -73,19 +78,11 @@ class CategoryController extends CategoryControllerCore {
(int)($this->n),
$this->orderBy,
$this->orderWay,
FALSE,
TRUE,
FALSE,
false,
true,
false,
1,
TRUE/*,
(
(
(
$sale = self::$smarty->getTemplateVars('sale')
) !== NULL
)
&& (int) $sale->id > 0 && $sale->avail_only == TRUE? TRUE: FALSE
)*/
true
);
$id_products = array();
@ -115,17 +112,17 @@ class CategoryController extends CategoryControllerCore {
$attributes = array();
$id_attributes = array();
foreach($result as $attr) {
if(!isset($attributes[$attr['id_product']])) {
if (!isset($attributes[$attr['id_product']])) {
$attributes[$attr['id_product']] = array();
}
if(!isset($attributes[$attr['id_product']][$attr['group']])) {
if (!isset($attributes[$attr['id_product']][$attr['group']])) {
$attributes[$attr['id_product']][$attr['group']] = array();
}
if(!isset($id_attributes[$attr['id_product']])) {
if (!isset($id_attributes[$attr['id_product']])) {
$id_attributes[$attr['id_product']] = array();
}
if(!in_array($attr['id_attribute'], $id_attributes[$attr['id_product']])) {
if (!in_array($attr['id_attribute'], $id_attributes[$attr['id_product']])) {
$attributes[$attr['id_product']][$attr['group']][] = array(
'name' => $attr['name'],
'quantity' => $attr['quantity'],
@ -150,7 +147,14 @@ class CategoryController extends CategoryControllerCore {
self::$smarty->assign('images', $images);
self::$smarty->assign('categoryProductAttributes', $attributes);
} else { // Hook executed, use the override
// Filters
$id_category = Tools::getValue('id_category');
if ($cookie->filters_category == $id_category && !empty($cookie->filters_value)) {
$this->cat_products = $this->category->getProductsByAttributes($cookie->id_lang, $cookie->filters_value, (int)$this->p, (int)$this->n, $this->orderBy, $this->orderWay);
}
}
else { // Hook executed, use the override
$this->pagination((int)$this->nbProducts); // Pagination must be call after "getProducts"
}
self::$smarty->assign('nb_products', (int)$this->nbProducts);