]> git.mxchange.org Git - city.git/blob - application/city/classes/commands/html/class_CityHtmlLoginAreaCommand.php
Continued:
[city.git] / application / city / classes / commands / html / class_CityHtmlLoginAreaCommand.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\City\Command;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Controller\Controller;
8 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
9 use Org\Mxchange\CoreFramework\Helper\Application\ApplicationHelper;
10 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
11 use Org\Mxchange\CoreFramework\Request\Requestable;
12 use Org\Mxchange\CoreFramework\Resolver\Command\CommandResolver;
13 use Org\Mxchange\CoreFramework\Response\Responseable;
14
15 /**
16  * A command for the login area (member/gamer area)
17  *
18  * @author              Roland Haeder <webmaster@shipsimu.org>
19  * @version             0.0.0
20  * @copyright   Copyright (c) 2015, 2016 City Developer Team
21  * @license             GNU GPL 3.0 or any newer version
22  * @link                http://www.shipsimu.org
23  *
24  * This program is free software: you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation, either version 3 of the License, or
27  * (at your option) any later version.
28  *
29  * This program is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  * GNU General Public License for more details.
33  *
34  * You should have received a copy of the GNU General Public License
35  * along with this program. If not, see <http://www.gnu.org/licenses/>.
36  */
37 class CityHtmlLoginAreaCommand extends BaseCommand implements Commandable {
38         /**
39          * Name of the action
40          */
41         private $actionName = '';
42
43         /**
44          * Protected constructor
45          *
46          * @return      void
47          */
48         protected function __construct () {
49                 // Call parent constructor
50                 parent::__construct(__CLASS__);
51         }
52
53         /**
54          * Creates an instance of this class
55          *
56          * @param       $resolverInstance       An instance of a command resolver class
57          * @return      $commandInstance        An instance a prepared command class
58          */
59         public static final function createCityHtmlLoginAreaCommand (CommandResolver $resolverInstance) {
60                 // Get new instance
61                 $commandInstance = new CityHtmlLoginAreaCommand();
62
63                 // Set the application instance
64                 $commandInstance->setResolverInstance($resolverInstance);
65
66                 // Load general data like user status and such
67                 $commandInstance->prepareCommand();
68
69                 // Return the prepared instance
70                 return $commandInstance;
71         }
72
73         /**
74          * Prepares some general data which shall be displayed on every page
75          *
76          * @return      void
77          * @todo        Add some stuff here: Some personal data, app/game related data
78          */
79         protected function prepareCommand () {
80         }
81
82         /**
83          * Executes the given command with given request and response objects
84          *
85          * @param       $requestInstance        An instance of a class with an Requestable interface
86          * @param       $responseInstance       An instance of a class with an Responseable interface
87          * @return      void
88          */
89         public function execute (Requestable $requestInstance, Responseable $responseInstance) {
90                 // Get the action instance from registry
91                 $actionInstance = GenericRegistry::getRegistry()->getInstance('action');
92
93                 // Do we have an action here?
94                 if ($actionInstance instanceof PerformableAction) {
95                         // Execute the action (shall not output anything, see below why)
96                         $actionInstance->execute($requestInstance, $responseInstance);
97                 } // END - if
98
99                 // Get the application instance
100                 $applicationInstance = ApplicationHelper::getSelfInstance();
101
102                 // Prepare a template instance
103                 $templateInstance = $this->prepareTemplateInstance($applicationInstance);
104
105                 // Assign base URL
106                 $templateInstance->assignConfigVariable('base_url');
107
108                 // Assign all the application's data with template variables
109                 $templateInstance->assignApplicationData();
110
111                 // Load the master template
112                 $masterTemplate = $applicationInstance->buildMasterTemplateName();
113
114                 // Load header template
115                 $templateInstance->loadCodeTemplate('header');
116
117                 // Compile and assign it with a variable
118                 $templateInstance->compileTemplate();
119                 $templateInstance->assignTemplateWithVariable('header', 'header');
120
121                 // Load footer template
122                 $templateInstance->loadCodeTemplate('footer');
123
124                 // Compile and assign it with a variable
125                 $templateInstance->compileTemplate();
126                 $templateInstance->assignTemplateWithVariable('footer', 'footer');
127
128                 // Load the matching template
129                 $templateInstance->loadCodeTemplate('action_' . $this->actionName);
130
131                 // Assign the template with the master template as a content ... ;)
132                 $templateInstance->compileTemplate();
133                 $templateInstance->assignTemplateWithVariable('action_' . $this->actionName, 'login_content');
134
135                 // Load main template
136                 $templateInstance->loadCodeTemplate('login_main');
137
138                 // Assign the main template with the master template as a content ... ;)
139                 $templateInstance->compileTemplate();
140                 $templateInstance->assignTemplateWithVariable('login_main', 'main_content');
141
142                 // Load the master template
143                 $templateInstance->loadCodeTemplate($masterTemplate);
144
145                 // Set title
146                 $templateInstance->assignVariable('title', FrameworkBootstrap::getLanguageInstance()->getMessage($this->actionName . '_title'));
147
148                 // Construct the menu in every command. We could do this in BaseCommand class. But this means
149                 // *every* command has a navigation system and that is want we don't want.
150                 $menuInstance = ObjectFactory::createObjectByConfiguredName('login_area_menu_class', array($applicationInstance));
151
152                 // Render the menu
153                 $menuInstance->renderMenu();
154
155                 // Transfer it to the template engine instance
156                 $menuInstance->transferContentToTemplateEngine($templateInstance);
157
158                 /*
159                  * ... and all variables. This should be merged together in a pattern
160                  * to make things easier. A cache mechanism should be added between
161                  * these two calls to cache compiled templates.
162                  */
163                 $templateInstance->compileVariables();
164
165                 // Get the content back from the template engine and put it in response class
166                 $templateInstance->transferToResponse($responseInstance);
167         }
168
169         /**
170          * Adds extra filters to the given controller instance. An corresponding action class must now exist!
171          *
172          * @param       $controllerInstance             A controller instance
173          * @param       $requestInstance                An instance of a class with an Requestable interface
174          * @return      void
175          */
176         public function addExtraFilters (Controller $controllerInstance, Requestable $requestInstance) {
177                 // Default is no action
178                 $actionInstance = NULL;
179
180                 // Get registry
181                 $registryInstance = GenericRegistry::getRegistry();
182
183                 // Get our application instance from the registry
184                 $applicationInstance = $registryInstance->getInstance('application');
185
186                 // Default action is the one from configuration
187                 $this->actionName = $this->convertDashesToUnderscores($applicationInstance->getAppShortName()) . '_login_' . FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('login_default_action');
188
189                 // Get "action" from request
190                 $actReq = $requestInstance->getRequestElement('action');
191
192                 // Do we have a "action" parameter set?
193                 if ((is_string($actReq)) && (!empty($actReq))) {
194                         // Then use it with prefix
195                         $this->actionName = $this->convertDashesToUnderscores($applicationInstance->getAppShortName()) . '_login_' . $actReq;
196                 } // END - if
197
198                 // Get a resolver
199                 $actionResolver = HtmlActionResolver::createHtmlActionResolver($this->actionName);
200
201                 // Resolve the action
202                 $actionInstance = $actionResolver->resolveAction();
203
204                 // Add more action-specific filters
205                 $actionInstance->addExtraFilters($controllerInstance, $requestInstance);
206
207                 // Remember this action in registry
208                 GenericRegistry::getRegistry()->addInstance('action', $actionInstance);
209         }
210
211 }