From d61d668b64c44fc046095b807834c4836a8c05c5 Mon Sep 17 00:00:00 2001 From: Aleksander Machniak <alec@alec.pl> Date: Wed, 08 Apr 2015 02:57:21 -0400 Subject: [PATCH] Remove useless code --- program/lib/Roundcube/rcube_contacts.php | 91 ++++++++++++++++++++++++++++++--------------- 1 files changed, 60 insertions(+), 31 deletions(-) diff --git a/program/lib/Roundcube/rcube_contacts.php b/program/lib/Roundcube/rcube_contacts.php index 8ba3e63..23e0b71 100644 --- a/program/lib/Roundcube/rcube_contacts.php +++ b/program/lib/Roundcube/rcube_contacts.php @@ -302,26 +302,27 @@ */ function search($fields, $value, $mode=0, $select=true, $nocount=false, $required=array()) { - if (!is_array($fields)) - $fields = array($fields); if (!is_array($required) && !empty($required)) $required = array($required); - $where = $and_where = array(); + $where = $and_where = $post_search = array(); $mode = intval($mode); $WS = ' '; $AS = self::SEPARATOR; - foreach ($fields as $idx => $col) { - // direct ID search - if ($col == 'ID' || $col == $this->primary_key) { - $ids = !is_array($value) ? explode(self::SEPARATOR, $value) : $value; - $ids = $this->db->array2list($ids, 'integer'); - $where[] = 'c.' . $this->primary_key.' IN ('.$ids.')'; - continue; - } - else if (is_array($value)) { + // direct ID search + if ($fields == 'ID' || $fields == $this->primary_key) { + $ids = !is_array($value) ? explode(self::SEPARATOR, $value) : $value; + $ids = $this->db->array2list($ids, 'integer'); + $where[] = 'c.' . $this->primary_key.' IN ('.$ids.')'; + } + else if (is_array($value)) { + foreach ((array)$fields as $idx => $col) { $val = $value[$idx]; + + if (!strlen($val)) + continue; + // table column if (in_array($col, $this->table_cols)) { switch ($mode) { @@ -347,22 +348,30 @@ $post_search[$col] = mb_strtolower($val); } } - // fulltext search in all fields - else if ($col == '*') { - $where[] = $this->fulltext_sql_where($value, $mode, 'words'); - } - else { - $where[] = $this->fulltext_sql_where($value, $mode, $col, 'OR'); + } + // fulltext search in all fields + else if ($fields == '*') { + $where[] = $this->fulltext_sql_where($value, $mode, 'words'); + } + else { + // require each word in to be present in one of the fields + foreach (rcube_utils::tokenize_string($value, 1) as $word) { + $groups = array(); + foreach ((array)$fields as $idx => $col) { + $groups[] = $this->fulltext_sql_where($word, $mode, $col); + } + $where[] = '(' . join(' OR ', $groups) . ')'; } } foreach (array_intersect($required, $this->table_cols) as $col) { $and_where[] = $this->db->quote_identifier($col).' <> '.$this->db->quote(''); } + $required = array_diff($required, $this->table_cols); if (!empty($where)) { // use AND operator for advanced searches - $where = join(is_array($value) || $fields[0] != '*' ? ' AND ' : ' OR ', $where); + $where = join(" AND ", $where); } if (!empty($and_where)) @@ -370,7 +379,7 @@ // Post-searching in vCard data fields // we will search in all records and then build a where clause for their IDs - if (!empty($post_search)) { + if (!empty($post_search) || !empty($required)) { $ids = array(0); // build key name regexp $regexp = '/^(' . implode(array_keys($post_search), '|') . ')(?:.*)$/'; @@ -379,7 +388,7 @@ $this->set_search_set($where); // count result pages - $cnt = $this->count(); + $cnt = $this->count()->count; $pages = ceil($cnt / $this->page_size); $scnt = count($post_search); @@ -389,14 +398,33 @@ while ($row = $this->result->next()) { $id = $row[$this->primary_key]; $found = array(); - foreach (preg_grep($regexp, array_keys($row)) as $col) { - $pos = strpos($col, ':'); - $colname = $pos ? substr($col, 0, $pos) : $col; - $search = $post_search[$colname]; - foreach ((array)$row[$col] as $value) { - if ($this->compare_search_value($colname, $value, $search, $mode)) { - $found[$colname] = true; - break 2; + if (!empty($post_search)) { + foreach (preg_grep($regexp, array_keys($row)) as $col) { + $pos = strpos($col, ':'); + $colname = $pos ? substr($col, 0, $pos) : $col; + $search = $post_search[$colname]; + foreach ((array)$row[$col] as $value) { + if ($this->compare_search_value($colname, $value, $search, $mode)) { + $found[$colname] = true; + break 2; + } + } + } + } + // check if required fields are present + if (!empty($required)) { + foreach ($required as $req) { + $hit = false; + foreach ($row as $c => $values) { + if ($c === $req || strpos($c, $req.':') === 0) { + if ((is_string($row[$c]) && strlen($row[$c])) || !empty($row[$c])) { + $hit = true; + break; + } + } + } + if (!$hit) { + continue 2; } } } @@ -438,9 +466,10 @@ { $WS = ' '; $AS = $col == 'words' ? $WS : self::SEPARATOR; + $words = $col == 'words' ? rcube_utils::normalize_string($value, true) : array($value); $where = array(); - foreach (rcube_utils::normalize_string($value, true) as $word) { + foreach ($words as $word) { switch ($mode) { case 1: // strict $where[] = '(' . $this->db->ilike($col, $word . '%') @@ -456,7 +485,7 @@ } } - return '(' . join(" $bool ", $where) . ')'; + return count($where) ? '(' . join(" $bool ", $where) . ')' : ''; } /** -- Gitblit v1.9.1