1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
<?php
/**
* IMAP_Sort provides functions for sorting lists of IMAP mailboxes/folders.
*
* $Horde: framework/IMAP/IMAP/Sort.php,v 1.6.8.13 2006/02/15 18:07:52 slusarz Exp $
*
* Copyright 2004-2006 Michael Slusarz <slusarz@bigworm.colorado.edu>
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
*
* @author Michael Slusarz <slusarz@bigworm.colorado.edu>
* @since Horde 3.0
* @package Horde_IMAP
*/
class IMAP_Sort {
/**
* The delimiter character to use.
*
* @var string
*/
var $_delimiter;
/**
* Should we sort with 'INBOX' at the front of the list?
*
* @var boolean
*/
var $_sortinbox;
/**
* Constructor.
*
* @param string $delimiter The delimiter used to separate mailboxes.
*/
function IMAP_Sort($delimiter)
{
$this->_delimiter = $delimiter;
}
/**
* Sort a list of mailboxes (by value).
*
* @param array &$mbox The list of mailboxes to sort.
* @param boolean $inbox When sorting, always put 'INBOX' at the head of
* the list?
* @param boolean $index Maintain index association?
*/
function sortMailboxes(&$mbox, $inbox = true, $index = false)
{
$this->_sortinbox = $inbox;
if ($index) {
uasort($mbox, array(&$this, '_mbox_cmp'));
} else {
usort($mbox, array(&$this, '_mbox_cmp'));
}
}
/**
* Sort a list of mailboxes (by key).
*
* @param array &$mbox The list of mailboxes to sort, with the keys
* being the mailbox names.
* @param boolean $inbox When sorting, always put 'INBOX' at the head of
* the list?
*/
function sortMailboxesByKey(&$mbox, $inbox = true)
{
$this->_sortinbox = $inbox;
uksort($mbox, array(&$this, '_mbox_cmp'));
}
/**
* Hierarchical folder sorting function (used with usort()).
*
* @access private
*
* @param string $a Comparison item 1.
* @param string $b Comparison item 2.
*
* @return integer See usort().
*/
function _mbox_cmp($a, $b)
{
/* Always return INBOX as "smaller". */
if ($this->_sortinbox) {
if (strcasecmp($a, 'INBOX') == 0) {
return -1;
} elseif (strcasecmp($b, 'INBOX') == 0) {
return 1;
}
}
$a_parts = explode($this->_delimiter, $a);
$b_parts = explode($this->_delimiter, $b);
$a_count = count($a_parts);
$b_count = count($b_parts);
$iMax = min($a_count, $b_count);
for ($i = 0; $i < $iMax; $i++) {
if ($a_parts[$i] != $b_parts[$i]) {
/* If only one of the folders is under INBOX, return it as
* "smaller". */
if ($this->_sortinbox && ($i == 0)) {
$a_base = (strcasecmp($a_parts[0], 'INBOX') == 0);
$b_base = (strcasecmp($b_parts[0], 'INBOX') == 0);
if ($a_base && !$b_base) {
return -1;
} elseif (!$a_base && $b_base) {
return 1;
}
}
$cmp = strnatcasecmp($a_parts[$i], $b_parts[$i]);
if ($cmp == 0) {
return strcmp($a_parts[$i], $b_parts[$i]);
}
return $cmp;
}
}
return ($a_count - $b_count);
}
}
|