3 * A command for the login area (member/gamer area)
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, this is free software
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class WebLoginAreaCommand extends BaseCommand implements Commandable {
28 private $actionName = "";
31 * Protected constructor
35 protected function __construct () {
36 // Call parent constructor
37 parent::__construct(__CLASS__);
41 * Creates an instance of this class
43 * @param $resolverInstance An instance of a command resolver class
44 * @return $commandInstance An instance a prepared command class
46 public final static function createWebLoginAreaCommand (CommandResolver $resolverInstance) {
48 $commandInstance = new WebLoginAreaCommand();
50 // Set the application instance
51 $commandInstance->setResolverInstance($resolverInstance);
53 // Load general data like user status and such
54 $commandInstance->prepareCommand();
56 // Return the prepared instance
57 return $commandInstance;
61 * Prepares some general data which shall be displayed on every page
64 * @todo Add some stuff here: Some personal data, app/game related data
66 protected function prepareCommand () {
70 * Executes the given command with given request and response objects
72 * @param $requestInstance An instance of a class with an Requestable interface
73 * @param $responseInstance An instance of a class with an Responseable interface
76 public function execute (Requestable $requestInstance, Responseable $responseInstance) {
77 // Get the action instance from registry
78 $actionInstance = Registry::getRegistry()->getInstance('action');
80 // Do we have an action here?
81 if ($actionInstance instanceof PerformableAction) {
82 // Execute the action (shall not output anything, see below why)
83 $actionInstance->execute($requestInstance, $responseInstance);
86 // Get the application instance
87 $appInstance = $this->getResolverInstance()->getApplicationInstance();
89 // Prepare a template instance
90 $templateInstance = $this->prepareTemplateInstance($appInstance);
93 $templateInstance->assignConfigVariable('base_url');
95 // Assign all the application's data with template variables
96 $templateInstance->assignApplicationData($appInstance);
98 // Load the master template
99 $masterTemplate = $appInstance->buildMasterTemplateName();
101 // Load header template
102 $templateInstance->loadCodeTemplate('header');
104 // Compile and assign it with a variable
105 $templateInstance->compileTemplate();
106 $templateInstance->assignTemplateWithVariable('header', 'header');
108 // Load footer template
109 $templateInstance->loadCodeTemplate('footer');
111 // Compile and assign it with a variable
112 $templateInstance->compileTemplate();
113 $templateInstance->assignTemplateWithVariable('footer', 'footer');
115 // Load the matching template
116 $templateInstance->loadCodeTemplate('action_' . $this->actionName);
118 // Assign the template with the master template as a content ... ;)
119 $templateInstance->compileTemplate();
120 $templateInstance->assignTemplateWithVariable('action_' . $this->actionName, 'login_content');
122 // Load main template
123 $templateInstance->loadCodeTemplate('login_main');
125 // Assign the main template with the master template as a content ... ;)
126 $templateInstance->compileTemplate();
127 $templateInstance->assignTemplateWithVariable('login_main', 'content');
129 // Load the master template
130 $templateInstance->loadCodeTemplate($masterTemplate);
133 $templateInstance->assignVariable('title', $this->getLanguageInstance()->getMessage($this->actionName . '_title'));
135 // ... and all variables. This should be merged together in a pattern
136 // to make things easier. A cache mechanism should be added between
137 // these two calls to cache compiled templates.
138 $templateInstance->compileVariables();
140 // Get the content back from the template engine and put it in response class
141 $templateInstance->transferToResponse($responseInstance);
145 * Adds extra filters to the given controller instance. An corresponding action class must now exist!
147 * @param $controllerInstance A controller instance
148 * @param $requestInstance An instance of a class with an Requestable interface
151 public function addExtraFilters (Controller $controllerInstance, Requestable $requestInstance) {
152 // Default is no action
153 $actionInstance = null;
156 $registryInstance = Registry::getRegistry();
158 // Get our application instance from the registry
159 $appInstance = $registryInstance->getInstance('application');
161 // Default action is the one from configuration
162 $this->actionName = sprintf("%s_login_%s",
163 str_replace("-", "_", $appInstance->getAppShortName()),
164 $this->getConfigInstance()->readConfig('login_default_action')
167 // Get "action" from request
168 $actReq = $requestInstance->getRequestElement('action');
170 // Do we have a "action" parameter set?
171 if ((is_string($actReq)) && (!empty($actReq))) {
172 // Then use it with prefix
173 $this->actionName = sprintf("%s_login_%s",
174 str_replace("-", "_", $appInstance->getAppShortName()),
179 // Get application instance
180 $applicationInstance = $this->getResolverInstance()->getApplicationInstance();
183 $actionResolver = WebActionResolver::createWebActionResolver($this->actionName, $applicationInstance);
185 // Resolve the action
186 $actionInstance = $actionResolver->resolveAction();
188 // Add more action-specific filters
189 $actionInstance->addExtraFilters($controllerInstance, $requestInstance);
191 // Remember this action in registry
192 Registry::getRegistry()->addInstance('action', $actionInstance);