7c09754c0b33d0f7cd7cf4c43b46a25ce84d0f3e
[core.git] / inc / main / classes / commands / class_BaseCommand.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Command;
4
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Object\BaseFrameworkSystem;
8
9 /**
10  * A general (base) command
11  *
12  * @author              Roland Haeder <webmaster@shipsimu.org>
13  * @version             0.0.0
14  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15  * @license             GNU GPL 3.0 or any newer version
16  * @link                http://www.shipsimu.org
17  *
18  * This program is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program. If not, see <http://www.gnu.org/licenses/>.
30  */
31 class BaseCommand extends BaseFrameworkSystem {
32         /**
33          * Protected constructor
34          *
35          * @param       $className      Name of the class
36          * @return      void
37          */
38         protected function __construct ($className) {
39                 // Call parent constructor
40                 parent::__construct($className);
41         }
42
43         /**
44          * Sends a generic HTTP response with header, menu, content and footer
45          *
46          * @param       $requestInstance        An instance of a class with an Requestable interface
47          * @param       $responseInstance       An instance of a class with an Responseable interface
48          * @param       $suffix                         Optional template suffix, e.g. '_form' for forms
49          * @return      void
50          */
51         protected function sendGenericGetResponse (Requestable $requestInstance, Responseable $responseInstance, $suffix = '') {
52                 // This command doesn't handle any POST requests, so only handle get request
53                 assert(!$requestInstance->isPostRequestMethod());
54
55                 // Get the application instance
56                 $applicationInstance = $this->getResolverInstance()->getApplicationInstance();
57
58                 // Prepare a template instance
59                 $templateInstance = $this->prepareTemplateInstance($applicationInstance);
60
61                 // Transfer application data
62                 $templateInstance->assignApplicationData($applicationInstance);
63
64                 // Assign base URL
65                 $templateInstance->assignConfigVariable('base_url');
66
67                 // Load the master template
68                 $masterTemplate = $applicationInstance->buildMasterTemplateName();
69
70                 // Load header template
71                 $templateInstance->loadCodeTemplate('header');
72
73                 // Compile and assign it with a variable
74                 $templateInstance->compileTemplate();
75                 $templateInstance->assignTemplateWithVariable('header', 'header');
76
77                 // Load footer template
78                 $templateInstance->loadCodeTemplate('footer');
79
80                 // Compile and assign it with a variable
81                 $templateInstance->compileTemplate();
82                 $templateInstance->assignTemplateWithVariable('footer', 'footer');
83
84                 // Load the content template
85                 $templateInstance->loadCodeTemplate($this->getResolverInstance()->getCommandName() . $suffix);
86
87                 // Assign the content template with the master template as a content ... ;)
88                 $templateInstance->assignTemplateWithVariable($applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName(), 'main_content');
89
90                 // Load the master template
91                 $templateInstance->loadCodeTemplate($masterTemplate);
92
93                 // Set title
94                 $templateInstance->assignVariable('title', $this->getLanguageInstance()->getMessage('page_' . $applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_title'));
95
96                 // Construct the menu in every command. We could do this in BaseCommand class. But this means
97                 // *every* command has a navigation system and that is want we don't want.
98                 $menuInstance = ObjectFactory::createObjectByConfiguredName($applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_menu_class', array($applicationInstance));
99
100                 // Render the menu
101                 $menuInstance->renderMenu();
102
103                 // Transfer it to the template engine instance
104                 $menuInstance->transferContentToTemplateEngine($templateInstance);
105
106                 /*
107                  * ... and all variables. This should be merged together in a pattern
108                  * to make things easier. A cache mechanism should be added between
109                  * these two calls to cache compiled templates.
110                  */
111                 $templateInstance->compileVariables();
112
113                 // Get the content back from the template engine and put it in response class
114                 $templateInstance->transferToResponse($responseInstance);
115         }
116
117 }