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