| 1 |
<?php |
|---|
| 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 |
session_cache_limiter('private'); |
|---|
| 41 |
|
|---|
| 42 |
include("include/session.php"); |
|---|
| 43 |
|
|---|
| 44 |
class smartSearch{ |
|---|
| 45 |
|
|---|
| 46 |
var $totalcount = 0; |
|---|
| 47 |
|
|---|
| 48 |
function smartSearch($db, $sdbid){ |
|---|
| 49 |
|
|---|
| 50 |
$this->db = $db; |
|---|
| 51 |
|
|---|
| 52 |
$this->getSearchParams($sdbid); |
|---|
| 53 |
|
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
function getSearchParams($sdbid){ |
|---|
| 58 |
|
|---|
| 59 |
$querystatement = " |
|---|
| 60 |
SELECT |
|---|
| 61 |
* |
|---|
| 62 |
FROM |
|---|
| 63 |
smartsearches |
|---|
| 64 |
WHERE |
|---|
| 65 |
id = ".((int) $sdbid); |
|---|
| 66 |
|
|---|
| 67 |
$this->searchParams = $this->db->fetchArray($this->db->query($querystatement)); |
|---|
| 68 |
|
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
function find($term, $offset=0){ |
|---|
| 73 |
|
|---|
| 74 |
$term = trim(mysql_real_escape_string($term)); |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
$terms = explode(" ",$term); |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
$searchFields = explode(",", $this->searchParams["searchfields"]); |
|---|
| 81 |
|
|---|
| 82 |
$wheres=""; |
|---|
| 83 |
foreach($terms as $value){ |
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
$wheres .="AND ("; |
|---|
| 90 |
|
|---|
| 91 |
foreach($searchFields as $field) |
|---|
| 92 |
$wheres .= trim($field)." LIKE '".$value."%' OR ".trim($field)." LIKE '% ".$value."%'\nOR "; |
|---|
| 93 |
|
|---|
| 94 |
$wheres = substr($wheres,0,strlen($wheres)-3); |
|---|
| 95 |
$wheres .= ")"; |
|---|
| 96 |
|
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
if($wheres){ |
|---|
| 100 |
|
|---|
| 101 |
$finalsearch = ""; |
|---|
| 102 |
foreach($searchFields as $field) |
|---|
| 103 |
$finalsearch .= trim($field)." LIKE '".$term."%'\nOR "; |
|---|
| 104 |
|
|---|
| 105 |
$finalsearch = substr($finalsearch,0,strlen($finalsearch)-3); |
|---|
| 106 |
|
|---|
| 107 |
$wheres = "AND ( (".$finalsearch.") OR (".substr($wheres,4)."))"; |
|---|
| 108 |
|
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
$securityWhere = ""; |
|---|
| 112 |
|
|---|
| 113 |
if($this->searchParams["rolefield"]){ |
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
if ($_SESSION["userinfo"]["admin"]!=1){ |
|---|
| 119 |
|
|---|
| 120 |
if(count($_SESSION["userinfo"]["roles"])>0) |
|---|
| 121 |
$securityWhere = " AND ".$this->searchParams["rolefield"]." IN (".implode(",",$_SESSION["userinfo"]["roles"]).",0)"; |
|---|
| 122 |
else |
|---|
| 123 |
$securityWhere = " AND ".$this->searchParams["rolefield"]." = 0"; |
|---|
| 124 |
|
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
$querystatement = " |
|---|
| 130 |
SELECT DISTINCT |
|---|
| 131 |
".$this->searchParams["displayfield"]." AS display, |
|---|
| 132 |
".$this->searchParams["valuefield"]." AS value, |
|---|
| 133 |
".$this->searchParams["secondaryfield"]." AS secondary, |
|---|
| 134 |
".$this->searchParams["classfield"]." AS classname |
|---|
| 135 |
FROM |
|---|
| 136 |
".$this->searchParams["fromclause"]." |
|---|
| 137 |
WHERE |
|---|
| 138 |
(".$this->searchParams["filterclause"].") |
|---|
| 139 |
".$securityWhere." |
|---|
| 140 |
".$wheres." |
|---|
| 141 |
ORDER BY |
|---|
| 142 |
".$this->searchParams["displayfield"]." |
|---|
| 143 |
LIMIT ".((int) $offset).", 8"; |
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
$totalCountStatement = " |
|---|
| 149 |
SELECT |
|---|
| 150 |
COUNT(".$this->searchParams["displayfield"].") AS thecount |
|---|
| 151 |
FROM |
|---|
| 152 |
".$this->searchParams["fromclause"]." |
|---|
| 153 |
WHERE |
|---|
| 154 |
(".$this->searchParams["filterclause"].") |
|---|
| 155 |
".$securityWhere." |
|---|
| 156 |
".$wheres; |
|---|
| 157 |
|
|---|
| 158 |
$countrecord = $this->db->fetchArray($this->db->query($totalCountStatement)); |
|---|
| 159 |
$this->totalcount = $countrecord["thecount"]; |
|---|
| 160 |
|
|---|
| 161 |
return $this->db->query($querystatement); |
|---|
| 162 |
|
|---|
| 163 |
} |
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
function display($result){ |
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
$output = "{totalRecords: ".$this->totalcount.", resultRecords: ["; |
|---|
| 170 |
|
|---|
| 171 |
while($therecord = $this->db->fetchArray($result)){ |
|---|
| 172 |
|
|---|
| 173 |
$output .= "{display: '".str_replace("'", "\'", formatVariable($therecord["display"],"bbcode"))."',"; |
|---|
| 174 |
$output .= "value: '".str_replace("'", "\'", formatVariable($therecord["value"]))."',"; |
|---|
| 175 |
$output .= "secondary: '".str_replace("'", "\'", formatVariable($therecord["secondary"],"bbcode"))."',"; |
|---|
| 176 |
$output .= "classname: '".str_replace("'", "\'", formatVariable($therecord["classname"]))."'},"; |
|---|
| 177 |
|
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
if($output != "{totalRecords: ".$this->totalcount.", resultRecords: [") |
|---|
| 181 |
$output = substr($output, 0, strlen($output)-1); |
|---|
| 182 |
|
|---|
| 183 |
$output .= "] }"; |
|---|
| 184 |
|
|---|
| 185 |
header("Content-type: text/plain"); |
|---|
| 186 |
echo $output; |
|---|
| 187 |
|
|---|
| 188 |
} |
|---|
| 189 |
|
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
if(isset($_GET["sdbid"]) && isset($_GET["t"])){ |
|---|
| 196 |
|
|---|
| 197 |
$smartSearch = new smartSearch($db, $_GET["sdbid"]); |
|---|
| 198 |
|
|---|
| 199 |
if(!isset($_GET["o"])) |
|---|
| 200 |
$_GET["o"] = 0; |
|---|
| 201 |
|
|---|
| 202 |
$theresult = $smartSearch->find($_GET["t"],((int) $_GET["o"])); |
|---|
| 203 |
|
|---|
| 204 |
if(isset($theresult)) |
|---|
| 205 |
$smartSearch->display($theresult); |
|---|
| 206 |
|
|---|
| 207 |
} |
|---|
| 208 |
?> |
|---|