Smutty
MVC Framework
View Code: Smutty_Controller
Browse: All · Classes · Plugins
1
<?php
2
3
/**
4
* this is the base class for all smutty controllers. it defines
5
* a lot of required functionality and helper methods for classes
6
* that inherit from it.
7
*
8
*/
9
10
class Smutty_Controller extends Smutty_Object {
11
12
/** stores data to use for the view */
13
private $_viewData = array();
14
15
/** current controller */
16
private static $currentController = null;
17
18
/**
19
* a static method to return the current controller
20
*
21
* @return Smutty_Controller the current controller
22
*
23
*/
24
25
public static function &_getCurrentController() {
26
return Smutty_Controller::_setCurrentController( $I_DONT_EXIST );
27
}
28
29
/**
30
* sets and/or returns a controller
31
*
32
* @param Smutty_Controller $controller optional controller to set
33
* @return Smutty_Controller the current controller
34
*
35
*/
36
37
public static function &_setCurrentController( &$ctlr ) {
38
if ( self::$currentController == null )
39
self::$currentController = $ctlr;
40
return self::$currentController;
41
}
42
43
/**
44
* shows the view file using the specified template class
45
*
46
* @param String $viewFile path to view file
47
* @param Smutty_Template $template instance of templace class to use
48
* @return nothing
49
*
50
*/
51
53
foreach ( $this->_viewData as $key => $value )
54
$template->assign( $key, $value );
55
$template->display( $viewFile );
56
}
57
58
/**
59
* show the view file using the smutty template class
60
*
61
* @param String $viewFile the file to view
62
* @return nothing
63
*
64
*/
65
67
$this->_view( $viewFile, new Smutty_Template_Smutty() );
68
}
69
70
/**
71
* show the specified file with the standard user
72
* template class. if the view file is not specifed
73
* then it is assumed to be located as "views/controller/action.tpl"
74
*
75
* @param String $viewFile the file to view
76
* @return nothing
77
*
78
*/
79
81
if ( !$viewFile ) {
82
$router = Smutty_Router::getInstance();
83
$viewFile = $router->getActionName() . '.tpl';
84
}
85
$this->_view( $viewFile, new Smutty_Template() );
86
}
87
88
/**
89
* sets a variable for use by the template
90
*
91
* @param String $name name of the variable to set
92
* @param mixed $value the value to set
93
* @return nothing
94
*
95
*/
96
98
$this->_viewData[ $name ] = $value;
99
}
100
101
/**
102
* returns the value of a template var
103
*
104
* @param String $name var name
105
* @return String value
106
*
107
*/
108
110
return isset($this->_viewData[$name])
111
? $this->_viewData[$name] : '';
112
}
113
114
/**
115
* this function pushes a value onto the named array,
116
* if the array doesn't exist then it's created.
117
*
118
* @param String $name name of array
119
* @param String $value value to add
120
* @return nothing
121
*
122
*/
123
125
126
$array = $this->get( $name );
127
if ( !is_array($array) )
128
$array = array();
129
130
array_push( $array, $value );
131
132
$this->set( $name, $array );
133
134
}
135
136
/**
137
* redirects the user to the application url specified. the
138
* url must be absolute from the base of the app.
139
*
140
* @param String $url the url to redirect to
141
* @return nothing
142
*
143
*/
144
146
147
$base = Smutty_Utils::getBaseUrl();
148
149
header( 'Location: ' . $base . $url );
150
Smutty_Main::completeRequest();
151
exit();
152
153
}
154
155
/**
156
* redirects the user to the given url. the parameter array
157
* should be a hash of name value pairs. the names being anything
158
* set up for the route.
159
*
160
* @param array $params array of parameters
161
* @param array $args query string args
162
* @return nothing
163
*
164
*/
165
167
if ( !$params ) {
168
$router = Smutty_Router::getInstance();
169
$params = array(
170
'controller' => $router->getDefaultControllerName(),
171
'action' => $router->getDefaultActionName()
172
);
173
}
174
$url = Smutty_Utils::getUrl( $params, $args );
175
$base = Smutty_Utils::getBaseUrl();
176
$this->redirectUrl( substr($url,strlen($base)) );
177
}
178
179
/**
180
* this function takes the same arguments as the normal
181
* redirect function, but prints out javascript code
182
* to do the redirect itself. this can be useful when
183
* doing ajax callbacks with forms.
184
*
185
* it would be nice if i could think of a way to merge these
186
* two methods and have it "know" which one to do, but that
187
* seems a bit impossible i think?
188
*
189
* @param array $params params for the redirect
190
* @return nothing
191
*
192
*/
193
195
$url = Smutty_Utils::getUrl( $params );
196
echo '<script type="text/javascript">self.location.href=\'' . $url . '\';</script>';
197
exit();
198
}
199
200
/**
201
* this function is called before an action. it has no
202
* implementation here but can be over-rided by sub-classes
203
* if they want to use it.
204
*
205
* @param Smutty_Data $data the data object
206
* @param Smutty_Session $session the session object
207
* @return nothing
208
*
209
*/
210
212
213
/**
214
* executes an action on the current controller
215
*
216
* @param String $name name of action
217
* @return nothing
218
*
219
*/
220
222
223
$router =& Smutty_Router::getInstance();
224
$session =& Smutty_Session::getInstance();
225
$data =& Smutty_Data::getInstance();
226
227
// make sure the method exists
228
$method = $name . 'Action';
229
if ( !method_exists($this,$method) )
230
Smutty_Error::fatal( ERR_ACTION_INVALID, 'ClassSmutty_Controller', 404 );
231
232
// mark this action as current
233
$router->setActionName( $name );
234
235
// call the action and it's associated methods
236
$this->actionBefore( $data, $session );
237
$this->$method( $data, $session );
238
239
}
240
241
/**
242
* adds errors to the current controller
243
*
244
* @param array $errors array of errors
245
* @return nothing
246
*
247
*/
248
250
251
// set errors on current controller
252
$curr =& Smutty_Controller::_getCurrentController();
253
if ( !isset($curr->_viewData['errors']) || !is_array($curr->_viewData['errors']) )
254
$curr->_viewData['errors'] = array();
255
256
$curr->_viewData['errors'] = array_merge(
257
$curr->_viewData['errors'],
258
$errors
259
);
260
261
}
262
263
/**
264
* adds an error to this controller
265
*
266
* @param String $message error message
267
* @return nothing
268
*
269
*/
270
272
273
if ( !isset($this->_viewData['errors']) )
274
$this->_viewData['errors'] = array();
275
276
array_push( $this->_viewData['errors'], $message );
277
278
}
279
280
}
281
282
?>
The code shown here is the code that is currently running this site. If you want to view the latest SVN version of the code then go to the Subversion repository.