File: Thread.php

package info (click to toggle)
horde3 3.1.3-4etch7
  • links: PTS
  • area: main
  • in suites: etch
  • size: 22,876 kB
  • ctags: 18,071
  • sloc: php: 75,151; xml: 2,979; sql: 1,069; makefile: 79; sh: 64
file content (240 lines) | stat: -rw-r--r-- 6,974 bytes parent folder | download | duplicates (2)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/**
 * IMAP_Thread provides functions for working with imap_thread() output.
 *
 * $Horde: framework/IMAP/IMAP/Thread.php,v 1.4.10.13 2006/01/01 21:28:21 jan Exp $
 *
 * Copyright 2004-2006 Michael Slusarz <slusarz@curecanti.org>
 *
 * 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@curecanti.org>
 * @since   Horde 3.0
 * @package Horde_IMAP
 */
class IMAP_Thread {

    /**
     * imap_thread() output.
     *
     * @var array
     */
    var $_threadarray;

    /**
     * Internal thread data structure.
     *
     * @var array
     */
    var $_thread = array();

    /**
     * Array index to Message index lookup array.
     *
     * @var array
     */
    var $_lookup = array();

    /**
     * Constructor.
     *
     * @param array $ob  Output from imap_thread().
     */
    function IMAP_Thread($ob)
    {
        $this->_threadarray = $ob;
        $this->_processStructure();
    }

   /**
     * Gets the indention level for an IMAP message index.
     *
     * @param integer $index  The IMAP message index.
     *
     * @return mixed  Returns the thread indent level if $index found.
     *                Returns false on failure.
     */
    function getThreadIndent($index)
    {
        $key = $this->_getKey($index);
        if (!is_null($key)) {
            if (isset($this->_thread[$key]['level'])) {
                return $this->_thread[$key]['level'];
            }
        }

         return false;
    }

    /**
     * Gets the base thread index for an IMAP message index.
     *
     * @param integer $index  The IMAP message index.
     *
     * @return mixed  Returns the base IMAP index if $index is part of a
     *                thread.
     *                Returns false on failure.
     */
    function getThreadBase($index)
    {
        $key = $this->_getKey($index);
        if (!is_null($key)) {
            if (!empty($this->_thread[$key]['base'])) {
                return $this->_thread[$key]['base'];
            }
        }

        return false;
    }

    /**
     * Is this index the last in the current level?
     *
     * @param integer $index  The IMAP message index.
     *
     * @return boolean  Returns true if $index is the last element in the
     *                  current thread level.
     *                  Returns false if not, or on failure.
     */
    function lastInLevel($index)
    {
        $key = $this->_getKey($index);
        if (!is_null($key)) {
            if (!empty($this->_thread[$key]['last'])) {
                return $this->_thread[$key]['last'];
            }
        }

        return false;
    }

    /**
     * Do the message index -> array index lookup.
     *
     * @access private
     *
     * @param integer $index  The IMAP message index.
     *
     * @return mixed  The array index value or null if no index.
     */
    function _getKey($index)
    {
        return (isset($this->_lookup[$index])) ? $this->_lookup[$index] : null;
    }

    /**
     * Return the sorted list of messages indices.
     *
     * @param boolean $new  True for newest first, false for oldest first.
     *
     * @return array  The sorted list of messages.
     */
    function messageList($new)
    {
        return ($new) ? array_reverse(array_keys($this->_lookup)) : array_keys($this->_lookup);
    }

    /**
     * Returns the list of messages in the current thread.
     *
     * @param integer $index  The IMAP index of the current message.
     *
     * @return array  A list of IMAP message indices.
     */
    function getThread($index)
    {
        /* Find the beginning of the thread. */
        $begin = $this->getThreadBase($index);
        $key = $this->_getKey($begin);
        if (is_null($key) || empty($begin)) {
            return array($index);
        }

        /* Work forward from the first thread element to find the end of the
         * thread. */
        $thread_list = array($this->_thread[$key]['index']);
        while (++$key) {
            if (!isset($this->_thread[$key])) {
                break;
            }
            $curr = $this->_thread[$key];
            if ($curr['base'] == $begin) {
                $thread_list[] = $this->_thread[$key]['index'];
            } else {
                break;
            }
        }

        return $thread_list;
    }

    /**
     * Process the output from imap_thread() into an internal data structure.
     *
     * @access private
     */
    function _processStructure()
    {
        $container = $container_base = $last_index = $thread_base = $thread_base_idx =  null;
        $indices = array();
        $i = $last_i = $level = 0;

        foreach ($this->_threadarray as $key => $val) {
            $pos = strpos($key, '.');
            $index = substr($key, 0, $pos);
            $type = substr($key, $pos + 1);

            switch ($type) {
            case 'num':
                if ($val === 0) {
                    $container = $index;
                } else {
                    $i++;
                    if (is_null($container) && empty($level)) {
                        $thread_base = $val;
                        $thread_base_idx = $index;
                    }
                    $this->_lookup[$val] = $index;
                    $this->_thread[$index] = array();
                    $this->_thread[$index]['index'] = $val;
                }
                break;

            case 'next':
                if (!is_null($container) && ($container === $index)) {
                    $container_base = $this->_threadarray[$val . '.num'];
                } else {
                    $i++;
                    if (!is_null($container)) {
                        $this->_thread[$index]['base'] = $container_base;
                    } else {
                        $this->_thread[$index]['base'] = (!empty($level) || ($val != 0)) ? $thread_base : null;
                    }
                    $level++;
                }
                break;

            case 'branch':
                if ($container === $index) {
                    $container = $container_base = null;
                    $this->_thread[$last_index]['last'] = true;
                } else {
                    $this->_thread[$index]['level'] = $level--;
                    $this->_thread[$index]['last'] = !(!is_null($container) && empty($level));
                    if ($index === $thread_base_idx) {
                        $index = null;
                    } elseif (!empty($level) &&
                              !is_null($last_index) &&
                              isset($this->_thread[$last_index])) {
                        $this->_thread[$last_index]['last'] = ($last_i == ($i - 1));
                    }
                }
                $last_index = $index;
                $last_i = $i++;
                break;
            }
        }
    }

}