Some H4 conversions.
Implement Countable interface on several objects.
/* 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) {
$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'));
}
/* 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) {
$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."),
$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);
$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"));
}
$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.");
}
$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];
$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."));
}
if (!($members instanceof Turba_List)) {
continue;
}
- if ($members->count() > 0) {
+ if (count($members)) {
if (!isset($results[$name])) {
$results[$name] = array();
}
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)));
}
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));
}
$res = $driver->search(array('email' => $address));
if ($res instanceof Turba_List) {
- if ($res->count() > 1) {
+ if (count($res) > 1) {
continue;
}
* @author Jon Parise <jon@csh.rit.edu>
* @package Turba
*/
-class Turba_Driver
+class Turba_Driver implements Countable
{
/**
* The internal name of this source.
}
/**
- * 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.
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;
+ }
+
}
* 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));
}
}
}
*
* @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;
*
* @return Turba_Object The next object in the list.
*/
- function reset()
+ public function reset()
{
return reset($this->objects);
}
*
* @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;
}
$this->_usortCriteria = $order;
- usort($sorted_objects, array($this, 'cmp'));
+ usort($sorted_objects, array($this, '_cmp'));
$this->objects = $sorted_objects;
}
*
* @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;
}
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);
}
}
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']);
* @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) {}
}
} 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];
}
/**
$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) {
// 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) {
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);
*/
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);
* @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.
*
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;
if ($this->type == 'search') {
$page = Horde_Util::getFormData('page', 0);
- $numitem = $this->count();
+ $numitem = count($this);
$maxpage = $prefs->getValue('maxpage');
$perpage = $prefs->getValue('perpage');
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';
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));
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();
+ }
+
}
/**
$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()
-<?php if ($this->count() > $prefs->getValue('perpage')): ?>
+<?php if (count($this) > $prefs->getValue('perpage')): ?>
<div class="turbaPager">
<?php
$show = Horde_Util::getFormData('show', 'all');
<?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>