Changeset 4267 in subversion


Ignore:
Timestamp:
Nov 24, 2010 2:33:58 PM (2 years ago)
Author:
thomasb
Message:

Implement adding to and removing from groups (distribution-lists)

File:
1 edited

Legend:

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

    r4266 r4267  
    175175        // list member of the selected group 
    176176        if ($this->gid) { 
     177            $seen = array(); 
     178            $this->result->count = 0; 
    177179            foreach ((array)$this->distlists[$this->gid]['member'] as $member) { 
    178                 $this->result->add($this->contacts[$member['ID']]); 
     180                if ($this->contacts[$member['ID']] && !$seen[$member['ID']]++) { 
     181                    $this->result->add($this->contacts[$member['ID']]); 
     182                    $this->result->count++; 
     183                } 
    179184            } 
    180185        } 
     
    217222    { 
    218223        $this->_fetch_contacts(); 
     224        $this->_fetch_groups(); 
    219225        $count = $this->gid ? count($this->distlists[$this->gid]['member']) : count($this->contacts); 
    220226        return new rcube_result_set($count, ($this->list_page-1) * $this->page_size); 
     
    304310            $object = $this->_from_rcube_contact($save_data); 
    305311            $object['uid'] = $this->contactstorage->generateUID(); 
    306             $object['creation-date'] = time(); 
    307             $object['last-modification-date'] = time(); 
    308312 
    309313            $saved = $this->contactstorage->save($object); 
     
    345349            $old = $this->contactstorage->getObject($uid); 
    346350            $object = array_merge($old, $this->_from_rcube_contact($save_data)); 
    347             $object['last-modification-date'] = time(); 
    348 console($save_data, $object); 
     351 
    349352            $saved = $this->contactstorage->save($object, $uid); 
    350353            if (PEAR::isError($saved)) { 
     
    389392                } 
    390393                else { 
     394                    // TODO: remove from distribution lists 
    391395                    unset($this->contacts[$id], $this->id2uid[$id]); 
    392396                    $count++; 
     
    403407    public function delete_all() 
    404408    { 
    405         return !PEAR::isError($this->contactstorage->deleteAll()); 
     409        if (!PEAR::isError($this->contactstorage->deleteAll())) { 
     410            $this->contacts = array(); 
     411            $this->id2uid = array(); 
     412            $this->result = null; 
     413        } 
    406414    } 
    407415 
     
    417425 
    418426 
     427    /** 
     428     * Create a contact group with the given name 
     429     * 
     430     * @param string The group name 
     431     * @return mixed False on error, array with record props in success 
     432     */ 
    419433    function create_group($name) 
    420434    { 
     435        // TODO: implement this 
    421436        return false; 
    422437    } 
    423438 
     439    /** 
     440     * Delete the given group and all linked group members 
     441     * 
     442     * @param string Group identifier 
     443     * @return boolean True on success, false if no data was changed 
     444     */ 
    424445    function delete_group($gid) 
    425446    { 
     447        // TODO: implement this 
    426448        return false; 
    427449    } 
    428450 
     451    /** 
     452     * Rename a specific contact group 
     453     * 
     454     * @param string Group identifier 
     455     * @param string New name to set for this group 
     456     * @return boolean New name on success, false if no data was changed 
     457     */ 
    429458    function rename_group($gid, $newname) 
    430459    { 
    431       return $newname; 
    432     } 
    433  
    434     function add_to_group($group_id, $ids) 
    435     { 
     460        $this->_fetch_groups(); 
     461        $list = $this->distlists[$gid]; 
     462         
     463        if ($newname != $list['last-name']) { 
     464            $list['last-name'] = $newname; 
     465            $saved = $this->liststorage->save($list, $list['uid']); 
     466        } 
     467 
     468        if (PEAR::isError($saved)) { 
     469            raise_error(array( 
     470              'code' => 600, 'type' => 'php', 
     471              'file' => __FILE__, 'line' => __LINE__, 
     472              'message' => "Error saving distribution-list object to Kolab server:" . $saved->getMessage()), 
     473            true, false); 
     474            return false; 
     475        } 
     476 
     477        return $newname; 
     478    } 
     479 
     480    /** 
     481     * Add the given contact records the a certain group 
     482     * 
     483     * @param string  Group identifier 
     484     * @param array   List of contact identifiers to be added 
     485     * @return int    Number of contacts added 
     486     */ 
     487    function add_to_group($gid, $ids) 
     488    { 
     489        if (!is_array($ids)) 
     490            $ids = explode(',', $ids); 
     491 
     492        $added = 0; 
     493        $exists = array(); 
     494         
     495        $this->_fetch_groups(); 
     496        $this->_fetch_contacts(); 
     497        $list = $this->distlists[$gid]; 
     498 
     499        foreach ($list['member'] as $i => $member) 
     500            $exists[] = $member['ID']; 
     501             
     502        // substract existing assignments from list 
     503        $ids = array_diff($ids, $exists); 
     504         
     505        foreach ($ids as $contact_id) { 
     506            if ($uid = $this->id2uid[$contact_id]) { 
     507                $contact = $this->contacts[$contact_id]; 
     508                foreach ($this->get_col_values('email', $contact, true) as $email) { 
     509                    $list['member'][] = array( 
     510                        'uid' => $uid, 
     511                        'display-name' => $contact['name'], 
     512                        'smtp-address' => $email, 
     513                    ); 
     514                } 
     515                $added++; 
     516            } 
     517        } 
     518         
     519        if ($added) 
     520            $saved = $this->liststorage->save($list, $list['uid']); 
     521         
     522        if (PEAR::isError($saved)) { 
     523            raise_error(array( 
     524              'code' => 600, 'type' => 'php', 
     525              'file' => __FILE__, 'line' => __LINE__, 
     526              'message' => "Error saving distribution-list to Kolab server:" . $saved->getMessage()), 
     527            true, false); 
     528            $added = false; 
     529        } 
     530        else { 
     531            $this->distlists[$gid] = $list; 
     532        } 
     533         
     534        return $added; 
     535    } 
     536 
     537    /** 
     538     * Remove the given contact records from a certain group 
     539     * 
     540     * @param string  Group identifier 
     541     * @param array   List of contact identifiers to be removed 
     542     * @return int    Number of deleted group members 
     543     */ 
     544    function remove_from_group($gid, $ids) 
     545    { 
     546        if (!is_array($ids)) 
     547            $ids = explode(',', $ids); 
     548         
     549        $this->_fetch_groups(); 
     550        if (!($list = $this->distlists[$gid])) 
     551            return false; 
     552 
     553        $new_member = array(); 
     554        foreach ((array)$list['member'] as $member) { 
     555            if (!in_array($member['ID'], $ids)) 
     556                $new_member[] = $member; 
     557        } 
     558 
     559        // write distribution list back to server 
     560        $list['member'] = $new_member; 
     561        $saved = $this->liststorage->save($list, $list['uid']); 
     562         
     563        if (PEAR::isError($saved)) { 
     564            raise_error(array( 
     565              'code' => 600, 'type' => 'php', 
     566              'file' => __FILE__, 'line' => __LINE__, 
     567              'message' => "Error saving distribution-list object to Kolab server:" . $saved->getMessage()), 
     568            true, false); 
     569        } 
     570        else { 
     571            $this->distlists[$gid] = $list; 
     572            return true; 
     573        } 
     574 
    436575        return false; 
    437     } 
    438  
    439     function remove_from_group($group_id, $ids) 
    440     { 
    441          return false; 
    442576    } 
    443577 
Note: See TracChangeset for help on using the changeset viewer.