Location: PHPKode > projects > Jethro Pastoral Ministry Manager > jethro-pmm/include/system_controller.class.php
<?php
require_once dirname(__FILE__).'/view.class.php';
class System_Controller
{
	var $_view = NULL;
	var $_views = Array();
	var $_friendly_errors = false;
	var $_base_dir = '';

	function System_Controller($base_dir=NULL)
	{
		if (is_null($base_dir)) $base_dir = dirname(dirname(__FILE__));
		$this->_base_dir = $base_dir;
		$dh = opendir($this->_base_dir.'/views');
		while (FALSE !== ($filename = readdir($dh))) {
			if (($filename == '.') || ($filename == '..')) continue;
			$raw_filenames[] = $filename;
		}
		sort($raw_filenames);
		foreach ($raw_filenames as $filename) {
			if (preg_match('/^view_([0-9]*)_(.*)__([0-9]*)_(.*)\.class\.php/', $filename, $matches)) {
				$this->_views[$matches[2]]['children'][$matches[4]]['filename'] = $filename;
			} else if (preg_match('/^view_([0-9]*)_(.*)\.class\.php/', $filename, $matches)) {
				if ($matches[1] == 0) $matches[2] = '_'.$matches[2];
				$this->_views[$matches[2]]['filename'] = $filename;
			}
		}
	}

	function run()
	{
		if (!empty($_REQUEST['call'])) {
			$call_name = str_replace('/', '', $_REQUEST['call']);
			// Try both the Jethro and system_root calls folders
			$filename = dirname(dirname(__FILE__)).'/calls/call_'.$call_name.'.class.php';
			if (!file_exists($filename)) {
				$filename = $this->_base_dir.'/calls/call_'.$call_name.'.class.php';
			}
			if (file_exists($filename)) {
				include_once dirname(__FILE__).'/call.class.php';
				include_once $filename;
				$classname = 'Call_'.$call_name;
				$call_obj =& new $classname;
				$call_obj->run();
			} else {
				trigger_error('Unknown call '.htmlentities($_REQUEST['call']), E_USER_WARNING);
			}
		} else {
			set_error_handler(Array($this, '_handleError'));
			$raw_view_name = array_get($_REQUEST, 'view', 'home');
			$bits = explode('__', $raw_view_name);
			$view_filename = null;
			if (count($bits) > 1) {
				if (!empty($this->_views[$bits[0]]['children'][$bits[1]])) {
					$view_filename = $this->_views[$bits[0]]['children'][$bits[1]]['filename'];
					$view_classname = 'View_'.$bits[0].'__'.$bits[1];
				}
			} else if (isset($this->_views[$bits[0]])) {
				$view_filename = $this->_views[$bits[0]]['filename'];
				$view_classname = 'View_'.$bits[0];
			}

			if (!is_null($view_filename)) {
				require_once 'views/'.$view_filename;
				$this->_view =& new $view_classname();
				$this->_view->processView();
			}
			require $this->_base_dir.'/templates/main.template.php';
			restore_error_handler();
		}
	}


	function getTitle()
	{
		if (is_null($this->_view)) {
			return '';
		} else {
			return $this->_view->getTitle();
		}
	}


	function printNavigation()
	{
		$current_view = array_get($_REQUEST, 'view', 'home');
		?>
		<ul id="nav">
			<?php
			foreach ($this->_views as $name => $data) {
				if ($name[0] == '_') continue;
				$class = '';
				if (($current_view == $name) || (strpos($current_view, $name.'__') === 0)) $class = 'current';
				?>
				<li class="<?php echo $class; ?>">
					<?php
					if (empty($data['children'])) {
						// deliberately - only leaf nodes can be navigated to directly
						?>
						<a href="?view=<?php echo $name; ?>" ><?php echo ucwords(str_replace('_', ' ', $name)); ?></a>
						<?php
					} else {
						// pardon the formatting - IE is having a white-space tantrum
						?>
						<span><?php echo ucwords(str_replace('_', ' ', $name)); ?></span>
						<ul style="display: none"><?php
							foreach ($data['children'] as $subname => $sub_details) {
								$class = ($current_view == $name.'__'.$subname) ? 'current' : '';
								?><li class="<?php echo $class; ?>"><a href="?view=<?php echo $name.'__'.$subname; ?>"><?php echo ucwords(str_replace('_', ' ', $subname)); ?></a></li><?php
							}
							?></ul>
						<?php
					}
					?>
				</li>
				<?php
			}
			?>
			</ul>
		<?php
	}


	function printBody()
	{
		if (is_null($this->_view)) {
			echo 'Error: Undefined view';
		} else {
			$this->_view->printView();
		}
	}

	function includeDBClass($classname)
	{
		$classname = strtolower($classname);
		require_once dirname(__FILE__).'/db_object.class.php';
		require_once 'db_objects/'.$classname.'.class.php';
	}

	function &getDBObject($classname, $id)
	{
		$this->includeDBClass($classname);
		$res =& new $classname($id);
		if (!$res->id) $res = null;
		return $res;

	}

	function getDBObjectData($classname, $params=Array(), $logic='OR', $order='')
	{
		$this->includeDBClass($classname);
		$sample =& new $classname();
		return $sample->getInstancesData($params, $logic, $order);
	}

	function doTransaction($operation)
	{
		switch (strtoupper($operation)) {
			case 'BEGIN':
			case 'COMMIT':
			case 'ROLLBACK':
				$r = $GLOBALS['db']->query(strtoupper($operation));
				check_db_result($r);
		}
	}

	function setFriendlyErrors($enabled)
	{
		$this->_friendly_errors = $enabled;
	}

	function _handleError($errno, $errstr, $errfile, $errline)
	{
		$this->_friendly_errors = FALSE;

		switch ($errno) { 
			case E_ERROR:
			case E_USER_ERROR:
				?>
				<div style="background: red; border: 2px solid black; color: black; font-weight: bold; padding: 5px; clear: both; margin: 5px">
					<b>ERROR: <?php echo $errstr; ?><br />
					Line <?php echo $errline; ?> of File <?php echo $errfile; ?>
					<u class="clickable" onclick="var parentDiv=this.parentNode; while (parentDiv.tagName != 'DIV') { parentDiv = parentDiv.parentNode; }; with (parentDiv.getElementsByTagName('PRE')[0].style) { display = (display == 'block') ? 'none' : 'block' }">Show Backtrace</u>
					<pre style="display: none; background: white; font-weight: normal;">
						<?php
						$bt = debug_backtrace();
						array_shift($bt);
						array_shift($bt);
						print_r($bt); 
						?>
					</pre>
				</div>
				<?php
				exit(1);
				break;
			case E_WARNING:
			case E_USER_WARNING:
				?>
				<div style="background: orange; border: 2px solid black; color: black; font-weight: bold; padding: 5px; clear: both; margin: 5px">
					<b>WARNING: <?php echo $errstr; ?><br />
					Line <?php echo $errline; ?> of File <?php echo $errfile; ?>
					<u class="clickable" onclick="var parentDiv=this.parentNode; while (parentDiv.tagName != 'DIV') { parentDiv = parentDiv.parentNode; } with (parentDiv.getElementsByTagName('PRE')[0].style) { display = (display == 'block') ? 'none' : 'block' }">Show Backtrace</u>
					<pre style="display: none; background: white; font-weight: normal;">
						<?php
						$bt = debug_backtrace();
						array_shift($bt);
						array_shift($bt);
						print_r($bt); 
						?>
					</pre>
				</div>
				<?php
				break;
			case E_STRICT:
				 break;
			case E_USER_NOTICE: 
			case E_NOTICE:
				if ($this->_friendly_errors) {
					add_message('Error: '.$errstr, 'failure');
				} else {
					?>
					<div style="background: green; border: 2px solid black; color: black; font-weight: bold; padding: 5px; clear: both; margin: 5px">
						<b>NOTICE: <?php echo $errstr; ?><br />
						Line <?php echo $errline; ?> of File <?php echo $errfile; ?>
						<u class="clickable" onclick="var parentDiv=this.parentNode; while (parentDiv.tagName != 'DIV') { parentDiv = parentDiv.parentNode; } with (parentDiv.getElementsByTagName('PRE')[0].style) { display = (display == 'block') ? 'none' : 'block' }">Show Backtrace</u>
						<pre style="display: none; background: white; font-weight: normal;">
							<?php
							$bt = debug_backtrace();
							array_shift($bt);
							array_shift($bt);
							print_r($bt); 
							?>
						</pre>
					</div>
					<?php
				}
				break;
			default:
				?>
				<div style="background: green; border: 2px solid black; color: black; font-weight: bold; padding: 5px; clear: both; margin: 5px">
					<b>UNKNOWN ERROR TYPE <?php echo $errno; ?>: <?php echo $errstr; ?><br />
					Line <?php echo $errline; ?> of File <?php echo $errfile; ?>
					<u class="clickable" onclick="var parentDiv=this.parentNode; while (parentDiv.tagName != 'DIV') { parentDiv = parentDiv.parentNode; } with (parentDiv.getElementsByTagName('PRE')[0].style) { display = (display == 'block') ? 'none' : 'block' }">Show Backtrace</u>
					<pre style="display: none; background: white; font-weight: normal;">
						<?php
						$bt = debug_backtrace();
						array_shift($bt);
						array_shift($bt);
						print_r($bt); 
						?>
					</pre>
				</div>
				<?php
				break;
		 }
	}

	function runHooks($hook_name, $params)
	{
		require_once 'include/hook.class.php';
		$dir = @opendir(JETHRO_ROOT.'/hooks/'.$hook_name);
		while ($dir && ($hook_file = readdir($dir))) {
			if (is_dir(JETHRO_ROOT.'/hooks/'.$hook_file)) continue;
			if ($hook_file[0] == '.') continue;
			if (0 === strpos($hook_file, 'sample.')) continue;
			require_once 'hooks/'.$hook_name.'/'.$hook_file;
			$class_name = str_replace('.class.php', '', $hook_file);
			call_user_func(Array($class_name, 'run'), $params);
		}
	}


}//end class
?>
Return current item: Jethro Pastoral Ministry Manager