Put spacing in between the submit row buttons
[isso.git] / Router.php
1 <?php
2 /*=====================================================================*
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /**
23 * Request Router (Router.php)
24 *
25 * @package ISSO
26 */
27
28 require_once('ISSO/Functions.php');
29
30 /**
31 * Router
32 *
33 * Run route() in a file with a defined routing pattern and send all
34 * requests to that file, this will then load and appropriatley run
35 * the right source files
36 *
37 * @author Blue Static
38 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
39 * @version $Revision$
40 * @package ISSO
41 *
42 */
43 class BSRouter
44 {
45 const MAP_ACTION = 0;
46 const MAP_PARAMS = 1;
47
48 /**
49 * Routing map of request:(method,args...)
50 * @var array
51 */
52 private $map = array();
53
54 /**
55 * The base URL
56 * @var string
57 */
58 private $basePath;
59
60 /**
61 * Map action for errors
62 * @var string
63 */
64 private $errorAction = ':undefined:';
65
66 /**
67 * Router action requested
68 * @var string
69 */
70 private $request;
71
72 /**
73 * Request paramaters
74 * @var array
75 */
76 private $params = array();
77
78 // ###################################################################
79 /**
80 * Constructor
81 */
82 public function __construct()
83 {
84 $this->basePath = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['SCRIPT_NAME']);
85 }
86
87 // ###################################################################
88 /**
89 * Listens for routing requests and handles them appropriately
90 */
91 public function route()
92 {
93 $request = str_replace($this->basePath, '', $_SERVER['REQUEST_URI']);
94
95 $params = explode('/', $request);
96 $this->request = $params[0];
97 unset($params[0]);
98
99 if (sizeof($params) == 0)
100 {
101 // do nothing
102 }
103 else if (sizeof($params) == 1)
104 {
105 $this->params[':id'] = $params[1];
106 }
107 else
108 {
109 $key = '';
110 for ($i = 1; $i <= sizeof($params); $i++)
111 {
112 if ($i == 1 AND $params["$i"][0] != '+')
113 {
114 $this->params[':id'] = $params["$i"];
115 continue;
116 }
117
118 if ($params["$i"] == '')
119 {
120 continue;
121 }
122
123 if ($params["$i"][0] == '+')
124 {
125 if (!empty($key))
126 {
127 $this->_error(sprintf(_('The paramter "%1$s" does not have a value'), $key));
128 }
129 $key = substr($params["$i"], 1);
130 }
131 else
132 {
133 if (empty($key))
134 {
135 $this->_error(sprintf(_('The value "%1$s" does not have a paramatized key'), $params["$i"]));
136 }
137 $this->params["$key"] = $params["$i"];
138 $key = '';
139 }
140 }
141 }
142
143 if (isset($this->map[ $this->request ]))
144 {
145 call_user_func_array($this->map[ $this->request ][self::MAP_ACTION], $this->map[ $this->request ][self::MAP_PARAMS]);
146 }
147 else
148 {
149 $this->_error(sprintf(_('Routing request "%1$s" not found in map'), $this->request));
150 }
151 }
152
153 // ###################################################################
154 /**
155 * Sets the error handler of the router; when an error occurs while
156 * routing, this method is called
157 *
158 * @param string Error handler name
159 */
160 public function setErrorAction($name)
161 {
162 $this->errorAction = $name;
163 }
164
165 // ###################################################################
166 /**
167 * Adds a directory of files to the maps, with the file names (without .php)
168 * as the map request name
169 *
170 * @param string Directory path
171 */
172 public function addFileDirectory($path)
173 {
174 $path = BSFunctions::FetchSourcePath($path);
175 $controllers = BSFunctions::ScanDirectory($path);
176 $controllers = $controllers[''];
177
178 foreach ($controllers AS $file)
179 {
180 $controller = str_replace('.php', '', $file);
181 $this->addFileRequest($controller, $path . $file);
182 }
183 }
184
185 // ###################################################################
186 /**
187 * Routes a request to the loading of a file; this is the most common
188 * routing request and it performs no extra functions
189 *
190 * @param string Request name
191 * @param string File name
192 */
193 public function addFileRequest($request, $file)
194 {
195 $this->map["$request"] = array(array($this, '_loadFile'), array($file));
196 }
197
198 // ###################################################################
199 /**
200 * Includes a given file name
201 *
202 * @param string File name
203 */
204 private function _loadFile($filename)
205 {
206 if (!file_exists($filename))
207 {
208 $this->_error(sprintf(_('Cannot find the file "%1$s"'), $filename));
209 }
210
211 include($filename);
212 }
213
214 // ###################################################################
215 /**
216 * Sends an error to the error action handler
217 *
218 * @param string Error message
219 */
220 private function _error($errMsg)
221 {
222 if (function_exists($this->errorAction) OR (is_array($this->errorAction) AND method_exists($this->errorAction[0], $this->errorAction[1])))
223 {
224 $this->errorAction($errMsg);
225 }
226 else
227 {
228 trigger_error($errMsg);
229 }
230 exit;
231 }
232 }
233
234 /*=====================================================================*
235 || ###################################################################
236 || # $HeadURL$
237 || # $Id$
238 || ###################################################################
239 \*=====================================================================*/
240 ?>