Changeset 4266 in subversion


Ignore:
Timestamp:
Nov 24, 2010 1:50:29 PM (3 years ago)
Author:
thomasb
Message:

Implement insert/delete of Kolab contacts

File:
1 edited

Legend:

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

    r4251 r4266  
    3333      'im'           => array('limit' => 1), 
    3434      'website'      => array('limit' => 1, 'subtypes' => null), 
    35       'address'      => array('limit' => 2, 'subtypes' => array('home','work')), 
     35      'address'      => array('limit' => 2, 'subtypes' => array('home','business')), 
    3636      'notes'        => array(), 
    3737      // define additional coltypes 
     
    5454    private $imap_folder = 'INBOX/Contacts'; 
    5555    private $gender_map = array(0 => 'male', 1 => 'female'); 
     56    private $phonetypemap = array('home' => 'home1', 'work' => 'business1', 'work2' => 'business2', 'workfax' => 'businessfax'); 
     57    private $addresstypemap = array('work' => 'business'); 
    5658    private $fieldmap = array( 
    5759      // kolab       => roundcube 
     
    8385        $format = rcube_kolab::get_format('contact'); 
    8486        $this->coltypes['phone']['subtypes'] = $format->_phone_types; 
     87        $this->coltypes['address']['subtypes'] = $format->_address_types; 
    8588        $this->coltypes['anniversary']['label'] = rcube_label('anniversary'); 
    8689         
     
    201204    public function search($fields, $value, $strict=false, $select=true) 
    202205    { 
    203         // TODO: currently not implemented, just list all records 
    204         return $this->list_records(); 
     206        // TODO: currently not implemented 
     207        return new rcube_result_set(0, ($this->list_page-1) * $this->page_size); 
    205208    } 
    206209 
     
    282285    public function insert($save_data, $check=false) 
    283286    { 
    284         if (is_object($save_data) && is_a($save_data, rcube_result_set)) 
    285             return $this->insert_recset($save_data, $check); 
     287        if (!is_array($save_data)) 
     288            return false; 
    286289 
    287290        $insert_id = $existing = false; 
     
    289292        // check for existing records by e-mail comparison 
    290293        if ($check) { 
    291             foreach ($this->_get_col_values('email', $save_data, true) as $email) { 
    292                 if ($existing = $this->search('email', $email, true, false)) 
     294            foreach ($this->get_col_values('email', $save_data, true) as $email) { 
     295                if (($res = $this->search('email', $email, true, false)) && $res->count) { 
     296                    $existing = true; 
    293297                    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          
     298                } 
     299            } 
     300        } 
     301         
     302        if (!$existing) { 
     303            // generate new Kolab contact item 
     304            $object = $this->_from_rcube_contact($save_data); 
     305            $object['uid'] = $this->contactstorage->generateUID(); 
     306            $object['creation-date'] = time(); 
     307            $object['last-modification-date'] = time(); 
     308 
     309            $saved = $this->contactstorage->save($object); 
     310 
     311            if (PEAR::isError($saved)) { 
     312                raise_error(array( 
     313                  'code' => 600, 'type' => 'php', 
     314                  'file' => __FILE__, 'line' => __LINE__, 
     315                  'message' => "Error saving contact object to Kolab server:" . $saved->getMessage()), 
     316                true, false); 
     317            } 
     318            else { 
     319                $contact = $this->_to_rcube_contact($object); 
     320                $id = $contact['ID']; 
     321                $this->contacts[$id] = $contact; 
     322                $this->id2uid[$id] = $object['uid']; 
     323                $insert_id = $id; 
     324            } 
     325        } 
    302326         
    303327        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; 
    319328    } 
    320329 
     
    337346            $object = array_merge($old, $this->_from_rcube_contact($save_data)); 
    338347            $object['last-modification-date'] = time(); 
    339  
     348console($save_data, $object); 
    340349            $saved = $this->contactstorage->save($object, $uid); 
    341350            if (PEAR::isError($saved)) { 
     
    362371    public function delete($ids) 
    363372    { 
    364  
     373        $this->_fetch_contacts(); 
     374         
     375        if (!is_array($ids)) 
     376            $ids = explode(',', $ids); 
     377 
     378        $count = 0; 
     379        foreach ($ids as $id) { 
     380            if ($uid = $this->id2uid[$id]) { 
     381                $deleted = $this->contactstorage->delete($uid); 
     382 
     383                if (PEAR::isError($deleted)) { 
     384                    raise_error(array( 
     385                      'code' => 600, 'type' => 'php', 
     386                      'file' => __FILE__, 'line' => __LINE__, 
     387                      'message' => "Error deleting a contact object from the Kolab server:" . $deleted->getMessage()), 
     388                    true, false); 
     389                } 
     390                else { 
     391                    unset($this->contacts[$id], $this->id2uid[$id]); 
     392                    $count++; 
     393                } 
     394            } 
     395        } 
     396         
     397        return $count; 
    365398    } 
    366399 
     
    370403    public function delete_all() 
    371404    { 
    372         /* empty for read-only address books */ 
     405        return !PEAR::isError($this->contactstorage->deleteAll()); 
    373406    } 
    374407 
     
    499532            if (isset($contact[$rcube])) 
    500533                $object[$kolab] = is_array($contact[$rcube]) ? $contact[$rcube][0] : $contact[$rcube]; 
     534            else if ($rcube .= ':home' && isset($contact[$rcube])) 
     535                $object[$kolab] = is_array($contact[$rcube]) ? $contact[$rcube][0] : $contact[$rcube]; 
    501536        } 
    502537 
     
    511546            $object['gender'] = $gendermap[$contact['gender']]; 
    512547 
    513         foreach (($emails = $this->_get_col_values('email', $contact, true)) as $email) 
    514             $object['email'][] = array('smtp-address' => $email, 'display-name' => $object['full-name']); 
     548        $emails = $this->get_col_values('email', $contact, true); 
    515549        $object['emails'] = join(', ', $emails); 
    516550 
    517         foreach ($this->_get_col_values('phone', $contact) as $type => $values) { 
     551        foreach ($this->get_col_values('phone', $contact) as $type => $values) { 
     552            if ($this->phonetypemap[$type]) 
     553                $type = $this->phonetypemap[$type]; 
    518554            foreach ((array)$values as $phone) 
    519555                $object['phone'][] = array('number' => $phone, 'type' => $type); 
    520556        } 
    521557 
    522         foreach ($this->_get_col_values('address', $contact) as $type => $values) { 
     558        foreach ($this->get_col_values('address', $contact) as $type => $values) { 
     559            if ($this->addresstypemap[$type]) 
     560                $type = $this->addresstypemap[$type]; 
     561             
     562            $basekey = 'addr-' . $type . '-'; 
    523563            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); 
     564                // switch type if slot is already taken 
     565                if (isset($object[$basekey . 'type'])) { 
     566                    $type = $type == 'home' ? 'business' : 'home'; 
     567                    $basekey = 'addr-' . $type . '-'; 
     568                } 
     569                 
     570                if (!isset($object[$basekey . 'type'])) { 
     571                    $object[$basekey . 'type'] = $type; 
     572                    $object[$basekey . 'street'] = $adr['street']; 
     573                    $object[$basekey . 'locality'] = $adr['locality']; 
     574                    $object[$basekey . 'postal-code'] = $adr['zipcode']; 
     575                    $object[$basekey . 'region'] = $adr['region']; 
     576                    $object[$basekey . 'country'] = $adr['country']; 
    546577                } 
    547578                else { 
    548                     list($f, $type) = explode(':', $c); 
    549                     $out[$type] = array_merge((array)$out[$type], (array)$values); 
     579                    $object['address'][] = array( 
     580                        'type' => $type, 
     581                        'street' => $adr['street'], 
     582                        'locality' => $adr['locality'], 
     583                        'postal-code' => $adr['zipcode'], 
     584                        'region' => $adr['region'], 
     585                        'country' => $adr['country'], 
     586                    ); 
    550587                } 
    551588            } 
    552589        } 
    553        
    554         return $out; 
     590 
     591        return $object; 
    555592    } 
    556593 
Note: See TracChangeset for help on using the changeset viewer.