View.php (5415B)
1 <?php 2 3 /** 4 * Class View 5 * The part that handles all the output 6 */ 7 class View 8 { 9 /** 10 * simply includes (=shows) the view. this is done from the controller. In the controller, you usually say 11 * $this->view->render('help/index'); to show (in this example) the view index.php in the folder help. 12 * Usually the Class and the method are the same like the view, but sometimes you need to show different views. 13 * @param string $filename Path of the to-be-rendered view, usually folder/file(.php) 14 * @param array $data Data to be used in the view 15 */ 16 public function render($filename, $data = null) 17 { 18 if ($data) { 19 foreach ($data as $key => $value) { 20 $this->{$key} = $value; 21 } 22 } 23 24 require Config::get('PATH_VIEW') . '_templates/header.php'; 25 require Config::get('PATH_VIEW') . $filename . '.php'; 26 require Config::get('PATH_VIEW') . '_templates/footer.php'; 27 } 28 29 /** 30 * Similar to render, but accepts an array of separate views to render between the header and footer. Use like 31 * the following: $this->view->renderMulti(array('help/index', 'help/banner')); 32 * @param array $filenames Array of the paths of the to-be-rendered view, usually folder/file(.php) for each 33 * @param array $data Data to be used in the view 34 * @return bool 35 */ 36 public function renderMulti($filenames, $data = null) 37 { 38 if (!is_array($filenames)) { 39 self::render($filenames, $data); 40 return false; 41 } 42 43 if ($data) { 44 foreach ($data as $key => $value) { 45 $this->{$key} = $value; 46 } 47 } 48 49 require Config::get('PATH_VIEW') . '_templates/header.php'; 50 51 foreach($filenames as $filename) { 52 require Config::get('PATH_VIEW') . $filename . '.php'; 53 } 54 55 require Config::get('PATH_VIEW') . '_templates/footer.php'; 56 } 57 58 /** 59 * Same like render(), but does not include header and footer 60 * @param string $filename Path of the to-be-rendered view, usually folder/file(.php) 61 * @param mixed $data Data to be used in the view 62 */ 63 public function renderWithoutHeaderAndFooter($filename, $data = null) 64 { 65 if ($data) { 66 foreach ($data as $key => $value) { 67 $this->{$key} = $value; 68 } 69 } 70 71 require Config::get('PATH_VIEW') . $filename . '.php'; 72 } 73 74 /** 75 * Renders pure JSON to the browser, useful for API construction 76 * @param $data 77 */ 78 public function renderJSON($data) 79 { 80 header("Content-Type: application/json"); 81 echo json_encode($data); 82 } 83 84 /** 85 * renders the feedback messages into the view 86 */ 87 public function renderFeedbackMessages() 88 { 89 // echo out the feedback messages (errors and success messages etc.), 90 // they are in $_SESSION["feedback_positive"] and $_SESSION["feedback_negative"] 91 require Config::get('PATH_VIEW') . '_templates/feedback.php'; 92 93 // delete these messages (as they are not needed anymore and we want to avoid to show them twice 94 Session::set('feedback_positive', null); 95 Session::set('feedback_negative', null); 96 } 97 98 /** 99 * Checks if the passed string is the currently active controller. 100 * Useful for handling the navigation's active/non-active link. 101 * 102 * @param string $filename 103 * @param string $navigation_controller 104 * 105 * @return bool Shows if the controller is used or not 106 */ 107 public static function checkForActiveController($filename, $navigation_controller) 108 { 109 $split_filename = explode("/", $filename); 110 $active_controller = $split_filename[0]; 111 112 if ($active_controller == $navigation_controller) { 113 return true; 114 } 115 116 return false; 117 } 118 119 /** 120 * Checks if the passed string is the currently active controller-action (=method). 121 * Useful for handling the navigation's active/non-active link. 122 * 123 * @param string $filename 124 * @param string $navigation_action 125 * 126 * @return bool Shows if the action/method is used or not 127 */ 128 public static function checkForActiveAction($filename, $navigation_action) 129 { 130 $split_filename = explode("/", $filename); 131 $active_action = $split_filename[1]; 132 133 if ($active_action == $navigation_action) { 134 return true; 135 } 136 137 return false; 138 } 139 140 /** 141 * Checks if the passed string is the currently active controller and controller-action. 142 * Useful for handling the navigation's active/non-active link. 143 * 144 * @param string $filename 145 * @param string $navigation_controller_and_action 146 * 147 * @return bool 148 */ 149 public static function checkForActiveControllerAndAction($filename, $navigation_controller_and_action) 150 { 151 $split_filename = explode("/", $filename); 152 $active_controller = $split_filename[0]; 153 $active_action = $split_filename[1]; 154 155 $split_filename = explode("/", $navigation_controller_and_action); 156 $navigation_controller = $split_filename[0]; 157 $navigation_action = $split_filename[1]; 158 159 if ($active_controller == $navigation_controller AND $active_action == $navigation_action) { 160 return true; 161 } 162 163 return false; 164 } 165 }