1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| <?php
/**
* Arfooo
*
* @package Arfooo
* @copyright Copyright (c) Arfooo Annuaire (fr) and Arfooo Directory (en)
* by Guillaume Hocine (c) 2007 - 2010
* http://www.arfooo.com/ (fr) and http://www.arfooo.net/ (en)
* @author Guillaume Hocine & Adrian Galewski
* @license http://creativecommons.org/licenses/by/2.0/fr/ Creative Commons
*/
class SiteSearcherModel extends SiteModel
{
function createSearchCriteria($phrase, $where = false)
{
$c = new Criteria();
if ($phrase) {
$rawPhrase = addslashes($phrase);
$phrase = strtr($phrase, '+-><()~*"', ' ');
$words = preg_split("#\s+#", $phrase);
$booleanOperator = Config::get("searchEngineWordDefaultOperator") == "AND" ? "+" : "";
$phrase = $booleanOperator . implode(" " . $booleanOperator, $words);
$phrase = addslashes($phrase);
$searchEngineSearchInFields = explode("|", Config::get("searchEngineSearchIn"));
$fullTextSearchFields = array("siteTitle", "description");
$searchInFields = "";
foreach ($fullTextSearchFields as $field) {
if (in_array($field, $searchEngineSearchInFields)) {
if ($searchInFields) {
$searchInFields .= ", ";
}
$searchInFields .= $field;
}
}
if ($searchInFields) {
$cc = new Criteria();
$cc->add("MATCH ($searchInFields) AGAINST ('$phrase' IN BOOLEAN MODE)");
$searchEngineLikeMethodEnabled = Config::get('searchEngineLikeMethodEnabled');
$searchEngineLikeMethodWordMinLength = Config::get('searchEngineLikeMethodWordMinLength');
$searchEngineLikeMethodWordMaxLength = Config::get('searchEngineLikeMethodWordMaxLength');
if ($searchEngineLikeMethodEnabled) {
$conjunction = Config::get("searchEngineWordDefaultOperator") == 'AND' ? 'AND' : 'OR';
foreach ($words as $word) {
$strlen = strlen($word);
if ($strlen <= $searchEngineLikeMethodWordMaxLength
&& $strlen >= $searchEngineLikeMethodWordMinLength
) {
foreach ($fullTextSearchFields as $field) {
if (in_array($field, $searchEngineSearchInFields)) {
$cc->add($field, "%" . str_replace(array("%", "?"), "", $word) . "%", "LIKE", $conjunction);
}
}
}
}
}
if (in_array("url", $searchEngineSearchInFields)) {
$cc->addOr("LOCATE( '$rawPhrase', url ) > 0 ");
}
$c->add($cc);
}
}
if ($where) {
$rawWhere = addslashes($where);
$c2 = new Criteria();
$c2->addOr("LOCATE( '$rawWhere', address ) > 0 ");
$c2->addOr("LOCATE( '$rawWhere', city ) > 0 ");
$c2->addOr("LOCATE( '$rawWhere',ZipCode) > 0 ");
$c->add($c2);
}
return $c;
}
function searchValidated($values, $page = false)
{
$c = new Criteria();
$c->add("status", "validated");
$c->add($this->getForbiddenRule());
$c->addOrder("sites.priority DESC");
$this->addDefaultSortingOrder($c);
if (!empty($values['phrase'])) {
$phrase = !empty($values['phrase']) ? $values['phrase'] : false;
$c->add($this->createSearchCriteria($phrase, false));
}
if (!empty($values['where'])) {
$where = !empty($values['where']) ? $values['where'] : false;
$c->add($this->createSearchCriteria(false, $where));
}
if (!empty($values['phrase']) && !empty($values['where'])) {
$phrase = !empty($values['phrase']) ? $values['phrase'] : false;
$where = !empty($values['where']) ? $values['where'] : false;
$c->add($this->createSearchCriteria($phrase, $where));
}
if (empty($values['categoryId']))
$values['categoryId'] = 0;
if (isset($values['categoryId'])) {
$c->addInnerJoin("categoryparents", "categoryparents.childId", "sites.categoryId");
$c->add("categoryparents.parentId", $values['categoryId']);
if (!empty($values['extraField'])) {
$c2 = $this->extraField->createExtraFieldsCriteria($values['categoryId'], $values['extraField']);
$c->add($c2);
}
}
if ($page !== false) {
$c->setPagination($page, Config::get("sitesPerPageInSearch"));
}
$c->setCalcFoundRows(true);
$items = $this->selectWithNewFlag($c);
return $items;
}
function search($phrase, $page = false)
{
$c = $this->createSearchCriteria($phrase);
if ($page !== false) {
$this->addPageCriteria($c, $page, Config::get("sitesPerPageInSearch"));
}
return $this->selectWithNewFlag($c);
}
function searchValidatedSite($phrase)
{
$c = new Criteria();
$c->add("status", "validated");
$c2 = $this->createSearchCriteria($phrase);
$c->add($c2);
$c->setLimit(1);
return $this->find($c, "url, description, siteTitle");
}
function getSearchValidatedCount($phrase)
{
$c = $this->createSearchCriteria($phrase);
$c2 = new Criteria();
$c2->add($c);
$c2->add("status", "validated");
return $this->getCount($c2);
}
} |
Partager