diff options
Diffstat (limited to 'program/lib/Roundcube/rcube_contacts.php')
-rw-r--r-- | program/lib/Roundcube/rcube_contacts.php | 114 |
1 files changed, 63 insertions, 51 deletions
diff --git a/program/lib/Roundcube/rcube_contacts.php b/program/lib/Roundcube/rcube_contacts.php index 6ac9fd5de..6438c431a 100644 --- a/program/lib/Roundcube/rcube_contacts.php +++ b/program/lib/Roundcube/rcube_contacts.php @@ -302,8 +302,6 @@ class rcube_contacts extends rcube_addressbook */ 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); @@ -312,36 +310,19 @@ class rcube_contacts extends rcube_addressbook $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; - } - // fulltext search in all fields - else if ($col == '*') { - $words = array(); - foreach (explode($WS, rcube_utils::normalize_string($value)) as $word) { - switch ($mode) { - case 1: // strict - $words[] = '(' . $this->db->ilike('words', $word . '%') - . ' OR ' . $this->db->ilike('words', '%' . $WS . $word . $WS . '%') - . ' OR ' . $this->db->ilike('words', '%' . $WS . $word) . ')'; - break; - case 2: // prefix - $words[] = '(' . $this->db->ilike('words', $word . '%') - . ' OR ' . $this->db->ilike('words', '%' . $WS . $word . '%') . ')'; - break; - default: // partial - $words[] = $this->db->ilike('words', '%' . $word . '%'); - } - } - $where[] = '(' . join(' AND ', $words) . ')'; - } - else { - $val = is_array($value) ? $value[$idx] : $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) { @@ -362,28 +343,26 @@ class rcube_contacts extends rcube_addressbook // vCard field else { if (in_array($col, $this->fulltext_cols)) { - foreach (rcube_utils::normalize_string($val, true) as $word) { - switch ($mode) { - case 1: // strict - $words[] = '(' . $this->db->ilike('words', $word . $WS . '%') - . ' OR ' . $this->db->ilike('words', '%' . $AS . $word . $WS .'%') - . ' OR ' . $this->db->ilike('words', '%' . $AS . $word) . ')'; - break; - case 2: // prefix - $words[] = '(' . $this->db->ilike('words', $word . '%') - . ' OR ' . $this->db->ilike('words', $AS . $word . '%') . ')'; - break; - default: // partial - $words[] = $this->db->ilike('words', '%' . $word . '%'); - } - } - $where[] = '(' . join(' AND ', $words) . ')'; + $where[] = $this->fulltext_sql_where($val, $mode, 'words'); } - if (is_array($value)) - $post_search[$col] = mb_strtolower($val); + $post_search[$col] = mb_strtolower($val); } } } + // 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(''); @@ -391,7 +370,7 @@ class rcube_contacts extends rcube_addressbook if (!empty($where)) { // use AND operator for advanced searches - $where = join(is_array($value) ? ' AND ' : ' OR ', $where); + $where = join(" AND ", $where); } if (!empty($and_where)) @@ -460,6 +439,34 @@ class rcube_contacts extends rcube_addressbook return $this->result; } + /** + * Helper method to compose SQL where statements for fulltext searching + */ + private function fulltext_sql_where($value, $mode, $col = 'words', $bool = 'AND') + { + $WS = ' '; + $AS = $col == 'words' ? $WS : self::SEPARATOR; + $words = $col == 'words' ? rcube_utils::normalize_string($value, true) : array($value); + + $where = array(); + foreach ($words as $word) { + switch ($mode) { + case 1: // strict + $where[] = '(' . $this->db->ilike($col, $word . '%') + . ' OR ' . $this->db->ilike($col, '%' . $WS . $word . $WS . '%') + . ' OR ' . $this->db->ilike($col, '%' . $WS . $word) . ')'; + break; + case 2: // prefix + $where[] = '(' . $this->db->ilike($col, $word . '%') + . ' OR ' . $this->db->ilike($col, '%' . $AS . $word . '%') . ')'; + break; + default: // partial + $where[] = $this->db->ilike($col, '%' . $word . '%'); + } + } + + return '(' . join(" $bool ", $where) . ')'; + } /** * Count number of available contacts in database @@ -714,6 +721,11 @@ class rcube_contacts extends rcube_addressbook // copy values into vcard object $vcard = new rcube_vcard($record['vcard'] ? $record['vcard'] : $save_data['vcard'], RCUBE_CHARSET, false, $this->vcard_fieldmap); $vcard->reset(); + + // don't store groups in vCard (#1490277) + $vcard->set('groups', null); + unset($save_data['groups']); + foreach ($save_data as $key => $values) { list($field, $section) = explode(':', $key); $fulltext = in_array($field, $this->fulltext_cols); |