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 list($d, $m, $y)= split('\.', $a);
62 $a= (int)sprintf("%04d%02d%02d", $y, $m, $d);
63 list($d, $m, $y)= split('\.', $b);
64 $b= (int)sprintf("%04d%02d%02d", $y, $m, $d);
65 return $b-$a;
67 // Sort for string by default
68 default:
69 return strcoll($a, $b);
70 }
71 }
73 // Sort for attribute
74 if ($attribute != "") {
75 uasort($data, "attrSort");
76 }
78 // Invert if direction is set
79 if ($direction) {
80 $this->data= array_reverse($data, true);
81 } else {
82 $this->data= $data;
83 }
84 }
86 function rewind() {
87 return reset($this->data);
88 }
90 function current() {
91 return current($this->data);
92 }
94 function key() {
95 return key($this->data);
96 }
98 function next() {
99 return next($this->data);
100 }
102 function valid() {
103 return key($this->data) !== null;
104 }
105 }
107 ?>