From f17d09743ff2e9d5d88492671f091f56e49b515e Mon Sep 17 00:00:00 2001 From: lvessiller Date: Tue, 12 Apr 2022 16:40:59 +0200 Subject: [PATCH 1/4] NEW product categories filter on inventory list --- htdocs/product/inventory/list.php | 69 +++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/htdocs/product/inventory/list.php b/htdocs/product/inventory/list.php index c4548b3aa66..a2b9fdfa194 100644 --- a/htdocs/product/inventory/list.php +++ b/htdocs/product/inventory/list.php @@ -27,6 +27,9 @@ require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php'; require_once DOL_DOCUMENT_ROOT.'/product/inventory/class/inventory.class.php'; +if (!empty($conf->categorie->enabled)) { + require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; +} // Load translation files required by the page $langs->loadLangs(array("stocks", "other")); @@ -88,6 +91,13 @@ foreach ($object->fields as $key => $val) { $search[$key.'_dtend'] = dol_mktime(23, 59, 59, GETPOST('search_'.$key.'_dtendmonth', 'int'), GETPOST('search_'.$key.'_dtendday', 'int'), GETPOST('search_'.$key.'_dtendyear', 'int')); } } +$searchCategoryProductOperator = 0; +if (GETPOSTISSET('formfilteraction')) { + $searchCategoryProductOperator = GETPOST('search_category_product_operator', 'int'); +} elseif (!empty($conf->global->MAIN_SEARCH_CAT_OR_BY_DEFAULT)) { + $searchCategoryProductOperator = $conf->global->MAIN_SEARCH_CAT_OR_BY_DEFAULT; +} +$searchCategoryProductList = GETPOST('search_category_product_list', 'array'); // List of fields to search into when doing a "search in all" $fieldstosearchall = array(); @@ -166,6 +176,7 @@ if (empty($reshook)) { $search[$key.'_dtend'] = ''; } } + $searchCategoryProductList = array(); $toselect = array(); $search_array_options = array(); } @@ -259,6 +270,50 @@ foreach ($search as $key => $val) { if ($search_all) { $sql .= natural_search(array_keys($fieldstosearchall), $search_all); } +$searchCategoryProductSqlList = array(); +if ($searchCategoryProductOperator == 1) { + $existsCategoryProductList = array(); + foreach ($searchCategoryProductList as $searchCategoryProduct) { + if (intval($searchCategoryProduct) == -2) { + $sqlCategoryProductNotExists = " NOT EXISTS ("; + $sqlCategoryProductNotExists .= " SELECT cp.fk_product"; + $sqlCategoryProductNotExists .= " FROM ".$db->prefix()."categorie_product AS cp"; + $sqlCategoryProductNotExists .= " WHERE cp.fk_product = t.fk_product"; + $sqlCategoryProductNotExists .= " )"; + $searchCategoryProductSqlList[] = $sqlCategoryProductNotExists; + } elseif (intval($searchCategoryProduct) > 0) { + $existsCategoryProductList[] = $db->escape($searchCategoryProduct); + } + } + if (!empty($existsCategoryProductList)) { + $sqlCategoryProductExists = " EXISTS ("; + $sqlCategoryProductExists .= " SELECT cp.fk_product"; + $sqlCategoryProductExists .= " FROM ".$db->prefix()."categorie_product AS cp"; + $sqlCategoryProductExists .= " WHERE cp.fk_product = t.fk_product"; + $sqlCategoryProductExists .= " AND cp.fk_categorie IN (".implode(",", $existsCategoryProductList).")"; + $sqlCategoryProductExists .= " )"; + $searchCategoryProductSqlList[] = $sqlCategoryProductExists; + } + if (!empty($searchCategoryProductSqlList)) { + $sql .= " AND (".implode(' OR ', $searchCategoryProductSqlList).")"; + } +} else { + foreach ($searchCategoryProductList as $searchCategoryProduct) { + if (intval($searchCategoryProduct) == -2) { + $sqlCategoryProductNotExists = " NOT EXISTS ("; + $sqlCategoryProductNotExists .= " SELECT cp.fk_product"; + $sqlCategoryProductNotExists .= " FROM ".$db->prefix()."categorie_product AS cp"; + $sqlCategoryProductNotExists .= " WHERE cp.fk_product = t.fk_product"; + $sqlCategoryProductNotExists .= " )"; + $searchCategoryProductSqlList[] = $sqlCategoryProductNotExists; + } elseif (intval($searchCategoryProduct) > 0) { + $searchCategoryProductSqlList[] = "t.fk_product IN (SELECT fk_product FROM ".$db->prefix()."categorie_product WHERE fk_categorie = ".((int) $searchCategoryProduct).")"; + } + } + if (!empty($searchCategoryProductSqlList)) { + $sql .= " AND (".implode(' AND ', $searchCategoryProductSqlList).")"; + } +} //$sql.= dolSqlDateFilter("t.field", $search_xxxday, $search_xxxmonth, $search_xxxyear); // Add where from extra fields include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_sql.tpl.php'; @@ -348,6 +403,9 @@ foreach ($search as $key => $val) { if ($optioncss != '') { $param .= '&optioncss='.urlencode($optioncss); } +foreach ($searchCategoryProductList as $searchCategoryProduct) { + $param .= "&search_category_product_list[]=".urlencode($searchCategoryProduct); +} // Add $param from extra fields include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_param.tpl.php'; // Add $param from hooks @@ -405,6 +463,17 @@ $moreforfilter = ''; $moreforfilter.= $langs->trans('MyFilter') . ': '; $moreforfilter.= '';*/ +// Filter on categories +if (!empty($conf->categorie->enabled) && $user->rights->categorie->lire) { + $moreforfilter .= '
'; + $moreforfilter .= img_picto($langs->trans('Categories'), 'category', 'class="pictofixedwidth"'); + $categoriesProductArr = $form->select_all_categories(Categorie::TYPE_PRODUCT, '', '', 64, 0, 1); + $categoriesProductArr[-2] = '- '.$langs->trans('NotCategorized').' -'; + $moreforfilter .= Form::multiselectarray('search_category_product_list', $categoriesProductArr, $searchCategoryProductList, 0, 0, 'minwidth300'); + $moreforfilter .= ' '; + $moreforfilter .= '
'; +} + $parameters = array(); $reshook = $hookmanager->executeHooks('printFieldPreListTitle', $parameters, $object); // Note that $action and $object may have been modified by hook if (empty($reshook)) { From 4510af67478628bd7b36ac69a5a91ac785944596 Mon Sep 17 00:00:00 2001 From: lvessiller Date: Tue, 12 Apr 2022 16:59:46 +0200 Subject: [PATCH 2/4] FIX travis SQL syntax --- htdocs/product/inventory/list.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/product/inventory/list.php b/htdocs/product/inventory/list.php index a2b9fdfa194..7e795d821bf 100644 --- a/htdocs/product/inventory/list.php +++ b/htdocs/product/inventory/list.php @@ -290,7 +290,7 @@ if ($searchCategoryProductOperator == 1) { $sqlCategoryProductExists .= " SELECT cp.fk_product"; $sqlCategoryProductExists .= " FROM ".$db->prefix()."categorie_product AS cp"; $sqlCategoryProductExists .= " WHERE cp.fk_product = t.fk_product"; - $sqlCategoryProductExists .= " AND cp.fk_categorie IN (".implode(",", $existsCategoryProductList).")"; + $sqlCategoryProductExists .= " AND cp.fk_categorie IN (".$db->sanitize(implode(',', $existsCategoryProductList)).")"; $sqlCategoryProductExists .= " )"; $searchCategoryProductSqlList[] = $sqlCategoryProductExists; } From 1435e2666e8d698b9b7f1cbabc75306e2b62bea8 Mon Sep 17 00:00:00 2001 From: lvessiller Date: Tue, 12 Apr 2022 17:18:39 +0200 Subject: [PATCH 3/4] FIX reload travis From 512aff8fa471b1fdab6f9c5cd0911d4892b162af Mon Sep 17 00:00:00 2001 From: lvessiller Date: Mon, 23 May 2022 12:17:02 +0200 Subject: [PATCH 4/4] NEW MAIN_SEARCH_CATEGORY_PRODUCT_ON_LISTS const to show category customer filter --- htdocs/product/inventory/list.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/htdocs/product/inventory/list.php b/htdocs/product/inventory/list.php index 7e795d821bf..553f4ed8baa 100644 --- a/htdocs/product/inventory/list.php +++ b/htdocs/product/inventory/list.php @@ -464,13 +464,15 @@ $moreforfilter.= $langs->trans('MyFilter') . ': '; - $moreforfilter .= img_picto($langs->trans('Categories'), 'category', 'class="pictofixedwidth"'); + $tmptitle = $langs->transnoentities('ProductsCategoriesShort'); + $moreforfilter .= img_picto($tmptitle, 'category', 'class="pictofixedwidth"'); $categoriesProductArr = $form->select_all_categories(Categorie::TYPE_PRODUCT, '', '', 64, 0, 1); $categoriesProductArr[-2] = '- '.$langs->trans('NotCategorized').' -'; - $moreforfilter .= Form::multiselectarray('search_category_product_list', $categoriesProductArr, $searchCategoryProductList, 0, 0, 'minwidth300'); - $moreforfilter .= ' '; + $moreforfilter .= Form::multiselectarray('search_category_product_list', $categoriesProductArr, $searchCategoryProductList, 0, 0, 'minwidth300', 0, 0, '', 'category', $tmptitle); + $moreforfilter .= ' '; + $moreforfilter .= $form->textwithpicto('', $langs->trans('UseOrOperatorForCategories') . ' : ' . $tmptitle, 1, 'help', '', 0, 2, 'tooltip_cat_pro'); // Tooltip on click $moreforfilter .= ''; }