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 strnatcmp($a, $b);
57 // Sort for string by default
58 default:
59 return strnatcmp($a, $b);
60 }
61 }
63 // Sort for attribute
64 if ($attribute != "") {
65 uasort($data, "attrSort");
66 }
68 // Invert if direction is set
69 if ($direction) {
70 $this->data= array_reverse($data, true);
71 } else {
72 $this->data= $data;
73 }
74 }
76 function rewind() {
77 return reset($this->data);
78 }
80 function current() {
81 return current($this->data);
82 }
84 function key() {
85 return key($this->data);
86 }
88 function next() {
89 return next($this->data);
90 }
92 function valid() {
93 return key($this->data) !== null;
94 }
95 }
97 ?>