FusionDirectory
 All Data Structures Files Functions Variables
class_listingSortIterator.inc
Go to the documentation of this file.
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4  Copyright (C) 2003-2010 Cajus Pollmeier
5  Copyright (C) 2011-2016 FusionDirectory
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21 
31 class listingSortIterator implements Iterator {
32  protected $data;
33 
45  public function __construct($data, $direction, $attribute, $type = 'string')
46  {
47  // Sort for attribute
48  if ($attribute != '') {
49  uasort(
50  $data,
51  function ($ao, $bo) use ($attribute, $type)
52  {
53  if ($attribute == 'sort-attribute') {
54  // Override sort attribute from data if needed
55  $attribute_a = $ao['sort-attribute'];
56  $attribute_b = $bo['sort-attribute'];
57  } else {
58  $attribute_a = $attribute;
59  $attribute_b = $attribute;
60  }
61 
62  // Extract values from ao and bo
63  $a = $b = '';
64  if (isset($ao[$attribute_a])) {
65  $a = $ao[$attribute_a];
66  if (is_array($a)) {
67  $a = $a[0];
68  }
69  }
70  if (isset($bo[$attribute_b])) {
71  $b = $bo[$attribute_b];
72  if (is_array($b)) {
73  $b = $b[0];
74  }
75  }
76 
77  // Take a look at the several types
78  switch ($type) {
79  // Sort for string by default
80  default:
81  case 'string':
82  return strcoll($a, $b);
83 
84  case 'department':
85  return strnatcmp($a, $b);
86 
87  case 'integer':
88  return $b - $a;
89 
90  case 'date':
91  if ($a == '') {
92  $a = '31.12.0000';
93  }
94  if ($b == '') {
95  $b = '31.12.0000';
96  }
97  list($d, $m, $y) = explode('.', $a);
98  $a = (int)sprintf('%04d%02d%02d', $y, $m, $d);
99  list($d, $m, $y) = explode('.', $b);
100  $b = (int)sprintf('%04d%02d%02d', $y, $m, $d);
101  return $b - $a;
102 
103  case 'ip':
104  $parts_a = explode('.', $a, 4);
105  $parts_b = explode('.', $b, 4);
106  for ($i = 0; $i < 4; $i++) {
107  if ((int)($parts_a[$i]) != (int)($parts_b[$i])) {
108  return (int)($parts_a[$i]) - (int)($parts_b[$i]);
109  }
110  }
111  return 0;
112  }
113  }
114  );
115  }
116 
117  // Invert if direction is set
118  if ($direction) {
119  $this->data = array_reverse($data, TRUE);
120  } else {
121  $this->data = $data;
122  }
123  }
124 
130  function rewind()
131  {
132  return reset($this->data);
133  }
134 
140  function current()
141  {
142  return current($this->data);
143  }
144 
150  function key()
151  {
152  return key($this->data);
153  }
154 
160  function next()
161  {
162  return next($this->data);
163  }
164 
170  function valid()
171  {
172  return (key($this->data) !== NULL);
173  }
174 }
175 ?>
key()
Get the key element.
current()
Get the current data element.
__construct($data, $direction, $attribute, $type= 'string')
listingSortIterator constructor
This class contains all the function needed to sort list go up, go down , back , next. etc...
next()
Get the next data element.
valid()
Check if the data array is valid.
rewind()
Put the array pointer to the first element.