From 566b142aaa7f03a8e2bbc96da9e60d7436bc76f5 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Thu, 22 Apr 2010 08:56:24 -0400
Subject: [PATCH] - last commit fix + better performance with counters caching

---
 program/include/rcube_contacts.php |   35 ++++++++++++++++++++++++++---------
 1 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/program/include/rcube_contacts.php b/program/include/rcube_contacts.php
index 897d1f4..cc610bc 100644
--- a/program/include/rcube_contacts.php
+++ b/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;
     }
   
 
@@ -165,9 +169,9 @@
         $sql_result = $this->db->limitquery(
             "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,
@@ -212,12 +216,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 +249,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);
     }
 
 
@@ -272,7 +279,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 +366,8 @@
         if ($insert_id && $this->group_id)
             $this->add_to_group($this->group_id, $insert_id);
 
+        $this->cache = null;
+
         return $insert_id;
     }
 
@@ -416,8 +428,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 +442,8 @@
             $this->user_id
         );
 
+        $this->cache = null;
+
         return $this->db->affected_rows();
     }
 
@@ -440,6 +454,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 +501,8 @@
             $gid
         );
 
+        $this->cache = null;
+
         return $this->db->affected_rows();
     }
 

--
Gitblit v1.9.1