1 |
commit: 200852fe6592cf0e4aa026acf29499542c173806 |
2 |
Author: Brian Evans <grknight <AT> gentoo <DOT> org> |
3 |
AuthorDate: Tue Jan 30 15:18:07 2018 +0000 |
4 |
Commit: Brian Evans <grknight <AT> gentoo <DOT> org> |
5 |
CommitDate: Tue Jan 30 15:18:07 2018 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/bouncer.git/commit/?id=200852fe |
7 |
|
8 |
Revert "Remove the unused php/lib/list.php" |
9 |
|
10 |
This reverts commit 011b963d1f61307fa0dbcef713224a0353e5801e. |
11 |
|
12 |
php/lib/list.php | 391 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
13 |
1 file changed, 391 insertions(+) |
14 |
|
15 |
diff --git a/php/lib/list.php b/php/lib/list.php |
16 |
new file mode 100644 |
17 |
index 0000000..5deb5e9 |
18 |
--- /dev/null |
19 |
+++ b/php/lib/list.php |
20 |
@@ -0,0 +1,391 @@ |
21 |
+<?php |
22 |
+/** |
23 |
+ * List functions for lists of values. |
24 |
+ * @package mirror |
25 |
+ * @subpackage lib |
26 |
+ * @author Mike Morgan <mike.morgan@×××××××××××.edu> |
27 |
+ * |
28 |
+ * Usage example: |
29 |
+ * <code> |
30 |
+ * $orderby=get_order(); |
31 |
+ * $query="SELECT * FROM fic_courses $orderby"; |
32 |
+ * $courses=db_get($query,MYSQL_ASSOC); |
33 |
+ * $headers=array( |
34 |
+ * 'course_id'=>'', |
35 |
+ * 'title'=>'Course Title', |
36 |
+ * 'date_start_course'=>'Start', |
37 |
+ * 'date_end_course'=>'End', |
38 |
+ * 'date_start_reg'=>'Reg Starts', |
39 |
+ * 'date_end_reg'=>'Reg Ends', |
40 |
+ * 'active'=>'Active?', |
41 |
+ * 'entry_date'=>'Created' |
42 |
+ * ); |
43 |
+ * show_list($courses,$headers); |
44 |
+ * </code> |
45 |
+ * |
46 |
+ * Accompanying CSS for table output: |
47 |
+ * <code> |
48 |
+ * .list |
49 |
+ * { |
50 |
+ * border:1px solid #999; |
51 |
+ * } |
52 |
+ * .list th |
53 |
+ * { |
54 |
+ * background:#eee; |
55 |
+ * border:1px solid #000; |
56 |
+ * font-weight:bold; |
57 |
+ * } |
58 |
+ * .list th a |
59 |
+ * { |
60 |
+ * display:block; |
61 |
+ * padding:0 14px; |
62 |
+ * } |
63 |
+ * .list th a:hover |
64 |
+ * { |
65 |
+ * background-color:#fff; |
66 |
+ * } |
67 |
+ * .row1 |
68 |
+ * { |
69 |
+ * background:#ddd; |
70 |
+ * } |
71 |
+ * .row2 |
72 |
+ * { |
73 |
+ * background:#ccc; |
74 |
+ * } |
75 |
+ * .row1:hover, .row2:hover |
76 |
+ * { |
77 |
+ * background-color:#fec; |
78 |
+ * } |
79 |
+ * .current-sort |
80 |
+ * { |
81 |
+ * background:#fda; |
82 |
+ * } |
83 |
+ * .sort-desc |
84 |
+ * { |
85 |
+ * background:#fec url(../img/up.gif) no-repeat right; |
86 |
+ * } |
87 |
+ * .sort-asc |
88 |
+ * { |
89 |
+ * background:#fec url(../img/down.gif) no-repeat right; |
90 |
+ * } |
91 |
+ * </code> |
92 |
+ |
93 |
+ * Accompanying JavaScript for select all / inverse: |
94 |
+ * <code> |
95 |
+ * <script type="text/javascript"> |
96 |
+ * //<!-- |
97 |
+ * function selectAll(formObj,invert) |
98 |
+ * { |
99 |
+ * for (var i=0;i < formObj.elements.length;i++) |
100 |
+ * { |
101 |
+ * fldObj = formObj.elements[i]; |
102 |
+ * if (fldObj.type == 'checkbox') |
103 |
+ * { |
104 |
+ * if (invert==1) |
105 |
+ * { |
106 |
+ * fldObj.checked = (fldObj.checked) ? false : true; |
107 |
+ * } |
108 |
+ * else |
109 |
+ * { |
110 |
+ * fldObj.checked = true; |
111 |
+ * } |
112 |
+ * } |
113 |
+ * } |
114 |
+ * } |
115 |
+ * //--> |
116 |
+ * </script> |
117 |
+ * </code> |
118 |
+ */ |
119 |
+ |
120 |
+/** |
121 |
+ * Show a list of values, for forms. |
122 |
+ * @param array $list associative array |
123 |
+ * @param array $headers column name => column title (for table heads) |
124 |
+ * @param string $type checkbox, radio, simple |
125 |
+ * @param array $array actions to display in actions select list |
126 |
+ * @param string $form_id id of form holding list |
127 |
+ * @param bool $sortable whether or not to show sortable column headers (links in th's) |
128 |
+ * @param array|string $selected if type is checkbox, array otherwise string with one val |
129 |
+ */ |
130 |
+function show_list($list,$headers,$type='checkbox',$actions=null,$form_id=null,$sortable=true,$selected=null) |
131 |
+{ |
132 |
+ if ( is_array($list) && count($list)>0 && is_array($headers) ) |
133 |
+ { |
134 |
+ if ( $type!='simple' && !empty($_GET['sort']) && !empty($_GET['order']) ) |
135 |
+ { |
136 |
+ form_hidden('sort',$_GET['sort']); |
137 |
+ form_hidden('order',$_GET['order']); |
138 |
+ } |
139 |
+ echo "\n".'<table class="list">'; |
140 |
+ show_headers($headers,$type,$sortable); |
141 |
+ echo "\n".'<tbody>'; |
142 |
+ foreach ($list as $row) |
143 |
+ { |
144 |
+ show_row($headers,$row,$type,$count++,$selected); |
145 |
+ } |
146 |
+ echo "\n".'</tbody>'; |
147 |
+ echo "\n".'</table>'; |
148 |
+ if ($type=='checkbox') |
149 |
+ { |
150 |
+echo <<<js |
151 |
+<script type="text/javascript"> |
152 |
+//<!-- |
153 |
+function list_select(formObj,invert) |
154 |
+{ |
155 |
+ for (var i=0;i < formObj.elements.length;i++) |
156 |
+ { |
157 |
+ fldObj = formObj.elements[i]; |
158 |
+ if (fldObj.type == 'checkbox') |
159 |
+ { |
160 |
+ if (invert==1) |
161 |
+ { |
162 |
+ fldObj.checked = (fldObj.checked) ? false : true; |
163 |
+ } |
164 |
+ else |
165 |
+ { |
166 |
+ fldObj.checked = true; |
167 |
+ } |
168 |
+ } |
169 |
+ } |
170 |
+} |
171 |
+//--> |
172 |
+</script> |
173 |
+js; |
174 |
+ echo "\n".'<p><input type="button" name="selectall" onclick="list_select(this.form,0);" class="button2" value="Select All"/> <input type="button" name="selectall" onclick="list_select(this.form,1);" class="button2" value="Invert"/></p>'; |
175 |
+ } |
176 |
+ if ($type=='radio'||$type='checkbox-small') |
177 |
+ { |
178 |
+ echo '<br />'; |
179 |
+ } |
180 |
+ if (is_array($actions)&&$type!='simple') |
181 |
+ { |
182 |
+ if (count($actions) == 1) { |
183 |
+ $actions = array_values($actions); |
184 |
+ echo '<p>'; |
185 |
+ form_submit('submit','submit','button1',$actions[0].' »'); |
186 |
+ echo '</p>'; |
187 |
+ } else { |
188 |
+ echo '<p>'; |
189 |
+ echo '<label for="action">With selected: </label>'; |
190 |
+ form_select('action','action','text2',$actions,''); |
191 |
+ form_submit('submit','submit','button1','Go »'); |
192 |
+ echo '</p>'; |
193 |
+ } |
194 |
+ } |
195 |
+ } |
196 |
+ elseif ( !is_array($headers) ) |
197 |
+ { |
198 |
+ echo "\n".'<h1>FIX HEADERS ARRAY</h1>'; |
199 |
+ } |
200 |
+ else |
201 |
+ { |
202 |
+ echo "\n".'<p>No records found.</p>'; |
203 |
+ } |
204 |
+} |
205 |
+ |
206 |
+/** |
207 |
+ * Show table headers. |
208 |
+ * @param array $headers column name => column title (for table heads) |
209 |
+ * @param string $type type of list that is being shown |
210 |
+ * @param bool $sortable whether or not to show sortable column headers (links in th's) |
211 |
+ */ |
212 |
+function show_headers($headers,$type,$sortable=true) |
213 |
+{ |
214 |
+ echo "\n".'<thead><tr>'; |
215 |
+ $sort=$_GET['sort']; |
216 |
+ $order=get_order(); |
217 |
+ $count=0; |
218 |
+ foreach ($headers as $col=>$title) |
219 |
+ { |
220 |
+ if ( !empty($sort) && !empty($order) ) |
221 |
+ { |
222 |
+ if ($col==$sort && $order=='ASC') |
223 |
+ { |
224 |
+ $a_class=' class="sort-asc current-sort" '; |
225 |
+ } |
226 |
+ elseif ($col==$sort && $order=='DESC') |
227 |
+ { |
228 |
+ $a_class=' class="sort-desc current-sort" '; |
229 |
+ } |
230 |
+ else |
231 |
+ { |
232 |
+ $a_class=null; |
233 |
+ } |
234 |
+ } |
235 |
+ if ($type!='simple'&&$count==0) |
236 |
+ { |
237 |
+ echo "\n".'<th> </th>'; |
238 |
+ next; |
239 |
+ } |
240 |
+ elseif($sortable) |
241 |
+ { |
242 |
+ $qs = array(); |
243 |
+ foreach ($_GET as $qn=>$qv) { $qs[$qn] = $qv; } // existing query string variables |
244 |
+ $qs['sort'] = $col; // add/replace sort to query string |
245 |
+ $qs['order'] = $order; // add/replace order by to query string |
246 |
+ foreach ($qs as $qn=>$qv) { $querystring[] = $qn.'='.$qv; } // existing query string variables |
247 |
+ echo "\n".'<th><a '.$a_class.'href="'.$_SERVER['PHP_SELF'].'?'.implode('&',$querystring).'">'.$title.'</a></th>'; |
248 |
+ unset($qs); |
249 |
+ unset($querystring); |
250 |
+ } |
251 |
+ else |
252 |
+ { |
253 |
+ echo "\n".'<th>'.$title.'</th>'; |
254 |
+ } |
255 |
+ $count++; |
256 |
+ } |
257 |
+ echo "\n".'</tr></thead>'; |
258 |
+} |
259 |
+ |
260 |
+/** |
261 |
+ * Show table data. |
262 |
+ * @param array $headers column name => column title (for knowing which ones to display) |
263 |
+ * @param array $row table row, assoc |
264 |
+ * @param string $type type of table, determines first column, which could be an input |
265 |
+ * @param array|string $selected selected items; if type is checkbox, array otherwise string with one val |
266 |
+ */ |
267 |
+function show_row($headers,$row,$type,$num=null,$selected=null) |
268 |
+{ |
269 |
+ $indexes=array_keys($headers); |
270 |
+ $idname = $indexes[0]; |
271 |
+ $count=0; |
272 |
+ $tr_class=($num%2)?' class="row1" ':' class="row2" '; |
273 |
+ echo "\n".'<tr'.$tr_class.'>'; |
274 |
+ foreach ($indexes as $index) |
275 |
+ { |
276 |
+ $row[$index]=clean_out($row[$index]); |
277 |
+ if ($type!='simple'&&$count==0) |
278 |
+ { |
279 |
+ $id=preg_replace('/[^[:alnum:]]/', '', $index).$row[$index]; |
280 |
+ if ($type=='checkbox'||$type=='checkbox-small') |
281 |
+ { |
282 |
+ echo "\n".'<td>'; |
283 |
+ form_checkbox($idname.'[]',$id,null,$row[$index],(is_array($selected) && in_array($row[$index], $selected))); |
284 |
+ echo "\n".'</td>'; |
285 |
+ } |
286 |
+ elseif ($type=='radio') |
287 |
+ { |
288 |
+ echo "\n".'<td>'; |
289 |
+ form_radio($idname,$id,null,$row[$index], ($row[$index] == $selected)); |
290 |
+ echo "\n".'</td>'; |
291 |
+ } |
292 |
+ } |
293 |
+ else |
294 |
+ { |
295 |
+ echo ($type=='simple')?"\n".'<td>'.$row[$index].'</td>':"\n".'<td><label for="'.$id.'">'.$row[$index].'</label></td>'; |
296 |
+ } |
297 |
+ $count++; |
298 |
+ } |
299 |
+ echo "\n".'</tr>'; |
300 |
+} |
301 |
+ |
302 |
+/** |
303 |
+ * Determine current sort order. |
304 |
+ */ |
305 |
+function get_order() |
306 |
+{ |
307 |
+ return ($_GET['order']=='ASC')?'DESC':'ASC'; |
308 |
+} |
309 |
+ |
310 |
+/** |
311 |
+ * Determine whether or not list is currently sorted. |
312 |
+ * @param string $method which http method to check for sort information |
313 |
+ * @return mixed cleaned orderby clause based on saved sort information or null if no orderby is set in the defined method |
314 |
+ */ |
315 |
+function get_orderby($method='get') |
316 |
+{ |
317 |
+ if ( $method=='get' && !empty($_GET['sort']) && !empty($_GET['order']) ) |
318 |
+ { |
319 |
+ $sort=clean_in($_GET['sort']); |
320 |
+ $order=clean_in($_GET['order']); |
321 |
+ return " ORDER BY $sort $order "; |
322 |
+ } |
323 |
+ elseif ( $method=='post' && !empty($_POST['sort']) && !empty($_POST['order']) ) |
324 |
+ { |
325 |
+ $sort=clean_in($_POST['sort']); |
326 |
+ $order=clean_in($_POST['order']); |
327 |
+ return " ORDER BY $sort $order "; |
328 |
+ } |
329 |
+ elseif ( $method=='session' && !empty($_SESSION['sort']) && !empty($_SESSION['order']) ) |
330 |
+ { |
331 |
+ $sort=clean_in($_SESSION['sort']); |
332 |
+ $order=clean_in($_SESSION['order']); |
333 |
+ return " ORDER BY $sort $order "; |
334 |
+ } |
335 |
+ else return null; |
336 |
+} |
337 |
+ |
338 |
+/** |
339 |
+ * Parses $_POST for ids, shows edit forms for each id with populated data. |
340 |
+ * <ul> |
341 |
+ * <li>name will be used to retrieve an _array_ from $_POST of the same name</li> |
342 |
+ * <li>the form will be an include, with $posts[col_name] as the default for all values</li> |
343 |
+ * <li>try to keep your query simple (no crazy sorting, etc.) -- we're talking one record at a time here anyway</li> |
344 |
+ * </ul> |
345 |
+ * Example: |
346 |
+ * <code> |
347 |
+ * list_edit_ids('course_id','../forms/course.php','SELECT * FROM fic_courses','1'); |
348 |
+ * </code> |
349 |
+ * @param string $name name of id field |
350 |
+ * @param string $form path to form to be used to items |
351 |
+ * @param string $q_front front half of query |
352 |
+ * @param string $q_where where statement |
353 |
+ * @param array $dates array of date field names, so they can be fixed for forms |
354 |
+ * @param array $datetimes array of datetime field names, so they can be fixed for forms |
355 |
+ */ |
356 |
+function list_edit_ids($name,$form,$q_front,$q_where='1',$dates=null,$datetimes=null) |
357 |
+{ |
358 |
+ if ( !empty($_SESSION[$name]) && is_array($_SESSION[$name]) ) |
359 |
+ { |
360 |
+ $ids=implode(',',$_SESSION[$name]); |
361 |
+ $orderby=get_orderby('session'); |
362 |
+ $query=$q_front.' WHERE '.$q_where." AND $name IN($ids) ".$orderby; |
363 |
+ $records=db_get($query); |
364 |
+ form_start($name); |
365 |
+ foreach ($records as $record) |
366 |
+ { |
367 |
+ echo "\n".'<div class="record">'; |
368 |
+ $record=form_array_fix_dates($dates,$datetimes,2,$record); |
369 |
+ foreach ($record as $key=>$val) |
370 |
+ { |
371 |
+ $posts[$key]=clean_out($val); |
372 |
+ } |
373 |
+ include($form); |
374 |
+ echo "\n".'<div class="record-submit">'; |
375 |
+ form_submit('submit', '', 'button1'); |
376 |
+ echo "\n".'</div>'; |
377 |
+ echo "\n".'</div>'; |
378 |
+ } |
379 |
+ form_end(); |
380 |
+ } |
381 |
+ else |
382 |
+ { |
383 |
+ echo '<p>You must select a record. <a href="javascript:history.back();">Go back</a>.</p>'; |
384 |
+ } |
385 |
+} |
386 |
+ |
387 |
+/** |
388 |
+ * Process a submitted list_edit_ids form. |
389 |
+ * @param array $name array of primary ids posted from the form, these are vital to the WHERE clause of the UPDATE statements. |
390 |
+ * @param string $table name of table being affected |
391 |
+ */ |
392 |
+function list_update_ids($name,$table) |
393 |
+{ |
394 |
+ $keys=array_keys($_POST[$name]); |
395 |
+ foreach ($keys as $index) |
396 |
+ { |
397 |
+ foreach ($_POST as $key=>$val) |
398 |
+ { |
399 |
+ if ($key!='submit') |
400 |
+ { |
401 |
+ $posts[$index][$key]=$val[$index]; |
402 |
+ } |
403 |
+ } |
404 |
+ } |
405 |
+ foreach ($posts as $dataset) |
406 |
+ { |
407 |
+ $query=db_makeupdate($dataset,$table," WHERE $name='".$dataset[$name]."' "); |
408 |
+ db_query($query); |
409 |
+ } |
410 |
+} |
411 |
+?> |