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