| 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 | */ |
|---|
| 10 | class rcube_header_sorter |
|---|
| 11 | { |
|---|
| 12 | var $sequence_numbers = array(); |
|---|
| 13 | |
|---|
| 14 | /** |
|---|
| 15 | * set the predetermined sort order. |
|---|
| 16 | * |
|---|
| 17 | * @param array $seqnums numerically indexed array of IMAP message sequence numbers |
|---|
| 18 | */ |
|---|
| 19 | public function set_sequence_numbers($seqnums) |
|---|
| 20 | { |
|---|
| 21 | $this->sequence_numbers = $seqnums; |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * sort the array of header objects |
|---|
| 26 | * |
|---|
| 27 | * @param array $headers array of iilBasicHeader objects indexed by UID |
|---|
| 28 | */ |
|---|
| 29 | public function sort_headers(&$headers) |
|---|
| 30 | { |
|---|
| 31 | /** |
|---|
| 32 | * uksort would work if the keys were the sequence number, but unfortunately |
|---|
| 33 | * the keys are the UIDs. We'll use uasort instead and dereference the value |
|---|
| 34 | * to get the sequence number (in the "id" field). |
|---|
| 35 | * |
|---|
| 36 | * uksort($headers, array($this, "compare_seqnums")); |
|---|
| 37 | */ |
|---|
| 38 | uasort($headers, array($this, "compare_seqnums")); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | /** |
|---|
| 42 | * get the position of a message sequence number in my sequence_numbers array |
|---|
| 43 | * |
|---|
| 44 | * @param integer $seqnum message sequence number contained in sequence_numbers |
|---|
| 45 | */ |
|---|
| 46 | public function position_of($seqnum) |
|---|
| 47 | { |
|---|
| 48 | $c = count($this->sequence_numbers); |
|---|
| 49 | for ($pos = 0; $pos <= $c; $pos++) { |
|---|
| 50 | if ($this->sequence_numbers[$pos] == $seqnum) { |
|---|
| 51 | return $pos; |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | return -1; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | /** |
|---|
| 58 | * Sort method called by uasort() |
|---|
| 59 | */ |
|---|
| 60 | public function compare_seqnums($a, $b) |
|---|
| 61 | { |
|---|
| 62 | // First get the sequence number from the header object (the 'id' field). |
|---|
| 63 | $seqa = $a->id; |
|---|
| 64 | $seqb = $b->id; |
|---|
| 65 | |
|---|
| 66 | // then find each sequence number in my ordered list |
|---|
| 67 | $posa = $this->position_of($seqa); |
|---|
| 68 | $posb = $this->position_of($seqb); |
|---|
| 69 | |
|---|
| 70 | // return the relative position as the comparison value |
|---|
| 71 | $ret = $posa - $posb; |
|---|
| 72 | return $ret; |
|---|
| 73 | } |
|---|
| 74 | } |
|---|