1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id: class_plugin.inc 13896 2009-07-07 07:06:37Z hickert $$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 class listingSortIterator implements Iterator {
24 private $data;
26 public function __construct($data, $direction, $attribute, $type= "string") {
27 global $_sortAttribute;
28 global $_sortType;
29 $_sortAttribute= $attribute;
30 $_sortType= $type;
32 // Inline function to
33 function attrSort($ao, $bo) {
34 global $_sortAttribute;
35 global $_sortType;
37 // Extract values from ao and bo
38 $a= $b= "";
39 if (isset($ao[$_sortAttribute])) {
40 $a= $ao[$_sortAttribute];
41 if (is_array($a)) {
42 $a= $a[0];
43 }
44 }
45 if (isset($bo[$_sortAttribute])) {
46 $b= $bo[$_sortAttribute];
47 if (is_array($b)) {
48 $b= $b[0];
49 }
50 }
52 // Take a look at the several types
53 switch ($_sortType) {
54 case 'string':
55 return strcoll($a, $b);
57 case 'integer':
58 return $b - $a;
60 case 'date':
61 if ($a == "") {
62 $a= "31.12.0000";
63 }
64 if ($b == "") {
65 $b= "31.12.0000";
66 }
67 list($d, $m, $y)= explode('.', $a);
68 $a= (int)sprintf("%04d%02d%02d", $y, $m, $d);
69 list($d, $m, $y)= explode('.', $b);
70 $b= (int)sprintf("%04d%02d%02d", $y, $m, $d);
71 return $b-$a;
73 // Sort for string by default
74 default:
75 return strcoll($a, $b);
76 }
77 }
79 // Sort for attribute
80 if ($attribute != "") {
81 uasort($data, "attrSort");
82 }
84 // Invert if direction is set
85 if ($direction) {
86 $this->data= array_reverse($data, true);
87 } else {
88 $this->data= $data;
89 }
90 }
92 function rewind() {
93 return reset($this->data);
94 }
96 function current() {
97 return current($this->data);
98 }
100 function key() {
101 return key($this->data);
102 }
104 function next() {
105 return next($this->data);
106 }
108 function valid() {
109 return key($this->data) !== null;
110 }
111 }
113 ?>