It is now 'main_content'.
[core.git] / inc / classes / main / commands / html / class_HtmlLoginAreaCommand.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 - 2015 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 HtmlLoginAreaCommand 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 createHtmlLoginAreaCommand (CommandResolver $resolverInstance) {
47                 // Get new instance
48                 $commandInstance = new HtmlLoginAreaCommand();
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', '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                 /*
146                  * ... and all variables. This should be merged together in a pattern
147                  * to make things easier. A cache mechanism should be added between
148                  * these two calls to cache compiled templates.
149                  */
150                 $templateInstance->compileVariables();
151
152                 // Get the content back from the template engine and put it in response class
153                 $templateInstance->transferToResponse($responseInstance);
154         }
155
156         /**
157          * Adds extra filters to the given controller instance. An corresponding action class must now exist!
158          *
159          * @param       $controllerInstance             A controller instance
160          * @param       $requestInstance                An instance of a class with an Requestable interface
161          * @return      void
162          */
163         public function addExtraFilters (Controller $controllerInstance, Requestable $requestInstance) {
164                 // Default is no action
165                 $actionInstance = NULL;
166
167                 // Get registry
168                 $registryInstance = Registry::getRegistry();
169
170                 // Get our application instance from the registry
171                 $applicationInstance = $registryInstance->getInstance('application');
172
173                 // Default action is the one from configuration
174                 $this->actionName = $this->convertDashesToUnderscores($applicationInstance->getAppShortName()) . '_login_' . $this->getConfigInstance()->getConfigEntry('login_default_action');
175
176                 // Get "action" from request
177                 $actReq = $requestInstance->getRequestElement('action');
178
179                 // Do we have a "action" parameter set?
180                 if ((is_string($actReq)) && (!empty($actReq))) {
181                         // Then use it with prefix
182                         $this->actionName = $this->convertDashesToUnderscores($applicationInstance->getAppShortName()) . '_login_' . $actReq;
183                 } // END - if
184
185                 // Get application instance
186                 $applicationInstance = $this->getResolverInstance()->getApplicationInstance();
187
188                 // Get a resolver
189                 $actionResolver = HtmlActionResolver::createHtmlActionResolver($this->actionName, $applicationInstance);
190
191                 // Resolve the action
192                 $actionInstance = $actionResolver->resolveAction();
193
194                 // Add more action-specific filters
195                 $actionInstance->addExtraFilters($controllerInstance, $requestInstance);
196
197                 // Remember this action in registry
198                 Registry::getRegistry()->addInstance('action', $actionInstance);
199         }
200 }
201
202 // [EOF]
203 ?>