ToDo tags now all lower-case
[shipsimu.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, 2008 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                 // Set special description
40                 $this->setObjectDescription("Command for the &quot;login area&quot; page");
41
42                 // Create unique ID number
43                 $this->generateUniqueId();
44
45                 // Clean up a little
46                 $this->removeSystemArray();
47         }
48
49         /**
50          * Creates an instance of this class
51          *
52          * @param       $resolverInstance               An instance of a command resolver class
53          * @return      $commandInstance                An instance a prepared command class
54          */
55         public final static function createWebLoginAreaCommand (CommandResolver $resolverInstance) {
56                 // Get new instance
57                 $commandInstance = new WebLoginAreaCommand();
58
59                 // Set the application instance
60                 $commandInstance->setResolverInstance($resolverInstance);
61
62                 // Load general data like user status and such
63                 $commandInstance->prepareCommand();
64
65                 // Return the prepared instance
66                 return $commandInstance;
67         }
68
69         /**
70          * Prepares some general data which shall be displayed on every page
71          *
72          * @return      void
73          */
74         protected function prepareCommand () {
75                 /* @todo Add some stuff here: Some personal data, app/game related data */
76         }
77
78         /**
79          * Executes the given command with given request and response objects
80          *
81          * @param       $requestInstance                An instance of a class with an Requestable interface
82          * @param       $responseInstance               An instance of a class with an Responseable interface
83          * @return      void
84          */
85         public function execute (Requestable $requestInstance, Responseable $responseInstance) {
86                 // Get the action instance from registry
87                 $actionInstance = Registry::getRegistry()->getInstance('action');
88
89                 // Do we have an action here?
90                 if ($actionInstance instanceof PerformableAction) {
91                         // Execute the action (shall not output anything, see below why)
92                         $actionInstance->execute($requestInstance, $responseInstance);
93                 } // END - if
94
95                 // Get the application instance
96                 $appInstance = $this->getResolverInstance()->getApplicationInstance();
97
98                 // Prepare a template instance
99                 $templateInstance = $this->prepareTemplateEngine($appInstance);
100
101                 // Assign base URL
102                 $templateInstance->assignConfigVariable('base_url');
103
104                 // Assign all the application's data with template variables
105                 $templateInstance->assignApplicationData($appInstance);
106
107                 // Load the master template
108                 $masterTemplate = $appInstance->getMasterTemplate();
109
110                 // Load header template
111                 $templateInstance->loadCodeTemplate("header");
112
113                 // Compile and assign it with a variable
114                 $templateInstance->compileTemplate();
115                 $templateInstance->assignTemplateWithVariable("header", "header");
116
117                 // Load footer template
118                 $templateInstance->loadCodeTemplate("footer");
119
120                 // Compile and assign it with a variable
121                 $templateInstance->compileTemplate();
122                 $templateInstance->assignTemplateWithVariable("footer", "footer");
123
124                 // Load the matching template
125                 $templateInstance->loadCodeTemplate($this->actionName);
126
127                 // Assign the template with the master template as a content ... ;)
128                 $templateInstance->compileTemplate();
129                 $templateInstance->assignTemplateWithVariable($this->actionName, "login_content");
130
131                 // Load main template
132                 $templateInstance->loadCodeTemplate("login_main");
133
134                 // Assign the main template with the master template as a content ... ;)
135                 $templateInstance->compileTemplate();
136                 $templateInstance->assignTemplateWithVariable("login_main", "content");
137
138                 // Load the master template
139                 $templateInstance->loadCodeTemplate($masterTemplate);
140
141                 // Set title
142                 $templateInstance->assignVariable('title', $this->getLanguageInstance()->getMessage($this->actionName."_title"));
143
144                 // ... and all variables. This should be merged together in a pattern
145                 // to make things easier. A cache mechanism should be added between
146                 // these two calls to cache compiled templates.
147                 //$templateInstance->debugInstance();
148                 $templateInstance->compileVariables();
149
150                 // Get the content back from the template engine and put it in the response class
151                 $templateInstance->transferToResponse($responseInstance);
152         }
153
154         /**
155          * Adds extra filters to the given controller instance
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                 // Default action is the one from configuration
166                 $this->actionName = sprintf("login_%s", $this->getConfigInstance()->readConfig('login_default_action'));
167
168                 // Get "action" from request
169                 $actReq = $requestInstance->getRequestElement('action');
170
171                 // Do we have a "action" parameter set?
172                 if ((is_string($actReq)) && (!empty($actReq))) {
173                         // Then use it with prefix
174                         $this->actionName = sprintf("login_%s", $actReq);
175                 } // END - if
176
177                 // Get application instance
178                 $applicationInstance = $this->getResolverInstance()->getApplicationInstance();
179
180                 // Try to get an action resolver for the given action
181                 try {
182                         // Get a resolver
183                         $actionResolver = WebActionResolver::createWebActionResolver($this->actionName, $applicationInstance);
184
185                         // Resolve the action
186                         $actionInstance = $actionResolver->resolveAction();
187
188                         // Add more action-specific filters
189                         $actionInstance->addExtraFilters($controllerInstance, $requestInstance);
190
191                         // Remember this action in registry
192                         Registry::getRegistry()->addInstance('action', $actionInstance);
193                 } catch (InvalidActionException $e) {
194                         // Silently ignored because no special action was found
195                 }
196         }
197 }
198
199 // [EOF]
200 ?>