Changeset 4205 in subversion


Ignore:
Timestamp:
Nov 9, 2010 4:04:28 PM (3 years ago)
Author:
thomasb
Message:

Little code cleanup: use static getters of rcube_kolab class to get Kolab_Storage objects

Location:
trunk/plugins/kolab_addressbook
Files:
3 edited

Legend:

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

    r4201 r4205  
    1717class kolab_addressbook extends rcube_plugin 
    1818{ 
    19     private $kolab; 
    2019    private $folders; 
    2120    private $sources; 
     
    3130        $this->add_hook('addressbooks_list', array($this, 'address_sources')); 
    3231        $this->add_hook('addressbook_get', array($this, 'get_address_book')); 
    33         $this->add_hook('imap_init', array($this, 'imap_init')); 
    3432 
    3533        // extend include path to load bundled Horde classes 
    3634        $include_path = $this->home . '/lib' . PATH_SEPARATOR . ini_get('include_path'); 
    3735        set_include_path($include_path); 
     36 
     37        // extend list of address sources to be used for autocompletion 
     38        $rcmail = rcmail::get_instance(); 
     39        if ($rcmail->action == 'autocomplete' || $rcmail->action == 'group-expand') { 
     40            $sources = (array) $rcmail->config->get('autocomplete_addressbooks', array()); 
     41            foreach ($this->_list_sources() as $abook_id => $abook) { 
     42                if (!in_array($abook_id, $sources)) 
     43                    $sources[] = $abook_id; 
     44            } 
     45            $rcmail->config->set('autocomplete_addressbooks', $sources); 
     46        } 
    3847    } 
    3948 
     
    4958    public function address_sources($p) 
    5059    { 
    51         // setup Kolab backend 
    52         rcube_kolab::setup(); 
     60        foreach ($this->_list_sources() as $abook_id => $abook) { 
     61            // register this address source 
     62            $p['sources'][$abook_id] = array( 
     63                'id' => $abook_id, 
     64                'name' => $abook->get_name(), 
     65                'readonly' => $abook->readonly, 
     66                'groups' => $abook->groups, 
     67            ); 
     68        } 
     69 
     70        return $p; 
     71    } 
     72 
     73 
     74    /** 
     75     * Getter for the rcube_addressbook instance 
     76     */ 
     77    public function get_address_book($p) 
     78    { 
     79        if ($this->sources[$p['id']]) { 
     80            $p['instance'] = $this->sources[$p['id']]; 
     81        } 
     82         
     83        return $p; 
     84    } 
     85     
     86     
     87    private function _list_sources() 
     88    { 
     89        // already read sources 
     90        if (isset($this->sources)) 
     91            return $this->sources; 
    5392 
    5493        // get all folders that have "contact" type 
    55         $this->kolab = Kolab_List::singleton(); 
    56         $this->folders = $this->kolab->getByType('contact'); 
     94        $this->folders = rcube_kolab::get_folders('contact'); 
     95        $this->sources = array(); 
    5796 
    5897        if (PEAR::isError($this->folders)) { 
     
    69108                $abook = new rcube_kolab_contacts($c_folder->name); 
    70109                $this->sources[$abook_id] = $abook; 
    71                  
    72                 // register this address source 
    73                 $p['sources'][$abook_id] = array( 
    74                     'id' => $abook_id, 
    75                     'name' => $c_folder->name, 
    76                     'readonly' => $abook->readonly, 
    77                     'groups' => $abook->groups, 
    78                 ); 
    79110            } 
    80111        } 
     112         
     113        return $this->sources; 
     114    } 
    81115 
    82         return $p; 
    83     } 
    84   
    85   
    86     /** 
    87      * Getter for the rcube_addressbook instance 
    88      */ 
    89     public function get_address_book($p) 
    90     { 
    91         if ($this->sources[$p['id']]) { 
    92             $p['instance'] = $this->sources[$p['id']]; 
    93         } 
    94          
    95         return $p; 
    96     } 
    97   
    98   
    99     /** 
    100      * Make sure the X-Kolab-Type headers are also fetched when listing messages 
    101      */ 
    102     function imap_init($p) 
    103     { 
    104         $p['fetch_headers'] = strtoupper('X-Kolab-Type'); 
    105         return $p; 
    106     } 
    107116} 
  • trunk/plugins/kolab_addressbook/lib/rcube_kolab.php

    r4134 r4205  
    11<?php 
    22 
    3 require_once 'Horde/Kolab/Format/XML.php'; 
    43require_once 'Horde/Kolab/Storage/List.php'; 
    54require_once 'Horde/Auth.php'; 
     
    1615{ 
    1716    private static $horde_auth; 
     17     
    1818     
    1919    /** 
     
    5050    } 
    5151 
     52    /** 
     53     * Get a list of storage folders for the given data type 
     54     * 
     55     * @param string Data type to list folders for (contact,event,task,note) 
     56     * @return array List of Kolab_Folder objects 
     57     */ 
     58    public static function get_folders($type) 
     59    { 
     60        self::setup(); 
     61        $kolab = Kolab_List::singleton(); 
     62        return $kolab->getByType($type); 
     63    } 
    5264 
     65    /** 
     66     * Get storage object for read/write access to the Kolab backend 
     67     * 
     68     * @param string IMAP folder to access 
     69     * @param string Object type to deal with (leave empty for auto-detection using annotations) 
     70     * @return object Kolab_Data The data storage object 
     71     */ 
     72    public static function get_storage($folder, $data_type = null) 
     73    { 
     74        self::setup(); 
     75        $kolab = Kolab_List::singleton(); 
     76        return $kolab->getFolder($folder)->getData($data_type); 
     77    } 
     78 
     79    /** 
     80     * Cleanup session data when done 
     81     */ 
     82    public static function shutdown() 
     83    { 
     84        if (isset($_SESSION['__auth'])) { 
     85            // unset auth data from session. no need to store it persistantly 
     86            unset($_SESSION['__auth']); 
     87             
     88            // FIXME: remove strange numeric entries 
     89            foreach ($_SESSION as $key => $val) { 
     90                if (!$val && is_numeric($key)) 
     91                    unset($_SESSION[$key]); 
     92            } 
     93        } 
     94    } 
    5395} 
  • trunk/plugins/kolab_addressbook/rcube_kolab_contacts.php

    r4204 r4205  
    1717    public $groups = true; 
    1818 
    19     private static $instance; 
    20      
    2119    private $gid; 
    2220    private $imap; 
     
    3937         
    4038        // fetch objects from the given IMAP folder 
    41         $this->kolab = Kolab_List::singleton(); 
    42         $this->folder = $this->kolab->getFolder($this->imap_folder); 
    43         $this->contactstorage = $this->folder->getData(); 
    44         $this->liststorage = $this->folder->getData('distributionlist'); 
     39        $this->contactstorage = rcube_kolab::get_storage($this->imap_folder); 
     40        $this->liststorage = rcube_kolab::get_storage($this->imap_folder, 'distributionlist'); 
    4541 
    4642        $this->ready = !PEAR::isError($this->contactstorage) && !PEAR::isError($this->liststorage); 
     
    4945 
    5046    /** 
     47     * Getter for the address book name to be displayed 
     48     * 
     49     * @return string Name of this address book 
     50     */ 
     51    public function get_name() 
     52    { 
     53        return strtr(preg_replace('!^(INBOX|user)/!i', '', $this->imap_folder), '/', ':'); 
     54    } 
     55 
     56 
     57    /** 
    5158     * Setter for the current group 
    5259     */ 
    53     function set_group($gid) 
     60    public function set_group($gid) 
    5461    { 
    5562        $this->gid = $gid; 
     
    133140        return $this->result; 
    134141    } 
     142 
     143 
     144    /** 
     145     * Search records 
     146     * 
     147     * @param array   List of fields to search in 
     148     * @param string  Search value 
     149     * @param boolean True if results are requested, False if count only 
     150     * @return Indexed list of contact records and 'count' value 
     151     */ 
     152    public function search($fields, $value, $strict=false, $select=true) 
     153    { 
     154        // TODO: currently not implemented, just list all records 
     155        return $this->list_records(); 
     156    } 
     157 
     158 
     159    /** 
     160     * Count number of available contacts in database 
     161     * 
     162     * @return rcube_result_set Result set with values for 'count' and 'first' 
     163     */ 
     164    public function count() 
     165    { 
     166        $this->_fetch_data(); 
     167        $count = $this->gid ? count($this->distlists[$this->gid]['member']) : count($this->contacts); 
     168        return new rcube_result_set($count, ($this->list_page-1) * $this->page_size); 
     169    } 
     170 
     171 
     172    /** 
     173     * Return the last result set 
     174     * 
     175     * @return rcube_result_set Current result set or NULL if nothing selected yet 
     176     */ 
     177    public function get_result() 
     178    { 
     179        return $this->result; 
     180    } 
     181 
     182    /** 
     183     * Get a specific contact record 
     184     * 
     185     * @param mixed record identifier(s) 
     186     * @param boolean True to return record as associative array, otherwise a result set is returned 
     187     * @return mixed Result object with all record fields or False if not found 
     188     */ 
     189    public function get_record($id, $assoc=false) 
     190    { 
     191        $this->_fetch_data(); 
     192        if ($this->contacts[$id] && $assoc) { 
     193            return $this->contacts[$id]; 
     194        } 
     195        else if ($this->contacts[$id]) { 
     196            $this->result = new rcube_result_set(1); 
     197            $this->result->add($this->contacts[$id]); 
     198            return $this->result; 
     199        } 
     200 
     201        return false; 
     202    } 
     203 
     204 
     205    /** 
     206     * Get group assignments of a specific contact record 
     207     * 
     208     * @param mixed Record identifier 
     209     * @return array List of assigned groups as ID=>Name pairs 
     210     */ 
     211    function get_record_groups($id) 
     212    { 
     213        $out = array(); 
     214         
     215        foreach ($this->distlists as $gid => $group) { 
     216            foreach ($group['member'] as $member) { 
     217                if ($member['ID'] == $id) 
     218                    $out[$gid] = $group['last-name']; 
     219            } 
     220        } 
     221         
     222        return $out; 
     223    } 
    135224     
    136225     
     226    /** 
     227     * Close connection to source 
     228     * Called on script shutdown 
     229     */ 
     230    function close() 
     231    { 
     232        rcube_kolab::shutdown(); 
     233    } 
     234 
     235 
     236    function create_group($name) 
     237    { 
     238        return false; 
     239    } 
     240 
     241    function delete_group($gid) 
     242    { 
     243        return false; 
     244    } 
     245 
     246    function rename_group($gid, $newname) 
     247    { 
     248      return $newname; 
     249    } 
     250 
     251    function add_to_group($group_id, $ids) 
     252    { 
     253        return false; 
     254    } 
     255 
     256    function remove_from_group($group_id, $ids) 
     257    { 
     258         return false; 
     259    } 
     260 
     261 
    137262    /** 
    138263     * Simply fetch all records and store them in private member vars 
     
    153278            $this->distlists = array(); 
    154279            foreach ((array)$this->liststorage->getObjects() as $record) { 
     280                // FIXME: folders without any distribution-list objects return contacts instead ?! 
     281                if ($record['__type'] != 'Group') 
     282                    continue; 
    155283                $record['ID'] = md5($record['uid']); 
    156284                foreach ($record['member'] as $i => $member) 
     
    183311    } 
    184312 
    185  
    186     /** 
    187      * Search records 
    188      * 
    189      * @param array   List of fields to search in 
    190      * @param string  Search value 
    191      * @param boolean True if results are requested, False if count only 
    192      * @return Indexed list of contact records and 'count' value 
    193      */ 
    194     public function search($fields, $value, $strict=false, $select=true) 
    195     { 
    196         // no search implemented, just list all records 
    197         return $this->list_records(); 
    198     } 
    199  
    200  
    201     /** 
    202      * Count number of available contacts in database 
    203      * 
    204      * @return rcube_result_set Result set with values for 'count' and 'first' 
    205      */ 
    206     public function count() 
    207     { 
    208         $this->_fetch_data(); 
    209         $count = $this->gid ? count($this->distlists[$this->gid]['member']) : count($this->contacts); 
    210         return new rcube_result_set($count, ($this->list_page-1) * $this->page_size); 
    211     } 
    212  
    213  
    214     /** 
    215      * Return the last result set 
    216      * 
    217      * @return rcube_result_set Current result set or NULL if nothing selected yet 
    218      */ 
    219     public function get_result() 
    220     { 
    221         return $this->result; 
    222     } 
    223  
    224     /** 
    225      * Get a specific contact record 
    226      * 
    227      * @param mixed record identifier(s) 
    228      * @param boolean True to return record as associative array, otherwise a result set is returned 
    229      * @return mixed Result object with all record fields or False if not found 
    230      */ 
    231     public function get_record($id, $assoc=false) 
    232     { 
    233         $this->_fetch_data(); 
    234         if ($this->contacts[$id]) { 
    235             $this->result = new rcube_result_set(1); 
    236             $this->result->add($this->contacts[$id]); 
    237             return $assoc ? $rec : $this->result; 
    238         } 
    239  
    240         return false; 
    241     } 
    242  
    243  
    244     /** 
    245      * Get group assignments of a specific contact record 
    246      * 
    247      * @param mixed Record identifier 
    248      * @return array List of assigned groups as ID=>Name pairs 
    249      */ 
    250     function get_record_groups($id) 
    251     { 
    252         $out = array(); 
    253          
    254         foreach ($this->distlists as $gid => $group) { 
    255             foreach ($group['member'] as $member) { 
    256                 if ($member['ID'] == $id) 
    257                     $out[$gid] = $group['last-name']; 
    258             } 
    259         } 
    260          
    261         return $out; 
    262     } 
    263      
    264      
    265     /** 
    266      * Close connection to source 
    267      * Called on script shutdown 
    268      */ 
    269     function close() 
    270     { 
    271          
    272     } 
    273  
    274  
    275     function create_group($name) 
    276     { 
    277         return false; 
    278     } 
    279  
    280     function delete_group($gid) 
    281     { 
    282         return false; 
    283     } 
    284  
    285     function rename_group($gid, $newname) 
    286     { 
    287       return $newname; 
    288     } 
    289  
    290     function add_to_group($group_id, $ids) 
    291     { 
    292         return false; 
    293     } 
    294  
    295     function remove_from_group($group_id, $ids) 
    296     { 
    297          return false; 
    298     } 
    299    
    300313} 
Note: See TracChangeset for help on using the changeset viewer.