9 |
* @package phpHtmlLib |
* @package phpHtmlLib |
10 |
*/ |
*/ |
11 |
|
|
12 |
|
/** |
13 |
|
* We require the DataListSource |
14 |
|
*/ |
15 |
|
require_once($phphtmllib."/widgets/data_list/DataListSource.inc"); |
16 |
|
|
17 |
|
|
18 |
/** |
/** |
30 |
|
|
31 |
|
|
32 |
/** |
/** |
|
* If true the prequery filter will be |
|
|
* run on the data array |
|
|
*/ |
|
|
var $_prequery_filter_flag = FALSE; |
|
|
|
|
|
/** |
|
33 |
* The constructor. |
* The constructor. |
34 |
* |
* |
35 |
* @param array data - an array of array elements for each row |
* @param array data - an array of array elements for each row |
45 |
function do_query() { |
function do_query() { |
46 |
|
|
47 |
// do data pre-processing if neccessary |
// do data pre-processing if neccessary |
48 |
if ($this->_prequery_filter_flag) $this->_prequery_filter(); |
if ($this->get_searchby_value()) { |
49 |
|
$this->_prequery_filter(); |
50 |
|
} |
51 |
|
|
52 |
$count = count($this->_data); |
$count = count($this->_data); |
53 |
|
|
90 |
return $this->_data[$index]; |
return $this->_data[$index]; |
91 |
} |
} |
92 |
} else { |
} else { |
93 |
if (isset($this->_data[$index])) { |
// Check if we are still in the display range |
94 |
return $this->_data[$index]; |
if (!is_null($index) && $index < $limit && isset($this->_data[$offset + $index])) { |
95 |
|
return $this->_data[$offset + $index]; |
96 |
} else { |
} else { |
97 |
return NULL; |
return NULL; |
98 |
} |
} |
109 |
* @return boolean - TRUE = allow the row. FALSE = drop it. |
* @return boolean - TRUE = allow the row. FALSE = drop it. |
110 |
*/ |
*/ |
111 |
function prequery_row_filter(&$row_data) { |
function prequery_row_filter(&$row_data) { |
112 |
return TRUE; |
return $this->_find_data($row_data); |
113 |
} |
} |
114 |
|
|
115 |
/** |
/** |
133 |
} |
} |
134 |
|
|
135 |
/** |
/** |
|
* Sets the pre-query filter flag |
|
|
* If true, a filter will be run on the data array |
|
|
* before any processing is done |
|
|
* |
|
|
* @param bool flag |
|
|
*/ |
|
|
function set_prequery_filter_flag($flag) { |
|
|
$this->_prequery_filter_flag = $flag; |
|
|
|
|
|
} |
|
|
|
|
|
/** |
|
136 |
* This function returns the |
* This function returns the |
137 |
* data_index value and increments it |
* data_index value and increments it |
138 |
* |
* |
144 |
return $this->_data_index; |
return $this->_data_index; |
145 |
} |
} |
146 |
|
|
147 |
|
/** |
148 |
|
* This method sorts our data array by the requested |
149 |
|
* column order by. This could be expensive for HUGE |
150 |
|
* arrays. |
151 |
|
* |
152 |
|
* @return none |
153 |
|
*/ |
154 |
function sort() { |
function sort() { |
155 |
$sortby = $this->get_orderby(); |
$sortby = $this->get_orderby(); |
156 |
|
|
157 |
|
$column_name = NULL; |
158 |
|
// get the column name |
159 |
|
foreach($this->_columns as $name => $data) { |
160 |
|
if ($sortby == $data["data_name"]) { |
161 |
|
$column_name = $name; |
162 |
|
break; |
163 |
|
} |
164 |
|
} |
165 |
|
|
166 |
// we need to sort the data by the |
// we need to sort the data by the |
167 |
// column in the table |
// column in the table |
168 |
if ($this->_is_column_sortable($sortby)) { |
if ($column_name != NULL && $this->_columns[$column_name]["sortable"] != NOT_SORTABLE) { |
169 |
// looks like we can sort this column |
// looks like we can sort this column |
170 |
usort($this->_data, array($this, "cmp")); |
if ($this->_columns[$column_name]["sortable"] == SORTABLE_NUMERIC) { |
171 |
|
usort($this->_data, array($this, "cmp_numeric")); |
172 |
|
} else { |
173 |
|
usort($this->_data, array($this, "cmp")); |
174 |
|
} |
175 |
} |
} |
176 |
} |
} |
177 |
|
|
178 |
|
/** |
179 |
|
* This method is used for comparing 2 string values |
180 |
|
* |
181 |
|
* @param string first entry to compare |
182 |
|
* @param string second entry to compare |
183 |
|
* @return natural compare results |
184 |
|
*/ |
185 |
function cmp($data1, $data2) { |
function cmp($data1, $data2) { |
186 |
$sortby = $this->get_orderby(); |
$sortby = $this->get_orderby(); |
187 |
|
|
188 |
$ret = strnatcmp( $data1[$sortby], $data2[$sortby]); |
$ret = strnatcmp( $data1[$sortby], $data2[$sortby]); |
189 |
|
|
190 |
if ($this->get_reverseorder() == "true") { |
if ($this->get_reverseorder() == "true") { |
191 |
$ret *= -1; |
$ret *= -1; |
192 |
} |
} |
193 |
return $ret; |
return $ret; |
194 |
} |
} |
195 |
|
|
196 |
|
/** |
197 |
|
* This method is used for comparing 2 numerical |
198 |
|
* values |
199 |
|
* |
200 |
|
* @param string first entry to compare |
201 |
|
* @param string second entry to compare |
202 |
|
* @return compare results |
203 |
|
*/ |
204 |
|
function cmp_numeric($data1, $data2) { |
205 |
|
|
206 |
|
$sortby = $this->get_orderby(); |
207 |
|
if ($data1[$sortby]==$data2[$sortby]) $ret = 0; |
208 |
|
else $ret = ($data1[$sortby] < $data2[$sortby]) ? -1 : 1; |
209 |
|
|
210 |
|
if ($this->get_reverseorder() == "true") { |
211 |
|
$ret *= -1; |
212 |
|
} |
213 |
|
return $ret; |
214 |
|
} |
215 |
|
|
216 |
|
/** |
217 |
|
* This is used to do the default search |
218 |
|
* capability in the DataListSource parent class. |
219 |
|
* This is used so we can filter our results to |
220 |
|
* what the user searched for. |
221 |
|
* |
222 |
|
* @param array the row data entry. |
223 |
|
* We will look for a specific column's |
224 |
|
* value here to test against. |
225 |
|
* @return boolean found it or not |
226 |
|
*/ |
227 |
|
function _find_data( $row_data ) { |
228 |
|
//look for the 'needle' in the 'haystack' |
229 |
|
|
230 |
|
$needle = $this->get_searchby_value(); |
231 |
|
$haystack = $row_data[$this->get_searchby()]; |
232 |
|
|
233 |
|
switch ($this->get_simplesearch_modifier()) { |
234 |
|
case "BEGINS": |
235 |
|
if (strncasecmp($needle, $haystack, strlen($needle)) == 0) { |
236 |
|
return TRUE; |
237 |
|
} else { |
238 |
|
return FALSE; |
239 |
|
} |
240 |
|
break; |
241 |
|
|
242 |
|
case "CONTAINS": |
243 |
|
if (stristr($haystack, $needle)) { |
244 |
|
return TRUE; |
245 |
|
} else { |
246 |
|
return FALSE; |
247 |
|
} |
248 |
|
break; |
249 |
|
case "EXACT": |
250 |
|
if (strcasecmp($needle, $haystack) == 0) { |
251 |
|
return TRUE; |
252 |
|
} else { |
253 |
|
return FALSE; |
254 |
|
} |
255 |
|
break; |
256 |
|
case "ENDS": |
257 |
|
$needle_len = strlen( $needle ); |
258 |
|
$haystack_len = strlen( $haystack ); |
259 |
|
if ($needle_len > $haystack_len) { |
260 |
|
return FALSE; |
261 |
|
} else if ($needle_len == $haystack_len) { |
262 |
|
$tmp = $haystack; |
263 |
|
} else { |
264 |
|
$tmp = substr($haystack, $haystack_len - $needle_len); |
265 |
|
} |
266 |
|
if (strncasecmp($needle, $tmp, $needle_len) == 0) { |
267 |
|
return TRUE; |
268 |
|
} else { |
269 |
|
return FALSE; |
270 |
|
} |
271 |
|
break; |
272 |
|
} |
273 |
|
} |
274 |
|
|
275 |
} |
} |
276 |
?> |
?> |