Campustream 1.0
A social network MQP for WPI
|
00001 <?php 00002 00003 00004 class View { 00005 00006 protected $local_data = array(); 00007 protected static $global_data = array(); 00008 00009 private $filename; 00010 00011 function __construct($name, $data = null) { 00012 00013 $this->filename = $name . '.php'; 00014 00015 if (is_array($data) AND !empty($data)) { 00016 $this->local_data = array_merge($this->local_data, $data); 00017 } 00018 00019 } 00020 00021 public static function factory($name, $data = null) { 00022 return new View($name, $data); 00023 } 00024 00025 function render() { 00026 00027 $data = array_merge(View::$global_data, $this->local_data); 00028 00029 $output = Hub::$activecontroller->_loadView($this->filename, $data); 00030 return $output; 00031 00032 } 00033 00034 function set($name, $value) { 00035 $this->__set($name, $value); 00036 return $this; 00037 } 00038 00039 function __set($name, $value) { 00040 $this->local_data[$name] = $value; 00041 View::$global_data[$name] = $value; 00042 } 00043 00044 function __get($key) { 00045 if (isset($this->local_data[$key])) 00046 return $this->local_data[$key]; 00047 00048 if (isset(View::$global_data[$key])) 00049 return View::$global_data[$key]; 00050 00051 if (isset($this->$key)) 00052 return $this->$key; 00053 } 00054 00055 // gives controllers the ability to have format-specific code that only runs 00056 // when the given format is requested. the format can be a string or array 00057 00058 public static function respond_to( $format, $block ) { 00059 00060 if ( is_array( $format ) ) { 00061 foreach( $format as $extension ) { 00062 View::respond_to( $extension, $block ); 00063 } 00064 00065 return; 00066 } 00067 00068 if ( $format === Hub::$response_format ) { 00069 00070 isset( Hub::$mime_types[$format] ) ? header( "Content-Type:" . Hub::$mime_types[$format] ) : null; 00071 00072 $block($format); 00073 } 00074 00075 } 00076 00077 }