alecpl
2010-04-23 bf67d60e435a6f01b835a0d5afb9737f81560433
program/include/rcube_contacts.php
@@ -34,6 +34,7 @@
    private $result = null;
    private $search_fields;
    private $search_string;
    private $cache;
    private $table_cols = array('name', 'email', 'firstname', 'surname', 'vcard');
  
    /** public properties */
@@ -69,6 +70,7 @@
    function set_search_set($filter)
    {
        $this->filter = $filter;
        $this->cache = null;
    }
  
@@ -90,6 +92,7 @@
    function set_group($gid)
    {
        $this->group_id = $gid;
        $this->cache = null;
    }
@@ -102,6 +105,7 @@
        $this->filter = null;
        $this->search_fields = null;
        $this->search_string = null;
        $this->cache = null;
    }
  
@@ -118,7 +122,7 @@
        if (!$this->groups)
            return $results;
        $sql_filter = $search ? "AND " . $this->db->ilike('name', '%'.$search.'%') : '';
        $sql_filter = $search ? " AND " . $this->db->ilike('name', '%'.$search.'%') : '';
        $sql_result = $this->db->query(
            "SELECT * FROM ".get_table_name('contactgroups').
@@ -159,15 +163,16 @@
        $length = $subset != 0 ? abs($subset) : $this->page_size;
        if ($this->group_id)
            $join = "LEFT JOIN ".get_table_name('contactgroupmembers')." AS m".
            $join = " LEFT JOIN ".get_table_name('contactgroupmembers')." AS m".
                " ON (m.contact_id = c.".$this->primary_key.")";
        $sql_result = $this->db->limitquery(
            "SELECT * FROM ".$this->db_name." AS c ".$join .
            "SELECT * FROM ".$this->db_name." AS c" .
            $join .
            " WHERE c.del<>1" .
            " AND c.user_id=?" .
            ($this->group_id ? " AND m.contactgroup_id=?" : "").
            ($this->filter ? " AND (".$this->filter.")" : "") .
                " AND c.user_id=?" .
                ($this->group_id ? " AND m.contactgroup_id=?" : "").
                ($this->filter ? " AND (".$this->filter.")" : "") .
            " ORDER BY c.name",
            $start_row,
            $length,
@@ -190,6 +195,8 @@
        else if ($this->list_page <= 1) {
            if ($cnt < $this->page_size && $subset == 0)
                $this->result->count = $cnt;
            else if (isset($this->cache['count']))
                $this->result->count = $this->cache['count'];
            else
                $this->result->count = $this->_count();
        }
@@ -212,12 +219,13 @@
    {
        if (!is_array($fields))
            $fields = array($fields);
        $add_where = array();
        foreach ($fields as $col) {
            if ($col == 'ID' || $col == $this->primary_key) {
                $ids         = !is_array($value) ? explode(',', $value) : $value;
                $add_where[] = 'c.' . $this->primary_key.' IN ('.join(',', $ids).')';
                $ids         = join(',', array_map(array($this->db, 'quote'), $ids));
                $add_where[] = 'c.' . $this->primary_key.' IN ('.$ids.')';
            }
            else if ($strict)
                $add_where[] = $this->db->quoteIdentifier($col).'='.$this->db->quote($value);
@@ -244,7 +252,9 @@
     */
    function count()
    {
        return new rcube_result_set($this->_count(), ($this->list_page-1) * $this->page_size);
        $count = isset($this->cache['count']) ? $this->cache['count'] : $this->_count();
        return new rcube_result_set($count, ($this->list_page-1) * $this->page_size);
    }
@@ -256,14 +266,15 @@
    private function _count()
    {
        if ($this->group_id)
            $join = "LEFT JOIN ".get_table_name('contactgroupmembers')." AS m".
            $join = " LEFT JOIN ".get_table_name('contactgroupmembers')." AS m".
                " ON (m.contact_id=c.".$this->primary_key.")";
      
        // count contacts for this user
        $sql_result = $this->db->query(
            "SELECT COUNT(c.contact_id) AS rows".
            " FROM ".$this->db_name." AS c ".$join.
            " WHERE  c.del<>1".
            " FROM ".$this->db_name." AS c".
                $join.
            " WHERE c.del<>1".
            " AND c.user_id=?".
            ($this->group_id ? " AND m.contactgroup_id=?" : "").
            ($this->filter ? " AND (".$this->filter.")" : ""),
@@ -272,7 +283,10 @@
        );
        $sql_arr = $this->db->fetch_assoc($sql_result);
        return (int) $sql_arr['rows'];
        $this->cache['count'] = (int) $sql_arr['rows'];
        return $this->cache['count'];
    }
@@ -356,6 +370,8 @@
        if ($insert_id && $this->group_id)
            $this->add_to_group($this->group_id, $insert_id);
        $this->cache = null;
        return $insert_id;
    }
@@ -416,8 +432,8 @@
     */
    function delete($ids)
    {
        if (is_array($ids))
            $ids = join(',', $ids);
        if (!is_array($ids))
            $ids = explode(',', $ids);
        $ids = join(',', array_map(array($this->db, 'quote'), $ids));
@@ -430,6 +446,8 @@
            $this->user_id
        );
        $this->cache = null;
        return $this->db->affected_rows();
    }
@@ -440,6 +458,7 @@
    function delete_all()
    {
        $this->db->query("DELETE FROM {$this->db_name} WHERE user_id=?", $this->user_id);
        $this->cache = null;
        return $this->db->affected_rows();
    }
@@ -486,6 +505,8 @@
            $gid
        );
        $this->cache = null;
        return $this->db->affected_rows();
    }