source: subversion/branches/devel-vnext/program/include/rcube_header_sorter.php @ 1018

Last change on this file since 1018 was 1018, checked in by tomekp, 5 years ago

more merging

  • Property svn:executable set to *
File size: 2.0 KB
Line 
1<?php
2/**
3 * rcube_header_sorter
4 *
5 * Class for sorting an array of iilBasicHeader objects in a predetermined order.
6 *
7 * @author  Eric Stadtherr
8 * @license GPL
9 */
10class rcube_header_sorter {
11    private $sequence_numbers = array();
12
13    /**
14     * Set the predetermined sort order.
15     *
16     * @param array Numerically indexed array of IMAP message sequence numbers
17     */
18    public function set_sequence_numbers($seqnums = array()) {
19        $this->sequence_numbers = $seqnums;
20    }
21
22    /**
23     * Sort the array of header objects
24     *
25     * @param array Array of iilBasicHeader objects indexed by UID
26     */
27    public function sort_headers($headers) {
28        /**
29         * uksort would work if the keys were the sequence number, but unfortunately
30         * the keys are the UIDs.  We'll use uasort instead and dereference the value
31         * to get the sequence number (in the "id" field).
32         *
33         * uksort($headers, array($this, "compare_seqnums"));
34         */
35        uasort($headers, array($this, 'compare_seqnums'));
36    }
37
38    /**
39     * Get the position of a message sequence number in my sequence_numbers array
40     *
41     * @param int Message sequence number contained in sequence_numbers
42     * @return int Position, -1 if not found
43     */
44    public function position_of($seqnum) {
45        for ($pos = 0, $c = count($this->sequence_numbers); $pos <= $c; $pos++) {
46            if ($this->sequence_numbers[$pos] == $seqnum) {
47                return $pos;
48            }
49        }
50        return -1;
51    }
52
53    /**
54     * Sort method called by uasort()
55     */
56    public function compare_seqnums($a, $b) {
57        // First get the sequence number from the header object (the 'id' field).
58        $seqa = $a->id;
59        $seqb = $b->id;
60
61        // then find each sequence number in my ordered list
62        $posa = $this->position_of($seqa);
63        $posb = $this->position_of($seqb);
64
65        // return the relative position as the comparison value
66        $ret = $posa - $posb;
67        return $ret;
68    }
69}
70
71?>
Note: See TracBrowser for help on using the repository browser.