From 92d0dff65da2a8bd0df293e0d9cf301c5a75a0a1 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 11 Oct 2022 02:26:12 +0200 Subject: [PATCH] Clean code --- htdocs/categories/class/categorie.class.php | 2 + htdocs/compta/bank/list.php | 41 +++++++++++++------- htdocs/product/list.php | 16 +++----- htdocs/product/stock/list.php | 35 +++++++++++++---- htdocs/projet/list.php | 37 +++++++++++++----- htdocs/user/list.php | 43 ++++++++++++++------- 6 files changed, 117 insertions(+), 57 deletions(-) diff --git a/htdocs/categories/class/categorie.class.php b/htdocs/categories/class/categorie.class.php index acdf2e71a32..d9a7eb4b1c7 100644 --- a/htdocs/categories/class/categorie.class.php +++ b/htdocs/categories/class/categorie.class.php @@ -1981,6 +1981,7 @@ class Categorie extends CommonObject * @param string $type The category type (e.g Categorie::TYPE_WAREHOUSE) * @param string $rowIdName The name of the row id inside the whole sql query (e.g. "e.rowid") * @return string A additional SQL JOIN query + * @deprecated search on some categories must be done using a WHERE EXISTS or NOT EXISTS and not a LEFT JOIN. @TODO Replace with getWhereQuery($type, $searchCategoryList) */ public static function getFilterJoinQuery($type, $rowIdName) { @@ -1998,6 +1999,7 @@ class Categorie extends CommonObject * @param string $rowIdName The name of the row id inside the whole sql query (e.g. "e.rowid") * @param Array $searchList A list with the selected categories * @return string A additional SQL SELECT query + * @deprecated search on some categories must be done using a WHERE EXISTS or NOT EXISTS and not a LEFT JOIN */ public static function getFilterSelectQuery($type, $rowIdName, $searchList) { diff --git a/htdocs/compta/bank/list.php b/htdocs/compta/bank/list.php index 5f223450ab7..bccad47e2d8 100644 --- a/htdocs/compta/bank/list.php +++ b/htdocs/compta/bank/list.php @@ -59,9 +59,7 @@ $search_number = GETPOST('search_number', 'alpha'); $search_status = GETPOST('search_status') ?GETPOST('search_status', 'alpha') : 'opened'; // 'all' or ''='opened' $optioncss = GETPOST('optioncss', 'alpha'); -if (isModEnabled('categorie')) { - $search_category_list = GETPOST("search_category_".Categorie::TYPE_ACCOUNT."_list", "array"); -} +$search_category_list = GETPOST("search_category_".Categorie::TYPE_ACCOUNT."_list", "array"); $socid = 0; // Security check @@ -162,6 +160,7 @@ if (empty($reshook)) { $search_label = ''; $search_number = ''; $search_status = ''; + $search_category_list = array(); } // Mass actions @@ -197,11 +196,6 @@ $sql .= " FROM ".MAIN_DB_PREFIX."bank_account as b"; if (!empty($extrafields->attributes[$object->table_element]['label']) && is_array($extrafields->attributes[$object->table_element]['label']) && count($extrafields->attributes[$object->table_element]['label'])) { $sql .= " LEFT JOIN ".MAIN_DB_PREFIX.$object->table_element."_extrafields as ef on (b.rowid = ef.fk_object)"; } - -if (isModEnabled('categorie')) { - $sql .= Categorie::getFilterJoinQuery(Categorie::TYPE_ACCOUNT, "b.rowid"); -} - $sql .= " WHERE b.entity IN (".getEntity('bank_account').")"; if ($search_status == 'opened') { $sql .= " AND clos = 0"; @@ -209,11 +203,6 @@ if ($search_status == 'opened') { if ($search_status == 'closed') { $sql .= " AND clos = 1"; } - -if (isModEnabled('categorie')) { - $sql .= Categorie::getFilterSelectQuery(Categorie::TYPE_ACCOUNT, "b.rowid", $search_category_list); -} - if ($search_ref != '') { $sql .= natural_search('b.ref', $search_ref); } @@ -223,6 +212,32 @@ if ($search_label != '') { if ($search_number != '') { $sql .= natural_search('b.number', $search_number); } +// Search for tag/category ($searchCategoryBankList is an array of ID) +$searchCategoryBankList = $search_category_list; +$searchCategoryBankOperator = 0; +if (!empty($searchCategoryBankList)) { + $searchCategoryBankSqlList = array(); + $listofcategoryid = ''; + foreach ($searchCategoryBankList as $searchCategoryBank) { + if (intval($searchCategoryBank) == -2) { + $searchCategoryBankSqlList[] = "NOT EXISTS (SELECT ck.fk_account FROM ".MAIN_DB_PREFIX."categorie_account as ck WHERE b.rowid = ck.fk_account)"; + } elseif (intval($searchCategoryBank) > 0) { + $listofcategoryid .= ($listofcategoryid ? ', ' : '') .((int) $searchCategoryBank); + } + } + if ($listofcategoryid) { + $searchCategoryBankSqlList[] = " EXISTS (SELECT ck.fk_account FROM ".MAIN_DB_PREFIX."categorie_account as ck WHERE b.rowid = ck.fk_account AND ck.fk_categorie IN (".$db->sanitize($listofcategoryid)."))"; + } + if ($searchCategoryBankOperator == 1) { + if (!empty($searchCategoryBankSqlList)) { + $sql .= " AND (".implode(' OR ', $searchCategoryBankSqlList).")"; + } + } else { + if (!empty($searchCategoryBankSqlList)) { + $sql .= " AND (".implode(' AND ', $searchCategoryBankSqlList).")"; + } + } +} // Add where from extra fields include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_sql.tpl.php'; // Add where from hooks diff --git a/htdocs/product/list.php b/htdocs/product/list.php index 6121004fd37..26f6a3a410c 100644 --- a/htdocs/product/list.php +++ b/htdocs/product/list.php @@ -77,12 +77,15 @@ if (GETPOSTISSET('formfilteraction')) { $searchCategoryProductOperator = $conf->global->MAIN_SEARCH_CAT_OR_BY_DEFAULT; } $searchCategoryProductList = GETPOST('search_category_product_list', 'array'); +$catid = GETPOST('catid', 'int'); +if (!empty($catid) && empty($searchCategoryProductList)) { + $searchCategoryProductList = array($catid); +} $search_tosell = GETPOST("search_tosell", 'int'); $search_tobuy = GETPOST("search_tobuy", 'int'); $search_country = GETPOST("search_country", 'int'); $search_state = GETPOST("state_id", 'int'); $fourn_id = GETPOST("fourn_id", 'int'); -$catid = GETPOST('catid', 'int'); $search_tobatch = GETPOST("search_tobatch", 'int'); $search_accountancy_code_sell = GETPOST("search_accountancy_code_sell", 'alpha'); $search_accountancy_code_sell_intra = GETPOST("search_accountancy_code_sell_intra", 'alpha'); @@ -296,6 +299,7 @@ if ($search_type == '0') { $result = restrictedArea($user, 'produit|service', '', '', '', '', '', 0); } + /* * Actions */ @@ -452,7 +456,6 @@ if (!empty($conf->global->PRODUCT_USE_UNITS)) { $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_units cu ON cu.rowid = p.fk_unit"; } - $sql .= ' WHERE p.entity IN ('.getEntity('product').')'; if ($sall) { $sql .= natural_search(array_keys($fieldstosearchall), $sall); @@ -497,13 +500,6 @@ if ($search_vatrate) { if (dol_strlen($canvas) > 0) { $sql .= " AND p.canvas = '".$db->escape($canvas)."'"; } -if ($catid > 0) { - $sql .= " AND cp.fk_categorie = ".((int) $catid); -} -if ($catid == -2) { - $sql .= " AND cp.fk_categorie IS NULL"; -} - // Search for tag/category ($searchCategoryProductList is an array of ID) if (!empty($searchCategoryProductList)) { $searchCategoryProductSqlList = array(); @@ -528,7 +524,6 @@ if (!empty($searchCategoryProductList)) { } } } - if ($fourn_id > 0) { $sql .= " AND pfp.fk_soc = ".((int) $fourn_id); } @@ -692,7 +687,6 @@ if ($resql) { if ($fourn_id > 0) { $param .= "&fourn_id=".urlencode($fourn_id); } - //if ($seach_categ) $param.=($search_categ?"&search_categ=".urlencode($search_categ):""); if ($show_childproducts) { $param .= ($show_childproducts ? "&search_show_childproducts=".urlencode($show_childproducts) : ""); } diff --git a/htdocs/product/stock/list.php b/htdocs/product/stock/list.php index 496a2720e97..af2bb49b05e 100644 --- a/htdocs/product/stock/list.php +++ b/htdocs/product/stock/list.php @@ -227,9 +227,6 @@ $reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters, $obje $sql .= $hookmanager->resPrint; $sql = preg_replace('/,\s*$/', '', $sql); $sql .= " FROM ".MAIN_DB_PREFIX.$object->table_element." as t"; -if (isModEnabled('categorie')) { - $sql .= Categorie::getFilterJoinQuery(Categorie::TYPE_WAREHOUSE, "t.rowid"); -} if (!empty($extrafields->attributes[$object->table_element]['label']) && is_array($extrafields->attributes[$object->table_element]['label']) && count($extrafields->attributes[$object->table_element]['label'])) { $sql .= " LEFT JOIN ".MAIN_DB_PREFIX.$object->table_element."_extrafields as ef on (t.rowid = ef.fk_object)"; } @@ -240,12 +237,7 @@ $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as ccount ON ccount.rowid = t.fk if ($separatedPMP) { $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_perentity as pa ON pa.fk_product = p.rowid AND pa.fk_product = ps.fk_product AND pa.entity = ". (int) $conf->entity; } - $sql .= " WHERE t.entity IN (".getEntity('stock').")"; - -if (isModEnabled('categorie')) { - $sql .= Categorie::getFilterSelectQuery(Categorie::TYPE_WAREHOUSE, "t.rowid", $search_category_list); -} foreach ($search as $key => $val) { $class_key = $key; if ($class_key == 'status') { @@ -268,6 +260,33 @@ foreach ($search as $key => $val) { if ($search_all) { $sql .= natural_search(array_keys($fieldstosearchall), $search_all); } +// Search for tag/category ($searchCategoryWarehouseList is an array of ID) +$searchCategoryWarehouseList = $search_category_list; +$searchCategoryWarehouseOperator = 0; +if (!empty($searchCategoryWarehouseList)) { + $searchCategoryWarehouseSqlList = array(); + $listofcategoryid = ''; + foreach ($searchCategoryWarehouseList as $searchCategoryWarehouse) { + if (intval($searchCategoryWarehouse) == -2) { + $searchCategoryWarehouseSqlList[] = "NOT EXISTS (SELECT ck.fk_warehouse FROM ".MAIN_DB_PREFIX."categorie_warehouse as ck WHERE p.rowid = ck.fk_warehouse)"; + } elseif (intval($searchCategoryWarehouse) > 0) { + $listofcategoryid .= ($listofcategoryid ? ', ' : '') .((int) $searchCategoryWarehouse); + } + } + if ($listofcategoryid) { + $searchCategoryWarehouseSqlList[] = " EXISTS (SELECT ck.fk_warehouse FROM ".MAIN_DB_PREFIX."categorie_warehouse as ck WHERE p.rowid = ck.fk_warehouse AND ck.fk_categorie IN (".$db->sanitize($listofcategoryid)."))"; + } + if ($searchCategoryWarehouseOperator == 1) { + if (!empty($searchCategoryWarehouseSqlList)) { + $sql .= " AND (".implode(' OR ', $searchCategoryWarehouseSqlList).")"; + } + } else { + if (!empty($searchCategoryWarehouseSqlList)) { + $sql .= " AND (".implode(' AND ', $searchCategoryWarehouseSqlList).")"; + } + } +} + // Add where from extra fields include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_sql.tpl.php'; // Add where from hooks diff --git a/htdocs/projet/list.php b/htdocs/projet/list.php index 29078c054a5..9ca5fc63c76 100644 --- a/htdocs/projet/list.php +++ b/htdocs/projet/list.php @@ -143,15 +143,12 @@ $search_date_end_endmonth = GETPOST('search_date_end_endmonth', 'int'); $search_date_end_endyear = GETPOST('search_date_end_endyear', 'int'); $search_date_end_endday = GETPOST('search_date_end_endday', 'int'); $search_date_end_end = dol_mktime(23, 59, 59, $search_date_end_endmonth, $search_date_end_endday, $search_date_end_endyear); // Use tzserver - +$search_category_array = GETPOST("search_category_".Categorie::TYPE_PROJECT."_list", "array"); if ($search_status == '') { $search_status = -1; // -1 or 1 } -if (isModEnabled('categorie')) { - $search_category_array = GETPOST("search_category_".Categorie::TYPE_PROJECT."_list", "array"); -} // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context $object = new Project($db); @@ -424,9 +421,6 @@ $reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters, $obje $sql .= preg_replace('/^,/', '', $hookmanager->resPrint); $sql = preg_replace('/,\s*$/', '', $sql); $sql .= " FROM ".MAIN_DB_PREFIX.$object->table_element." as p"; -if (isModEnabled('categorie')) { - $sql .= Categorie::getFilterJoinQuery(Categorie::TYPE_PROJECT, "p.rowid"); -} if (!empty($extrafields->attributes[$object->table_element]['label']) &&is_array($extrafields->attributes[$object->table_element]['label']) && count($extrafields->attributes[$object->table_element]['label'])) { $sql .= " LEFT JOIN ".MAIN_DB_PREFIX.$object->table_element."_extrafields as ef on (p.rowid = ef.fk_object)"; } @@ -447,9 +441,6 @@ if ($search_project_contact > 0) { $sql .= ", ".MAIN_DB_PREFIX."element_contact as ecp_contact"; } $sql .= " WHERE p.entity IN (".getEntity('project').')'; -if (isModEnabled('categorie')) { - $sql .= Categorie::getFilterSelectQuery(Categorie::TYPE_PROJECT, "p.rowid", $search_category_array); -} if (empty($user->rights->projet->all->lire)) { $sql .= " AND p.rowid IN (".$db->sanitize($projectsListId).")"; // public and assigned to, or restricted to company for external users } @@ -569,6 +560,32 @@ if ($search_price_booth != '') { if ($search_login) { $sql .= natural_search(array('u.login', 'u.firstname', 'u.lastname'), $search_login); } +// Search for tag/category ($searchCategoryProjectList is an array of ID) +$searchCategoryProjectList = $search_category_array; +$searchCategoryProjectOperator = 0; +if (!empty($searchCategoryProjectList)) { + $searchCategoryProjectSqlList = array(); + $listofcategoryid = ''; + foreach ($searchCategoryProjectList as $searchCategoryProject) { + if (intval($searchCategoryProject) == -2) { + $searchCategoryProjectSqlList[] = "NOT EXISTS (SELECT ck.fk_project FROM ".MAIN_DB_PREFIX."categorie_project as ck WHERE p.rowid = ck.fk_project)"; + } elseif (intval($searchCategoryProject) > 0) { + $listofcategoryid .= ($listofcategoryid ? ', ' : '') .((int) $searchCategoryProject); + } + } + if ($listofcategoryid) { + $searchCategoryProjectSqlList[] = " EXISTS (SELECT ck.fk_project FROM ".MAIN_DB_PREFIX."categorie_project as ck WHERE p.rowid = ck.fk_project AND ck.fk_categorie IN (".$db->sanitize($listofcategoryid)."))"; + } + if ($searchCategoryProjectOperator == 1) { + if (!empty($searchCategoryProjectSqlList)) { + $sql .= " AND (".implode(' OR ', $searchCategoryProjectSqlList).")"; + } + } else { + if (!empty($searchCategoryProjectSqlList)) { + $sql .= " AND (".implode(' AND ', $searchCategoryProjectSqlList).")"; + } + } +} // Add where from extra fields include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_sql.tpl.php'; // Add where from hooks diff --git a/htdocs/user/list.php b/htdocs/user/list.php index dfc8fdb3568..72b00c75577 100644 --- a/htdocs/user/list.php +++ b/htdocs/user/list.php @@ -163,9 +163,11 @@ $search_statut = GETPOST('search_statut', 'intcomma'); $search_thirdparty = GETPOST('search_thirdparty', 'alpha'); $search_warehouse = GETPOST('search_warehouse', 'alpha'); $search_supervisor = GETPOST('search_supervisor', 'intcomma'); -$optioncss = GETPOST('optioncss', 'alpha'); $search_categ = GETPOST("search_categ", 'int'); $catid = GETPOST('catid', 'int'); +if (!empty($catid) && empty($search_categ)) { + $search_categ = $catid; +} // Default search if ($search_statut == '') { @@ -368,9 +370,6 @@ if (isset($extrafields->attributes[$object->table_element]['label']) && is_array } $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON u.fk_soc = s.rowid"; $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u2 ON u.fk_user = u2.rowid"; -if (!empty($search_categ) || !empty($catid)) { - $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX."categorie_user as cu ON u.rowid = cu.fk_user"; // We'll need this table joined to the select in order to filter by categ -} // Add table from hooks $parameters = array(); $reshook = $hookmanager->executeHooks('printFieldListFrom', $parameters, $object); // Note that $action and $object may have been modified by hook @@ -428,17 +427,31 @@ if ($search_statut != '' && $search_statut >= 0) { if ($sall) { $sql .= natural_search(array_keys($fieldstosearchall), $sall); } -if ($catid > 0) { - $sql .= " AND cu.fk_categorie = ".((int) $catid); -} -if ($catid == -2) { - $sql .= " AND cu.fk_categorie IS NULL"; -} -if ($search_categ > 0) { - $sql .= " AND cu.fk_categorie = ".((int) $search_categ); -} -if ($search_categ == -2) { - $sql .= " AND cu.fk_categorie IS NULL"; + +// Search for tag/category ($searchCategoryProductList is an array of ID) +$searchCategoryProductList = array($search_categ); +if (!empty($searchCategoryProductList)) { + $searchCategoryProductSqlList = array(); + $listofcategoryid = ''; + foreach ($searchCategoryProductList as $searchCategoryProduct) { + if (intval($searchCategoryProduct) == -2) { + $searchCategoryProductSqlList[] = "NOT EXISTS (SELECT ck.fk_user FROM ".MAIN_DB_PREFIX."categorie_user as ck WHERE u.rowid = ck.fk_user)"; + } elseif (intval($searchCategoryProduct) > 0) { + $listofcategoryid .= ($listofcategoryid ? ', ' : '') .((int) $searchCategoryProduct); + } + } + if ($listofcategoryid) { + $searchCategoryProductSqlList[] = " EXISTS (SELECT ck.fk_user FROM ".MAIN_DB_PREFIX."categorie_user as ck WHERE u.rowid = ck.fk_user AND ck.fk_categorie IN (".$db->sanitize($listofcategoryid)."))"; + } + if ($searchCategoryProductOperator == 1) { + if (!empty($searchCategoryProductSqlList)) { + $sql .= " AND (".implode(' OR ', $searchCategoryProductSqlList).")"; + } + } else { + if (!empty($searchCategoryProductSqlList)) { + $sql .= " AND (".implode(' AND ', $searchCategoryProductSqlList).")"; + } + } } if ($search_warehouse > 0) { $sql .= " AND u.fk_warehouse = ".((int) $search_warehouse);