Fixes #149
[akelos.git] / lib / AkActionController / AkWebRequest.php
blobf80e89181daa5e6d28f5945bc5813f5287553426
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4 // +----------------------------------------------------------------------+
5 // | Akelos Framework - http://www.akelos.org |
6 // +----------------------------------------------------------------------+
7 // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez |
8 // | Released under the GNU Lesser General Public License, see LICENSE.txt|
9 // +----------------------------------------------------------------------+
11 /**
12 * @package ActionController
13 * @subpackage Base
14 * @author Bermi Ferrer <bermi a.t akelos c.om>
15 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
16 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
17 * @deprecated Please use AkDispatcher on your public/index.php instead
20 defined('AK_WEB_REQUEST_CONNECT_TO_DATABASE_ON_INSTANTIATE') ? null :
21 define('AK_WEB_REQUEST_CONNECT_TO_DATABASE_ON_INSTANTIATE', true);
23 defined('AK_WEB_REQUEST_START_SESSION_ON_INSTANTIATE') ? null :
24 define('AK_WEB_REQUEST_START_SESSION_ON_INSTANTIATE', true);
26 defined('AK_WEB_REQUEST_ENABLE_INTERNATIONALIZATION_SUPPORT_ON_INSTANTIATE') ? null :
27 define('AK_WEB_REQUEST_ENABLE_INTERNATIONALIZATION_SUPPORT_ON_INSTANTIATE', true);
29 /**
30 * Http Web Browser requests are handled by this class
32 class AkWebRequest extends AkActionController
34 var $__ParentController;
35 var $AppController;
37 function init(&$ParentController)
39 $this->__ParentController =& $ParentController;
41 $this->__ParentController->_ssl_requirement ? $this->__ParentController->beforeFilter('_ensureProperProtocol') : false;
43 if($this->__ParentController->_autoIncludePaginator){
44 require_once(AK_LIB_DIR.DS.'AkActionController'.DS.'AkPaginator.php');
47 if(AK_WEB_REQUEST_CONNECT_TO_DATABASE_ON_INSTANTIATE){
48 $this->__ParentController->__connectToDatabase();
51 if(AK_WEB_REQUEST_START_SESSION_ON_INSTANTIATE){
52 $this->__ParentController->__startSession();
55 require_once(AK_LIB_DIR.DS.'AkRequest.php');
56 $this->__ParentController->Request =& AkRequest();
58 if(AK_WEB_REQUEST_ENABLE_INTERNATIONALIZATION_SUPPORT_ON_INSTANTIATE && AK_AVAILABLE_LOCALES != 'en'){
59 $this->__ParentController->__enableInternationalizationSupport();
62 $this->__ParentController->__mapRoutes();
67 function handle()
69 $this->__ParentController->params = $this->__ParentController->Request->getParams();
71 $this->_file_name = AkInflector::underscore($this->__ParentController->params['controller']).'_controller.php';
72 $this->_class_name = AkInflector::camelize($this->__ParentController->params['controller']).'Controller';
74 $this->_includeController();
76 Ak::t('Akelos'); // We need to get locales ready
78 $class_name = $this->_class_name;
79 $this->AppController =& new $class_name(array('controller'=>true));
81 if(!empty($this->AppController)){
82 $this->AppController->beforeFilter('instantiateHelpers');
85 // Mixing bootstrap controller attributes with this controller attributes
86 foreach (array_keys(get_class_vars('AkActionController')) as $varname){
87 if(empty($this->AppController->$varname)){
88 $this->AppController->$varname =& $this->__ParentController->$varname;
92 empty($this->__ParentController->params) ?
93 ($this->__ParentController->params = $this->__ParentController->Request->getParams()) : null;
95 $action_name = $this->_getActionName();
97 $this->_before($this->AppController);
99 $this->AppController->performActionWithFilters($action_name);
101 $this->_after($this->AppController);
103 $this->AppController->Response->outputResults();
108 function _before(&$Controller)
110 empty($Controller->model) ? ($Controller->model = $Controller->params['controller']) : null;
111 empty($Controller->models) ? ($Controller->models = array()) : null;
112 empty($Controller->_assigns) ? ($Controller->_assigns = array()) : null;
113 empty($Controller->_default_render_status_code) ? ($Controller->_default_render_status_code = 200) : null;
114 $Controller->_enableLayoutOnRender =
115 !isset($Controller->_enableLayoutOnRender) ? true : $Controller->_enableLayoutOnRender;
117 empty($Controller->cookies) && isset($_COOKIE) ? ($Controller->cookies =& $_COOKIE) : null;
119 if(empty($Controller->Response)){
120 require_once(AK_LIB_DIR.DS.'AkResponse.php');
121 $Controller->Response =& AkResponse();
124 if(empty($Controller->Template)){
125 require_once(AK_LIB_DIR.DS.'AkActionView.php');
126 require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'AkPhpTemplateHandler.php');
127 $Controller->Template =& new AkActionView(AK_APP_DIR.DS.'views'.DS.$Controller->Request->getController(),
128 $Controller->Request->getParameters(),$Controller->Request->getController());
130 $Controller->Template->_controllerInstance =& $Controller;
131 $Controller->Template->_registerTemplateHandler('tpl','AkPhpTemplateHandler');
134 $Controller->passed_args = !isset($Controller->Request->pass)? array() : $Controller->Request->pass;
136 Ak::loadPlugins();
138 $Controller->instantiateIncludedModelClasses();
140 if(isset($Controller->api)){
141 require_once(AK_LIB_DIR.DS.'AkActionWebService.php');
142 $Controller->aroundFilter(new AkActionWebService($Controller));
146 function _after(&$Controller)
148 $Controller->_handleFlashAttribute();
149 if (!$Controller->_hasPerformed()){
150 $Controller->_enableLayoutOnRender ? $Controller->renderWithLayout() : $Controller->renderWithoutLayout();
152 if(!AK_DESKTOP && defined('AK_ENABLE_STRICT_XHTML_VALIDATION') && AK_ENABLE_STRICT_XHTML_VALIDATION || !empty($Controller->validate_output)){
153 $Controller->_validateGeneratedXhtml();
159 function _includeController()
161 $controller_path = AK_CONTROLLERS_DIR.DS.$this->_file_name;
162 if(!file_exists($controller_path)){
163 $this->_raiseError(
164 Ak::t('Could not find the file /app/controllers/<i>%controller_file_name</i> for '.
165 'the controller %controller_class_name',
166 array('%controller_file_name'=>$this->_file_name,
167 '%controller_class_name'=>$this->_class_name)));
169 require_once(AK_APP_DIR.DS.'application_controller.php');
170 require_once($controller_path);
171 if(!class_exists($this->_class_name)){
172 $this->_raiseError(Ak::t('Controller <i>%controller_name</i> does not exist',
173 array('%controller_name' => $this->_class_name)));
177 function _getActionName()
179 $this->AppController->_action_name =
180 empty($this->AppController->_action_name) ?
181 (AkInflector::underscore($this->AppController->params['action'])) :
182 $this->AppController->_action_name;
184 if ($this->AppController->_action_name[0] == '_' ||
185 !method_exists($this->AppController, $this->AppController->_action_name)){
186 $this->_raiseError(Ak::t('Action <i>%action</i> does not exist for controller <i>%controller_name</i>',
187 array('%controller_name'=>$this->_class_name,'%action'=>$this->AppController->_action_name)));
189 return $this->AppController->_action_name;
192 function _raiseError($error)
194 if(AK_ENVIRONMENT == 'production'){
195 header('Status: HTTP/1.1 404 Not Found');
196 header('Location: '.AK_URL.'404.html');
197 exit;
198 }else{
199 trigger_error($error, E_USER_ERROR);