Code

Some backport from trunk
[gosa.git] / gosa-core / include / class_departmentSortIterator.inc
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$$
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 departmentSortIterator implements Iterator {
24   private $data;
26   public function __construct($data, $direction) {
28     function depSort($ao, $bo) {
29       // Override sort attribute from data if needed
30       $attribute_a= $ao['sort-attribute'];
31       $attribute_b= $bo['sort-attribute'];
33       // Extract values from ao and bo
34       $a= $b= "";
35       if (isset($ao[$attribute_a])) {
36         $a= $ao[$attribute_a];
37         if (is_array($a)) {
38           $a= $a[0];
39         }
40       }
41       if (isset($bo[$attribute_b])) {
42         $b= $bo[$attribute_b];
43         if (is_array($b)) {
44           $b= $b[0];
45         }
46       }
48       // Sort for string by default
49       return strnatcmp($a, $b);
50     }
52     // Sort for attribute
53     uasort($data, "depSort");
55     // Invert if direction is set
56     if ($direction) {
57       $this->data= array_reverse($data, true);
58     } else {
59       $this->data= $data;
60     }
61   }
63   function rewind() {
64     return reset($this->data);
65   }
67   function current() {
68     return current($this->data);
69   }
71   function key() {
72     return key($this->data);
73   }
75   function next() {
76     return next($this->data);
77   }
79   function valid() {
80     return key($this->data) !== null;
81   }
82 }
84 ?>