Rewritten a lot double-quotes to single-requotes, removed deprecated exception, some...
[core.git] / inc / classes / main / commands / web / class_WebLoginAreaCommand.php
1 <?php
2 /**
3  * A command for the login area (member/gamer area)
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007 - 2009 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class WebLoginAreaCommand extends BaseCommand implements Commandable {
25         /**
26          * Name of the action
27          */
28         private $actionName = '';
29
30         /**
31          * Protected constructor
32          *
33          * @return      void
34          */
35         protected function __construct () {
36                 // Call parent constructor
37                 parent::__construct(__CLASS__);
38         }
39
40         /**
41          * Creates an instance of this class
42          *
43          * @param       $resolverInstance       An instance of a command resolver class
44          * @return      $commandInstance        An instance a prepared command class
45          */
46         public final static function createWebLoginAreaCommand (CommandResolver $resolverInstance) {
47                 // Get new instance
48                 $commandInstance = new WebLoginAreaCommand();
49
50                 // Set the application instance
51                 $commandInstance->setResolverInstance($resolverInstance);
52
53                 // Load general data like user status and such
54                 $commandInstance->prepareCommand();
55
56                 // Return the prepared instance
57                 return $commandInstance;
58         }
59
60         /**
61          * Prepares some general data which shall be displayed on every page
62          *
63          * @return      void
64          * @todo        Add some stuff here: Some personal data, app/game related data
65          */
66         protected function prepareCommand () {
67         }
68
69         /**
70          * Executes the given command with given request and response objects
71          *
72          * @param       $requestInstance        An instance of a class with an Requestable interface
73          * @param       $responseInstance       An instance of a class with an Responseable interface
74          * @return      void
75          */
76         public function execute (Requestable $requestInstance, Responseable $responseInstance) {
77                 // Get the action instance from registry
78                 $actionInstance = Registry::getRegistry()->getInstance('action');
79
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);
84                 } // END - if
85
86                 // Get the application instance
87                 $appInstance = $this->getResolverInstance()->getApplicationInstance();
88
89                 // Prepare a template instance
90                 $templateInstance = $this->prepareTemplateInstance($appInstance);
91
92                 // Assign base URL
93                 $templateInstance->assignConfigVariable('base_url');
94
95                 // Assign all the application's data with template variables
96                 $templateInstance->assignApplicationData($appInstance);
97
98                 // Load the master template
99                 $masterTemplate = $appInstance->buildMasterTemplateName();
100
101                 // Load header template
102                 $templateInstance->loadCodeTemplate('header');
103
104                 // Compile and assign it with a variable
105                 $templateInstance->compileTemplate();
106                 $templateInstance->assignTemplateWithVariable('header', 'header');
107
108                 // Load footer template
109                 $templateInstance->loadCodeTemplate('footer');
110
111                 // Compile and assign it with a variable
112                 $templateInstance->compileTemplate();
113                 $templateInstance->assignTemplateWithVariable('footer', 'footer');
114
115                 // Load the matching template
116                 $templateInstance->loadCodeTemplate('action_' . $this->actionName);
117
118                 // Assign the template with the master template as a content ... ;)
119                 $templateInstance->compileTemplate();
120                 $templateInstance->assignTemplateWithVariable('action_' . $this->actionName, 'login_content');
121
122                 // Load main template
123                 $templateInstance->loadCodeTemplate('login_main');
124
125                 // Assign the main template with the master template as a content ... ;)
126                 $templateInstance->compileTemplate();
127                 $templateInstance->assignTemplateWithVariable('login_main', 'content');
128
129                 // Load the master template
130                 $templateInstance->loadCodeTemplate($masterTemplate);
131
132                 // Set title
133                 $templateInstance->assignVariable('title', $this->getLanguageInstance()->getMessage($this->actionName . '_title'));
134
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();
139
140                 // Get the content back from the template engine and put it in response class
141                 $templateInstance->transferToResponse($responseInstance);
142         }
143
144         /**
145          * Adds extra filters to the given controller instance. An corresponding action class must now exist!
146          *
147          * @param       $controllerInstance             A controller instance
148          * @param       $requestInstance                An instance of a class with an Requestable interface
149          * @return      void
150          */
151         public function addExtraFilters (Controller $controllerInstance, Requestable $requestInstance) {
152                 // Default is no action
153                 $actionInstance = null;
154
155                 // Get registry
156                 $registryInstance = Registry::getRegistry();
157
158                 // Get our application instance from the registry
159                 $appInstance = $registryInstance->getInstance('application');
160
161                 // Default action is the one from configuration
162                 $this->actionName = $this->convertDashesToUnderscores($appInstance->getAppShortName()) . '_login_' . $this->getConfigInstance()->readConfig('login_default_action');
163
164                 // Get "action" from request
165                 $actReq = $requestInstance->getRequestElement('action');
166
167                 // Do we have a "action" parameter set?
168                 if ((is_string($actReq)) && (!empty($actReq))) {
169                         // Then use it with prefix
170                         $this->actionName = $this->convertDashesToUnderscores($appInstance->getAppShortName()) . '_login_' . $actReq;
171                 } // END - if
172
173                 // Get application instance
174                 $applicationInstance = $this->getResolverInstance()->getApplicationInstance();
175
176                 // Get a resolver
177                 $actionResolver = WebActionResolver::createWebActionResolver($this->actionName, $applicationInstance);
178
179                 // Resolve the action
180                 $actionInstance = $actionResolver->resolveAction();
181
182                 // Add more action-specific filters
183                 $actionInstance->addExtraFilters($controllerInstance, $requestInstance);
184
185                 // Remember this action in registry
186                 Registry::getRegistry()->addInstance('action', $actionInstance);
187         }
188 }
189
190 // [EOF]
191 ?>