Turba cleanups while tracking down a bug.
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 26 Aug 2010 17:30:22 +0000 (11:30 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 26 Aug 2010 17:46:16 +0000 (11:46 -0600)
Some H4 conversions.
Implement Countable interface on several objects.

14 files changed:
turba/add.php
turba/contact.php
turba/data.php
turba/lib/Api.php
turba/lib/Driver.php
turba/lib/List.php
turba/lib/LoginTasks/SystemTask/UpgradeLists.php
turba/lib/Object.php
turba/lib/View/Browse.php
turba/lib/View/Duplicates.php
turba/lib/View/List.php
turba/lib/tests/KolabTest.php
turba/templates/list/alphaPager.inc
turba/templates/search/duplicate/list.html.php

index 4412c17..2688b85 100644 (file)
@@ -40,7 +40,7 @@ if ($source) {
         /* Check permissions. */
         $max_contacts = Turba::getExtendedPermission($driver, 'max_contacts');
         if ($max_contacts !== true &&
-            $max_contacts <= $driver->count()) {
+            $max_contacts <= count($driver)) {
             try {
                 $message = Horde::callHook('perms_denied', array('turba:max_contacts'));
             } catch (Horde_Exception_HookNotSet $e) {
index 2b52407..dfa8959 100644 (file)
@@ -31,7 +31,7 @@ $contact = null;
 $uid = $vars->get('uid');
 if (!empty($uid)) {
     $search = $driver->search(array('__uid' => $uid));
-    if (!($search instanceof PEAR_Error) && $search->count()) {
+    if (!($search instanceof PEAR_Error) && count($search)) {
         $contact = $search->next();
         $vars->set('key', $contact->getValue('__key'));
     }
index 04a8f3c..166686a 100644 (file)
@@ -347,7 +347,7 @@ case Horde_Data::IMPORT_FILE:
     /* Check permissions. */
     $max_contacts = Turba::getExtendedPermission($driver, 'max_contacts');
     if ($max_contacts !== true &&
-        $max_contacts <= $driver->count()) {
+        $max_contacts <= count($driver)) {
         try {
             $message = Horde::callHook('perms_denied', array('turba:max_contacts'));
         } catch (Horde_Exception_HookNotSet $e) {
@@ -449,7 +449,7 @@ if (is_array($next_step)) {
                 $notification->push($result, 'horde.error');
                 $error = true;
                 break;
-            } elseif ($result->count()) {
+            } elseif (count($result)) {
                 $result->reset();
                 $object = $result->next();
                 $notification->push(sprintf(_("\"%s\" already exists and was not imported."),
index 46a877c..2a53bf2 100644 (file)
@@ -679,7 +679,7 @@ class Turba_Api extends Horde_Registry_Api
                             $result = $driver->search($content);
                             if ($result instanceof PEAR_Error) {
                                 throw new Horde_Exception($result->getMessage());
-                            } elseif ($result->count() > 0) {
+                            } elseif (count($result)) {
                                 continue;
                             }
                             $result = $driver->add($content);
@@ -715,7 +715,7 @@ class Turba_Api extends Horde_Registry_Api
         $result = $driver->search($content);
         if ($result instanceof PEAR_Error) {
             throw new Horde_Exception($result->getMessage());
-        } elseif ($result->count() > 0) {
+        } elseif (count($result)) {
             $o = $result->objects[0];
             throw new Horde_Exception(_("Already Exists"));
         }
@@ -793,9 +793,9 @@ class Turba_Api extends Horde_Registry_Api
             $result = $driver->search(array('__uid' => $uid));
             if ($result instanceof PEAR_Error) {
                 throw new Horde_Exception($result->getMessage());
-            } elseif ($result->count() == 0) {
+            } elseif (count($result) == 0) {
                 continue;
-            } elseif ($result->count() > 1) {
+            } elseif (count($result) > 1) {
                 throw new Horde_Exception("Internal Horde Error: multiple turba objects with same objectId.");
             }
 
@@ -986,7 +986,7 @@ class Turba_Api extends Horde_Registry_Api
             $result = $driver->search(array('__uid' => $uid));
             if ($result instanceof PEAR_Error) {
                 throw new Horde_Exception($result->getMessage());
-            } elseif ($result->count() == 0) {
+            } elseif (count($result) == 0) {
                 continue;
             } else {
                 $r = $result->objects[0];
@@ -1049,9 +1049,9 @@ class Turba_Api extends Horde_Registry_Api
             $result = $driver->search(array('__uid' => $uid));
             if ($result instanceof PEAR_Error) {
                 throw new Horde_Exception($result->getMessage());
-            } elseif (!$result->count()) {
+            } elseif (!count($result)) {
                 continue;
-            } elseif ($result->count() > 1) {
+            } elseif (count($result) > 1) {
                 throw new Horde_Exception(_("Multiple contacts found with same unique ID."));
             }
 
@@ -1267,7 +1267,7 @@ class Turba_Api extends Horde_Registry_Api
                         if (!($members instanceof Turba_List)) {
                             continue;
                         }
-                        if ($members->count() > 0) {
+                        if (count($members)) {
                             if (!isset($results[$name])) {
                                 $results[$name] = array();
                             }
@@ -1645,13 +1645,13 @@ class Turba_Api extends Horde_Registry_Api
             throw new Horde_Exception(sprintf(_("Search failed: %s"), $res->getMessage()));
         }
 
-        if ($res->count() > 1) {
+        if (count($res) > 1) {
             $res2 = $driver->search(array('email' => trim($address), 'name' => trim($name)), null, 'AND');
             if ($res2 instanceof PEAR_Error) {
                 throw new Horde_Exception(sprintf(_("Search failed: %s"), $res2->getMessage()));
             }
 
-            if (!$res2->count()) {
+            if (!count($res2)) {
                 throw new Horde_Exception(sprintf(_("Multiple persons with address [%s], but none with name [%s] already exist"), trim($address), trim($name)));
             }
 
@@ -1660,20 +1660,20 @@ class Turba_Api extends Horde_Registry_Api
                 throw new Horde_Exception(sprintf(_("Search failed: %s"), $res3->getMessage()));
             }
 
-            if ($res3->count()) {
+            if (count($res3)) {
                 throw new Horde_Exception(sprintf(_("This person already has a %s entry in the address book"), $field));
             }
 
             $ob = $res2->next();
             $ob->setValue($field, $value);
             $ob->store();
-        } elseif ($res->count() == 1) {
+        } elseif (count($res) == 1) {
             $res4 = $driver->search(array('email' => $address, $field => $value));
             if ($res4 instanceof PEAR_Error) {
                 throw new Horde_Exception(sprintf(_("Search failed: %s"), $res4->getMessage()));
             }
 
-            if ($res4->count()) {
+            if (count($res4)) {
                 throw new Horde_Exception(sprintf(_("This person already has a %s entry in the address book"), $field));
             }
 
@@ -1792,7 +1792,7 @@ class Turba_Api extends Horde_Registry_Api
 
                 $res = $driver->search(array('email' => $address));
                 if ($res instanceof Turba_List) {
-                    if ($res->count() > 1) {
+                    if (count($res) > 1) {
                         continue;
                     }
 
index 61e08b8..1d6b818 100644 (file)
@@ -8,7 +8,7 @@
  * @author  Jon Parise <jon@csh.rit.edu>
  * @package Turba
  */
-class Turba_Driver
+class Turba_Driver implements Countable
 {
     /**
      * The internal name of this source.
@@ -912,24 +912,6 @@ class Turba_Driver
     }
 
     /**
-     * Returns the number of contacts of the current user in this address book.
-     *
-     * @return integer  The number of contacts that the user owns.
-     */
-    function count()
-    {
-        if (is_null($this->_count)) {
-            $count = $this->_search(array('AND' => array(array('field' => $this->toDriver('__owner'), 'op' => '=', 'test' => $this->getContactOwner()))), array($this->toDriver('__key')));
-            if (is_a($count, 'PEAR_Error')) {
-                return $count;
-            }
-            $this->_count = count($count);
-        }
-
-        return $this->_count;
-    }
-
-    /**
      * Returns the criteria available for this source except '__key'.
      *
      * @return array  An array containing the criteria.
@@ -2817,4 +2799,25 @@ class Turba_Driver
         return $params['default'];
     }
 
+    /* Countable methods. */
+
+    /**
+     * Returns the number of contacts of the current user in this address book.
+     *
+     * @return integer  The number of contacts that the user owns.
+     * @throws Turba_Exception
+     */
+    public function count()
+    {
+        if (is_null($this->_count)) {
+            $count = $this->_search(array('AND' => array(array('field' => $this->toDriver('__owner'), 'op' => '=', 'test' => $this->getContactOwner()))), array($this->toDriver('__key')));
+            if ($count instanceof PEAR_Error) {
+                throw new Turba_Exception($count);
+            }
+            $this->_count = count($count);
+        }
+
+        return $this->_count;
+    }
+
 }
index 564cc76..1dcb567 100644 (file)
@@ -3,38 +3,50 @@
  * The Turba_List:: class provides an interface for dealing with a
  * list of Turba_Objects.
  *
- * @author  Chuck Hagenbuch <chuck@horde.org>
- * @author  Jon Parise <jon@csh.rit.edu>
- * @package Turba
+ * Copyright 2000-2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file LICENSE for license information (ASL).  If you did
+ * did not receive this file, see http://www.horde.org/licenses/asl.php.
+ *
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @author   Jon Parise <jon@csh.rit.edu>
+ * @category Horde
+ * @license  http://www.horde.org/licenses/asl.php ASL
+ * @package  Turba
  */
-class Turba_List {
-
+class Turba_List implements Countable
+{
     /**
      * The array containing the Turba_Objects represented in this list.
      *
      * @var array
      */
-    var $objects = array();
+    public $objects = array();
+
+    /**
+     * Cached attributes array.
+     *
+     * @var array
+     */
+    protected $_attributes = null;
 
     /**
      * The field to compare objects by.
      *
      * @var string
      */
-    var $_usortCriteria;
+    protected $_usortCriteria;
 
     /**
      * Constructor.
      */
-    function Turba_List($ids = array())
+    public function __construct($ids = array())
     {
-        if ($ids) {
-            foreach ($ids as $value) {
-                list($source, $key) = explode(':', $value);
-                $driver = Turba_Driver::singleton($source);
-                if (is_a($driver, 'Turba_Driver')) {
-                    $this->insert($driver->getObject($key));
-                }
+        foreach ($ids as $value) {
+            list($source, $key) = explode(':', $value);
+            $driver = Turba_Driver::singleton($source);
+            if ($driver instanceof Turba_Driver) {
+                $this->insert($driver->getObject($key));
             }
         }
     }
@@ -44,9 +56,9 @@ class Turba_List {
      *
      * @param Turba_Object $object  The object to insert.
      */
-    function insert($object)
+    public function insert($object)
     {
-        if (is_a($object, 'Turba_Object')) {
+        if ($object instanceof Turba_Object) {
             $key = $object->getSource() . ':' . $object->getValue('__key');
             if (!isset($this->objects[$key])) {
                 $this->objects[$key] = $object;
@@ -60,7 +72,7 @@ class Turba_List {
      *
      * @return Turba_Object  The next object in the list.
      */
-    function reset()
+    public function reset()
     {
         return reset($this->objects);
     }
@@ -71,44 +83,41 @@ class Turba_List {
      *
      * @return Turba_Object  The next object in the list.
      */
-    function next()
+    public function next()
     {
         list(,$tmp) = each($this->objects);
         return $tmp;
     }
 
     /**
-     * Returns the number of Turba_Objects that are in the list. Use this to
-     * hide internal implementation details from client objects.
-     *
-     * @return integer  The number of objects in the list.
-     */
-    function count()
-    {
-        return count($this->objects);
-    }
-
-    /**
      * Filters/Sorts the list based on the specified sort routine.
      * The default sort order is by last name, ascending.
      *
-     * @param array $order    Array of hashes describing sort fields.  Each
-     *                        hash has the following fields:
-     *                        - field:     String sort field
-     *                        - ascending: Boolean indicating sort direction
+     * @param array $order  Array of hashes describing sort fields.  Each
+     *                      hash has the following fields:
+     * <pre>
+     * ascending - (boolean) Sort direction.
+     * field - (string) Sort field.
+     * </pre>
      */
-    function sort($order = null)
+    public function sort($order = null)
     {
         if (!$order) {
-            $order = array(array('field' => 'lastname', 'ascending' => true));
+            $order = array(
+                array(
+                    'ascending' => true,
+                    'field' => 'lastname'
+                )
+            );
         }
 
         $need_lastname = false;
-        $last_first = $GLOBALS['prefs']->getValue('name_format') == 'last_first';
+        $last_first = ($GLOBALS['prefs']->getValue('name_format') == 'last_first');
         foreach ($order as &$field) {
-            if ($last_first && $field['field'] == 'name') {
+            if ($last_first && ($field['field'] == 'name')) {
                 $field['field'] = 'lastname';
             }
+
             if ($field['field'] == 'lastname') {
                 $field['field'] = '__lastname';
                 $need_lastname = true;
@@ -131,7 +140,7 @@ class Turba_List {
         }
 
         $this->_usortCriteria = $order;
-        usort($sorted_objects, array($this, 'cmp'));
+        usort($sorted_objects, array($this, '_cmp'));
         $this->objects = $sorted_objects;
     }
 
@@ -147,25 +156,45 @@ class Turba_List {
      *
      * @return integer  Comparison of the two field values.
      */
-    function cmp(&$a, &$b)
+    protected function _cmp($a, $b)
     {
-        require TURBA_BASE . '/config/attributes.php';
+        if (is_null($this->_attributes)) {
+            $this->_attributes = Horde::loadConfiguration('attributes.php', 'attributes', 'turba');
+        }
+
         foreach ($this->_usortCriteria as $field) {
             // Set the comparison type based on the type of attribute we're
             // sorting by.
-            $usortType = 'text';
-            if (isset($attributes[$field['field']])) {
-                if (!empty($attributes[$field['field']]['cmptype'])) {
-                    $usortType = $attributes[$field['field']]['cmptype'];
-                } elseif ($attributes[$field['field']]['type'] == 'int' ||
-                          $attributes[$field['field']]['type'] == 'intlist'||
-                          $attributes[$field['field']]['type'] == 'number') {
-                    $usortType = 'int';
+            $sortmethod = 'text';
+            if (isset($this->_attributes[$field['field']])) {
+                $f = $this->_attributes[$field['field']];
+
+                if (!empty($f['cmptype'])) {
+                    $sortmethod = $f['cmptype'];
+                } elseif (in_array($f['type'], array('int', 'intlist', 'number'))) {
+                    $sortmethod = 'int';
                 }
             }
 
-            $method = 'cmp_' . $usortType;
-            $result = $this->$method($a, $b, $field['field']);
+            $field = $field['field'];
+            switch ($sortmethod) {
+            case 'int':
+                $result = ($a->getValue($field) > $b->getValue($field)) ? 1 : -1;
+                break;
+
+            case 'text':
+                if (!isset($a->sortValue[$field])) {
+                    $a->sortValue[$field] = Horde_String::lower($a->getValue($field), true);
+                }
+                if (!isset($b->sortValue[$field])) {
+                    $b->sortValue[$field] = Horde_String::lower($b->getValue($field), true);
+                }
+
+                // Use strcoll for locale-safe comparisons.
+                $result = strcoll($a->sortValue[$field], $b->sortValue[$field]);
+                break;
+            }
+
             if (!$field['ascending']) {
                 $result = -$result;
             }
@@ -173,25 +202,15 @@ class Turba_List {
                 return $result;
             }
         }
+
         return 0;
     }
 
-    function cmp_text(&$a, &$b, $field)
-    {
-        if (!isset($a->sortValue[$field])) {
-            $a->sortValue[$field] = Horde_String::lower($a->getValue($field), true);
-        }
-        if (!isset($b->sortValue[$field])) {
-            $b->sortValue[$field] = Horde_String::lower($b->getValue($field), true);
-        }
-
-        // Use strcoll for locale-safe comparisons.
-        return strcoll($a->sortValue[$field], $b->sortValue[$field]);
-    }
+    /* Countable methods. */
 
-    function cmp_int($a, $b, $field)
+    public function count()
     {
-        return ($a->getValue($field) > $b->getValue($field)) ? 1 : -1;
+        return count($this->objects);
     }
 
 }
index 4475c60..45bad2f 100644 (file)
@@ -44,8 +44,8 @@ class Turba_LoginTasks_SystemTask_UpgradeLists extends Horde_LoginTasks_SystemTa
                 if (is_a($lists, 'PEAR_Error')) {
                     return $lists;
                 }
-                $cnt = $lists->count();
-                for ($j = 0; $j < $cnt; ++$j) {
+
+                for ($j = 0, $cnt = count($lists); $j < $cnt; ++$j) {
                     $list = $lists->next();
                     $attributes = $list->getAttributes();
                     $members = @unserialize($attributes['__members']);
index 91ef61e..fd51542 100644 (file)
@@ -88,11 +88,11 @@ class Turba_Object {
      * @return mixed  The value of $attribute, an array (for photo type)
      *                or the empty string.
      */
-    function getValue($attribute)
+    public function getValue($attribute)
     {
         if (isset($this->attributes[$attribute])) {
             try {
-                return Horde::callHook('decode_attribute', array($attribute, $this->attributes[$attribute], $this), 'turba');
+                return Horde::callHook('decode_attribute', array($attribute, $this->attributes[$attribute]), 'turba');
             } catch (Horde_Exception_HookNotSet $e) {
             } catch (Turba_Exception $e) {}
         }
@@ -107,14 +107,18 @@ class Turba_Object {
         } elseif (!isset($this->attributes[$attribute])) {
             return null;
         } elseif (isset($GLOBALS['attributes'][$attribute]) &&
-                  $GLOBALS['attributes'][$attribute]['type'] == 'image') {
+                  ($GLOBALS['attributes'][$attribute]['type'] == 'image')) {
             return empty($this->attributes[$attribute])
                 ? null
-                : array('load' => array('file' => basename(tempnam(Horde::getTempDir(), 'horde_form_')),
-                                        'data' => $this->attributes[$attribute]));
-        } else {
-            return $this->attributes[$attribute];
+                : array(
+                      'load' => array(
+                          'data' => $this->attributes[$attribute],
+                          'file' => basename(tempnam(Horde::getTempDir(), 'horde_form_'))
+                      )
+                  );
         }
+
+        return $this->attributes[$attribute];
     }
 
     /**
index ff645d4..9fda375 100644 (file)
@@ -144,7 +144,7 @@ class Turba_View_Browse {
 
                 $max_contacts = Turba::getExtendedPermission($targetDriver, 'max_contacts');
                 if ($max_contacts !== true
-                    && $max_contacts <= $targetDriver->count()) {
+                    && $max_contacts <= count($targetDriver)) {
                     try {
                         $message = Horde::callHook('perms_denied', array('turba:max_contacts'));
                     } catch (Horde_Exception_HookNotSet $e) {
@@ -284,7 +284,7 @@ class Turba_View_Browse {
                     // Check permissions.
                     $max_contacts = Turba::getExtendedPermission($driver, 'max_contacts');
                     if ($max_contacts !== true &&
-                        $max_contacts <= $driver->count()) {
+                        $max_contacts <= count($driver)) {
                         try {
                             $message = Horde::callHook('perms_denied', array('turba:max_contacts'));
                         } catch (Horde_Exception $e) {
@@ -358,8 +358,8 @@ class Turba_View_Browse {
                     if (!is_object($results = $list->listMembers($sortorder))) {
                         $notification->push(_("Failed to browse list"), 'horde.error');
                     } else {
-                        if ($results->count() != $list->count()) {
-                            $count = $list->count() - $results->count();
+                        if (count($results) != count($list)) {
+                            $count = count($list) - count($results);
                             $notification->push(sprintf(ngettext("There is %d contact in this list that is not viewable to you", "There are %d contacts in this list that are not viewable to you", $count), $count), 'horde.message');
                         }
                         $view = new Turba_View_List($results, null, $columns);
index 23f2100..a70fbf5 100644 (file)
@@ -68,7 +68,7 @@ class Turba_View_Duplicates
      */
     public function display()
     {
-        require TURBA_BASE . '/config/attributes.php';
+        $attributes = Horde::loadConfiguration(TURBA_BASE . '/config/attributes.php', 'attributes', 'turba');
 
         $view = new Horde_View(array('templatePath' => TURBA_TEMPLATES . '/search/duplicate'));
         new Horde_View_Helper_Text($view);
index 688bfff..7807fc2 100644 (file)
@@ -7,8 +7,8 @@
  * @author  Jon Parise <jon@csh.rit.edu>
  * @package Turba
  */
-class Turba_View_List {
-
+class Turba_View_List implements Countable
+{
     /**
      * The Turba_List object that we are visualizing.
      *
@@ -141,17 +141,6 @@ class Turba_View_List {
         return $this->type;
     }
 
-    /**
-     * Returns the number of Turba_Objects that are in the list. Use this to
-     * hide internal implementation details from client objects.
-     *
-     * @return integer  The number of objects in the list.
-     */
-    function count()
-    {
-        return $this->list->count();
-    }
-
     function display()
     {
         global $prefs, $default_source, $copymove_source_options;
@@ -181,7 +170,7 @@ class Turba_View_List {
 
         if ($this->type == 'search') {
             $page = Horde_Util::getFormData('page', 0);
-            $numitem = $this->count();
+            $numitem = count($this);
             $maxpage = $prefs->getValue('maxpage');
             $perpage = $prefs->getValue('perpage');
 
@@ -229,7 +218,7 @@ class Turba_View_List {
             if (!preg_match('/^[A-Za-z*]$/', $page)) {
                 $page = '*';
             }
-            if ($this->count() > $prefs->getValue('perpage')) {
+            if (count($this) > $prefs->getValue('perpage')) {
                 $page = Horde_Util::getFormData('page', 'A');
                 if (!preg_match('/^[A-Za-z*]$/', $page)) {
                     $page = 'A';
@@ -262,7 +251,7 @@ class Turba_View_List {
     function getPage(&$numDisplayed, $min = 0, $max = null)
     {
         if (is_null($max)) {
-            $max = $this->list->count();
+            $max = count($this);
         }
         return $this->_get($numDisplayed,
                            new Turba_View_List_PageFilter($min, $max));
@@ -446,6 +435,19 @@ class Turba_View_List {
         return array($addToList, $addToListSources);
     }
 
+    /* Countable methods. */
+
+    /**
+     * Returns the number of Turba_Objects that are in the list. Use this to
+     * hide internal implementation details from client objects.
+     *
+     * @return integer  The number of objects in the list.
+     */
+    public function count()
+    {
+        return $this->list->count();
+    }
+
 }
 
 /**
index f418415..a1fb2f5 100644 (file)
@@ -52,12 +52,12 @@ class Turba_KolabTest extends Turba_KolabTestBase {
 
         $result = $turba->search(array(), array('last-name'));
         $this->assertNoError($result);
-        $this->assertEquals(2, $result->count());
+        $this->assertEquals(2, count($result));
 
         $turba = Turba_Driver::singleton('INBOX%2Ftest2');
         $result = $turba->search(array(), array('last-name'));
 
-        $this->assertEquals(0, $result->count());
+        $this->assertEquals(0, count($result));
     }
 
     function testPhoto()
index 3bb7cfd..918cab3 100644 (file)
@@ -1,4 +1,4 @@
-<?php if ($this->count() > $prefs->getValue('perpage')): ?>
+<?php if (count($this) > $prefs->getValue('perpage')): ?>
 <div class="turbaPager">
 <?php
 $show = Horde_Util::getFormData('show', 'all');
index 3d9b8a3..5faca95 100644 (file)
@@ -15,7 +15,7 @@
     <?php foreach ($duplicates as $value => $list): ?>
     <tr>
       <td><a href="<?php echo $this->link->add(array('type' => $field, 'dupe' => $value)) ?>"><?php echo $this->h($value) ?></a></td>
-      <td><a href="<?php echo $this->link->add(array('type' => $field, 'dupe' => $value)) ?>"><?php echo $list->count() ?></a></td>
+      <td><a href="<?php echo $this->link->add(array('type' => $field, 'dupe' => $value)) ?>"><?php echo count($list) ?></a></td>
     </tr>
     <?php endforeach; ?>
   </tbody>