From 5dc48c829484fdd06ca09e6d896688fcf877d1be Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 11 Oct 2022 00:01:31 +0200 Subject: [PATCH] NEW Replace join on category (for filter on categ) with EXISTS/NOT --- htdocs/contact/list.php | 99 +++++++++++++++++++++++++++++++---------- htdocs/societe/list.php | 4 +- 2 files changed, 77 insertions(+), 26 deletions(-) diff --git a/htdocs/contact/list.php b/htdocs/contact/list.php index 572bf85da3f..8f4c59bb2f8 100644 --- a/htdocs/contact/list.php +++ b/htdocs/contact/list.php @@ -399,15 +399,6 @@ if (isset($extrafields->attributes[$object->table_element]['label']) && is_array $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON co.rowid = p.fk_pays"; $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = p.fk_soc"; $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_stcommcontact as st ON st.id = p.fk_stcommcontact"; -if (!empty($search_categ) && $search_categ != '-1') { - $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX."categorie_contact as cc ON p.rowid = cc.fk_socpeople"; // We need this table joined to the select in order to filter by categ -} -if (!empty($search_categ_thirdparty) && $search_categ_thirdparty != '-1') { - $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX."categorie_societe as cs ON s.rowid = cs.fk_soc"; // We need this table joined to the select in order to filter by categ -} -if (!empty($search_categ_supplier) && $search_categ_supplier != '-1') { - $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX."categorie_fournisseur as cs2 ON s.rowid = cs2.fk_soc"; // We need this table joined to the select in order to filter by categ -} if (empty($user->rights->societe->client->voir) && !$socid) { $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc"; } @@ -437,23 +428,83 @@ if ($search_priv != '0' && $search_priv != '1') { } } -if ($search_categ > 0) { - $sql .= " AND cc.fk_categorie = ".((int) $search_categ); +$searchCategoryContactList = $search_categ ? array($search_categ) : array(); +$searchCategoryContactOperator = 0; +// Search for tag/category ($searchCategoryContactList is an array of ID) +if (!empty($searchCategoryContactList)) { + $searchCategoryContactSqlList = array(); + $listofcategoryid = ''; + foreach ($searchCategoryContactList as $searchCategoryContact) { + if (intval($searchCategoryContact) == -2) { + $searchCategoryContactSqlList[] = "NOT EXISTS (SELECT ck.fk_socpeople FROM ".MAIN_DB_PREFIX."categorie_contact as ck WHERE s.rowid = ck.fk_socpeople)"; + } elseif (intval($searchCategoryContact) > 0) { + $listofcategoryid .= ($listofcategoryid ? ', ' : '') .((int) $searchCategoryContact); + } + } + if ($listofcategoryid) { + $searchCategoryContactSqlList[] = " EXISTS (SELECT ck.fk_socpeople FROM ".MAIN_DB_PREFIX."categorie_contact as ck WHERE s.rowid = ck.fk_socpeople AND ck.fk_categorie IN (".$db->sanitize($listofcategoryid)."))"; + } + if ($searchCategoryContactOperator == 1) { + if (!empty($searchCategoryContactSqlList)) { + $sql .= " AND (".implode(' OR ', $searchCategoryContactSqlList).")"; + } + } else { + if (!empty($searchCategoryContactSqlList)) { + $sql .= " AND (".implode(' AND ', $searchCategoryContactSqlList).")"; + } + } } -if ($search_categ == -2) { - $sql .= " AND cc.fk_categorie IS NULL"; +$searchCategoryCustomerList = $search_categ_thirdparty ? array($search_categ_thirdparty) : array(); +$searchCategoryCustomerOperator = 0; +// Search for tag/category ($searchCategoryCustomerList is an array of ID) +if (!empty($searchCategoryCustomerList)) { + $searchCategoryCustomerSqlList = array(); + $listofcategoryid = ''; + foreach ($searchCategoryCustomerList as $searchCategoryCustomer) { + if (intval($searchCategoryCustomer) == -2) { + $searchCategoryCustomerSqlList[] = "NOT EXISTS (SELECT ck.fk_soc FROM ".MAIN_DB_PREFIX."categorie_societe as ck WHERE s.rowid = ck.fk_soc)"; + } elseif (intval($searchCategoryCustomer) > 0) { + $listofcategoryid .= ($listofcategoryid ? ', ' : '') .((int) $searchCategoryCustomer); + } + } + if ($listofcategoryid) { + $searchCategoryCustomerSqlList[] = " EXISTS (SELECT ck.fk_soc FROM ".MAIN_DB_PREFIX."categorie_societe as ck WHERE s.rowid = ck.fk_soc AND ck.fk_categorie IN (".$db->sanitize($listofcategoryid)."))"; + } + if ($searchCategoryCustomerOperator == 1) { + if (!empty($searchCategoryCustomerSqlList)) { + $sql .= " AND (".implode(' OR ', $searchCategoryCustomerSqlList).")"; + } + } else { + if (!empty($searchCategoryCustomerSqlList)) { + $sql .= " AND (".implode(' AND ', $searchCategoryCustomerSqlList).")"; + } + } } -if ($search_categ_thirdparty > 0) { - $sql .= " AND cs.fk_categorie = ".((int) $search_categ_thirdparty); -} -if ($search_categ_thirdparty == -2) { - $sql .= " AND cs.fk_categorie IS NULL"; -} -if ($search_categ_supplier > 0) { - $sql .= " AND cs2.fk_categorie = ".((int) $search_categ_supplier); -} -if ($search_categ_supplier == -2) { - $sql .= " AND cs2.fk_categorie IS NULL"; +$searchCategorySupplierList = $search_categ_supplier ? array($search_categ_supplier) : array(); +$searchCategorySupplierOperator = 0; +// Search for tag/category ($searchCategorySupplierList is an array of ID) +if (!empty($searchCategorySupplierList)) { + $searchCategorySupplierSqlList = array(); + $listofcategoryid = ''; + foreach ($searchCategorySupplierList as $searchCategorySupplier) { + if (intval($searchCategorySupplier) == -2) { + $searchCategorySupplierSqlList[] = "NOT EXISTS (SELECT ck.fk_soc FROM ".MAIN_DB_PREFIX."categorie_fournisseur as ck WHERE s.rowid = ck.fk_soc)"; + } elseif (intval($searchCategorySupplier) > 0) { + $listofcategoryid .= ($listofcategoryid ? ', ' : '') .((int) $searchCategorySupplier); + } + } + if ($listofcategoryid) { + $searchCategorySupplierSqlList[] = " EXISTS (SELECT ck.fk_soc FROM ".MAIN_DB_PREFIX."categorie_fournisseur as ck WHERE s.rowid = ck.fk_soc AND ck.fk_categorie IN (".$db->sanitize($listofcategoryid)."))"; + } + if ($searchCategorySupplierOperator == 1) { + if (!empty($searchCategorySupplierSqlList)) { + $sql .= " AND (".implode(' OR ', $searchCategorySupplierSqlList).")"; + } + } else { + if (!empty($searchCategorySupplierSqlList)) { + $sql .= " AND (".implode(' AND ', $searchCategorySupplierSqlList).")"; + } + } } if ($sall) { diff --git a/htdocs/societe/list.php b/htdocs/societe/list.php index 92db68c7744..f32317163ad 100644 --- a/htdocs/societe/list.php +++ b/htdocs/societe/list.php @@ -526,7 +526,7 @@ if ($search_sale == -2) { } elseif ($search_sale > 0) { $sql .= " AND sc.fk_user = ".((int) $search_sale); } -$searchCategoryCustomerList = array($search_categ_cus); +$searchCategoryCustomerList = $search_categ_cus ? array($search_categ_cus) : array();; $searchCategoryCustomerOperator = 0; // Search for tag/category ($searchCategoryCustomerList is an array of ID) if (!empty($searchCategoryCustomerList)) { @@ -552,7 +552,7 @@ if (!empty($searchCategoryCustomerList)) { } } } -$searchCategorySupplierList = array($search_categ_sup); +$searchCategorySupplierList = $search_categ_sup ? array($search_categ_sup) : array(); $searchCategorySupplierOperator = 0; // Search for tag/category ($searchCategorySupplierList is an array of ID) if (!empty($searchCategorySupplierList)) {