Turba_List::sort PHP Method

sort() public method

The default sort order is by last name, ascending.
public sort ( array $order = null )
$order array Array of hashes describing sort fields. Each hash has the following fields:
ascending - (boolean) Sort direction.
field - (string) Sort field.
    public function sort($order = null)
    {
        global $attributes, $prefs;
        if (!$order) {
            $order = array(array('ascending' => true, 'field' => 'lastname'));
        }
        $need_lastname = $need_firstname = false;
        $name_format = $prefs->getValue('name_format');
        $name_sort = $prefs->getValue('name_sort');
        foreach ($order as &$field) {
            if ($field['field'] == 'name') {
                if ($name_sort == 'last_first') {
                    $field['field'] = 'lastname';
                } elseif ($name_sort == 'first_last') {
                    $field['field'] = 'firstname';
                }
            }
            if ($field['field'] == 'lastname') {
                $field['field'] = '__lastname';
                $need_lastname = true;
                break;
            }
            if ($field['field'] == 'firstname') {
                $field['field'] = '__firstname';
                $need_firstname = true;
                break;
            }
        }
        if ($need_firstname || $need_lastname) {
            $sorted_objects = array();
            foreach ($this->objects as $key => $object) {
                $name = $object->getValue('name');
                $firstname = $object->getValue('firstname');
                $lastname = $object->getValue('lastname');
                if (!$lastname) {
                    $lastname = Turba::guessLastname($name);
                }
                if (!$firstname) {
                    switch ($name_format) {
                        case 'last_first':
                            $firstname = preg_replace('/' . preg_quote($lastname, '/') . ',\\s*/', '', $name);
                            break;
                        case 'first_last':
                            $firstname = preg_replace('/\\s+' . preg_quote($lastname, '/') . '/', '', $name);
                            break;
                        default:
                            $firstname = preg_replace('/\\s*' . preg_quote($lastname, '/') . '(,\\s*)?/', '', $name);
                            break;
                    }
                }
                $object->setValue('__lastname', $lastname);
                $object->setValue('__firstname', $firstname);
                $sorted_objects[$key] = $object;
            }
        } else {
            $sorted_objects = $this->objects;
        }
        // Set the comparison type based on the type of attribute we're
        // sorting by.
        foreach ($order as &$val) {
            $sm = 'text';
            if (isset($attributes[$val['field']])) {
                $f = $attributes[$val['field']];
                if (!empty($f['cmptype'])) {
                    $sm = $f['cmptype'];
                } elseif (in_array($f['type'], array('int', 'intlist', 'number'))) {
                    $sm = 'int';
                }
            }
            $val['sortmethod'] = $sm;
        }
        $this->_usortCriteria = $order;
        /* Exceptions thrown inside a sort incorrectly cause an error. See
         * Bug #9202. */
        @usort($sorted_objects, array($this, '_cmp'));
        $this->objects = $sorted_objects;
    }

Usage Example

コード例 #1
0
ファイル: Group.php プロジェクト: DSNS-LAB/Dmail
 /**
  * Retrieve the Objects in this group
  *
  * @param array $sort   The requested sort order which is passed to
  *                      Turba_List::sort().
  *
  * @return Turba_List   List containing the members of this group
  */
 public function listMembers($sort = null)
 {
     $list = new Turba_List();
     $children = unserialize($this->attributes['__members']);
     if (!is_array($children)) {
         $children = array();
     }
     reset($children);
     $modified = false;
     foreach ($children as $member) {
         if (strpos($member, ':') === false) {
             try {
                 $contact = $this->driver->getObject($member);
             } catch (Horde_Exception_NotFound $e) {
                 if (!empty($this->_options['removeMissing'])) {
                     // Remove the contact if it no longer exists
                     $this->removeMember($member);
                     $modified = true;
                 }
                 continue;
             }
         } else {
             list($sourceId, $contactId) = explode(':', $member, 2);
             try {
                 $driver = $GLOBALS['injector']->getInstance('Turba_Factory_Driver')->create($sourceId);
             } catch (Turba_Exception $e) {
                 continue;
             }
             try {
                 $contact = $driver->getObject($contactId);
             } catch (Horde_Exception_NotFound $e) {
                 if (!empty($this->_options['removeMissing'])) {
                     // Remove the contact if it no longer exists
                     $this->removeMember($member);
                     $modified = true;
                 }
                 continue;
             }
         }
         $list->insert($contact);
     }
     // If we've pruned any dead entries, store the changes.
     if ($modified) {
         $this->store();
     }
     $list->sort($sort);
     return $list;
 }
All Usage Examples Of Turba_List::sort