Highway to PSR2
[openemr.git] / interface / super / rules / base / library / ActionRouter.php
blobfc2b4be145fbbec349183af84c406c09dc61afdd
1 <?php
2 // Copyright (C) 2010-2011 Aron Racho <aron@mi-squred.com>
3 //
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.
9 /**
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
16 * @author aron
18 class ActionRouter
21 var $controller;
22 var $path;
23 var $webRoot;
24 var $appRoot;
25 var $action;
27 function __construct($controller, $action, $path)
29 $this->controller = $controller;
30 $this->action = $action;
31 $this->path = $path;
32 $this->appRoot = base_dir();
33 $this->webRoot = $GLOBALS['webroot'];
36 function route()
38 if (!$this->action) {
39 $this->action = "default";
42 $result = $this->perform($this->action);
44 $forward = $result->_forward;
45 if ($forward) {
46 $this->perform($forward);
47 return;
50 $_redirect = $result->_redirect;
51 if ($_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)) {
70 // try common
71 $view_location = base_dir() . "base/view/" . $viewName;
74 // set viewbean in page scope
75 $viewBean = $result;
77 // set helpers
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)) {
89 // no view template
90 return $result;
93 $viewBean->_appRoot = $this->appRoot;
94 $viewBean->_webRoot = $this->webRoot;
95 $viewBean->_view_body = $view_location;
97 $template = $this->resolveTemplate($result->_template);
98 require($template);
100 return $result;
103 function resolveTemplate($templateName)
105 // try local
106 $template_location = $this->path . "/template/" . $templateName;
108 // try common
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;
116 } else {
117 // otherwise use the basic template
118 $baseTemplatesDir = base_dir() . "base/template";
119 return $baseTemplatesDir . "/basic.php";
123 function resolveHelper($name)
125 // try local
126 $location = $this->path . "/helper/" . $name;
128 // try common
129 if (!is_file($location)) {
130 $location = base_dir() . "base/helper/" . $name;
133 if (is_file($location)) {
134 // return template if its found
135 return $location;
136 } else {
137 return null;