alecpl
2011-05-21 bc8c2c57880523472b30f475d566a8133e2d2e20
program/include/rcube_contacts.php
@@ -5,7 +5,7 @@
 | program/include/rcube_contacts.php                                    |
 |                                                                       |
 | This file is part of the Roundcube Webmail client                     |
 | Copyright (C) 2006-2010, The Roundcube Dev Team                       |
 | Copyright (C) 2006-2011, The Roundcube Dev Team                       |
 | Licensed under the GNU GPL                                            |
 |                                                                       |
 | PURPOSE:                                                              |
@@ -41,10 +41,11 @@
    private $user_id = 0;
    private $filter = null;
    private $result = null;
    private $search_fields;
    private $search_string;
    private $cache;
    private $table_cols = array('name', 'email', 'firstname', 'surname', 'vcard');
    private $table_cols = array('name', 'email', 'firstname', 'surname');
    private $fulltext_cols = array('name', 'firstname', 'surname', 'middlename', 'nickname',
      'jobtitle', 'organization', 'department', 'maidenname', 'email', 'phone',
      'address', 'street', 'locality', 'zipcode', 'region', 'country', 'website', 'im', 'notes');
    // public properties
    public $primary_key = 'contact_id';
@@ -115,8 +116,6 @@
    {
        $this->result = null;
        $this->filter = null;
        $this->search_fields = null;
        $this->search_string = null;
        $this->cache = null;
    }
@@ -185,7 +184,7 @@
                " AND c.user_id=?" .
                ($this->group_id ? " AND m.contactgroup_id=?" : "").
                ($this->filter ? " AND (".$this->filter.")" : "") .
            " ORDER BY c.name",
            " ORDER BY ". $this->db->concat('c.name', 'c.email'),
            $start_row,
            $length,
            $this->user_id,
@@ -201,10 +200,13 @@
                $sql_arr = $this->convert_db_data($sql_arr);
            else
                $sql_arr['email'] = preg_split('/,\s*/', $sql_arr['email']);
            // make sure we have a name to display
            if (empty($sql_arr['name']))
            if (empty($sql_arr['name'])) {
                if (empty($sql_arr['email']))
                  $sql_arr['email'] = $this->get_col_values('email', $sql_arr, true);
                $sql_arr['name'] = $sql_arr['email'][0];
            }
            $this->result->add($sql_arr);
        }
@@ -253,8 +255,15 @@
                $ids     = $this->db->array2list($ids, 'integer');
                $where[] = 'c.' . $this->primary_key.' IN ('.$ids.')';
            }
            else if ($strict)
            else if ($strict) {
                $where[] = $this->db->quoteIdentifier($col).' = '.$this->db->quote($value);
            }
            else if ($col == '*') {
                $words = array();
                foreach(explode(" ", self::normalize_string($value)) as $word)
                    $words[] = $this->db->ilike('words', '%'.$word.'%');
                $where[] = '(' . join(' AND ', $words) . ')';
              }
            else
                $where[] = $this->db->ilike($col, '%'.$value.'%');
        }
@@ -404,10 +413,10 @@
     */
    public function validate($save_data)
    {
        // check for name input
        // validate e-mail addresses
        $valid = parent::validate($save_data);
        // require at least one e-mail address (syntax check is done later in save.inc)
        // require at least one e-mail address (syntax check is already done)
        if ($valid && !array_filter($this->get_col_values('email', $save_data, true))) {
            $this->set_error('warning', 'noemailwarning');
            $valid = false;
@@ -434,7 +443,7 @@
            foreach ($save_data as $col => $values) {
                if (strpos($col, 'email') === 0) {
                    foreach ((array)$values as $email) {
                        if ($existing = $this->search('email', $email, true, false))
                        if ($existing = $this->search('email', $email, false, false))
                            break 2;
                    }
                }
@@ -528,18 +537,24 @@
    private function convert_save_data($save_data, $record = array())
    {
        $out = array();
        $words = '';
        // copy values into vcard object
        $vcard = new rcube_vcard($record['vcard'] ? $record['vcard'] : $save_data['vcard']);
        $vcard->reset();
        foreach ($save_data as $key => $values) {
            list($field, $section) = explode(':', $key);
            $fulltext = in_array($field, $this->fulltext_cols);
            foreach ((array)$values as $value) {
                if (isset($value))
                    $vcard->set($field, $value, $section);
                if ($fulltext && is_array($value))
                    $words .= ' ' . self::normalize_string(join(" ", $value));
                else if ($fulltext && strlen($value) >= 3)
                    $words .= ' ' . self::normalize_string($value);
            }
        }
        $out['vcard'] = $vcard->export();
        $out['vcard'] = $vcard->export(false);
        foreach ($this->table_cols as $col) {
            $key = $col;
@@ -552,6 +567,9 @@
        // save all e-mails in database column
        $out['email'] = join(", ", $vcard->email);
        // join words for fulltext search
        $out['words'] = join(" ", array_unique(explode(" ", $words)));
        return $out;
    }