Changeset 89 for trunk/trax/vendor/trax/action_controller.php
- Timestamp:
- 12/14/05 00:00:55 (6 years ago)
- Files:
-
- 1 modified
-
trunk/trax/vendor/trax/action_controller.php (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/trax/vendor/trax/action_controller.php
r73 r89 31 31 $controllers_path, 32 32 $helpers_path, 33 $helpers_base_path, 33 34 $layouts_path, 34 35 $url_path, … … 39 40 $application_helper_file, 40 41 $loaded = false, 41 $router_loaded = false; 42 $router_loaded = false, 43 $helpers = array(), 44 $before_filters = array(), 45 $after_filters = array(); 42 46 protected 43 47 $before_filter = null, … … 61 65 $this->add_before_filter($value); 62 66 } elseif($key == "after_filter") { 63 $this->add_after_filter($value); 67 $this->add_after_filter($value); 68 } elseif($key == "helper") { 69 $this->add_helper($value); 64 70 } else { 65 71 $this->$key = $value; … … 111 117 $this->application_controller_file = $this->controllers_path . "/application.php"; 112 118 $this->application_helper_file = $this->helpers_path . "/application_helper.php"; 113 $this->layouts_path = TRAX_ROOT . $GLOBALS['TRAX_INCLUDES']['layouts'];114 $this->default_layout_file = $this->layouts_ path . "/" . DEFAULT_LAYOUT . "." . $this->views_file_extention;119 $this->layouts_path = $this->layouts_base_path = TRAX_ROOT . $GLOBALS['TRAX_INCLUDES']['layouts']; 120 $this->default_layout_file = $this->layouts_base_path . "/application." . $this->views_file_extention; 115 121 $this->views_path = TRAX_ROOT . $GLOBALS['TRAX_INCLUDES']['views']; 116 122 … … 135 141 if(@in_array(":id",$route_path)) { 136 142 $this->id = strtolower($this->url_path[@array_search(":id", $route_path)]); 143 if($this->id != "") { 144 $_REQUEST['id'] = $this->id; 145 } 137 146 } 138 147 … … 168 177 if(file_exists($this->application_controller_file)) { 169 178 include_once($this->application_controller_file); 170 }171 172 # Include main application helper file173 if(file_exists($this->application_helper_file)) {174 include_once($this->application_helper_file);175 179 } 176 180 … … 181 185 $class = $this->controller_class; 182 186 $this->controller_object = new $class(); 183 184 $layout = $this->controller_object->layout; 185 # Call class method to set the layout dynamically at runtime 186 if(method_exists($this->controller_object, $layout)) { 187 $layout = $this->controller_object->$layout(); 188 } 187 if(is_object($this->controller_object)) { 188 $GLOBALS['current_controller_path'] = "$this->added_path/$this->controller"; 189 $GLOBALS['current_controller_name'] = $this->controller; 190 } 191 192 # Which layout should we use? 193 $this->layout_file = $this->determine_layout(); 189 194 190 195 # Check if there is any defined scaffolding to load … … 199 204 $this->view_file = TRAX_LIB_ROOT . "/templates/scaffolds/index.phtml"; 200 205 } 201 if( $layout == "") {206 if(!file_exists($this->layout_file)) { 202 207 # the generic scaffold layout 203 208 $this->layout_file = TRAX_LIB_ROOT . "/templates/scaffolds/layout.phtml"; … … 207 212 } 208 213 209 if($this->id != "") { 210 $_REQUEST['id'] = $this->id; 214 # Include main application helper file 215 if(file_exists($this->application_helper_file)) { 216 include_once($this->application_helper_file); 211 217 } 212 218 … … 216 222 } 217 223 218 # Include any extra helper files 219 $this->include_extra_helpers(); 224 # Include any extra helper files defined in this controller 225 if(count($this->helpers) > 0) { 226 foreach($this->helpers as $helper) { 227 if(strstr($helper, "/")) { 228 $file = substr(strrchr($helper, "/"), 1); 229 $path = substr($helper, 0, strripos($helper, "/")); 230 $helper_path_with_file = $this->helpers_base_path."/".$path."/".$file."_helper.php"; 231 } else { 232 $helper_path_with_file = $this->helpers_base_path."/".$helper."_helper.php"; 233 } 234 if(file_exists($helper_path_with_file)) { 235 # Include the helper file 236 include($helper_path_with_file); 237 } 238 } 239 } 220 240 221 241 if(is_object($this->controller_object)) { … … 270 290 ob_end_clean(); 271 291 272 if(!$this->layout_file) {273 $this->layout_file = $this->layouts_path . "/" . $layout . "." . $this->views_file_extention;274 }275 276 292 if(file_exists($this->layout_file)) { 277 # render user definedlayout293 # render the layout 278 294 include($this->layout_file); 279 } elseif(file_exists($this->default_layout_file)) {280 # render default layout281 include($this->default_layout_file);282 295 } else { 283 296 # Can't find any layout so throw an exception 284 # $this->raise("No layout file found.", "Unknown layout", "404");297 # $this->raise("No layout file found.", "Unknown layout", "404"); 285 298 # No layout template so just echo out whatever is in $content_for_layout 286 299 echo $content_for_layout; … … 288 301 } 289 302 } else { 290 $this->raise("Failed to instantiate controller object \"".$this->controller."\".", "ActionController Error", "500"); 303 $this->raise("Failed to instantiate controller object \"".$this->controller."\".", "ActionController Error", "500"); 291 304 } 292 305 } else { … … 306 319 foreach($this->url_path as $path) { 307 320 if(file_exists($this->controllers_path . "/$path")) { 308 $this->controllers_path .= "/$path"; 309 $this->helpers_path .= "/$path"; 310 $this->views_path .= "/$path"; 321 $extra_path[] = $path; 311 322 } else { 312 323 $new_path[] = $path; 313 324 } 314 325 } 326 if(is_array($extra_path)) { 327 $extra_path = implode("/", $extra_path); 328 $this->added_path = $extra_path; 329 $this->controllers_path .= "/$extra_path"; 330 $this->helpers_path .= "/$extra_path"; 331 $this->views_path .= "/$extra_path"; 332 $this->layouts_path .= "/$extra_path"; 333 } 315 334 if(is_array($new_path)) { 316 335 $this->url_path = $new_path; … … 320 339 321 340 function execute_before_filters() { 322 if( is_array($this->controller_object->before_filter)) {323 foreach($this-> controller_object->before_filteras $filter_function) {341 if(count($this->before_filters) > 0) { 342 foreach($this->before_filters as $filter_function) { 324 343 if(method_exists($this->controller_object, $filter_function)) { 325 344 $this->controller_object->$filter_function(); 326 345 } 327 346 } 328 } elseif($this->controller_object->before_filter != "") {329 if(method_exists($this->controller_object, $this->controller_object->before_filter)) {330 $filter_function = $this->controller_object->before_filter;331 $this->controller_object->$filter_function();332 }333 347 } 334 348 } 335 349 336 350 function add_before_filter($filter_function_name) { 337 if (is_array($filter_function_name)) { 338 if(count($this->before_filter) > 0) { 339 $this->before_filter = array_merge($this->before_filter, $filter_function_name); 351 if(is_string($filter_function_name) && !empty($filter_function_name)) { 352 $this->before_filters[] = $filter_function_name; 353 } elseif(is_array($filter_function_name)) { 354 if(count($this->before_filters) > 0) { 355 $this->before_filters = array_merge($this->before_filters, $filter_function_name); 340 356 } else { 341 $this->before_filter = $filter_function_name; 342 } 343 } elseif($this->before_filter != "") { 344 $this->before_filter = array($this->before_filter, $filter_function_name); 345 } else { 346 $this->before_filter = $filter_function_name; 357 $this->before_filters = $filter_function_name; 358 } 347 359 } 348 360 } 349 361 350 362 function execute_after_filters() { 351 if( is_array($this->controller_object->after_filter)) {352 foreach($this-> controller_object->after_filteras $filter_function) {363 if(count($this->after_filters) > 0) { 364 foreach($this->after_filters as $filter_function) { 353 365 if(method_exists($this->controller_object, $filter_function)) { 354 366 $this->controller_object->$filter_function(); 355 367 } 356 368 } 357 } elseif($this->controller_object->after_filter != "") {358 if(method_exists($this->controller_object, $this->controller_object->after_filter)) {359 $filter_function = $this->controller_object->after_filter;360 $this->controller_object->$filter_function();361 }362 369 } 363 370 } 364 371 365 372 function add_after_filter($filter_function_name) { 366 if (is_array($filter_function_name)) { 367 if(count($this->after_filter) > 0) { 368 $this->after_filter = array_merge($this->after_filter, $filter_function_name); 373 if(is_string($filter_function_name) && !empty($filter_function_name)) { 374 $this->after_filters[] = $filter_function_name; 375 } elseif(is_array($filter_function_name)) { 376 if(count($this->after_filters) > 0) { 377 $this->after_filters = array_merge($this->after_filters, $filter_function_name); 369 378 } else { 370 $this->after_filter = $filter_function_name; 371 } 372 } elseif($this->after_filter != "") { 373 $this->after_filter = array($this->after_filter, $filter_function_name); 374 } else { 375 $this->after_filter = $filter_function_name; 376 } 377 } 378 379 function include_extra_helpers() { 380 if(is_array($this->controller_object->helpers)) { 381 foreach($this->controller_object->helpers as $helper) { 382 if(file_exists($this->helpers_base_path . "/$helper")) { 383 include_once($this->helpers_base_path . "/$helper"); 384 } 385 } 386 } 379 $this->after_filters = $filter_function_name; 380 } 381 } 382 } 383 384 function add_helper($helper_name) { 385 $this->helpers[] = $helper_name; 387 386 } 388 387 … … 401 400 include($path_with_file); 402 401 } 402 } 403 404 function determine_layout() { 405 # I guess you don't want any layout 406 if($this->controller_object->layout == "null") { 407 return null; 408 } 409 # $layout will be the layout defined in the current controller 410 # or try to use the controller name for the layout 411 $layout = $this->controller_object->layout ? $this->controller_object->layout : $this->controller; 412 # Check if a method has been defined to determine the layout at runtime 413 if(method_exists($this->controller_object, $layout)) { 414 $layout = $this->controller_object->$layout(); 415 } 416 if($layout) { 417 # Is this layout for from a different controller 418 if(strstr($layout, "/")) { 419 $file = substr(strrchr($layout, "/"), 1); 420 $path = substr($layout, 0, strripos($layout, "/")); 421 $layout = $this->layouts_base_path."/".$path."/".$file.".".$this->views_file_extention; 422 } else { 423 # Is there a layout for the current controller 424 $layout = $this->layouts_path."/".$layout.".".$this->views_file_extention; 425 } 426 if(file_exists($layout)) { 427 $layout_file = $layout; 428 } 429 } 430 # No defined layout found so just use the default layout app/views/layouts/application.phtml 431 if(!$layout_file) { 432 $layout_file = $this->default_layout_file; 433 } 434 return $layout_file; 403 435 } 404 436 … … 425 457 if($trace) { 426 458 echo "<pre style=\"background-color: #eee;padding:10px;font-size: 11px;\">"; 427 echo "<code>$trace</code></pre>\n"; 459 echo "<code>$trace</code></pre>\n"; 428 460 } 429 461 echo "</font>\n"; 430 462 } else { 431 463 echo "<font face=\"verdana, arial, helvetica, sans-serif\">\n"; 432 echo "<h2>Application Error</h2>Trax application failed to start properly"; 433 echo "</font>\n"; 464 echo "<h2>Application Error</h2>Trax application failed to start properly"; 465 echo "</font>\n"; 434 466 } 435 467 }
