public function find($strQuery, $blnExact = false) { global $_CONF; //*** Set query property. $objReturn = new SearchResults(); $objReturn->setQuery($strQuery); $strQuery = str_replace("*", "%", $strQuery); //*** Convert query to stem. $arrWords = array_values($this->stemPhrase($strQuery)); $intWordCount = count($arrWords); //*** Query does not validate. if (!$arrWords) { return $objReturn; } //*** Set query property. $objReturn->setQuery($strQuery); $strSql = sprintf("SELECT DISTINCT pcms_search_index.elementId, COUNT(pcms_search_index.id) as word,\n\t\t\t\t\tSUM(pcms_search_index.count) as count FROM pcms_search_index, pcms_element WHERE\n\t\t\t\t\tpcms_search_index.elementId = pcms_element.id AND\n\t\t\t\t\tpcms_element.accountId = '%s' AND ", quote_smart($_CONF['app']['account']->getId())); $strSql .= '(' . implode(' OR ', array_fill(0, $intWordCount, '?')) . ') GROUP BY pcms_search_index.elementId'; //*** AND query? if ($blnExact) { $strSql .= ' HAVING word = ' . $intWordCount; } $strSql .= ' ORDER BY word DESC, count DESC'; //*** Inject the search words into the query. $arrSql = explode('?', $strSql); $strTempSql = ""; for ($i = 0; $i < $intWordCount; $i++) { $equal = stripos($arrWords[$i], "%") !== false ? "LIKE" : "="; $strTempSql .= $arrSql[$i] . "word {$equal} '" . $arrWords[$i] . "'"; } $strTempSql .= $arrSql[$i]; //*** Query the database. $objSearchIndexes = SearchIndex::select($strTempSql); foreach ($objSearchIndexes as $objSearchIndex) { $objElement = Element::selectByPk($objSearchIndex->getElementId()); if (!isset($intMaxWeight)) { $intMaxWeight = $objSearchIndex->getCount(); } if (!isset($intMaxCount)) { $intMaxCount = $objSearchIndex->getWord(); } $intRatio = round(100 / ($intMaxWeight * $intMaxCount) * $objSearchIndex->getCount() * $objSearchIndex->getWord()); $objSearchResult = new SearchResult(); $objSearchResult->id = $objSearchIndex->getElementId(); $objSearchResult->name = $objElement->getName(); $objSearchResult->value = $objElement->getDescription(); $objSearchResult->ratio = $intRatio; $objReturn->addObject($objSearchResult); } return $objReturn; }
public function find($strQuery, $arrFilters = array(), $blnExact = FALSE, $arrAllowedTypes = array()) { /* Search for elements containing keywords * * Filters may look like this: * array("element.Shops", "template.HomePage > element.Misc > templates.Banner", "element:1399") * * Allowed types may look like this: * array("element.Shops", "template.HomePage", "element:1399") * */ $objReturn = new SearchResults(); if (!empty($strQuery)) { $objSearch = new Search(); $objReturn = $objSearch->find($strQuery, $blnExact); //*** Filters. if (count($arrFilters) > 0) { //*** Parse filters. $arrElementIds = array(); foreach ($arrFilters as $strFilter) { $arrRecursive = explode(">", $strFilter); $objElement = $this; foreach ($arrRecursive as $strRecursive) { $arrFilter = explode(".", trim($strRecursive)); if (!empty($arrFilter[1])) { switch ($arrFilter[0]) { case "element": if (is_numeric($arrFilter[1])) { $objElement = $this->getElementById($arrFilter[1]); } else { $objElement = $objElement->get($arrFilter[1]); } break; case "elements": if (is_numeric($arrFilter[1])) { $objElement = $this->getElementById($arrFilter[1])->getElements(); } else { $objElement = $objElement->getElements($arrFilter[1]); } break; case "template": if (is_numeric($arrFilter[1])) { $objElement = $objElement->getElementByTemplateId($arrFilter[1]); } else { $objElement = $objElement->getElementByTemplate($arrFilter[1]); } break; case "templates": if (is_numeric($arrFilter[1])) { $objElement = $objElement->getElementsByTemplateId($arrFilter[1]); } else { $objElement = $objElement->getElementsByTemplate($arrFilter[1]); } break; } } } if (method_exists($objElement, "count")) { $objElements = $objElement; foreach ($objElements as $objElement) { array_push($arrElementIds, $objElement->getElement()->getId()); } } else { array_push($arrElementIds, $objElement->getElement()->getId()); } } //*** Apply filters. $objResults = new SearchResults(); $objResults->setQuery($objReturn->getQuery()); foreach ($objReturn as $objResult) { foreach ($arrElementIds as $intElementId) { $objElement = $this->getElementById($objResult->id); if (is_object($objElement) && $objElement->hasParentId($intElementId)) { if (count($arrAllowedTypes) > 0) { foreach ($arrAllowedTypes as $allowedType) { $arrFilter = explode(".", trim($allowedType)); if (!empty($arrFilter[1])) { switch ($arrFilter[0]) { case "element": if (is_numeric($arrFilter[1])) { if ($objElement->getId() == $arrFilter[1]) { $objResults->addObject($objResult); } } else { if ($objElement->getName() == $arrFilter[1]) { $objResults->addObject($objResult); } } break; case "template": if ($objElement->getTemplateName() == $arrFilter[1]) { $objResults->addObject($objResult); } break; } } } } else { $objResults->addObject($objResult); } } } } $objReturn = $objResults; } } return $objReturn; }