. */ /** * @brief Script used by an Ajax autocompleter object to get the contacts data (from users or contacts) * * @file autocomplete_contacts.php * @author Claire Figueras * @date $date$ * @version $Revision$ * @ingroup indexing_searching_mlb */ require_once('core/class/class_request.php'); $req = new request(); $req->connect(); if (empty($_REQUEST['table'])) { exit(); } $table = $_REQUEST['table']; if ($table == 'users') { $select = array(); $select[$_SESSION['tablename']['users']]= array('lastname', 'firstname', 'user_id'); $where = " (lower(lastname) like lower('%".$req->protect_string_db($_REQUEST['Input'])."%') " ."or lower(firstname) like lower('%".$req->protect_string_db($_REQUEST['Input'])."%') " ."or user_id like '%".$req->protect_string_db($_REQUEST['Input'])."%') and (status = 'OK' or status = 'ABS') and enabled = 'Y'"; $other = 'order by lastname, firstname'; $res = $req->select($select, $where, $other, $_SESSION['config']['databasetype'], 11,false,"","","", false); echo ""; } elseif ($table == 'contacts') { $timestart=microtime(true); if (isset($_REQUEST['contact_type']) && $_REQUEST['contact_type'] <> '') { $contactTypeRequest = " AND contact_type = '" . $_REQUEST['contact_type'] . "'"; } $args = explode(' ', $_REQUEST['Input']); $args[] = $_REQUEST['Input']; $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_id || ')' AS result, " . " %d AS confidence" . " FROM contacts" . " WHERE (user_id = '' OR user_id IS NULL OR user_id = '".$req->protect_string_db($_SESSION['user']['UserId'])."' ) " . " AND enabled = 'Y' " . $contactTypeRequest . " 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 = $req->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"; $req->query($query); $nb = $req->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 "

...

"; }