1 |
<?php |
2 |
/** |
3 |
* $Id: ArrayDataListSource.inc,v 1.2 2003/07/31 22:11:44 hemna Exp $ |
4 |
* |
5 |
* This file contains the DataListSource class |
6 |
* that gets the data from an array |
7 |
* |
8 |
* @author Suren Markossian <suren_markossian@yahoo.com> |
9 |
* @package phpHtmlLib |
10 |
*/ |
11 |
|
12 |
|
13 |
|
14 |
|
15 |
/** |
16 |
* |
17 |
* This DataListSource child class gets the data from an |
18 |
* external array |
19 |
* |
20 |
* The array is a 2D array and each sub-array is a set of elements for |
21 |
* each row in the list |
22 |
* |
23 |
* @author Suren Markossian <suren_markossian@yahoo.com> |
24 |
* @package phpHtmlLib |
25 |
*/ |
26 |
class ArrayDataListSource extends DataListSource { |
27 |
|
28 |
|
29 |
/** |
30 |
* If true the prequery filter will be |
31 |
* run on the data array |
32 |
*/ |
33 |
var $_prequery_filter_flag = FALSE; |
34 |
|
35 |
/** |
36 |
* The constructor. |
37 |
* |
38 |
* @param array data - an array of array elements for each row |
39 |
*/ |
40 |
function ArrayDataListSource(&$data) { |
41 |
$this->_data = $data; |
42 |
} |
43 |
|
44 |
/** |
45 |
* This function does the query |
46 |
* and search/sort |
47 |
*/ |
48 |
function do_query() { |
49 |
|
50 |
// do data pre-processing if neccessary |
51 |
if ($this->_prequery_filter_flag) $this->_prequery_filter(); |
52 |
|
53 |
$count = count($this->_data); |
54 |
|
55 |
if ($count > 0) { |
56 |
$this->set_total_rows($count); |
57 |
$this->sort(); |
58 |
return true; |
59 |
} else { |
60 |
return false; |
61 |
} |
62 |
} |
63 |
|
64 |
/** |
65 |
* This is a method that should be defined by the |
66 |
* child class to do any pre-query type of things. |
67 |
* Such as building a sql query string for a DB, |
68 |
* or checking to make sure the file on disk exists |
69 |
* if the source is a file on disk. |
70 |
* |
71 |
*/ |
72 |
function do_prequery() { |
73 |
return NULL; |
74 |
} |
75 |
|
76 |
/** |
77 |
* This function returns the next row of |
78 |
* valid data. |
79 |
* |
80 |
*/ |
81 |
function get_next_data_row() { |
82 |
$index = $this->get_data_index(); |
83 |
$limit = $this->get_limit(); |
84 |
$offset = $this->get_offset(); |
85 |
|
86 |
if ($limit == -1) { |
87 |
// don't limit the data |
88 |
if ($index > $this->get_total_rows()) { |
89 |
return NULL; |
90 |
} else { |
91 |
return $this->_data[$index]; |
92 |
} |
93 |
} else { |
94 |
if (isset($this->_data[$index])) { |
95 |
return $this->_data[$index]; |
96 |
} else { |
97 |
return NULL; |
98 |
} |
99 |
} |
100 |
} |
101 |
|
102 |
/** |
103 |
* This is used to perform pre-query filtering |
104 |
* Gives us a chance to run the next row through a filter |
105 |
* before any processing has been done |
106 |
* |
107 |
* @param array - the row to run through the filter |
108 |
* |
109 |
* @return boolean - TRUE = allow the row. FALSE = drop it. |
110 |
*/ |
111 |
function prequery_row_filter(&$row_data) { |
112 |
return TRUE; |
113 |
} |
114 |
|
115 |
/** |
116 |
* This is called to allow rebuilding the data array |
117 |
* to remove elements that have to be filtered |
118 |
* |
119 |
* This is done on pre-processing phase and used for |
120 |
* simple and advanced searches |
121 |
* |
122 |
*/ |
123 |
function _prequery_filter() { |
124 |
|
125 |
while (list($key,$row_data) = each($this->_data)) { |
126 |
if (!$this->prequery_row_filter($row_data)) { |
127 |
unset($this->_data[$key]); |
128 |
} |
129 |
} |
130 |
|
131 |
// re-index the array |
132 |
$this->_data = array_values($this->_data); |
133 |
} |
134 |
|
135 |
/** |
136 |
* Sets the pre-query filter flag |
137 |
* If true, a filter will be run on the data array |
138 |
* before any processing is done |
139 |
* |
140 |
* @param bool flag |
141 |
*/ |
142 |
function set_prequery_filter_flag($flag) { |
143 |
$this->_prequery_filter_flag = $flag; |
144 |
|
145 |
} |
146 |
|
147 |
/** |
148 |
* This function returns the |
149 |
* data_index value and increments it |
150 |
* |
151 |
* @return int |
152 |
*/ |
153 |
function get_data_index() { |
154 |
$this->_data_index = key($this->_data); |
155 |
next($this->_data); |
156 |
return $this->_data_index; |
157 |
} |
158 |
|
159 |
function sort() { |
160 |
$sortby = $this->get_orderby(); |
161 |
// we need to sort the data by the |
162 |
// column in the table |
163 |
if ($this->_is_column_sortable($sortby)) { |
164 |
// looks like we can sort this column |
165 |
usort($this->_data, array($this, "cmp")); |
166 |
} |
167 |
} |
168 |
|
169 |
function cmp($data1, $data2) { |
170 |
$sortby = $this->get_orderby(); |
171 |
$ret = strnatcmp( $data1[$sortby], $data2[$sortby]); |
172 |
if ($this->get_reverseorder() == "true") { |
173 |
$ret *= -1; |
174 |
} |
175 |
return $ret; |
176 |
} |
177 |
|
178 |
} |
179 |
?> |