Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
<?php
/**
 * Copyright 2008-2017 Horde LLC (http://www.horde.org/)
 *
 * See the enclosed file LICENSE for license information (LGPL). If you
 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
 *
 * @category  Horde
 * @copyright 2008-2017 Horde LLC
 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
 * @package   Imap_Client
 */

/**
 * Object representing the threaded sort results from
 * Horde_Imap_Client_Base#thread().
 *
 * @author    Michael Slusarz <slusarz@horde.org>
 * @category  Horde
 * @copyright 2008-2017 Horde LLC
 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
 * @package   Imap_Client
 */
class Horde_Imap_Client_Data_Thread implements Countable, Serializable
{
    /**
     * Internal thread data structure. Keys are base values, values are arrays
     * with keys as the ID and values as the level.
     *
     * @var array
     */
    protected $_thread = array();

    /**
     * The index type.
     *
     * @var string
     */
    protected $_type;

    /**
     * Constructor.
     *
     * @param array $data   See $_thread.
     * @param string $type  Either 'sequence' or 'uid'.
     */
    public function __construct($data, $type)
    {
        $this->_thread = $data;
        $this->_type = $type;
    }

    /**
     * Return the ID type.
     *
     * @return string  Either 'sequence' or 'uid'.
     */
    public function getType()
    {
        return $this->_type;
    }

    /**
     * Return the sorted list of messages indices.
     *
     * @return Horde_Imap_Client_Ids  The sorted list of messages.
     */
    public function messageList()
    {
        return new Horde_Imap_Client_Ids($this->_getAllIndices(), $this->getType() == 'sequence');
    }

    /**
     * Returns the list of messages in a thread.
     *
     * @param integer $index  An index contained in the thread.
     *
     * @return array  Keys are indices, values are objects with the following
     *                properties:
     *   - base: (integer) Base ID of the thread. If null, thread is a single
     *           message.
     *   - last: (boolean) If true, this is the last index in the sublevel.
     *   - level: (integer) The sublevel of the index.
     */
    public function getThread($index)
    {
        foreach ($this->_thread as $v) {
            if (isset($v[$index])) {
                reset($v);

                $ob = new stdClass;
                $ob->base = (count($v) > 1) ? key($v) : null;
                $ob->last = false;

                $levels = $out = array();
                $last = 0;

                while (($v2 = current($v)) !== false) {
                    $k2 = key($v);
                    $ob2 = clone $ob;
                    $ob2->level = $v2;
                    $out[$k2] = $ob2;

                    if (($last < $v2) && isset($levels[$v2])) {
                        $out[$levels[$v2]]->last = true;
                    }
                    $levels[$v2] = $k2;
                    $last = $v2;
                    next($v);
                }

                foreach ($levels as $v) {
                    $out[$v]->last = true;
                }

                return $out;
            }
        }

        return array();
    }

> /** /* Countable methods. */ > * Returns array of all threads. > * /** > * @return array Keys of thread arrays are indices, values are objects with the following */ > * properties: public function count() > * - base: (integer) Base ID of the thread. If null, thread is a single { > * message. return count($this->_getAllIndices()); > * - last: (boolean) If true, this is the last index in the sublevel. } > * - level: (integer) The sublevel of the index. > */ /* Serializable methods. */ > public function getThreads() > { /** > $data = array(); */ > foreach ($this->_thread as $v) { public function serialize() > reset($v); { > return json_encode(array( > $ob = new stdClass; $this->_thread, > $ob->base = (count($v) > 1) ? key($v) : null; $this->_type > $ob->last = false; )); > } > $levels = $out = array(); > $last = 0; /** > */ > while (($v2 = current($v)) !== false) { public function unserialize($data) > $k2 = key($v); { > $ob2 = clone $ob; list($this->_thread, $this->_type) = json_decode($data, true); > $ob2->level = $v2; } > $out[$k2] = $ob2; > /* Protected methods. */ > if (($last < $v2) && isset($levels[$v2])) { > $out[$levels[$v2]]->last = true; /** > } * Return all indices. > $levels[$v2] = $k2; * > $last = $v2; * @return array An array of indices. > next($v); */ > } protected function _getAllIndices() > { > foreach ($levels as $v) { $out = array(); > $out[$v]->last = true; > } foreach ($this->_thread as $val) { > $out += $val; > $data[] = $out; } > } > return array_keys($out); > return $data; } > } >
}