From de456e45eca048d499e2e4a25b7c322322b99686 Mon Sep 17 00:00:00 2001 From: Damien Metzger Date: Tue, 3 Dec 2013 11:44:32 +0100 Subject: [PATCH] [*] Bo : removed empty filters from the cookie (thus limiting its size) --- classes/AdminTab.php | 2 +- classes/controller/AdminController.php | 71 +++++++++++++++---------- classes/helper/HelperList.php | 21 ++++---- controllers/front/CompareController.php | 2 +- 4 files changed, 55 insertions(+), 41 deletions(-) diff --git a/classes/AdminTab.php b/classes/AdminTab.php index bc8b7d019..80fb65dfd 100644 --- a/classes/AdminTab.php +++ b/classes/AdminTab.php @@ -116,7 +116,7 @@ abstract class AdminTabCore protected $_tmpTableFilter = ''; /** @var array Number of results in list per page (used in select field) */ - protected $_pagination = array(20, 50, 100, 300); + protected $_pagination = array(20, 50, 100, 300, 1000); /** @var string ORDER BY clause determined by field/arrows in list header */ protected $_orderBy; diff --git a/classes/controller/AdminController.php b/classes/controller/AdminController.php index 0c2f9daef..3d23ea9c1 100644 --- a/classes/controller/AdminController.php +++ b/classes/controller/AdminController.php @@ -150,7 +150,10 @@ class AdminControllerCore extends Controller protected $_tmpTableFilter = ''; /** @var array Number of results in list per page (used in select field) */ - protected $_pagination = array(20, 50, 100, 300); + protected $_pagination = array(20, 50, 100, 300, 1000); + + /** @var integer Default number of results in list per page */ + protected $_default_pagination = 50; /** @var string ORDER BY clause determined by field/arrows in list header */ protected $_orderBy; @@ -497,20 +500,35 @@ class AdminControllerCore extends Controller $this->list_id = $this->table; $prefix = str_replace(array('admin', 'controller'), '', Tools::strtolower(get_class($this))); - // Filter memorization - if (isset($_POST) && !empty($_POST) && isset($this->list_id)) + + if (isset($this->list_id)) + { foreach ($_POST as $key => $value) { - if (stripos($key, $this->list_id.'Filter_') === 0) + if (empty($value)) + unset($this->context->cookie->{$prefix.$key}); + elseif (stripos($key, $this->list_id.'Filter_') === 0) $this->context->cookie->{$prefix.$key} = !is_array($value) ? $value : serialize($value); - elseif(stripos($key, 'submitFilter') === 0) + elseif (stripos($key, 'submitFilter') === 0) $this->context->cookie->$key = !is_array($value) ? $value : serialize($value); } - if (isset($_GET) && !empty($_GET) && isset($this->list_id)) foreach ($_GET as $key => $value) - if (stripos($key, $this->list_id.'OrderBy') === 0 || stripos($key, $this->list_id.'Orderway') === 0) - $this->context->cookie->{$prefix.$key} = $value; + if (stripos($key, $this->list_id.'OrderBy') === 0) + { + if (empty($value) || $value == $this->_defaultOrderBy) + unset($this->context->cookie->{$prefix.$key}); + else + $this->context->cookie->{$prefix.$key} = $value; + } + elseif (stripos($key, $this->list_id.'Orderway') === 0) + { + if (empty($value) || $value == $this->_defaultOrderWay) + unset($this->context->cookie->{$prefix.$key}); + else + $this->context->cookie->{$prefix.$key} = $value; + } + } $filters = $this->context->cookie->getFamily($prefix.$this->list_id.'Filter_'); @@ -959,32 +977,28 @@ class AdminControllerCore extends Controller */ public function processResetFilters($list_id = null) { - if (!isset($list_id)) + if ($list_id === null) $list_id = isset($this->list_id) ? $this->list_id : $this->table; $prefix = str_replace(array('admin', 'controller'), '', Tools::strtolower(get_class($this))); $filters = $this->context->cookie->getFamily($prefix.$list_id.'Filter_'); - foreach ($filters as $cookie_key => $filter) if (strncmp($cookie_key, $prefix.$list_id.'Filter_', 7 + Tools::strlen($prefix.$list_id)) == 0) { $key = substr($cookie_key, 7 + Tools::strlen($prefix.$list_id)); - if (is_array($this->fields_list) && array_key_exists($key, $this->fields_list)) $this->context->cookie->$cookie_key = null; - unset($this->context->cookie->$cookie_key); + unset($this->context->cookie->$cookie_key); } if (isset($this->context->cookie->{'submitFilter'.$list_id})) unset($this->context->cookie->{'submitFilter'.$list_id}); - if (isset($this->context->cookie->{$prefix.$list_id.'Orderby'})) unset($this->context->cookie->{$prefix.$list_id.'Orderby'}); - if (isset($this->context->cookie->{$prefix.$list_id.'Orderway'})) unset($this->context->cookie->{$prefix.$list_id.'Orderway'}); - unset($_POST); + $_POST = array(); $this->_filter = false; unset($this->_filterHaving); unset($this->_having); @@ -2374,7 +2388,7 @@ class AdminControllerCore extends Controller if (isset($this->context->cookie->{$this->list_id.'_pagination'}) && $this->context->cookie->{$this->list_id.'_pagination'}) $limit = $this->context->cookie->{$this->list_id.'_pagination'}; else - $limit = $this->_pagination[1]; + $limit = $this->_default_pagination; } if (!Validate::isTableOrIdentifier($this->table)) @@ -2401,7 +2415,10 @@ class AdminControllerCore extends Controller } $limit = (int)Tools::getValue($this->list_id.'_pagination', $limit); - $this->context->cookie->{$this->list_id.'_pagination'} = $limit; + if (in_array($limit, $this->_pagination) && $limit != $this->_default_pagination) + $this->context->cookie->{$this->list_id.'_pagination'} = $limit; + else + unset($this->context->cookie->{$this->list_id.'_pagination'}); /* Check params validity */ if (!Validate::isOrderBy($order_by) || !Validate::isOrderWay($order_way) @@ -2416,20 +2433,17 @@ class AdminControllerCore extends Controller $order_by = $this->fields_list[$order_by]['order_key']; /* Determine offset from current page */ - - - if ((isset($_POST['submitFilter'.$this->list_id]) || - isset($_POST['submitFilter'.$this->list_id.'_x']) || - isset($_POST['submitFilter'.$this->list_id.'_y'])) && - !empty($_POST['submitFilter'.$this->list_id]) && - is_numeric($_POST['submitFilter'.$this->list_id])) - $start = ((int)$_POST['submitFilter'.$this->list_id] - 1) * $limit; + $start = 0; + if ((int)Tools::getValue('submitFilter'.$this->list_id)) + $start = ((int)Tools::getValue('submitFilter'.$this->list_id) - 1) * $limit; elseif (empty($start) && isset($this->context->cookie->{$this->list_id.'_start'}) && Tools::isSubmit('export'.$this->table)) $start = $this->context->cookie->{$this->list_id.'_start'}; - else - $start = 0; - $this->context->cookie->{$this->list_id.'_start'} = $start; + // Either save or reset the offset in the cookie + if ($start) + $this->context->cookie->{$this->list_id.'_start'} = $start; + elseif (isset($this->context->cookie->{$this->list_id.'_start'})) + unset($this->context->cookie->{$this->list_id.'_start'}); /* Cache */ $this->_lang = (int)$id_lang; @@ -2549,7 +2563,6 @@ class AdminControllerCore extends Controller 'list' => &$this->_list, 'list_total' => &$this->_listTotal, )); - } public function getModulesList($filter_modules_list) diff --git a/classes/helper/HelperList.php b/classes/helper/HelperList.php index 75a42046d..21fd89203 100644 --- a/classes/helper/HelperList.php +++ b/classes/helper/HelperList.php @@ -39,7 +39,10 @@ class HelperListCore extends Helper protected $_filter; /** @var array Number of results in list per page (used in select field) */ - protected $_pagination = array(20, 50, 100, 300); + protected $_pagination = array(20, 50, 100, 300, 1000); + + /** @var integer Default number of results in list per page */ + protected $_default_pagination = 50; /** @var string ORDER BY clause determined by field/arrows in list header */ public $orderBy; @@ -511,15 +514,13 @@ class HelperListCore extends Helper $token = $this->token; /* Determine total page number */ - if (isset($this->context->cookie->{$this->list_id.'_pagination'}) && $this->context->cookie->{$this->list_id.'_pagination'}) - $default_pagination = $this->context->cookie->{$this->list_id.'_pagination'}; - else - $default_pagination = $this->_pagination[0]; + $pagination = $this->_default_pagination; + if (in_array((int)Tools::getValue($this->list_id.'_pagination'), $this->_pagination)) + $pagination = (int)Tools::getValue($this->list_id.'_pagination'); + elseif (isset($this->context->cookie->{$this->list_id.'_pagination'}) && $this->context->cookie->{$this->list_id.'_pagination'}) + $pagination = $this->context->cookie->{$this->list_id.'_pagination'}; - $total_pages = ceil($this->listTotal / Tools::getValue($this->list_id.'_pagination', ($default_pagination))); - - if (!$total_pages) - $total_pages = 1; + $total_pages = max(1, ceil($this->listTotal / $pagination)); $identifier = Tools::getIsset($this->identifier) ? '&'.$this->identifier.'='.(int)Tools::getValue($this->identifier) : ''; $order = ''; @@ -535,7 +536,7 @@ class HelperListCore extends Helper /* Choose number of results per page */ $selected_pagination = Tools::getValue($this->list_id.'_pagination', - isset($this->context->cookie->{$this->list_id.'_pagination'}) ? $this->context->cookie->{$this->list_id.'_pagination'} : null + isset($this->context->cookie->{$this->list_id.'_pagination'}) ? $this->context->cookie->{$this->list_id.'_pagination'} : $this->_default_pagination ); if (!isset($this->table_id) && $this->position_identifier && (int)Tools::getValue($this->position_identifier, 1)) diff --git a/controllers/front/CompareController.php b/controllers/front/CompareController.php index d7cbf3c50..d88826c8d 100644 --- a/controllers/front/CompareController.php +++ b/controllers/front/CompareController.php @@ -142,7 +142,7 @@ class CompareControllerCore extends FrontController )); $this->context->smarty->assign('HOOK_EXTRA_PRODUCT_COMPARISON', Hook::exec('displayProductComparison', array('list_ids_product' => $ids))); } - else if (isset($this->context->cookie->id_compare)) + elseif (isset($this->context->cookie->id_compare)) { $object = new CompareProduct((int)$this->context->cookie->id_compare); if (Validate::isLoadedObject($object))