. */ /** * @brief List of users for autocompletion * * * @file * @author Laurent Giovannoni * @date $date$ * @version $Revision$ * @ingroup admin */ require_once("core".DIRECTORY_SEPARATOR."class".DIRECTORY_SEPARATOR."class_request.php"); $db = new dbquery(); $db->connect(); $listArray = array(); $db->query("select lastname, firstname, user_id from ".$_SESSION['tablename']['users'] ." where lower(lastname) like lower('%".$db->protect_string_db($_REQUEST['what'])."%') and enabled = 'Y' order by lastname, firstname"); while($line = $db->fetch_object()) { array_push($listArray, $db->show_string($line->lastname)." ".$db->show_string($line->firstname)." (user:".$line->user_id.")"); //array_push($listArray, "user : [".$db->show_string($line->lastname)." ".$db->show_string($line->firstname))."]"; } $timestart=microtime(true); $args = explode(' ', $_REQUEST['what']); $args[] = $_REQUEST['what']; $num_args = count($args); if($num_args == 0) return ""; $query = "SELECT result, SUM(confidence) AS score, count(1) AS num FROM ("; $subQuery = "SELECT " . "(CASE " . " WHEN is_corporate_person = 'Y' THEN society" . " WHEN is_corporate_person = 'N' THEN UPPER(lastname) || ' ' || firstname " . " END) || ' (contact:' || contact_id || ')' AS result, " . ' %d AS confidence' . " FROM contacts" . " WHERE (user_id = '' OR user_id IS NULL OR user_id = '".$db->protect_string_db($_SESSION['user']['UserId'])."' ) " . " AND enabled = 'Y' " . " AND (" . " LOWER(lastname) LIKE LOWER('%s')" . " OR LOWER(firstname) LIKE LOWER('%s')" . " OR LOWER(society) LIKE LOWER('%s')" .")"; $queryParts = array(); foreach($args as $arg) { $arg = $db->protect_string_db($arg); if(strlen($arg) == 0) continue; # Full match of one given arg $expr = $arg; $conf = 100; $queryParts[] = sprintf($subQuery, $conf, $expr, $expr, $expr); # Partial match (starts with) $expr = $arg . "%"; ; $conf = 34; # If found, partial match contains will also be so score is sum of both confidences, i.e. 67) $queryParts[] = sprintf($subQuery, $conf, $expr, $expr, $expr); # Partial match (contains) $expr = "%" . $arg . "%"; $conf = 33; $queryParts[] = sprintf($subQuery, $conf, $expr, $expr, $expr); } $query .= implode (' UNION ALL ', $queryParts); $query .= ") matches" . " GROUP BY result " . " ORDER BY score DESC, result ASC"; $db->query($query); $nb = $db->nb_result(); $m = 30; if($nb >= $m) $l = $m; else $l = $nb; $timeend=microtime(true); $time = number_format(($timeend-$timestart), 3); $found = false; echo ""; if($nb == 0) echo "

...

"; if($nb > $m) echo "

...

";