1 <?php
2
3 4 5
6 class MagratheaController {
7
8 protected $Smarty;
9
10 public $forceMethodCall = false;
11 protected $displayStatic = false;
12 private $staticPage = false;
13
14 15 16
17 public function StartSmarty(){
18 global $Smarty;
19 $this->Smarty = $Smarty;
20 }
21
22 23 24 25
26 public static function GetSmarty(){
27 global $Smarty;
28 return $Smarty;
29 }
30
31 32 33 34 35 36
37 public function Redirect($control, $action){
38 self::Load($control, $action);
39 return;
40 }
41
42 43 44 45 46 47
48 public function ForwardTo($control, $action=null){
49 if( $action )
50 header("Location: /".$control."/".$action);
51 else
52 header("Location: /".$control);
53 exit;
54 }
55
56 57 58 59 60 61
62 public function AllowCache($allow){
63 $this->displayStatic = $allow;
64 return $this;
65 }
66
67 68 69 70
71 public function formatStaticPageName($name){
72 $name = strtolower($name);
73 if (!preg_match("{{(.htm(l)?)$/i}}", $name)) {
74 $name = $name.".html";
75 }
76 return $name;
77 }
78
79 80 81 82 83 84 85
86 public function GetStatic($name, $die=true){
87 if(!$this->displayStatic) return false;
88 $staticName = $this->formatStaticPageName($name);
89 $appFolder = MagratheaConfig::Instance()->GetConfigFromDefault("site_path");
90 $filePath = $appFolder."/Static/".$staticName;
91 if( file_exists($filePath) ){
92 $code = file_get_contents($filePath);
93 print($code);
94 if($die) die();
95 return true;
96 } else {
97 $this->staticPage = $staticName;
98 return false;
99 }
100 }
101
102 103 104 105
106 public function Display($template){
107 if(!$this->staticPage) {
108 $this->Smarty->display($template);
109 } else {
110 $code = $this->Smarty->fetch($template);
111 $code .= "\n<!-- page generated at ".date("Y-m-d h:i:s")." -->";
112 $appFolder = MagratheaConfig::Instance()->GetConfigFromDefault("site_path");
113 $filePath = $appFolder."/Static/".$this->staticPage;
114 $file_handler = @fopen($filePath, 'w');
115 fwrite($file_handler, $code);
116 fclose($file_handler);
117 echo $code;
118 }
119 }
120
121
122
123 124 125 126 127 128 129
130 public static function Load($control, $action, $params=""){
131 $controlName = ucfirst(strtolower($control))."Controller";
132 try {
133 if(!class_exists($controlName)){
134 $ex = new MagratheaControllerException("Class ".$controlName." does not exist! - parameters called [".$control."/".$action."/".$params."]");
135 $ex->killerError = false;
136 throw $ex;
137 }
138 $control = new $controlName;
139 $control->StartSmarty();
140 $control->$action($params);
141 } catch (Exception $e) {
142 throw $e;
143 self::ErrorHandle($e);
144 }
145 }
146
147 148 149 150
151 protected function Json($response){
152
153 MagratheaDebugger::Instance()->SetType(MagratheaDebugger::NONE);
154 header('Content-Type: application/json');
155 echo json_encode($response);
156 exit;
157 }
158
159
160 161 162
163 public static function IncludeAllControllers(){
164 $modelsFolder = MagratheaConfig::Instance()->GetConfigFromDefault("site_path")."/Controls";
165 if($handle = @opendir($modelsFolder)){
166 while (false !== ($file = readdir($handle))) {
167 $filename = explode('.', $file);
168 $ext = array_pop($filename);
169 if(empty($ext) || $file[0] == "_") continue;
170 if($ext == "php"){
171 include_once($modelsFolder."/".$file);
172 }
173 }
174 closedir($handle);
175 }
176 }
177
178 179 180 181
182 public static function GetAllStatic(){
183 $appFolder = MagratheaConfig::Instance()->GetConfigFromDefault("site_path");
184 $staticPath = $appFolder."/Static/";
185 $results = scandir($staticPath);
186 $statics = array();
187 foreach ($results as $result) {
188 if ($result === '.' or $result === '..') continue;
189 array_push($statics, $result);
190 }
191 sort($statics);
192 return $statics;
193 }
194
195 196 197 198
199 public static function ClearStatic(){
200 $appFolder = MagratheaConfig::Instance()->GetConfigFromDefault("site_path");
201 $staticPath = realpath($appFolder."/Static");
202 $output = shell_exec("rm ".$staticPath."/*");
203 return $output;
204 }
205
206
207
208 public static function ErrorHandle($ex){
209 if(is_a($ex, "MagratheaException")){
210 $ex->display();
211 die;
212 }
213 die($ex->getMessage());
214 }
215
216 public function __call($method_name, $args = array()){
217 if(method_exists($this->Smarty, $method_name)){
218 return call_user_func_array(array(&$this->Smarty, $method_name), $args);
219 } else {
220 throw new MagratheaControllerException("Function could not be found (even in Smarty):".$method_name);
221
222 }
223 }
224
225 }
226
227
228 ?>
229