aboutsummaryrefslogtreecommitdiff
blob: 8f8e1e4a14e3755aaa36a6822e83d8c96deab19a (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
<?php
/**
 *  Utility funcs.
 *  @package mirror
 *  @subpackage lib
 */

/**
 *  determine float value of now
 *  @return float value of current time in seconds
 */
function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

/**
 *	Add a message to SESSION['messages'] array.
 *	The $_SESSION['messages'] array stores general or success messages.
 *	@param string $str message to add (optional)
 */
function set_msg($str=null)
{
	if (!empty($str))
	{
		$_SESSION['messages'][]=$str;
	}
}

/**
 *	Show messages.
 *	Iterates through $_SESSION['messages'] and displays them in a ul.
 *	@param string $class css class for message style
 */
function show_msg($class='msg')
{
	if (is_array($_SESSION['messages']) && count($_SESSION['messages']) > 0)
	{
		echo ($class !== NULL) ? '<div class="'.$class.'">' : '';
		echo '<ul>';
		foreach ($_SESSION['messages'] as $message)
			echo '<li>'.$message.'</li>';
		echo '</ul>';
		echo ($class !== NULL) ? '</div>' : '';
		$ret = count($_SESSION['messages']);
	}
	else
	{
		$ret = 0;
	}
	unset($_SESSION['messages']);
	return $ret;
}

/**
 *	Add an error message to SESSION['errors'] array.
 *	The $_SESSION['errors'] array stores error messages.
 *	@param string $str message to add (optional)
 */
function set_error($str=null)
{
	if (!empty($str))
	{
		$_SESSION['errors'][]=$str;
	}
}

/**
 *	Show errors messages.
 *	Iterates through $_SESSION['errors'] and displays them in a ul.
 *	@param string $class css class for message style
 */
function show_error($class='error')
{
	if (@is_array($_SESSION['errors']) && count($_SESSION['errors']) > 0)
	{
		echo '<div class="'.$class.'">';
		echo '<ul>';
		foreach ($_SESSION['errors'] as $error)
			echo '<li>'.$error.'</li>';
		echo '</ul>';
		echo '</div>';
		$ret = count($_SESSION['errors']);
		unset($_SESSION['errors']);
	}
	else
	{
		$ret = 0;
	}
	return $ret;
}

/**
 *	Print out an varible enclosed by &lt;pre&gt; tags
 *	@param mixed $var the variable to print by print_r
 */
function debug_r(&$var)
{
	echo '<pre>';
	print_r($var);
	echo '</pre>';
}

/**
 *	Generate a random string good for passwords
 *	@param in $len the length of the password string
 *	@return string password
 */
function password_gen($len=6)
{

	$set = array( '0','1','2','3','4','5','6','7','8','9','a','e','i','o','u','y','b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z' );
	$pw = '';

	while (strlen($pw) <= $len)
	{
		// random alphanum
		$char = $set[array_rand($set)];
		$pw .= $char;
	}

	return $pw;
}

/**
 * This recursive function empty values in an 'multi-dimensional' array.
 * @param mixed $needle it accepts just one value or an array of values
 * @return mixed false if an empty needle passed in, else a copy of the array with needle values replaced with empty strings
 */
function emptify_in_array($array, $needle)
{
	if ($needle == '')
		return FALSE;

	foreach ($array as $key=>$val)
	{
		if (is_array($val))
			$array[$key] = emptify_in_array($val, $needle);
		elseif (is_array($needle) && in_array($val, $needle))
			$array[$key] = '';
		elseif ($val === $needle)
			$array[$key] = '';
	}

	return $array;
}

/**
 *	This function checks for the existence of a particular row in a particular table matching a value.
 *	Use this with libdb, unless you want lots of problems.  :)
 *	@param string $table name of table
 *	@param string $column name of column containing value to match
 *	@param string $val value to match against database (goes in WHERE clause)
 *	@param string $extra (optional) any AND or ORDER BY or LIMIT or anything you want to add.
 *	@ret bool if a match exists, return true -- otherwise return false
 */
function record_exists($table,$column,$val,$extra=NULL)
{
	$result = DB::query("SELECT * FROM {$table} WHERE {$column}='{$val}' {$extra}");
	if ($result&&$result->rowCount()>0)
	{
		return true;
	}
	return false;
}

/**
 *	Show user tabs, based on an array.
 *	@param array $tabs array of tabs (name=>href)
 *	@param string $current name of tab to highlight	
 */
function show_tabs($tabs,$current)
{
	if ( is_array($tabs) )
	{
		echo "\n".'<div id="tabs"><ul>';
		foreach ( $tabs as $key=>$val )
		{
			if ( strtolower($key) == $current)
				echo "\n".'<li class="active-tab"><a href="'.$val.'">'.$key.'</a></li>';
			else
				echo "\n".'<li><a href="'.$val.'">'.$key.'</a></li>';
		}
		echo "\n".'</ul></div>';
	}
}

/**
 * Sort a two dimensional array based on a 'column' key
 * @param array $array the array to be sorted
 * @param mixed $key the column key to be used for sorting, an array of keys are also acceptable
 * @param mixed $order the order of the sort, either 'asc' (ascending) or 'desc' (descending), can also be an array (with matching array keys to the $key param)
 * @param bool $retain_keys option to retain the original keys; default to true
 * @param bool $case_sensitive option for a case sensitive sort; default to false
 * @return array the original array on argument errors, the sorted array on success 
 */
function array_order_by(&$array, $key=null, $order=null, $retain_keys=TRUE, $case_sensitive=FALSE)
{
	if (is_array($key) && count($key)==1)
	{
		$temp = each($key);
		$key = $temp['value'];
		$order = $order[$temp['key']];
		unset($temp);
	}
	
	if (is_array($key))
	{
		if (!is_array($order))
		{
			$order = array();
		}
		if (count($key) > count($order))
		{
			$order = array_pad($order, count($key), 'asc');
		}

		// sort it according to the first key
		$temp_sort_key = reset($key);
		$temp_order_val = $order[key($key)];
		$return_arr = array_order_by($array, $temp_sort_key, $temp_order_val, $retain_keys, $case_sensitive);
		
		// set up the arrays for the 'inner', next recursion
		$key_copy = $key;
		$order_copy = $order;
		unset($key_copy[key($key)]);
		unset($order_copy[key($key)]);
		
		// get the sorting column's value in the first row
		$temp = current($return_arr);
		$temp_prev_sort_val = $temp[$temp_sort_key];
		unset($temp);
		
		$temp_return_arr = array();
		$temp_partial_array = array();
		
		foreach ($return_arr as $return_arr_key=>$return_arr_val)
		{
			if ($return_arr_val[$temp_sort_key] == $temp_prev_sort_val)
			{
				$temp_partial_array[$return_arr_key] = $return_arr_val;
			}
			else
			{
				if ($retain_keys) 
				{
					$temp_return_arr = $temp_return_arr + array_order_by($temp_partial_array, $key_copy, $order_copy, $retain_keys, $case_sensitive);
				}
				else
				{
					$temp = array_order_by($temp_partial_array, $key_copy, $order_copy, $retain_keys, $case_sensitive);
					foreach ($temp as $temp_val)
					{
						$temp_return_arr[] = $temp_val;
					}
					unset($temp);
				}
				$temp_prev_sort_val = $return_arr_val[$temp_sort_key];
				$temp_partial_array = array();
				$temp_partial_array[$return_arr_key] = $return_arr_val;
			}
		}

		// important! if the last n $temp_prev_sort_val has the same value, then they aren't sorted and added to the temp array
		if (count($return_arr) > count($temp_return_arr))
		{
			if ($retain_keys) 
			{
				$temp_return_arr = $temp_return_arr + array_order_by($temp_partial_array, $key_copy, $order_copy, $retain_keys, $case_sensitive);
			}
			else
			{
				$temp = array_order_by($temp_partial_array, $key_copy, $order_copy, $retain_keys, $case_sensitive);
				foreach ($temp as $temp_val)
				{
					$temp_return_arr[] = $temp_val;
				}
				unset($temp);
			}
		}

		return $temp_return_arr;
	}
		
	if (empty($array) || is_null($key))
		return $array;
	
	if (!array_key_exists($key, reset($array)))
		return $array;
	
	$order =& strtolower($order);
	if ($order == '' || ($order != 'asc' && $order != 'desc'))
		$order = 'asc';
		
	// construct an array that will be used to order the keys
	foreach($array as $row_key => $row)
	{
		$x[$row_key] = $row[$key];
	}
	
	if ($case_sensitive)
		natsort($x);
	else
		natcasesort($x);
	
	if ($order == 'desc')
		$x =& array_reverse($x, TRUE);
		
	// now use those keys to order the original array
	foreach($x as $row_key => $uselessvalue)
	{
		if ($retain_keys)
			$return_arr[$row_key] =& $array[$row_key];
		else
			$return_arr[] =& $array[$row_key];
	}
	
	return $return_arr;
}

?>