From c0681770276d5a12b383766ae000f483d746428a Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 2 Sep 2019 16:39:56 +0200 Subject: [PATCH] Work on search on global site files --- htdocs/core/lib/website.lib.php | 78 ++++++++++++++++++++++++++------- htdocs/langs/en_US/website.lang | 1 + htdocs/website/index.php | 2 + 3 files changed, 65 insertions(+), 16 deletions(-) diff --git a/htdocs/core/lib/website.lib.php b/htdocs/core/lib/website.lib.php index c1f3c6f9809..af0114c9c97 100644 --- a/htdocs/core/lib/website.lib.php +++ b/htdocs/core/lib/website.lib.php @@ -636,10 +636,11 @@ function getStructuredData($type, $data = array()) } /** - * Return list of containers object that match a criteria + * Return list of containers object that match a criteria. + * WARNING: This function can be used by websites. * * @param string $type Type of container to search into (Example: 'page') - * @param string $algo Algorithm used for search (Example: 'meta' is searching into meta information like title and description, 'metacontent') + * @param string $algo Algorithm used for search (Example: 'meta' is searching into meta information like title and description, 'content', 'sitefiles', or any combination, ...) * @param string $searchstring Search string * @param int $max Max number of answers * @return string HTML content @@ -674,20 +675,21 @@ function getPagesFromSearchCriterias($type, $algo, $searchstring, $max = 25) } $searchdone = 0; + $found = 0; - if (! $error && in_array($algo, array('meta', 'metacontent', 'content'))) + if (! $error && (empty($max) || ($found < $max)) && (preg_match('/meta/', $algo) || preg_match('/content/', $algo))) { $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'website_page'; $sql.= " WHERE fk_website = ".$website->id; if ($type) $sql.= " AND type_container = '".$db->escape($type)."'"; $sql.= " AND ("; $searchalgo = ''; - if ($algo == 'meta' || $algo == 'metacontent') + if (preg_match('/meta/', $algo)) { $searchalgo.= ($searchalgo?' OR ':'')."title LIKE '%".$db->escape($searchstring)."%' OR description LIKE '%".$db->escape($searchstring)."%'"; $searchalgo.= ($searchalgo?' OR ':'')."keywords LIKE '".$db->escape($searchstring).",%' OR keywords LIKE '% ".$db->escape($searchstring)."%'"; // TODO Use a better way to scan keywords } - if ($algo == 'metacontent' || $algo == 'content') + if (preg_match('/content/', $algo)) { $searchalgo.= ($searchalgo?' OR ':'')."content LIKE '%".$db->escape($searchstring)."%'"; } @@ -706,16 +708,10 @@ function getPagesFromSearchCriterias($type, $algo, $searchstring, $max = 25) $tmpwebsitepage = new WebsitePage($db); $tmpwebsitepage->fetch($obj->rowid); if ($tmpwebsitepage->id > 0) $arrayresult['list'][]=$tmpwebsitepage; + $found++; } $i++; } - - $arrayresult['code']='OK'; - if (empty($arrayresult['list'])) - { - $arrayresult['code']='KO'; - $arrayresult['message']=$weblangs->trans("NoRecordFound"); - } } else { @@ -727,11 +723,61 @@ function getPagesFromSearchCriterias($type, $algo, $searchstring, $max = 25) $searchdone = 1; } - if (! $searchdone) + if (! $error && (empty($max) || ($found < $max)) && (preg_match('/sitefiles/', $algo))) { - $error++; - $arrayresult['code']='KO'; - $arrayresult['message']='No supported algorithm found'; + $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'website'; + $sql.= " WHERE rowid = ".$website->id; + $sql.= " AND ("; + $searchalgo = ''; + // TODO + $searchalgo.= '...'; + + $sql.=$searchalgo; + $sql.= ")"; + $sql.= $db->plimit($max); + + $resql = $db->query($sql); + if ($resql) + { + $i = 0; + while (($obj = $db->fetch_object($resql)) && ($i < $max || $max == 0)) + { + if ($obj->rowid > 0) + { + $tmpwebsitepage = new WebsitePage($db); + $tmpwebsitepage->fetch($obj->rowid); + if ($tmpwebsitepage->id > 0) $arrayresult['list'][]=$tmpwebsitepage; + } + $i++; + } + } + else + { + $error++; + $arrayresult['code']=$db->lasterrno(); + $arrayresult['message']=$db->lasterror(); + } + + $searchdone = 1; + } + + if (! $error) + { + if ($searchdone) + { + $arrayresult['code']='OK'; + if (empty($arrayresult['list'])) + { + $arrayresult['code']='KO'; + $arrayresult['message']=$weblangs->trans("NoRecordFound"); + } + } + else + { + $error++; + $arrayresult['code']='KO'; + $arrayresult['message']='No supported algorithm found'; + } } return $arrayresult; diff --git a/htdocs/langs/en_US/website.lang b/htdocs/langs/en_US/website.lang index 0d6945a17e5..83e71073984 100644 --- a/htdocs/langs/en_US/website.lang +++ b/htdocs/langs/en_US/website.lang @@ -116,3 +116,4 @@ Dynamiccontent=Sample of a page with dynamic content ImportSite=Import site EditInLineOnOff=Mode 'Edit inline' is %s ShowSubContainersOnOff=Mode to execute 'dynamic content' is %s +GlobalCSSorJS=Global CSS/JS/Header file of web site \ No newline at end of file diff --git a/htdocs/website/index.php b/htdocs/website/index.php index e01017e88ac..f98024a726d 100644 --- a/htdocs/website/index.php +++ b/htdocs/website/index.php @@ -3216,6 +3216,7 @@ if ($action == 'replacesite' || $action == 'replacesiteconfirm') print '
'; print ' '.$langs->trans("Content"); print ' '.$langs->trans("Title").' | '.$langs->trans("Description").' | '.$langs->trans("Keywords"); + print ' '.$langs->trans("GlobalCSSorJS"); print '
'; print ''; @@ -3239,6 +3240,7 @@ if ($action == 'replacesite' || $action == 'replacesiteconfirm') $algo = ''; if (GETPOST('optionmeta')) $algo.='meta'; if (GETPOST('optioncontent')) $algo.='content'; + if (GETPOST('optionsitefiles')) $algo.='sitefiles'; $listofpages = getPagesFromSearchCriterias('', $algo, GETPOST('searchstring', 'none'), 1000);