Changeset 284 for trunk/trax/vendor

Show
Ignore:
Timestamp:
02/18/07 13:04:07 (5 years ago)
Author:
john
Message:

patched AC to be able to set layout per action

Files:
1 modified

Legend:

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

    r269 r284  
    323323        } elseif($key == "redirect_to") { 
    324324            $this->redirect_to($value);        
     325        } elseif($key == "layout") { 
     326            $this->layout = $value; 
     327            $this->determine_layout();     
    325328        } else { 
    326329            $this->$key = $value; 
     
    335338        if(method_exists($this, $method_name)) { 
    336339            # If the method exists, just call it 
    337             $result = call_user_func(array($this, $method_name), $parameters); 
     340            $result = call_user_func_array(array($this, $method_name), $parameters); 
    338341        } else {         
    339342            if($method_name == "before_filter") { 
     
    578581             
    579582            include_once($this->controller_file);             
    580             if(class_exists($this->controller_class, false)) { 
     583            if(class_exists($this->controller_class, false)) {                 
     584                $class = $this->controller_class; 
     585                $this->controller_object = new $class();                 
     586            } 
     587             
     588            if(is_object($this->controller_object)) { 
    581589                 
    582                 $class = $this->controller_class; 
    583                 $this->controller_object = new $class(); 
    584                  
    585                 if(is_object($this->controller_object)) { 
    586                     
    587                     $this->controller_object->controller = $this->controller; 
    588                     $this->controller_object->action = $this->action; 
    589                     $this->controller_object->controller_path = "$this->added_path/$this->controller"; 
    590                     $this->controller_object->views_path = $this->views_path; 
    591                     $this->controller_object->layouts_path = $this->layouts_path; 
    592                     Trax::$current_controller_path = "$this->added_path/$this->controller"; 
    593                     Trax::$current_controller_name = $this->controller; 
    594                     Trax::$current_action_name = $this->action; 
    595                     Trax::$current_controller_object =& $this->controller_object; 
    596                     # Which layout should we use? 
    597                     $layout_file = $this->controller_object->determine_layout(); 
    598                     //error_log('using layout_file "'.$layout_file.'"'); 
    599                     # Check if there is any defined scaffolding to load 
    600                     if(isset($this->controller_object->scaffold)) { 
    601                         $scaffold = $this->controller_object->scaffold; 
    602                         if(file_exists(TRAX_LIB_ROOT."/scaffold_controller.php")) { 
    603                             include_once(TRAX_LIB_ROOT."/scaffold_controller.php"); 
    604                             $this->controller_object = new ScaffoldController($scaffold); 
    605                             Trax::$current_controller_object =& $this->controller_object; 
    606                             $render_options['scaffold'] = true; 
    607                             if(!file_exists($layout_file)) { 
    608                                 # the generic scaffold layout 
    609                                 $layout_file = TRAX_LIB_ROOT . "/templates/scaffolds/layout.phtml"; 
    610                             } 
     590                $this->controller_object->controller = $this->controller; 
     591                $this->controller_object->action = $this->action; 
     592                $this->controller_object->controller_path = "$this->added_path/$this->controller"; 
     593                $this->controller_object->views_path = $this->views_path; 
     594                $this->controller_object->layouts_path = $this->layouts_path; 
     595                Trax::$current_controller_path = "$this->added_path/$this->controller"; 
     596                Trax::$current_controller_name = $this->controller; 
     597                Trax::$current_action_name = $this->action; 
     598                Trax::$current_controller_object =& $this->controller_object; 
     599                # Which layout should we use? 
     600                $this->controller_object->determine_layout(); 
     601                # Check if there is any defined scaffolding to load 
     602                if(isset($this->controller_object->scaffold)) { 
     603                    $scaffold = $this->controller_object->scaffold; 
     604                    if(file_exists(TRAX_LIB_ROOT."/scaffold_controller.php")) { 
     605                        include_once(TRAX_LIB_ROOT."/scaffold_controller.php"); 
     606                        $this->controller_object = new ScaffoldController($scaffold); 
     607                        Trax::$current_controller_object =& $this->controller_object; 
     608                        $render_options['scaffold'] = true; 
     609                        if(!file_exists($this->controller_object->layout_file)) { 
     610                            # the generic scaffold layout 
     611                            $this->controller_object->layout_file = TRAX_LIB_ROOT . "/templates/scaffolds/layout.phtml"; 
    611612                        } 
    612613                    } 
    613                 }                 
    614             } 
    615  
    616             # Include main application helper file 
    617             if(file_exists($this->application_helper_file)) { 
    618                 include_once($this->application_helper_file); 
    619             } 
    620  
    621             # Include helper file for this controller 
    622             if(file_exists($this->helper_file)) { 
    623                 include_once($this->helper_file); 
    624             } 
    625              
    626             if(is_object($this->controller_object)) { 
     614                } 
     615 
     616                # Include main application helper file 
     617                if(file_exists($this->application_helper_file)) { 
     618                    include_once($this->application_helper_file); 
     619                } 
     620     
     621                # Include helper file for this controller 
     622                if(file_exists($this->helper_file)) { 
     623                    include_once($this->helper_file); 
     624                }                                
    627625 
    628626                # Include any extra helper files defined in this controller 
     
    650648                # Call the controller method based on the URL 
    651649                if($this->controller_object->execute_before_filters()) { 
    652                     
     650                    $controller_layout = null; 
     651                    if(isset($this->controller_object->layout)) { 
     652                        $controller_layout = $this->controller_object->layout;        
     653                    } 
    653654                    if(method_exists($this->controller_object, $this->action)) { 
    654655                        //error_log('method '.$this->action.' exists, calling it'); 
     
    673674                        $this->raise("No action responded to ".$this->action, "Unknown action", "404"); 
    674675                    } 
     676                     
     677                    if(isset($this->controller_object->layout)) { 
     678                        if($controller_layout != $this->controller_object->layout) { 
     679                            # layout was set in the action need to redetermine the layout file to use. 
     680                            $this->controller_object->determine_layout(); 
     681                        }        
     682                    } 
     683                     
    675684                    $this->controller_object->execute_after_filters(); 
    676685                     
     
    708717                    if(isset($this->controller_object->render_layout) 
    709718                       && ($this->controller_object->render_layout !== false) 
    710                        && $layout_file) { 
     719                       && $this->controller_object->layout_file) { 
    711720                        $locals['content_for_layout'] = $content_for_layout; 
    712721                        # render the layout 
    713                         //error_log("rendering layout: $layout_file"); 
    714                         if(!$this->controller_object->render_file($layout_file, false, $locals)) { 
     722                        //error_log("rendering layout: ".$this->controller_object->layout_file); 
     723                        if(!$this->controller_object->render_file($this->controller_object->layout_file, false, $locals)) { 
    715724                            # No layout template so just echo out whatever is in $content_for_layout 
    716725                            //echo "HERE"; 
     
    721730                        # $this->raise("No layout file found.", "Unknown layout", "404");  
    722731                        # No layout template so just echo out whatever is in $content_for_layout 
     732                        //error_log("no layout found: ".$this->controller_object->layout_file); 
    723733                        echo $content_for_layout; 
    724734                    } 
    725                 } 
     735                }        
    726736            } else { 
    727737                $this->raise("Failed to instantiate controller object \"".$this->controller."\".", "ActionController Error", "500"); 
     
    734744        if(!$this->keep_flash) { 
    735745            # Nuke the flash 
     746            unset($_SESSION['flash']); 
    736747            Session::unset_var('flash'); 
    737748        } 
     
    763774    function set_paths() { 
    764775        if(is_array($this->url_path)) { 
     776            $test_path = null; 
    765777            foreach($this->url_path as $path) { 
    766                 if(file_exists($this->controllers_path . "/$path")) { 
     778                $test_path = (is_null($test_path) ? $path : "$test_path/$path"); 
     779                if(is_dir($this->controllers_path . "/$test_path")) { 
    767780                    $extra_path[] = $path; 
    768781                } else { 
     782                    $test_path = null; 
    769783                    $new_path[] = $path; 
    770784                } 
     
    11891203            $layout_file = $default_layout_file; 
    11901204        } 
     1205        $this->layout_file = $layout_file; 
    11911206        return $layout_file; 
    11921207    }