Added 'base_url'
[core.git] / inc / classes / main / commands / class_BaseCommand.php
1 <?php
2 /**
3  * A general (base) command
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 BaseCommand extends BaseFrameworkSystem {
25         /**
26          * Protected constructor
27          *
28          * @param       $className      Name of the class
29          * @return      void
30          */
31         protected function __construct ($className) {
32                 // Call parent constructor
33                 parent::__construct($className);
34         }
35
36         /**
37          * Sends a generic HTTP response with header, menu, content and footer
38          *
39          * @param       $requestInstance        An instance of a class with an Requestable interface
40          * @param       $responseInstance       An instance of a class with an Responseable interface
41          * @return      void
42          */
43         protected function sendGenericGetResponse (Requestable $requestInstance, Responseable $responseInstance) {
44                 // This command doesn't handle any POST requests, so only handle get request
45                 assert(!$requestInstance->isPostRequestMethod());
46
47                 // Get the application instance
48                 $applicationInstance = $this->getResolverInstance()->getApplicationInstance();
49
50                 // Prepare a template instance
51                 $templateInstance = $this->prepareTemplateInstance($applicationInstance);
52
53                 // Transfer application data
54                 $templateInstance->assignApplicationData($applicationInstance);
55
56                 // Assign base URL
57                 $templateInstance->assignConfigVariable('base_url');
58
59                 // Load the master template
60                 $masterTemplate = $applicationInstance->buildMasterTemplateName();
61
62                 // Load header template
63                 $templateInstance->loadCodeTemplate('header');
64
65                 // Compile and assign it with a variable
66                 $templateInstance->compileTemplate();
67                 $templateInstance->assignTemplateWithVariable('header', 'header');
68
69                 // Load footer template
70                 $templateInstance->loadCodeTemplate('footer');
71
72                 // Compile and assign it with a variable
73                 $templateInstance->compileTemplate();
74                 $templateInstance->assignTemplateWithVariable('footer', 'footer');
75
76                 // Load the content template
77                 $templateInstance->loadCodeTemplate($this->getResolverInstance()->getCommandName());
78
79                 // Assign the content template with the master template as a content ... ;)
80                 $templateInstance->assignTemplateWithVariable($applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName(), 'main_content');
81
82                 // Load the master template
83                 $templateInstance->loadCodeTemplate($masterTemplate);
84
85                 // Set title
86                 $templateInstance->assignVariable('title', $this->getLanguageInstance()->getMessage('page_' . $applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_title'));
87
88                 // Construct the menu in every command. We could do this in BaseCommand class. But this means
89                 // *every* command has a navigation system and that is want we don't want.
90                 $menuInstance = ObjectFactory::createObjectByConfiguredName($applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_menu_class', array($applicationInstance));
91
92                 // Render the menu
93                 $menuInstance->renderMenu();
94
95                 // Transfer it to the template engine instance
96                 $menuInstance->transferContentToTemplateEngine($templateInstance);
97
98                 /*
99                  * ... and all variables. This should be merged together in a pattern
100                  * to make things easier. A cache mechanism should be added between
101                  * these two calls to cache compiled templates.
102                  */
103                 $templateInstance->compileVariables();
104
105                 // Get the content back from the template engine and put it in response class
106                 $templateInstance->transferToResponse($responseInstance);
107         }
108 }
109
110 // [EOF]
111 ?>