Ignore:
Timestamp:
Jan 18, 2011 1:00:57 PM (2 years ago)
Author:
thomascube <thomas@…>
Branches:
master, HEAD, courier-fix, dev-browser-capabilities, pdo, release-0.6, release-0.7, release-0.8
Children:
1bcb2f3
Parents:
e81a307
Message:

Merge branch devel-addressbook (r4193:4382) back into trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • program/include/rcube_contacts.php

    rf5e7b353 r0501b63  
    4848 
    4949    // public properties 
    50     var $primary_key = 'contact_id'; 
    51     var $readonly = false; 
    52     var $groups = true; 
    53     var $list_page = 1; 
    54     var $page_size = 10; 
    55     var $group_id = 0; 
    56     var $ready = false; 
     50    public $primary_key = 'contact_id'; 
     51    public $readonly = false; 
     52    public $groups = true; 
     53    public $list_page = 1; 
     54    public $page_size = 10; 
     55    public $group_id = 0; 
     56    public $ready = false; 
     57    public $coltypes = array('name', 'firstname', 'surname', 'middlename', 'prefix', 'suffix', 'nickname', 
     58      'jobtitle', 'organization', 'department', 'assistant', 'manager', 
     59      'gender', 'maidenname', 'spouse', 'email', 'phone', 'address', 
     60      'birthday', 'anniversary', 'website', 'im', 'notes', 'photo'); 
    5761 
    5862 
     
    153157     * List the current set of contact records 
    154158     * 
    155      * @param  array   List of cols to show 
     159     * @param  array   List of cols to show, Null means all 
    156160     * @param  int     Only return this number of records, use negative values for tail 
    157161     * @param  boolean True to skip the count query (select only) 
     
    188192            $this->group_id); 
    189193 
     194        // determine whether we have to parse the vcard or if only db cols are requested 
     195        $read_vcard = !$cols || count(array_intersect($cols, $this->table_cols)) < count($cols); 
     196         
    190197        while ($sql_result && ($sql_arr = $this->db->fetch_assoc($sql_result))) { 
    191198            $sql_arr['ID'] = $sql_arr[$this->primary_key]; 
     199 
     200            if ($read_vcard) 
     201                $sql_arr = $this->convert_db_data($sql_arr); 
     202            else 
     203                $sql_arr['email'] = preg_split('/,\s*/', $sql_arr['email']); 
     204             
    192205            // make sure we have a name to display 
    193206            if (empty($sql_arr['name'])) 
    194                 $sql_arr['name'] = $sql_arr['email']; 
     207                $sql_arr['name'] = $sql_arr['email'][0]; 
     208 
    195209            $this->result->add($sql_arr); 
    196210        } 
     
    223237     * @param boolean True to skip the count query (select only) 
    224238     * @param array   List of fields that cannot be empty 
    225      * @return Indexed list of contact records and 'count' value 
     239     * @return object rcube_result_set Contact records and 'count' value 
    226240     */ 
    227241    function search($fields, $value, $strict=false, $select=true, $nocount=false, $required=array()) 
     
    346360 
    347361        if ($sql_arr = $this->db->fetch_assoc()) { 
    348             $sql_arr['ID'] = $sql_arr[$this->primary_key]; 
     362            $record = $this->convert_db_data($sql_arr); 
    349363            $this->result = new rcube_result_set(1); 
    350             $this->result->add($sql_arr); 
    351         } 
    352  
    353         return $assoc && $sql_arr ? $sql_arr : $this->result; 
     364            $this->result->add($record); 
     365        } 
     366 
     367        return $assoc && $record ? $record : $this->result; 
    354368    } 
    355369 
     
    390404    function insert($save_data, $check=false) 
    391405    { 
    392         if (is_object($save_data) && is_a($save_data, rcube_result_set)) 
    393             return $this->insert_recset($save_data, $check); 
     406        if (!is_array($save_data)) 
     407            return false; 
    394408 
    395409        $insert_id = $existing = false; 
    396410 
    397         if ($check) 
    398             $existing = $this->search('email', $save_data['email'], true, false); 
    399  
     411        if ($check) { 
     412            foreach ($save_data as $col => $values) { 
     413                if (strpos($col, 'email') === 0) { 
     414                    foreach ((array)$values as $email) { 
     415                        if ($existing = $this->search('email', $email, true, false)) 
     416                            break 2; 
     417                    } 
     418                } 
     419            } 
     420        } 
     421 
     422        $save_data = $this->convert_save_data($save_data); 
    400423        $a_insert_cols = $a_insert_values = array(); 
    401424 
    402         foreach ($this->table_cols as $col) 
    403             if (isset($save_data[$col])) { 
    404                 $a_insert_cols[]   = $this->db->quoteIdentifier($col); 
    405                 $a_insert_values[] = $this->db->quote($save_data[$col]); 
    406             } 
     425        foreach ($save_data as $col => $value) { 
     426            $a_insert_cols[]   = $this->db->quoteIdentifier($col); 
     427            $a_insert_values[] = $this->db->quote($value); 
     428        } 
    407429 
    408430        if (!$existing->count && !empty($a_insert_cols)) { 
     
    427449 
    428450    /** 
    429      * Insert new contacts for each row in set 
    430      */ 
    431     function insert_recset($result, $check=false) 
    432     { 
    433         $ids = array(); 
    434         while ($row = $result->next()) { 
    435             if ($insert = $this->insert($row, $check)) 
    436                 $ids[] = $insert; 
    437         } 
    438         return $ids; 
    439     } 
    440  
    441  
    442     /** 
    443451     * Update a specific contact record 
    444452     * 
     
    451459        $updated = false; 
    452460        $write_sql = array(); 
    453  
    454         foreach ($this->table_cols as $col) 
    455             if (isset($save_cols[$col])) 
    456                 $write_sql[] = sprintf("%s=%s", $this->db->quoteIdentifier($col), 
    457                     $this->db->quote($save_cols[$col])); 
     461        $record = $this->get_record($id, true); 
     462        $save_cols = $this->convert_save_data($save_cols, $record); 
     463 
     464        foreach ($save_cols as $col => $value) { 
     465            $write_sql[] = sprintf("%s=%s", $this->db->quoteIdentifier($col), $this->db->quote($value)); 
     466        } 
    458467 
    459468        if (!empty($write_sql)) { 
     
    469478 
    470479            $updated = $this->db->affected_rows(); 
     480            $this->result = null;  // clear current result (from get_record()) 
    471481        } 
    472482 
    473483        return $updated; 
     484    } 
     485     
     486     
     487    private function convert_db_data($sql_arr) 
     488    { 
     489        $record = array(); 
     490        $record['ID'] = $sql_arr[$this->primary_key]; 
     491         
     492        if ($sql_arr['vcard']) { 
     493            unset($sql_arr['email']); 
     494            $vcard = new rcube_vcard($sql_arr['vcard']); 
     495            $record += $vcard->get_assoc() + $sql_arr; 
     496        } 
     497        else { 
     498            $record += $sql_arr; 
     499            $record['email'] = preg_split('/,\s*/', $record['email']); 
     500        } 
     501         
     502        return $record; 
     503    } 
     504 
     505 
     506    private function convert_save_data($save_data, $record = array()) 
     507    { 
     508        $out = array(); 
     509 
     510        // copy values into vcard object 
     511        $vcard = new rcube_vcard($record['vcard'] ? $record['vcard'] : $save_data['vcard']); 
     512        $vcard->reset(); 
     513        foreach ($save_data as $key => $values) { 
     514            list($field, $section) = explode(':', $key); 
     515            foreach ((array)$values as $value) { 
     516                if (isset($value)) 
     517                    $vcard->set($field, $value, $section); 
     518            } 
     519        } 
     520        $out['vcard'] = $vcard->export(); 
     521 
     522        foreach ($this->table_cols as $col) { 
     523            $key = $col; 
     524            if (!isset($save_data[$key])) 
     525                $key .= ':home'; 
     526            if (isset($save_data[$key])) 
     527                $out[$col] = is_array($save_data[$key]) ? join(',', $save_data[$key]) : $save_data[$key]; 
     528        } 
     529 
     530        // save all e-mails in database column 
     531        $out['email'] = join(", ", $vcard->email); 
     532 
     533        return $out; 
    474534    } 
    475535 
Note: See TracChangeset for help on using the changeset viewer.