Show
Ignore:
Timestamp:
12/14/05 00:00:55 (6 years ago)
Author:
john
Message:

changed the default layout to be application and made it look for controller named layouts

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/trax/vendor/trax/action_controller.php

    r73 r89  
    3131        $controllers_path, 
    3232        $helpers_path, 
     33        $helpers_base_path, 
    3334        $layouts_path, 
    3435        $url_path, 
     
    3940        $application_helper_file, 
    4041        $loaded = false, 
    41         $router_loaded = false; 
     42        $router_loaded = false, 
     43        $helpers = array(), 
     44        $before_filters = array(), 
     45        $after_filters = array(); 
    4246    protected 
    4347        $before_filter = null, 
     
    6165            $this->add_before_filter($value); 
    6266        } 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); 
    6470        } else { 
    6571            $this->$key = $value; 
     
    111117            $this->application_controller_file = $this->controllers_path . "/application.php"; 
    112118            $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; 
    115121            $this->views_path = TRAX_ROOT . $GLOBALS['TRAX_INCLUDES']['views']; 
    116122 
     
    135141                if(@in_array(":id",$route_path)) { 
    136142                    $this->id = strtolower($this->url_path[@array_search(":id", $route_path)]); 
     143                    if($this->id != "") { 
     144                        $_REQUEST['id'] = $this->id; 
     145                    } 
    137146                } 
    138147 
     
    168177        if(file_exists($this->application_controller_file)) { 
    169178            include_once($this->application_controller_file); 
    170         } 
    171  
    172         # Include main application helper file 
    173         if(file_exists($this->application_helper_file)) { 
    174             include_once($this->application_helper_file); 
    175179        } 
    176180 
     
    181185                $class = $this->controller_class; 
    182186                $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(); 
    189194 
    190195                # Check if there is any defined scaffolding to load 
     
    199204                            $this->view_file = TRAX_LIB_ROOT . "/templates/scaffolds/index.phtml"; 
    200205                        } 
    201                         if($layout == "") { 
     206                        if(!file_exists($this->layout_file)) { 
    202207                            # the generic scaffold layout 
    203208                            $this->layout_file = TRAX_LIB_ROOT . "/templates/scaffolds/layout.phtml"; 
     
    207212            } 
    208213 
    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); 
    211217            } 
    212218 
     
    216222            } 
    217223 
    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            } 
    220240 
    221241            if(is_object($this->controller_object)) { 
     
    270290                    ob_end_clean(); 
    271291 
    272                     if(!$this->layout_file) { 
    273                         $this->layout_file = $this->layouts_path . "/" . $layout . "." . $this->views_file_extention; 
    274                     } 
    275  
    276292                    if(file_exists($this->layout_file)) { 
    277                         # render user defined layout 
     293                        # render the layout 
    278294                        include($this->layout_file); 
    279                     } elseif(file_exists($this->default_layout_file)) { 
    280                         # render default layout 
    281                         include($this->default_layout_file); 
    282295                    } else { 
    283296                        # 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");  
    285298                        # No layout template so just echo out whatever is in $content_for_layout 
    286299                        echo $content_for_layout; 
     
    288301                } 
    289302            } 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"); 
    291304            } 
    292305        } else { 
     
    306319            foreach($this->url_path as $path) { 
    307320                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; 
    311322                } else { 
    312323                    $new_path[] = $path; 
    313324                } 
    314325            } 
     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            } 
    315334            if(is_array($new_path)) { 
    316335                $this->url_path = $new_path; 
     
    320339 
    321340    function execute_before_filters() { 
    322         if(is_array($this->controller_object->before_filter)) { 
    323             foreach($this->controller_object->before_filter as $filter_function) { 
     341        if(count($this->before_filters) > 0) { 
     342            foreach($this->before_filters as $filter_function) { 
    324343                if(method_exists($this->controller_object, $filter_function)) { 
    325344                    $this->controller_object->$filter_function(); 
    326345                } 
    327346            } 
    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             } 
    333347        } 
    334348    } 
    335349 
    336350    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); 
    340356            } 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            } 
    347359        } 
    348360    } 
    349361 
    350362    function execute_after_filters() { 
    351         if(is_array($this->controller_object->after_filter)) { 
    352             foreach($this->controller_object->after_filter as $filter_function) { 
     363        if(count($this->after_filters) > 0) { 
     364            foreach($this->after_filters as $filter_function) { 
    353365                if(method_exists($this->controller_object, $filter_function)) { 
    354366                    $this->controller_object->$filter_function(); 
    355367                } 
    356368            } 
    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             } 
    362369        } 
    363370    } 
    364371 
    365372    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); 
    369378            } 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; 
    387386    } 
    388387 
     
    401400            include($path_with_file); 
    402401        } 
     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; 
    403435    } 
    404436 
     
    425457            if($trace) { 
    426458                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"; 
    428460            } 
    429461            echo "</font>\n"; 
    430462        } else { 
    431463            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"; 
    434466        } 
    435467    }