2 // Copyright (C) 2010-2011 Aron Racho <aron@mi-squred.com>
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
10 * This is a very simple action routing class for a given controller. Given
11 * a controller and action (typically from ControllerRouter), it goes through
12 * these steps to find out which function in that controller should be invoked:
14 * todo - document these steps
27 function __construct($controller, $action, $path)
29 $this->controller
= $controller;
30 $this->action
= $action;
32 $this->appRoot
= base_dir();
33 $this->webRoot
= $GLOBALS['webroot'];
39 $this->action
= "default";
42 $result = $this->perform($this->action
);
44 $forward = $result->_forward
;
46 $this->perform($forward);
50 $_redirect = $result->_redirect
;
52 $baseTemplatesDir = base_dir() . "base/template";
53 require($baseTemplatesDir . "/redirect.php");
57 function perform($action)
59 $action_method = '_action_' . $action;
61 // execute the default action if action is not found
62 method_exists($this->controller
, $action_method) ?
63 $this->controller
->$action_method() : $this->controller
->_action_default();
64 $result = $this->controller
->viewBean
;
66 // resolve view location
67 $viewName = $result->_view
;
68 $view_location = $this->path
. "/view/" . $viewName;
69 if (!is_file($view_location)) {
71 $view_location = base_dir() . "base/view/" . $viewName;
74 // set viewbean in page scope
78 $helpers = $viewBean->helpers
;
79 if (!is_null($helpers)) {
80 foreach ($helpers as $helper) {
81 $helperPath = $this->resolveHelper($helper);
82 if (!is_null($helperPath)) {
83 require_once($helperPath);
88 if (!is_file($view_location)) {
93 $viewBean->_appRoot
= $this->appRoot
;
94 $viewBean->_webRoot
= $this->webRoot
;
95 $viewBean->_view_body
= $view_location;
97 $template = $this->resolveTemplate($result->_template
);
103 function resolveTemplate($templateName)
106 $template_location = $this->path
. "/template/" . $templateName;
109 if (!is_file($template_location)) {
110 $template_location = base_dir() . "base/template/" . $templateName;
113 if (is_file($template_location)) {
114 // return template if its found
115 return $template_location;
117 // otherwise use the basic template
118 $baseTemplatesDir = base_dir() . "base/template";
119 return $baseTemplatesDir . "/basic.php";
123 function resolveHelper($name)
126 $location = $this->path
. "/helper/" . $name;
129 if (!is_file($location)) {
130 $location = base_dir() . "base/helper/" . $name;
133 if (is_file($location)) {
134 // return template if its found