Changeset 4251 in subversion


Ignore:
Timestamp:
Nov 22, 2010 3:37:28 PM (2 years ago)
Author:
thomasb
Message:

Implement updating a Kolab contact record (some minor problems remain unsolved)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/plugins/kolab_addressbook/rcube_kolab_contacts.php

    r4245 r4251  
    1414{ 
    1515    public $primary_key = 'ID'; 
    16     public $readonly = true; 
     16    public $readonly = false; 
    1717    public $groups = true; 
    1818    public $coltypes = array( 
     
    3333      'im'           => array('limit' => 1), 
    3434      'website'      => array('limit' => 1, 'subtypes' => null), 
    35       'address'      => array(), 
     35      'address'      => array('limit' => 2, 'subtypes' => array('home','work')), 
    3636      'notes'        => array(), 
    3737      // define additional coltypes 
     
    4040      // TODO: define more Kolab-specific fields such as: office-location, profession, manager-name, assistant, spouse-name, children, language, latitude, longitude, pgp-publickey, free-busy-url 
    4141    ); 
    42  
     42     
    4343    private $gid; 
    4444    private $imap; 
     
    5454    private $imap_folder = 'INBOX/Contacts'; 
    5555    private $gender_map = array(0 => 'male', 1 => 'female'); 
     56    private $fieldmap = array( 
     57      // kolab       => roundcube 
     58      'full-name'    => 'name', 
     59      'given-name'   => 'firstname', 
     60      'middle-names' => 'middlename', 
     61      'last-name'    => 'surname', 
     62      'prefix'       => 'prefix', 
     63      'suffix'       => 'suffix', 
     64      'nick-name'    => 'nickname', 
     65      'organization' => 'organization', 
     66      'department'   => 'department', 
     67      'job-title'    => 'jobtitle', 
     68      'initials'     => 'initials', 
     69      'birthday'     => 'birthday', 
     70      'anniversary'  => 'anniversary', 
     71      'im-address'   => 'im:aim', 
     72      'web-page'     => 'website', 
     73      'body'         => 'notes', 
     74    ); 
    5675 
    5776 
     
    6483        $format = rcube_kolab::get_format('contact'); 
    6584        $this->coltypes['phone']['subtypes'] = $format->_phone_types; 
    66         $this->coltypes['address']['subtypes'] = $format->_address_types; 
    6785        $this->coltypes['anniversary']['label'] = rcube_label('anniversary'); 
    6886         
     
    237255     * @return array List of assigned groups as ID=>Name pairs 
    238256     */ 
    239     function get_record_groups($id) 
     257    public function get_record_groups($id) 
    240258    { 
    241259        $out = array(); 
     
    251269        return $out; 
    252270    } 
    253      
     271 
     272 
     273    /** 
     274     * Create a new contact record 
     275     * 
     276     * @param array Assoziative array with save data 
     277     *  Keys:   Field name with optional section in the form FIELD:SECTION 
     278     *  Values: Field value. Can be either a string or an array of strings for multiple values 
     279     * @param boolean True to check for duplicates first 
     280     * @return mixed The created record ID on success, False on error 
     281     */ 
     282    public function insert($save_data, $check=false) 
     283    { 
     284        if (is_object($save_data) && is_a($save_data, rcube_result_set)) 
     285            return $this->insert_recset($save_data, $check); 
     286 
     287        $insert_id = $existing = false; 
     288 
     289        // check for existing records by e-mail comparison 
     290        if ($check) { 
     291            foreach ($this->_get_col_values('email', $save_data, true) as $email) { 
     292                if ($existing = $this->search('email', $email, true, false)) 
     293                    break; 
     294            } 
     295        } 
     296         
     297        $object = $this->_from_rcube_contact($save_data); 
     298        var_dump($object); 
     299         
     300        // TODO: how to create new Kolab objects? 
     301         
     302         
     303        return $insert_id; 
     304    } 
     305 
     306    /** 
     307     * Insert new contacts for each row in set 
     308     * 
     309     * @see rcube_kolab_contacts::insert() 
     310     */ 
     311    private function insert_recset($result, $check=false) 
     312    { 
     313        $ids = array(); 
     314        while ($row = $result->next()) { 
     315            if ($insert = $this->insert($row, $check)) 
     316                $ids[] = $insert; 
     317        } 
     318        return $ids; 
     319    } 
     320 
     321 
     322    /** 
     323     * Update a specific contact record 
     324     * 
     325     * @param mixed Record identifier 
     326     * @param array Assoziative array with save data 
     327     *  Keys:   Field name with optional section in the form FIELD:SECTION 
     328     *  Values: Field value. Can be either a string or an array of strings for multiple values 
     329     * @return boolean True on success, False on error 
     330     */ 
     331    public function update($id, $save_data) 
     332    { 
     333        $updated = false; 
     334        $this->_fetch_contacts(); 
     335        if ($this->contacts[$id] && ($uid = $this->id2uid[$id])) { 
     336            $old = $this->contactstorage->getObject($uid); 
     337            $object = array_merge($old, $this->_from_rcube_contact($save_data)); 
     338            $object['last-modification-date'] = time(); 
     339 
     340            $saved = $this->contactstorage->save($object, $uid); 
     341            if (PEAR::isError($saved)) { 
     342                raise_error(array( 
     343                  'code' => 600, 'type' => 'php', 
     344                  'file' => __FILE__, 'line' => __LINE__, 
     345                  'message' => "Error saving contact object to Kolab server:" . $saved->getMessage()), 
     346                true, false); 
     347            } 
     348            else { 
     349                $this->contacts[$id] = $this->_to_rcube_contact($object); 
     350                $updated = true; 
     351            } 
     352        } 
     353         
     354        return $updated; 
     355    } 
     356 
     357    /** 
     358     * Mark one or more contact records as deleted 
     359     * 
     360     * @param array  Record identifiers 
     361     */ 
     362    public function delete($ids) 
     363    { 
     364 
     365    } 
     366 
     367    /** 
     368     * Remove all records from the database 
     369     */ 
     370    public function delete_all() 
     371    { 
     372        /* empty for read-only address books */ 
     373    } 
     374 
    254375     
    255376    /** 
     
    257378     * Called on script shutdown 
    258379     */ 
    259     function close() 
     380    public function close() 
    260381    { 
    261382        rcube_kolab::shutdown(); 
     
    336457        $out = array( 
    337458          'ID' => md5($record['uid']), 
    338           'name' => $record['full-name'], 
    339           'firstname' => $record['given-name'], 
    340           'middlename' => $record['middle-names'], 
    341           'surname' => $record['last-name'], 
    342           'prefix' => $record['prefix'], 
    343           'suffix' => $record['suffix'], 
    344           'nickname' => $record['nick-name'], 
    345           'organization' => $record['organization'], 
    346           'department' => $record['department'], 
    347           'jobtitle' => $record['job-title'], 
    348           'initials' => $record['initials'], 
    349           'birthday' => $record['birthday'], 
    350           'anniversary' => $record['anniversary'], 
    351459          'email' => array(), 
    352460          'phone' => array(), 
    353           'notes' => $record['body'], 
    354461        ); 
     462         
     463        foreach ($this->fieldmap as $kolab => $rcube) { 
     464          if (strlen($record[$kolab])) 
     465            $out[$rcube] = $record[$kolab]; 
     466        } 
    355467         
    356468        if (isset($record['gender'])) 
     
    362474        foreach ((array)$record['phone'] as $i => $phone) 
    363475            $out['phone:'.$phone['type']][] = $phone['number']; 
    364              
    365         if ($record['im-address']) 
    366             $out['im:aim'] = array($record['im-address']); 
    367         if ($record['web-page']) 
    368             $out['website'] = array($record['web-page']); 
    369  
    370         if ($record['addr-home-type']) { 
    371             $key = 'address:' . $record['addr-home-type']; 
    372             $out[$key][] = array( 
    373                 'street' => $record['addr-home-street'], 
    374                 'locality' => $record['addr-home-locality'], 
    375                 'zipcode' => $record['addr-home-postal-code'], 
    376                 'region' => $record['addr-home-region'], 
    377                 'country' => $record['addr-home-country'], 
    378             ); 
     476 
     477        if (is_array($record['address'])) { 
     478            foreach ($record['address'] as $i => $adr) { 
     479                $key = 'address:' . $adr['type']; 
     480                $out[$key][] = array( 
     481                    'street' => $adr['street'], 
     482                    'locality' => $adr['locality'], 
     483                    'zipcode' => $adr['postal-code'], 
     484                    'region' => $adr['region'], 
     485                    'country' => $adr['country'], 
     486                ); 
     487            } 
    379488        } 
    380489 
     
    385494    private function _from_rcube_contact($contact) 
    386495    { 
    387         // TBD. 
     496        $object = array(); 
     497 
     498        foreach (array_flip($this->fieldmap) as $rcube => $kolab) { 
     499            if (isset($contact[$rcube])) 
     500                $object[$kolab] = is_array($contact[$rcube]) ? $contact[$rcube][0] : $contact[$rcube]; 
     501        } 
     502 
     503        // format dates 
     504        if ($object['birthday'] && ($date = @strtotime($object['birthday']))) 
     505            $object['birthday'] = date('Y-m-d', $date); 
     506        if ($object['anniversary'] && ($date = @strtotime($object['anniversary']))) 
     507            $object['anniversary'] = date('Y-m-d', $date); 
     508 
     509        $gendermap = array_flip($this->gender_map); 
     510        if (isset($contact['gender'])) 
     511            $object['gender'] = $gendermap[$contact['gender']]; 
     512 
     513        foreach (($emails = $this->_get_col_values('email', $contact, true)) as $email) 
     514            $object['email'][] = array('smtp-address' => $email, 'display-name' => $object['full-name']); 
     515        $object['emails'] = join(', ', $emails); 
     516 
     517        foreach ($this->_get_col_values('phone', $contact) as $type => $values) { 
     518            foreach ((array)$values as $phone) 
     519                $object['phone'][] = array('number' => $phone, 'type' => $type); 
     520        } 
     521 
     522        foreach ($this->_get_col_values('address', $contact) as $type => $values) { 
     523            foreach ((array)$values as $adr) { 
     524                $object['address'][] = array( 
     525                    'type' => $type, 
     526                    'street' => $adr['street'], 
     527                    'locality' => $adr['locality'], 
     528                    'postal-code' => $adr['zipcode'], 
     529                    'region' => $adr['region'], 
     530                    'country' => $adr['country'], 
     531                ); 
     532            } 
     533        } 
     534 
     535        return $object; 
     536    } 
     537 
     538 
     539    private function _get_col_values($col, $data, $flat = false) 
     540    { 
     541        $out = array(); 
     542        foreach ($data as $c => $values) { 
     543            if (strpos($c, $col) === 0) { 
     544                if ($flat) { 
     545                    $out = array_merge($out, (array)$values); 
     546                } 
     547                else { 
     548                    list($f, $type) = explode(':', $c); 
     549                    $out[$type] = array_merge((array)$out[$type], (array)$values); 
     550                } 
     551            } 
     552        } 
     553       
     554        return $out; 
    388555    } 
    389556 
Note: See TracChangeset for help on using the changeset viewer.