Merge branch 'contrib'
[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@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2013 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.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 static final 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                 $applicationInstance = $this->getResolverInstance()->getApplicationInstance();
88
89                 // Prepare a template instance
90                 $templateInstance = $this->prepareTemplateInstance($applicationInstance);
91
92                 // Assign base URL
93                 $templateInstance->assignConfigVariable('base_url');
94
95                 // Assign all the application's data with template variables
96                 $templateInstance->assignApplicationData($applicationInstance);
97
98                 // Load the master template
99                 $masterTemplate = $applicationInstance->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                 // Construct the menu in every command. We could do this in BaseCommand class. But this means
136                 // *every* command has a navigation system and that is want we don't want.
137                 $menuInstance = ObjectFactory::createObjectByConfiguredName('login_area_menu_class', array($applicationInstance));
138
139                 // Render the menu
140                 $menuInstance->renderMenu();
141
142                 // Transfer it to the template engine instance
143                 $menuInstance->transferContentToTemplateEngine($templateInstance);
144
145                 // ... and all variables. This should be merged together in a pattern
146                 // to make things easier. A cache mechanism should be added between
147                 // these two calls to cache compiled templates.
148                 $templateInstance->compileVariables();
149
150                 // Get the content back from the template engine and put it in response class
151                 $templateInstance->transferToResponse($responseInstance);
152         }
153
154         /**
155          * Adds extra filters to the given controller instance. An corresponding action class must now exist!
156          *
157          * @param       $controllerInstance             A controller instance
158          * @param       $requestInstance                An instance of a class with an Requestable interface
159          * @return      void
160          */
161         public function addExtraFilters (Controller $controllerInstance, Requestable $requestInstance) {
162                 // Default is no action
163                 $actionInstance = NULL;
164
165                 // Get registry
166                 $registryInstance = Registry::getRegistry();
167
168                 // Get our application instance from the registry
169                 $applicationInstance = $registryInstance->getInstance('application');
170
171                 // Default action is the one from configuration
172                 $this->actionName = $this->convertDashesToUnderscores($applicationInstance->getAppShortName()) . '_login_' . $this->getConfigInstance()->getConfigEntry('login_default_action');
173
174                 // Get "action" from request
175                 $actReq = $requestInstance->getRequestElement('action');
176
177                 // Do we have a "action" parameter set?
178                 if ((is_string($actReq)) && (!empty($actReq))) {
179                         // Then use it with prefix
180                         $this->actionName = $this->convertDashesToUnderscores($applicationInstance->getAppShortName()) . '_login_' . $actReq;
181                 } // END - if
182
183                 // Get application instance
184                 $applicationInstance = $this->getResolverInstance()->getApplicationInstance();
185
186                 // Get a resolver
187                 $actionResolver = WebActionResolver::createWebActionResolver($this->actionName, $applicationInstance);
188
189                 // Resolve the action
190                 $actionInstance = $actionResolver->resolveAction();
191
192                 // Add more action-specific filters
193                 $actionInstance->addExtraFilters($controllerInstance, $requestInstance);
194
195                 // Remember this action in registry
196                 Registry::getRegistry()->addInstance('action', $actionInstance);
197         }
198 }
199
200 // [EOF]
201 ?>