aboutsummaryrefslogtreecommitdiff
blob: c7e872ba5b8e244f29037eabf9c0e910b73f4eaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
/**
 *	List functions for lists of values.
 *	@package mirror 
 *	@subpackage lib
 *	@author Mike Morgan <mike.morgan@oregonstate.edu> 
 *	
 *	Usage example:
 *	<code>
 *	$orderby=get_order();
 *	$query="SELECT * FROM fic_courses $orderby";
 *	$courses=db_get($query,MYSQL_ASSOC);
 *	$headers=array(
 *		'course_id'=>'',
 *		'title'=>'Course Title',
 *		'date_start_course'=>'Start',
 *		'date_end_course'=>'End',
 *		'date_start_reg'=>'Reg Starts',
 *		'date_end_reg'=>'Reg Ends',
 *		'active'=>'Active?',
 *		'entry_date'=>'Created'
 *	);
 *	show_list($courses,$headers);
 *	</code>
 *
 *	Accompanying CSS for table output:
 *	<code>
 *	.list
 *	{
 *		border:1px solid #999;
 *	}
 *	.list th
 *	{
 *		background:#eee;
 *		border:1px solid #000;
 *		font-weight:bold;
 *	}
 *	.list th a
 *	{
 *		display:block;
 *		padding:0 14px;
 *	}
 *	.list th a:hover
 *	{
 *		background-color:#fff;
 *	}
 *	.row1
 *	{
 *		background:#ddd;
 *	}
 *	.row2
 *	{
 *		background:#ccc;
 *	}
 *	.row1:hover, .row2:hover
 *	{
 *		background-color:#fec;
 *	}
 *	.current-sort
 *	{
 *		background:#fda;
 *	}
 *	.sort-desc
 *	{
 *		background:#fec url(../img/up.gif) no-repeat right;
 *	}
 *	.sort-asc
 *	{
 *		background:#fec url(../img/down.gif) no-repeat right;
 *	}
 *	</code>

 *	Accompanying JavaScript for select all / inverse:
 *	<code>
 *	<script type="text/javascript">
 *	//<!--
 *	function selectAll(formObj,invert)
 *	{
 *		for (var i=0;i < formObj.elements.length;i++)
 *		{
 *			fldObj = formObj.elements[i];
 *			if (fldObj.type == 'checkbox')
 *			{
 *				if (invert==1)
 *				{
 *					fldObj.checked = (fldObj.checked) ? false : true;
 *				}
 *				else
 *				{
 *					fldObj.checked = true;
 *				}
 *			}
 *		}
 *	}
 *	//-->
 *	</script>
 *	</code>
 */

/**
 *	Show a list of values, for forms.
 *	@param array $list associative array
 *	@param array $headers column name => column title (for table heads)
 *	@param string $type checkbox, radio, simple
 *	@param array $array actions to display in actions select list 
 *	@param string $form_id id of form holding list
 *	@param bool $sortable whether or not to show sortable column headers (links in th's)
 *	@param array|string $selected if type is checkbox, array otherwise string with one val
 */
function show_list($list,$headers,$type='checkbox',$actions=null,$form_id=null,$sortable=true,$selected=null)
{
	if ( is_array($list) && count($list)>0 && is_array($headers) )
	{
		if ( $type!='simple' && !empty($_GET['sort']) && !empty($_GET['order']) )
		{
			form_hidden('sort',$_GET['sort']);
			form_hidden('order',$_GET['order']);
		}
		echo "\n".'<table class="list">';
		show_headers($headers,$type,$sortable);
		echo "\n".'<tbody>';
		foreach ($list as $row)
		{
			show_row($headers,$row,$type,$count++,$selected);
		}
		echo "\n".'</tbody>';
		echo "\n".'</table>';
		if ($type=='checkbox')
		{
echo <<<js
<script type="text/javascript">
//<!--
function list_select(formObj,invert)
{
	for (var i=0;i < formObj.elements.length;i++)
	{
		fldObj = formObj.elements[i];
		if (fldObj.type == 'checkbox')
		{
			if (invert==1)
			{
				fldObj.checked = (fldObj.checked) ? false : true;
			}
			else
			{
				fldObj.checked = true;
			}
		}
	}
}
//-->
</script>
js;
			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>';
		}
		if ($type=='radio'||$type='checkbox-small')
		{
			echo '<br />';	
		}
		if (is_array($actions)&&$type!='simple')
		{
			if (count($actions) == 1) {
				$actions = array_values($actions);
				echo '<p>';
				form_submit('submit','submit','button1',$actions[0].' &raquo;');
				echo '</p>';
			} else {
				echo '<p>';
				echo '<label for="action">With selected: </label>';
				form_select('action','action','text2',$actions,'');
				form_submit('submit','submit','button1','Go &raquo;');
				echo '</p>';
			}
		}
	}
	elseif ( !is_array($headers) )
	{
		echo "\n".'<h1>FIX HEADERS ARRAY</h1>';
	}
	else
	{
		echo "\n".'<p>No records found.</p>';
	}
}

/**
 *	Show table headers.
 *	@param array $headers column name => column title (for table heads)
 *	@param string $type type of list that is being shown
 *	@param bool $sortable whether or not to show sortable column headers (links in th's)
 */
function show_headers($headers,$type,$sortable=true)
{
	echo "\n".'<thead><tr>';
	$sort=$_GET['sort'];
	$order=get_order();
	$count=0;
	foreach ($headers as $col=>$title)
	{
		if ( !empty($sort) && !empty($order) )
		{
			if ($col==$sort && $order=='ASC')
			{
				$a_class=' class="sort-asc current-sort" ';
			}
			elseif ($col==$sort && $order=='DESC')
			{
				$a_class=' class="sort-desc current-sort" ';
			}
			else
			{
				$a_class=null;
			}
		}
		if ($type!='simple'&&$count==0)	
		{
			echo "\n".'<th> </th>';
			next;
		}
		elseif($sortable)
		{
			$qs = array();
			foreach ($_GET as $qn=>$qv) { $qs[$qn] = $qv; } // existing query string variables
			$qs['sort'] = $col; // add/replace sort to query string
			$qs['order'] = $order; // add/replace order by to query string
			foreach ($qs as $qn=>$qv) { $querystring[] = $qn.'='.$qv; } // existing query string variables
			echo "\n".'<th><a '.$a_class.'href="'.$_SERVER['PHP_SELF'].'?'.implode('&amp;',$querystring).'">'.$title.'</a></th>';
			unset($qs);
			unset($querystring);
		}
		else
		{
			echo "\n".'<th>'.$title.'</th>';
		}
		$count++;
	}
	echo "\n".'</tr></thead>';
}

/**
 *	Show table data.
 *	@param array $headers column name => column title (for knowing which ones to display)
 *	@param array $row table row, assoc
 *	@param string $type type of table, determines first column, which could be an input
 *	@param array|string $selected selected items; if type is checkbox, array otherwise string with one val
 */
function show_row($headers,$row,$type,$num=null,$selected=null)
{
	$indexes=array_keys($headers);
	$idname = $indexes[0];
	$count=0;
	$tr_class=($num%2)?' class="row1" ':' class="row2" ';
	echo "\n".'<tr'.$tr_class.'>';
	foreach ($indexes as $index)
	{
		$row[$index]=clean_out($row[$index]);
		if ($type!='simple'&&$count==0)
		{
			$id=preg_replace('/[^[:alnum:]]/', '', $index).$row[$index];
			if ($type=='checkbox'||$type=='checkbox-small')
			{
				echo "\n".'<td>';
				form_checkbox($idname.'[]',$id,null,$row[$index],(is_array($selected) && in_array($row[$index], $selected)));
				echo "\n".'</td>';
			}
			elseif ($type=='radio')
			{
				echo "\n".'<td>';
				form_radio($idname,$id,null,$row[$index], ($row[$index] == $selected));
				echo "\n".'</td>';
			}
		}
		else
		{
			echo ($type=='simple')?"\n".'<td>'.$row[$index].'</td>':"\n".'<td><label for="'.$id.'">'.$row[$index].'</label></td>';
		}
		$count++;
	}
	echo "\n".'</tr>';
}

/**
 *	Determine current sort order.
 */
function get_order()
{
	return ($_GET['order']=='ASC')?'DESC':'ASC';
}

/**
 *	Determine whether or not list is currently sorted.
 *	@param string $method which http method to check for sort information
 *	@return mixed cleaned orderby clause based on saved sort information or null if no orderby is set in the defined method
 */
function get_orderby($method='get')
{
	if ( $method=='get' && !empty($_GET['sort']) && !empty($_GET['order']) )
	{
		$sort=clean_in($_GET['sort']);
		$order=clean_in($_GET['order']);
		return " ORDER BY $sort $order ";
	}
	elseif ( $method=='post' && !empty($_POST['sort']) && !empty($_POST['order']) )
	{
		$sort=clean_in($_POST['sort']);
		$order=clean_in($_POST['order']);
		return " ORDER BY $sort $order ";
	}
	elseif ( $method=='session' && !empty($_SESSION['sort']) && !empty($_SESSION['order']) )
	{
		$sort=clean_in($_SESSION['sort']);
		$order=clean_in($_SESSION['order']);
		return " ORDER BY $sort $order ";
	}
	else return null;
}

/**
 *	Parses $_POST for ids, shows edit forms for each id with populated data.
 *	<ul>
 *	<li>name will be used to retrieve an _array_ from $_POST of the same name</li>
 *		<li>the form will be an include, with $posts[col_name] as the default for all values</li>
 *		<li>try to keep your query simple (no crazy sorting, etc.) -- we're talking one record at a time here anyway</li>
 *	</ul>
 *	Example:
 *	<code>
 *	list_edit_ids('course_id','../forms/course.php','SELECT * FROM fic_courses','1');
 *	</code>
 *	@param string $name name of id field
 *	@param string $form path to form to be used to items
 *	@param string $q_front front half of query
 *	@param string $q_where where statement
 *	@param array $dates array of date field names, so they can be fixed for forms
 *	@param array $datetimes array of datetime field names, so they can be fixed for forms
 */
function list_edit_ids($name,$form,$q_front,$q_where='1',$dates=null,$datetimes=null)
{
	if ( !empty($_SESSION[$name]) && is_array($_SESSION[$name]) )
	{
		$ids=implode(',',$_SESSION[$name]);
		$orderby=get_orderby('session');
		$query=$q_front.' WHERE '.$q_where." AND $name IN($ids) ".$orderby;
		$records=db_get($query);
		form_start($name);
		foreach ($records as $record)
		{
			echo "\n".'<div class="record">';
			$record=form_array_fix_dates($dates,$datetimes,2,$record);
			foreach ($record as $key=>$val)
			{
				$posts[$key]=clean_out($val);
			}
			include($form);
			echo "\n".'<div class="record-submit">';
			form_submit('submit', '', 'button1');
			echo "\n".'</div>';
			echo "\n".'</div>';
		}
		form_end();
	}
	else
	{
		echo '<p>You must select a record.  <a href="javascript:history.back();">Go back</a>.</p>';
	}
}