Location: PHPKode > projects > Jethro Pastoral Ministry Manager > jethro-pmm/include/general.php
<?php
function array_get($array, $index, $alt=NULL)
{
	if (array_key_exists($index, $array)) {
		return $array[$index];
	} else {
		return $alt;
	}
}

function stripslashes_array(&$array, $strip_keys=false) {
	if(is_string($array)) return stripslashes($array);
	$keys_to_replace = Array();
	foreach($array as $key => $value) {
		if (is_string($value)) {
			$array[$key] = stripslashes($value);
		} elseif (is_array($value)) {
			stripslashes_array($array[$key], $strip_keys);
		}

		if ($strip_keys && $key != ($stripped_key = stripslashes($key))) {
			$keys_to_replace[$key] = $stripped_key;
		}
	}
	# now replace any of the keys that needed strip slashing
	foreach($keys_to_replace as $from => $to) {
		$array[$to]   = &$array[$from];
		unset($array[$from]);
	}
	return $array;
}

function strip_all_slashes() {
	if (get_magic_quotes_gpc()) {
		stripslashes_array($_GET, true);
		stripslashes_array($_POST, true);
		stripslashes_array($_COOKIE, true);
		stripslashes_array($_REQUEST, true);
	}
}

function bam($x)
{
	echo '<pre>';
	print_r($x);
	echo '</pre>';
}


function check_db_result(&$res)
{
	if (PEAR::isError($res)) {
		print_r($res->userinfo);
		trigger_error("Database Error: ".print_r($res->userinfo, 1), E_USER_ERROR);
		exit();
	}
}

function format_datetime($d)
{
	if (!is_int($d)) {
		$d = strtotime($d);
	}
	return date('j M Y g:ia', $d);
}

function format_date($d)
{
	if (!is_int($d)) {
		$d = strtotime($d);
	}
	return date('j M Y', $d);
}

function redirect($view, $params=Array(), $hash='')
{
	$params['view'] = $view;
	$url = build_url($params);
	if ($hash) $url .= '#'.$hash;
	session_write_close();
	header('Location: '.$url);
	exit;
}

function add_message($msg, $class='success')
{
	$_SESSION['messages'][] = Array('message' => $msg, 'class' => $class);
}

function dump_messages()
{
	if (!empty($_SESSION['messages'])) {
		foreach ($_SESSION['messages'] as $msg) {
			?>
			<div class="message <?php echo $msg['class']; ?>"><?php echo htmlentities($msg['message']); ?></div>
			<?php
		}
		unset($_SESSION['messages']);
	}
}


function print_widget($name, $params, $value)
{
	$classes = array_get($params, 'class', '');
	if (!array_get($params, 'allow_empty', 1)) {
		$classes .= ' compulsory';
	}
	switch ($params['type']) {
		case 'bibleref':
			require_once 'bible_ref.class.php';
			$br = new bible_ref($value);
			$value = $br->toShortString();
			$params['class'] = 'bible-ref';
			// fall through
		case 'text':
			$maxlength_exp = empty($params['maxlength']) ? '' : 'maxlength="'.$params['maxlength'].'"';
			if (array_get($params, 'height', 1) > 1) {
				$cols_exp = empty($params['width']) ? '' : 'cols="'.$params['width'].'"';
				?>
				<textarea name="<?php echo $name; ?>" rows="<?php echo $params['height']; ?>" <?php echo $cols_exp; ?> class="<?php echo trim($classes); ?>" <?php echo $maxlength_exp; ?>><?php echo htmlentities($value); ?></textarea>
				<?php
			} else {
				$width_exp = empty($params['width']) ? '' : 'size="'.$params['width'].'"';
				?>
				<input type="text" name="<?php echo $name; ?>" value="<?php echo $value; ?>" class="<?php echo trim($classes); ?>" <?php echo $maxlength_exp; ?> <?php echo $width_exp; ?> />
				<?php
			}
			break;
		case 'int':
			$classes .= ' int-box';
			$width_exp = '';
			if (!empty($params['width'])) {
				$classes .= ' exact-width';
				$width_exp = 'size="'.$params['width'].'" maxlength="'.$params['width'].'"';
			}
			?>
			<input type="text" name="<?php echo $name; ?>" value="<?php echo $value; ?>" class="<?php echo trim($classes); ?>" <?php echo $width_exp; ?> />
			<?php
			break;
		case 'select':
			$multiple_exp = '';
			if (array_get($params, 'allow_multiple')) {
				$multiple_exp = ' multiple="multiple"';
				if (substr($name, -2) != '[]') $name .= '[]';
			}
			$our_val = is_array($value) ? $value : (($value === '') ? Array() : Array($value));
			?>
			<select name="<?php echo $name; ?>" class="<?php echo $classes;?>"<?php echo $multiple_exp; ?>>
				<?php
				foreach ($params['options'] as $k => $v) {
					$selected_exp = in_array("$k", $our_val, true) ? ' selected="selected"' : '';
					?>
					<option value="<?php echo $k; ?>"<?php echo $selected_exp; ?>><?php echo htmlentities($v); ?></option>
					<?php
				}
				?>
			</select>
			<?php
			break;
		case 'date':
			$day_year_classes = trim($classes.' int-box');
			if (FALSE === strpos($name, '[')) {
				$name_template = $name.'%s';
			} else {
				$name_template = substr($name, 0, strpos($name, '[')).'%s'.substr($name, strpos($name, '['));
			}
			$months = Array();
			for ($i = 1; $i < 13; $i++) $months[$i] = date(array_get($params, 'month_format', 'F'), strtotime("2007-$i-01")); 
			if (empty($value)) $value = date('Y-m-d'); // blank dates not allowed
			list($year_val, $month_val, $day_val) = explode('-', substr($value, 0, 10));
			?>
			<input type="text" name="<?php printf($name_template, '_d'); ?>" class="day-box <?php echo $day_year_classes; ?>" size="2" maxlength="2" value="<?php echo $day_val; ?>" /><select name="<?php printf($name_template, '_m'); ?>" class="<?php echo $classes; ?>">
				<?php
				foreach ($months as $i => $month_name) {
					$selected = (($i) == $month_val) ? ' selected="selected"' : '';
					?>
					<option value="<?php echo $i; ?>"<?php echo $selected; ?>><?php echo $month_name; ?></option>
					<?php
				}
				?>
			</select><input type="text" name="<?php printf($name_template, '_y'); ?>" class="year-box <?php echo $day_year_classes; ?>" size="4" maxlength="4" value="<?php echo $year_val; ?>" />
			<?php
			break;
		case 'reference':
			if (!empty($params['references'])) {
				$where = Array();
				if (!empty($params['filter']) && is_array($params['filter'])) $where = $params['filter'];
				$where_logic = array_get($params, 'filter_logic', 'AND');
				$options = $GLOBALS['system']->getDBObjectData($params['references'], $where, $where_logic, array_get($params, 'order_by'));
				$dummy =& new $params['references']();
				$multiple_exp = '';
				if (array_get($params, 'allow_multiple')) {
					$multiple_exp = ' multiple="multiple"';
					if (substr($name, -2) != '[]') $name .= '[]';
				}
				$our_val = is_array($value) ? $value : (empty($value) ? Array() : Array($value));
				?>
				<select name="<?php echo $name; ?>" class="<?php echo $classes;?>"<?php echo $multiple_exp; ?>>
					<?php
					if (array_get($params, 'allow_empty') && !array_get($params, 'allow_multiple')) {
						?>
						<option value=""><?php echo array_get($params, 'empty_text', '(None)'); ?></option>
						<?php
					}
					foreach ($options as $k => $v) {
						$dummy->populate($k, $v);
						$selected_exp = in_array($k, $our_val) ? ' selected="selected"' : '';
						if (empty($selected_exp)) {
							if (!empty($params['filter']) && is_callable($params['filter'])) {
								if (!$params['filter']($dummy)) continue;
							}
						}
						?>
						<option value="<?php echo $k; ?>"<?php echo $selected_exp; ?>><?php echo htmlentities($dummy->toString()); ?></option>
						<?php
					}
					?>
				</select>
				<?php
			}
			break;
	}
}


function process_widget($name, $params)
{
	$value = null;
	switch ($params['type']) {
		case 'date':
			if (isset($_REQUEST[$name])) {
				// might have an ISO8601 date
				if (preg_match('/^(\d\d\d\d-\d\d-\d\d)$/', $_REQUEST[$name])) {
					return $_REQUEST[$name];
				}
			}
			if (FALSE === strpos($name, '[')) {
				$subindex = NULL;
			} else {
				$subindex = substr($name, strpos($name, '[')+1, strpos($name, ']')-strpos($name, '[')-1);
				$name = substr($name, 0, strpos($name, '['));
			}
			if (!isset($_REQUEST[$name.'_d'])) return NULL;
			if (!is_null($subindex) && !isset($_REQUEST[$name.'_d'][$subindex])) return NULL;
			foreach (Array('y', 'm', 'd') as $comp) {
				$comp_vals[$comp] = array_get($_REQUEST, $name.'_'.$comp, 0);
				if (!is_null($subindex)) $comp_vals[$comp] = $comp_vals[$comp][$subindex];
			}
			$value = sprintf('%04d-%02d-%02d', $comp_vals['y'], $comp_vals['m'], $comp_vals['d']);
			$new_val = date('Y-m-d', strtotime($value));
			if ($new_val != $value) {
				trigger_error('The date "'.$value.'" is not valid and has not been set', E_USER_NOTICE);
				$value = NULL;
			}
			break;
		case 'bibleref':
			if (!empty($_REQUEST[$name])) {
				require_once 'bible_ref.class.php';
				$br = new bible_ref($_REQUEST[$name]);
				if ($br->book) $value = $br->toCode();
			}
			break;
		default:
			$value = array_get($_REQUEST, $name);
	}
	return $value;
}

function build_url($params)
{
	if (array_get($params, '*', 1) == NULL) {
		$vars = Array();
	} else {
		$vars = $_GET;
	}
	foreach ($params as $i => $v) {
		if (is_null($v)) {
			unset($vars[$i]);
		} else {
			$vars[$i] = $v;
		}
	}
	$bits = Array();
	foreach ($vars as $i => $v) {
		$bits[] = "$i=$v";
	}
	$protocol = (REQUIRE_HTTPS || !empty($_REQUEST['HTTPS'])) ? 'https://' : 'http://';
	$ubits = parse_url(BASE_URL);
	return $protocol.str_replace('index.php', '', $ubits['host'].$_SERVER['PHP_SELF']).'?'.implode('&', $bits);
}

function speed_log()
{
	$bt = debug_backtrace();
	if (!isset($GLOBALS['first_log_time'])) {
		$GLOBALS['first_log_time'] = getmicrotime();
		error_log("SPEED_LOG: 0 - ".$bt[0]['file'].':'.$bt[0]['line']);
	} else {
		$diff = getmicrotime() - $GLOBALS['last_log_time'];
		$total = getmicrotime() - $GLOBALS['first_log_time'];
		error_log("SPEED_LOG: Diff=$diff  Total=$total  ".$bt[0]['file'].':'.$bt[0]['line']);
	}
	$GLOBALS['last_log_time'] = getmicrotime();
}

function getmicrotime(){ 
  list($usec, $sec) = explode(" ",microtime()); 
     return (int)((float)$usec + (float)$sec) * 1000; 
  } 

?>
Return current item: Jethro Pastoral Ministry Manager