All pastes #511704 Raw Edit

Unnamed

public php v1 · immutable
#511704 ·published 2007-05-26 16:21 UTC
rendered paste body
<?php    class TemplateException extends Exception { }    class Template {		public $vars = array();	static private $template_directory = 'view/templates/default/';		static public function windowJS() {	    	    $output = '	        <script type="text/javascript" src="/view/templates/default/js/window/javascripts/prototype.js"> </script>		<script type="text/javascript" src="/view/templates/default/js/window/javascripts/effects.js"> </script>		<script type="text/javascript" src="/view/templates/default/js/window/javascripts/window.js"> </script>		<script type="text/javascript" src="/view/templates/default/js/window/javascripts/debug.js"> </script>		<script type="text/javascript" src="/view/templates/default/js/window/javascripts/window_effects.js"> </script>	    		<link href="/view/templates/default/js/window/themes/lighting.css" rel="stylesheet" type="text/css" />		<link href="/view/templates/default/js/window/themes/default.css" rel="stylesheet" type="text/css" />			    ';	    	    return $output;	    	}		static public function mooTools() {	    $output = '		<script type="text/javascript" src="/view/templates/default/js/mootools.v1.1.js"> </script>	    ';	    	    return $output;	    	}		static public function buildTopNav() {	    	    $dbObj = Database::connect();	    	    $tabs = array(			  array('name'=>'Home',				'text'=>'Home',				'url'=>'/Home',				'sub'=>array(				    array('text'=>'Something',					  'url'=>'/Home/something'))),			  array('name'=>'Applications',				'text'=>'Applications',				'url'=>'/Applications/',				'sub'=>array(				    array('text'=>'My Applications',					  'url'=>'/Applications/MyApps/'))),			  array('name'=>'Users',				'text'=>'Manage Users',				'url'=>'/Users/',				'sub'=>array(				    array('text'=>'Add User',					  'url'=>'/Users/add/'),				    array('text'=>'Delete User',					  'url'=>'/Users/delete'))),			  array('name'=>'Settings',				'text'=>'Account Settings',				'url'=>'/Settings/',				'sub'=>array(				    array('text'=>'Password',					  'url'=>'/Settings/password/'),				    array('text'=>'Contact Information',					  'url'=>'/Settings/contact'))),			  array('name'=>'Notes',				'text'=>'Account Notes',				'url'=>'/Notes/',				'sub'=>array()),			  array('name'=>'Support',				'text'=>'Support',				'url'=>'/Support/',				'sub'=>array(				    array('text'=>'Tickets',					  'url'=>'/Support/tickets/'),				    array('text'=>'Documentation',					  'url'=>'/Support/docs/'),				    array('text'=>'FAQ',					  'url'=>'/Support/faq/'))));	    	    $appsSubNavQuery = "		SELECT		    LinkText,		    DashboardURL		From		    Applications		Where		    IsActive = 1	    ";	    	    $apps = $dbObj->getAll($appsSubNavQuery);	    	    if( is_array($apps) ) {				foreach($apps as $app) {		    		    $i++;		    		    $tabs[1]['sub'][$i]['text'] = $app['LinkText'];		    $tabs[1]['sub'][$i]['url'] = $app['DashboardURL'];		}			    }	    	    $r = $_SERVER['REQUEST_URI'];	    $r = explode('/',$r);	    	    $output = '<ul id="nav">';	    	    foreach( $tabs as $tab ) {				if($r[1] == $tab['name']) {		    		    $class = 'class="active"';		    		    $sub .= '<div id="s">';		    		    if( is_array( $tab['sub'] )) {						$sub .= '<ul id="sub">';						foreach( $tab['sub'] as $s) {			    			    $sub .= '<li><a href="'. $s['url'] .'" title="'. $s['text'] .'">'. $s['text'] .'</a></li>';			    			}						$sub .= '</ul>';					    }		    		    $sub .= '</div>';		    		} else {		    		    unset( $class );		    		}				$output .= '<li '. $class .'><a href="'. $tab['url'] .'" title="'. $tab['text'] .'">'. $tab['text'] .'</a></li>';			    }	    	    $output .= '</ul>';	    	    $output .= $sub;	    	    return $output;	    	}		static public function getAppLeftNav() {	    	    $r = explode('/',$_GET['class']);	    $class = strtolower($r[1]);	    	    $dir = dirname(__FILE__);	    $file = $dir.'/helpers/'.$class.'.php';	    	    if( file_exists($file) ) {				require_once( $file );			    } else {				throw new TemplateException('Unable to find template helper: '.$file);			    }	    	    $object = new $class;	    $output = $object->getNav($_GET['method']);	    	    return $output;	    	}		public function addVar($index, $value) {	    $this->vars[$index] = $value;	}		static private function findHeader( $directory, $file ) {	    	    $default = self::$template_directory.'header.php';	    $custom = self::$template_directory.$directory.'/header.php';	    $veryCustom = self::$template_directory.$directory.'/'.$file.'_header.php';	    	    if( file_exists( $veryCustom )) {		return $veryCustom;	    }	    	    if( file_exists( $custom )) {		return $custom;	    }	    	    if( file_exists( $default )) {		return $default;	    } else {		throw new TemplateException('Unable to find header(s): '. $default);	    }	    	}		static private function findFooter( $directory ) {	    	    $default = self::$template_directory.'footer.php';	    $custom = self::$template_directory.$directory.'/footer.php';	    	    if( file_exists( $custom )) {		return $custom;	    }	    	    if( file_exists( $default )) {		return $default;	    } else {		throw new TemplateException('Unable to find footer(s): '. $default);	    }	    	}		public function render( $directory, $file ) {	    	    $header = self::findHeader( $directory, $file );	    $footer = self::findFooter( $directory );    	    	    $f1 = self::$template_directory.$directory.'/'.$file.'.php';	    $f2 = self::$template_directory.$directory.'/default.php';	    $f3 = self::$template_directory.'default.php';	    	    if( file_exists( $f1 ) ) {		$f = $f1;	    } else if ( file_exists( $f2 ) ) {		$f = $f2;	    } else if ( file_exists( $f3 ) ) {		$f = $f3;	    } else {		throw new TemplateException('Unable to find a template: '. $f1 .' && '. $f2 .' && '. $f3);	    }	    	    $contents = file_get_contents($f);	    	    foreach($this->vars as $index=>$value) {				$$index = $value;			    }	    	    ob_start();		include($header);		header('Content-type: text/html');		include($f);		include($footer);	    ob_end_flush();	    	}	    }?>