[ Index ]
 

Code source de Horde 3.1.3

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/Horde/IMAP/ -> Sort.php (source)

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


Généré le : Sun Feb 25 18:01:28 2007 par Balluche grâce à PHPXref 0.7