[ Index ]
 

Code source de Horde 3.1.3

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

title

Body

[fermer]

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

   1  <?php
   2  /**
   3   * IMAP_Thread provides functions for working with imap_thread() output.
   4   *
   5   * $Horde: framework/IMAP/IMAP/Thread.php,v 1.4.10.13 2006/01/01 21:28:21 jan Exp $
   6   *
   7   * Copyright 2004-2006 Michael Slusarz <slusarz@curecanti.org>
   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@curecanti.org>
  13   * @since   Horde 3.0
  14   * @package Horde_IMAP
  15   */
  16  class IMAP_Thread {
  17  
  18      /**
  19       * imap_thread() output.
  20       *
  21       * @var array
  22       */
  23      var $_threadarray;
  24  
  25      /**
  26       * Internal thread data structure.
  27       *
  28       * @var array
  29       */
  30      var $_thread = array();
  31  
  32      /**
  33       * Array index to Message index lookup array.
  34       *
  35       * @var array
  36       */
  37      var $_lookup = array();
  38  
  39      /**
  40       * Constructor.
  41       *
  42       * @param array $ob  Output from imap_thread().
  43       */
  44      function IMAP_Thread($ob)
  45      {
  46          $this->_threadarray = $ob;
  47          $this->_processStructure();
  48      }
  49  
  50     /**
  51       * Gets the indention level for an IMAP message index.
  52       *
  53       * @param integer $index  The IMAP message index.
  54       *
  55       * @return mixed  Returns the thread indent level if $index found.
  56       *                Returns false on failure.
  57       */
  58      function getThreadIndent($index)
  59      {
  60          $key = $this->_getKey($index);
  61          if (!is_null($key)) {
  62              if (isset($this->_thread[$key]['level'])) {
  63                  return $this->_thread[$key]['level'];
  64              }
  65          }
  66  
  67           return false;
  68      }
  69  
  70      /**
  71       * Gets the base thread index for an IMAP message index.
  72       *
  73       * @param integer $index  The IMAP message index.
  74       *
  75       * @return mixed  Returns the base IMAP index if $index is part of a
  76       *                thread.
  77       *                Returns false on failure.
  78       */
  79      function getThreadBase($index)
  80      {
  81          $key = $this->_getKey($index);
  82          if (!is_null($key)) {
  83              if (!empty($this->_thread[$key]['base'])) {
  84                  return $this->_thread[$key]['base'];
  85              }
  86          }
  87  
  88          return false;
  89      }
  90  
  91      /**
  92       * Is this index the last in the current level?
  93       *
  94       * @param integer $index  The IMAP message index.
  95       *
  96       * @return boolean  Returns true if $index is the last element in the
  97       *                  current thread level.
  98       *                  Returns false if not, or on failure.
  99       */
 100      function lastInLevel($index)
 101      {
 102          $key = $this->_getKey($index);
 103          if (!is_null($key)) {
 104              if (!empty($this->_thread[$key]['last'])) {
 105                  return $this->_thread[$key]['last'];
 106              }
 107          }
 108  
 109          return false;
 110      }
 111  
 112      /**
 113       * Do the message index -> array index lookup.
 114       *
 115       * @access private
 116       *
 117       * @param integer $index  The IMAP message index.
 118       *
 119       * @return mixed  The array index value or null if no index.
 120       */
 121      function _getKey($index)
 122      {
 123          return (isset($this->_lookup[$index])) ? $this->_lookup[$index] : null;
 124      }
 125  
 126      /**
 127       * Return the sorted list of messages indices.
 128       *
 129       * @param boolean $new  True for newest first, false for oldest first.
 130       *
 131       * @return array  The sorted list of messages.
 132       */
 133      function messageList($new)
 134      {
 135          return ($new) ? array_reverse(array_keys($this->_lookup)) : array_keys($this->_lookup);
 136      }
 137  
 138      /**
 139       * Returns the list of messages in the current thread.
 140       *
 141       * @param integer $index  The IMAP index of the current message.
 142       *
 143       * @return array  A list of IMAP message indices.
 144       */
 145      function getThread($index)
 146      {
 147          /* Find the beginning of the thread. */
 148          $begin = $this->getThreadBase($index);
 149          $key = $this->_getKey($begin);
 150          if (is_null($key) || empty($begin)) {
 151              return array($index);
 152          }
 153  
 154          /* Work forward from the first thread element to find the end of the
 155           * thread. */
 156          $thread_list = array($this->_thread[$key]['index']);
 157          while (++$key) {
 158              if (!isset($this->_thread[$key])) {
 159                  break;
 160              }
 161              $curr = $this->_thread[$key];
 162              if ($curr['base'] == $begin) {
 163                  $thread_list[] = $this->_thread[$key]['index'];
 164              } else {
 165                  break;
 166              }
 167          }
 168  
 169          return $thread_list;
 170      }
 171  
 172      /**
 173       * Process the output from imap_thread() into an internal data structure.
 174       *
 175       * @access private
 176       */
 177      function _processStructure()
 178      {
 179          $container = $container_base = $last_index = $thread_base = $thread_base_idx =  null;
 180          $indices = array();
 181          $i = $last_i = $level = 0;
 182  
 183          foreach ($this->_threadarray as $key => $val) {
 184              $pos = strpos($key, '.');
 185              $index = substr($key, 0, $pos);
 186              $type = substr($key, $pos + 1);
 187  
 188              switch ($type) {
 189              case 'num':
 190                  if ($val === 0) {
 191                      $container = $index;
 192                  } else {
 193                      $i++;
 194                      if (is_null($container) && empty($level)) {
 195                          $thread_base = $val;
 196                          $thread_base_idx = $index;
 197                      }
 198                      $this->_lookup[$val] = $index;
 199                      $this->_thread[$index] = array();
 200                      $this->_thread[$index]['index'] = $val;
 201                  }
 202                  break;
 203  
 204              case 'next':
 205                  if (!is_null($container) && ($container === $index)) {
 206                      $container_base = $this->_threadarray[$val . '.num'];
 207                  } else {
 208                      $i++;
 209                      if (!is_null($container)) {
 210                          $this->_thread[$index]['base'] = $container_base;
 211                      } else {
 212                          $this->_thread[$index]['base'] = (!empty($level) || ($val != 0)) ? $thread_base : null;
 213                      }
 214                      $level++;
 215                  }
 216                  break;
 217  
 218              case 'branch':
 219                  if ($container === $index) {
 220                      $container = $container_base = null;
 221                      $this->_thread[$last_index]['last'] = true;
 222                  } else {
 223                      $this->_thread[$index]['level'] = $level--;
 224                      $this->_thread[$index]['last'] = !(!is_null($container) && empty($level));
 225                      if ($index === $thread_base_idx) {
 226                          $index = null;
 227                      } elseif (!empty($level) &&
 228                                !is_null($last_index) &&
 229                                isset($this->_thread[$last_index])) {
 230                          $this->_thread[$last_index]['last'] = ($last_i == ($i - 1));
 231                      }
 232                  }
 233                  $last_index = $index;
 234                  $last_i = $i++;
 235                  break;
 236              }
 237          }
 238      }
 239  
 240  }


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