From 1cd23cf84722e6c80e83cfd4d431d348a3287537 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Wed, 28 Feb 2024 11:51:19 +0530 Subject: [PATCH] Fix 0 in range filters --- .../products/helpers/list/list_header.tpl | 6 +++--- classes/controller/AdminController.php | 21 ++++++++++++------- classes/helper/HelperList.php | 17 ++++++++++++--- controllers/admin/AdminProductsController.php | 15 ++++++++++--- 4 files changed, 43 insertions(+), 16 deletions(-) diff --git a/admin/themes/default/template/controllers/products/helpers/list/list_header.tpl b/admin/themes/default/template/controllers/products/helpers/list/list_header.tpl index 8c1726d7f..0255b15c6 100644 --- a/admin/themes/default/template/controllers/products/helpers/list/list_header.tpl +++ b/admin/themes/default/template/controllers/products/helpers/list/list_header.tpl @@ -97,15 +97,15 @@ {elseif $params.type == 'range' && $params.value|is_array} {assign var="filter_value" value=''} {foreach $params.value as $value} - {if ($value|is_string && !empty($value))} - {if empty($filter_value)} + {if (isset($value) && ($value !== '' || $value === 0))} + {if !(isset($filter_value) && ($filter_value !== '' || $filter_value === 0))} {assign var="filter_value" value=$value} {else} {assign var="filter_value" value="`$filter_value` - `$value`"} {/if} {/if} {/foreach} - {if !empty($filter_value)} + {if (isset($filter_value) && ($filter_value !== '' || $filter_value === 0))} {$params['title']|escape:'html':'UTF-8'}: {$filter_value|escape:'html':'UTF-8'} diff --git a/classes/controller/AdminController.php b/classes/controller/AdminController.php index 2d89a07ba..c88922943 100644 --- a/classes/controller/AdminController.php +++ b/classes/controller/AdminController.php @@ -905,18 +905,25 @@ class AdminControllerCore extends Controller $sql_filter .= ' AND '.pSQL($key).' IN ('.pSQL(implode(',', $value)).')'; } } elseif ($type == 'range') { - if (isset($value[0]) && !empty($value[0])) { - if (!Validate::isUnsignedInt($value[0])) { + // set validation type + if (isset($field['validation']) && $field['validation'] && method_exists('Validate', $field['validation'])) { + $validation = $field['validation']; + } else { + $validation = 'isUnsignedInt'; + } + + if (isset($value[0]) && ($value[0] !== '' || $value[0] === 0)) { + if (!Validate::$validation($value[0])) { $this->errors[] = Tools::displayError('The \'From\' value is invalid'); } else { $sql_filter .= ' AND '.pSQL($key).' >= '.pSQL($value[0]); } } - if (isset($value[1]) && !empty($value[1])) { - if (!Validate::isUnsignedInt($value[1])) { - $this->errors[] = Tools::displayError('The \'From\' value is invalid'); - } elseif (isset($value[0]) && !empty($value[0]) && $value[0] > $value[1]) { - $this->errors[] = Tools::displayError('The \'To\' value cannot be less than from value'); + if (isset($value[1]) && ($value[1] !== '' || $value[1] === 0)) { + if (!Validate::$validation($value[1])) { + $this->errors[] = Tools::displayError('The \'To\' value is invalid'); + } elseif ((isset($value[0]) && ($value[0] !== '' || $value[0] === 0)) && $value[0] > $value[1]) { + $this->errors[] = Tools::displayError('The \'To\' value cannot be less than \'From\' value'); } else { $sql_filter .= ' AND '.pSQL($key).' <= '.pSQL($value[1]); } diff --git a/classes/helper/HelperList.php b/classes/helper/HelperList.php index 306d6c1c3..63223cc11 100644 --- a/classes/helper/HelperList.php +++ b/classes/helper/HelperList.php @@ -660,13 +660,24 @@ class HelperListCore extends Helper $value = json_decode($value, true); } - if (isset($value[0]) && !Validate::isUnsignedInt($value[0])) { + // set validation type + if (isset($params['validation']) && $params['validation'] && method_exists('Validate', $params['validation'])) { + $validation = $params['validation']; + } else { + $validation = 'isUnsignedInt'; + } + + $hasValueFrom = isset($value[0]) && ($value[0] !== '' || $value[0] === 0); + if ($hasValueFrom && !Validate::$validation($value[0])) { $value[0] = ''; } - if (isset($value[1]) && !Validate::isUnsignedInt($value[1])) { + + $hasValueTo = isset($value[1]) && ($value[1] !== '' || $value[1] === 0); + if ($hasValueTo && !Validate::$validation($value[1])) { $value[1] = ''; } - if (isset($value[0]) && isset($value[1]) && $value[0] > $value[1]) { + + if ($hasValueFrom && $hasValueTo && $value[0] > $value[1]) { $value[1] = ''; } break; diff --git a/controllers/admin/AdminProductsController.php b/controllers/admin/AdminProductsController.php index 1ec64c8a7..7eaac0217 100644 --- a/controllers/admin/AdminProductsController.php +++ b/controllers/admin/AdminProductsController.php @@ -88,7 +88,6 @@ class AdminProductsControllerCore extends AdminController parent::__construct(); $this->imageType = 'jpg'; - $this->_defaultOrderBy = 'position'; $this->max_file_size = (int)(Configuration::get('PS_LIMIT_UPLOAD_FILE_VALUE') * 1000000); $this->max_image_size = (int)Configuration::get('PS_PRODUCT_PICTURE_MAX_SIZE'); $this->allow_export = true; @@ -270,7 +269,7 @@ class AdminProductsControllerCore extends AdminController 'type' => 'range', 'align' => 'center', ); - $this->fields_list['child'] = array( + $this->fields_list['children'] = array( 'title' => $this->l('Children'), 'filter_key' => 'hrt!children', 'type' => 'range', @@ -286,8 +285,10 @@ class AdminProductsControllerCore extends AdminController $this->fields_list['price'] = array( 'title' => $this->l('Base Price'), 'type' => 'range', + 'validation' => 'isFloat', 'align' => 'text-left', - 'filter_key' => 'a!price' + 'filter_key' => 'a!price', + 'callback' => 'displayBasePrice', ); $this->fields_list['price_final'] = array( 'title' => $this->l('Final price'), @@ -420,6 +421,9 @@ class AdminProductsControllerCore extends AdminController && (int) $this->id_current_category && HotelBranchInformation::getHotelIdByIdCategory($this->id_current_category) ) { + $this->_orderBy = 'position'; + $this->_orderWay = 'ASC'; + $this->fields_list['position'] = array( 'title' => $this->l('Position'), 'filter_key' => 'cp!position', @@ -441,6 +445,11 @@ class AdminProductsControllerCore extends AdminController } } + public static function displayBasePrice($basePrice, $tr) + { + return Tools::displayPrice($basePrice, (int) Configuration::get('PS_CURRENCY_DEFAULT')); + } + public static function getQuantities($hotelName, $tr) { if ((int)$tr['is_virtual'] == 1 && $tr['nb_downloadable'] == 0) {