Changeset c0297f4 in github


Ignore:
Timestamp:
Mar 31, 2010 11:23:22 AM (3 years ago)
Author:
thomascube <thomas@…>
Branches:
master, HEAD, courier-fix, dev-browser-capabilities, pdo, release-0.6, release-0.7, release-0.8
Children:
64d855c
Parents:
3baa72a
Message:

Asynchronously expand contact groups + skip count queries in autocompletion mode + check for the existance of contactgroups table

Files:
6 edited

Legend:

Unmodified
Added
Removed
  • index.php

    r3baa72a rc0297f4  
    214214    'display-attachment' => 'attachments.inc', 
    215215    'upload' => 'attachments.inc', 
     216    'group-expand' => 'autocomplete.inc', 
    216217  ), 
    217218   
  • program/include/rcmail.php

    r59c216f rc0297f4  
    299299    // We are using the DB address book 
    300300    if ($abook_type != 'ldap') { 
     301      $contacts = new rcube_contacts($this->db, null); 
    301302      $list['0'] = array( 
    302303        'id' => 0, 
    303304        'name' => rcube_label('personaladrbook'), 
    304         'groups' => true, 
     305        'groups' => $contacts->groups, 
    305306        'readonly' => false, 
    306307        'autocomplete' => in_array('sql', $autocomplete) 
     
    325326      foreach ($list as $idx => $item) { 
    326327        if ($item['readonly']) { 
    327           unset($list[$idx]); 
     328          unset($list[$idx]); 
    328329        } 
    329330      } 
  • program/include/rcube_contacts.php

    r3baa72a rc0297f4  
    4040  var $primary_key = 'contact_id'; 
    4141  var $readonly = false; 
    42   var $groups = true; 
     42  var $groups = false; 
    4343  var $list_page = 1; 
    4444  var $page_size = 10; 
     
    5959    $this->user_id = $user; 
    6060    $this->ready = $this->db && !$this->db->is_error(); 
     61     
     62    if (in_array('contactgroups', $this->db->list_tables())) 
     63      $this->groups = true; 
    6164  } 
    6265 
     
    114117  { 
    115118    $results = array(); 
     119     
     120    if (!$this->groups) 
     121      return $results; 
     122       
    116123    $sql_filter = $search ? "AND " . $this->db->ilike('name', '%'.$search.'%') : ''; 
    117124 
     
    135142   * List the current set of contact records 
    136143   * 
    137    * @param  array  List of cols to show 
    138    * @param  int    Only return this number of records, use negative values for tail 
     144   * @param  array   List of cols to show 
     145   * @param  int     Only return this number of records, use negative values for tail 
     146   * @param  boolean True to skip the count query (select only) 
    139147   * @return array  Indexed list of contact records, each a hash array 
    140148   */ 
    141   function list_records($cols=null, $subset=0) 
     149  function list_records($cols=null, $subset=0, $nocount=false) 
    142150  { 
    143151    // count contacts for this user 
    144     $this->result = $this->count(); 
     152    $this->result = $nocount ? new rcube_result_set(1) : $this->count(); 
    145153    $sql_result = NULL; 
    146154 
     
    149157    { 
    150158      if ($this->group_id) 
    151         $join = "LEFT JOIN ".get_table_name('contactgroupmembers')." AS rcmgrouplinks". 
    152           " ON (rcmgrouplinks.contact_id=rcmcontacts.".$this->primary_key.")"; 
     159        $join = "LEFT JOIN ".get_table_name('contactgroupmembers')." AS m". 
     160          " ON (m.contact_id=c.".$this->primary_key.")"; 
    153161       
    154162      $start_row = $subset < 0 ? $this->result->first + $this->page_size + $subset : $this->result->first; 
     
    156164       
    157165      $sql_result = $this->db->limitquery( 
    158         "SELECT * FROM ".$this->db_name." AS rcmcontacts ".$join." 
    159          WHERE  rcmcontacts.del<>1 
    160          AND    rcmcontacts.user_id=?" . 
    161         ($this->group_id ? " AND rcmgrouplinks.contactgroup_id=?" : ""). 
     166        "SELECT * FROM ".$this->db_name." AS c ".$join." 
     167         WHERE  c.del<>1 
     168         AND    c.user_id=?" . 
     169        ($this->group_id ? " AND m.contactgroup_id=?" : ""). 
    162170        ($this->filter ? " AND (".$this->filter.")" : "") . 
    163         " ORDER BY rcmcontacts.name", 
     171        " ORDER BY c.name", 
    164172        $start_row, 
    165173        $length, 
     
    177185    } 
    178186     
     187    if ($nocount) 
     188      $this->result->count = count($this->result->records); 
     189     
    179190    return $this->result; 
    180191  } 
     
    188199   * @param boolean True for strict (=), False for partial (LIKE) matching 
    189200   * @param boolean True if results are requested, False if count only 
     201   * @param boolean True to skip the count query (select only) 
    190202   * @return Indexed list of contact records and 'count' value 
    191203   */ 
    192   function search($fields, $value, $strict=false, $select=true) 
     204  function search($fields, $value, $strict=false, $select=true, $nocount=false) 
    193205  { 
    194206    if (!is_array($fields)) 
     
    213225      $this->set_search_set(join(' OR ', $add_where)); 
    214226      if ($select) 
    215         $this->list_records(); 
     227        $this->list_records(null, 0, $nocount); 
    216228      else 
    217229        $this->result = $this->count(); 
     
    230242  { 
    231243    if ($this->group_id) 
    232       $join = "LEFT JOIN ".get_table_name('contactgroupmembers')." AS rcmgrouplinks". 
    233         " ON (rcmgrouplinks.contact_id=rcmcontacts.".$this->primary_key.")"; 
     244      $join = "LEFT JOIN ".get_table_name('contactgroupmembers')." AS m". 
     245        " ON (m.contact_id=c.".$this->primary_key.")"; 
    234246       
    235247    // count contacts for this user 
    236248    $sql_result = $this->db->query( 
    237       "SELECT COUNT(rcmcontacts.contact_id) AS rows 
    238        FROM ".$this->db_name." AS rcmcontacts ".$join." 
    239        WHERE  rcmcontacts.del<>1 
    240        AND    rcmcontacts.user_id=?". 
    241        ($this->group_id ? " AND rcmgrouplinks.contactgroup_id=?" : ""). 
     249      "SELECT COUNT(c.contact_id) AS rows 
     250       FROM ".$this->db_name." AS c ".$join." 
     251       WHERE  c.del<>1 
     252       AND    c.user_id=?". 
     253       ($this->group_id ? " AND m.contactgroup_id=?" : ""). 
    242254       ($this->filter ? " AND (".$this->filter.")" : ""), 
    243255      $this->user_id, 
  • program/include/rcube_mdb2.php

    re1ac217 rc0297f4  
    3636class rcube_mdb2 
    3737  { 
     38  private static $tables; 
     39   
    3840  var $db_dsnw;               // DSN for write operations 
    3941  var $db_dsnr;               // DSN for read operations 
     
    268270 
    269271        raise_error(array('code' => 500, 'type' => 'db', 
    270           'line' => __LINE__, 'file' => __FILE__, 
     272          'line' => __LINE__, 'file' => __FILE__, 
    271273          'message' => $this->db_error_msg), TRUE, TRUE); 
    272274        } 
     
    392394    return $result->fetchRow($mode); 
    393395    } 
     396 
     397 
     398  /** 
     399   * Wrapper for the SHOW TABLES command 
     400   * 
     401   * @return array List of all tables of the current database 
     402   */ 
     403  function list_tables() 
     404  { 
     405    // get tables if not cached 
     406    if (!self::$tables) { 
     407      self::$tables = array(); 
     408 
     409      switch ($this->db_provider) { 
     410        case 'sqlite': 
     411          $result = $this->db_handle->query("SELECT name FROM sqlite_master WHERE type='table'"); 
     412          break; 
     413        default: 
     414          $result = $this->db_handle->query("SHOW TABLES"); 
     415      } 
     416 
     417      if ($result !== false && !PEAR::isError($result)) 
     418        while ($rec = $result->fetchRow(MDB2_FETCHMODE_ORDERED)) 
     419          self::$tables[] = $rec[0]; 
     420    } 
     421 
     422    return self::$tables; 
     423  } 
    394424 
    395425 
  • program/js/app.js

    r3baa72a rc0297f4  
    32393239     
    32403240    // insert all members of a group 
    3241     if (typeof this.env.contacts[id] == 'object' && this.env.contacts[id].members) { 
    3242       for (var i=0; i < this.env.contacts[id].members.length; i++) 
    3243         insert += this.env.contacts[id].members[i] + ', '; 
     3241    if (typeof this.env.contacts[id] == 'object' && this.env.contacts[id].id) { 
     3242      insert += this.env.contacts[id].name + ', '; 
     3243      this.group2expand = $.extend({}, this.env.contacts[id]); 
     3244      this.group2expand.input = this.ksearch_input; 
     3245      this.http_request('group-expand', '_source='+urlencode(this.env.contacts[id].source)+'&_gid='+urlencode(this.env.contacts[id].id), false); 
    32443246    } 
    32453247    else if (typeof this.env.contacts[id] == 'string') 
     
    32523254    if (this.ksearch_input.setSelectionRange) 
    32533255      this.ksearch_input.setSelectionRange(cpos, cpos); 
     3256  }; 
     3257   
     3258  this.replace_group_recipients = function(id, recipients) 
     3259  { 
     3260    if (this.group2expand && this.group2expand.id == id) { 
     3261      this.group2expand.input.value = this.group2expand.input.value.replace(this.group2expand.name, recipients); 
     3262      this.group2expand = null; 
     3263    } 
    32543264  }; 
    32553265 
  • program/steps/mail/autocomplete.inc

    ra61bbb2 rc0297f4  
    2121 
    2222$MAXNUM = 15; 
    23 $contacts = array(); 
    2423$book_types = (array) $RCMAIL->config->get('autocomplete_addressbooks', 'sql'); 
    2524 
    26 if ($book_types && $search = get_input_value('_search', RCUBE_INPUT_GPC, true)) { 
     25if ($RCMAIL->action == 'group-expand') { 
     26  $abook = $RCMAIL->get_address_book(get_input_value('_source', RCUBE_INPUT_GPC)); 
     27  if ($gid = get_input_value('_gid', RCUBE_INPUT_GPC)) { 
     28    $members = array(); 
     29    $abook->set_pagesize(1000);  // TODO: limit number of group members by config 
     30    $result = $abook->list_records(array('email','name')); 
     31    while ($result && ($sql_arr = $result->iterate())) 
     32      $members[] = format_email_recipient($sql_arr['email'], $sql_arr['name']); 
     33       
     34    $OUTPUT->command('replace_group_recipients', $gid, join(', ', $members)); 
     35  } 
     36} 
     37else if ($book_types && $search = get_input_value('_search', RCUBE_INPUT_GPC, true)) { 
     38  $contacts = array(); 
    2739 
    2840  foreach ($book_types as $id) { 
     
    3042    $abook->set_pagesize($MAXNUM); 
    3143 
    32     if ($result = $abook->search(array('email','name'), $search)) { 
     44    if ($result = $abook->search(array('email','name'), $search, false, true, true)) { 
    3345      while ($sql_arr = $result->iterate()) { 
    3446          $contacts[] = format_email_recipient($sql_arr['email'], $sql_arr['name']); 
     
    4153    if ($abook->groups) { 
    4254      foreach ($abook->list_groups($search) as $group) { 
    43         $members = array(); 
    4455        $abook->reset(); 
    4556        $abook->set_group($group['ID']); 
    46         $result = $abook->list_records(array('email','name')); 
    47         while ($result && ($sql_arr = $result->iterate())) 
    48           $members[] = format_email_recipient($sql_arr['email'], $sql_arr['name']); 
     57        $result = $abook->count(); 
    4958         
    50         if (count($members)) { 
    51           $contacts[] = array('name' => $group['name'] . ' (' . rcube_label('group') . ')', 'members' => $members); 
     59        if ($result->count) { 
     60          $contacts[] = array('name' => $group['name'] . ' (' . intval($result->count) . ')', 'id' => $group['ID'], 'source' => $id); 
    5261          if (count($contacts) >= $MAXNUM) 
    5362            break; 
Note: See TracChangeset for help on using the changeset viewer.