'public static final' is correct
[shipsimu.git] / application / ship-simu / class_ApplicationHelper.php
1 <?php
2 /**
3  * A class holding general data about the application and some methods for
4  * the management including the entry point.
5  *
6  * E.g.:
7  *
8  * index.php?app=my_app
9  *
10  * You need to create a folder in the folder "application" named "my_app"
11  * (without the quotes) and create a include file called
12  * class_ApplicationHelper.php. You have to write the same class for your
13  * application and implement the same interface called ManageableApplication
14  * because this class include file will be searched for.
15  *
16  * It is good when you avoid more GET parameters to keep URLs short and sweet.
17  * But sometimes you need some GET paramerers e.g. for your imprint or info page
18  * or other linked pages which you have to create and state some informations.
19  *
20  * Please remember that this include file is being loaded *before* the class
21  * loader is loading classes from "exceptions", "interfaces" and "main"!
22  *
23  * @author              Roland Haeder <webmaster@ship-simu.org>
24  * @version             0.0.0
25  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 Ship-Simu Developer Team
26  * @license             GNU GPL 3.0 or any newer version
27  * @link                http://www.ship-simu.org
28  *
29  * This program is free software: you can redistribute it and/or modify
30  * it under the terms of the GNU General Public License as published by
31  * the Free Software Foundation, either version 3 of the License, or
32  * (at your option) any later version.
33  *
34  * This program is distributed in the hope that it will be useful,
35  * but WITHOUT ANY WARRANTY; without even the implied warranty of
36  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37  * GNU General Public License for more details.
38  *
39  * You should have received a copy of the GNU General Public License
40  * along with this program. If not, see <http://www.gnu.org/licenses/>.
41  */
42 class ApplicationHelper extends BaseApplication implements ManageableApplication, Registerable {
43         /**
44          * The version number of this application
45          */
46         private $appVersion = "";
47
48         /**
49          * The human-readable name for this application
50          */
51         private $appName = "";
52
53         /**
54          * The short uni*-like name for this application
55          */
56         private $shortName = "";
57
58         /**
59          * An instance of a controller
60          */
61         private $controllerInstance = null;
62
63         /**
64          * An instance of this class
65          */
66         private static $thisInstance = null;
67
68         /**
69          * Protected constructor
70          *
71          * @return      void
72          */
73         protected function __construct () {
74                 // Call parent constructor
75                 parent::__construct(__CLASS__);
76         }
77
78         /**
79          * Getter for an instance of this class
80          *
81          * @return      $thisInstance           An instance of this class
82          */
83         public static final function getInstance () {
84                 // Is the instance there?
85                 if (is_null(self::$thisInstance)) {
86                         self::$thisInstance = new ApplicationHelper();
87                 }
88
89                 // Return the instance
90                 return self::$thisInstance;
91         }
92
93         /**
94          * Getter for the version number
95          *
96          * @return      $appVersion             The application's version number
97          */
98         public final function getAppVersion () {
99                 return $this->appVersion;
100         }
101
102         /**
103          * Setter for the version number
104          *
105          * @param       $appVersion             The application's version number
106          * @return      void
107          */
108         public final function setAppVersion ($appVersion) {
109                 // Cast and set it
110                 $appVersion = (string) $appVersion;
111                 $this->appVersion = $appVersion;
112         }
113
114         /**
115          * Getter for human-readable name
116          *
117          * @return      $appName        The application's human-readable name
118          */
119         public final function getAppName () {
120                 return $this->appName;
121         }
122
123         /**
124          * Setter for human-readable name
125          *
126          * @param       $appName        The application's human-readable name
127          * @return      void
128          */
129         public final function setAppName ($appName) {
130                 // Cast and set it
131                 $appName = (string) $appName;
132                 $this->appName = $appName;
133         }
134
135         /**
136          * Getter for short uni*-like name
137          *
138          * @return      $shortName      The application's short uni*-like name
139          */
140         public final function getAppShortName () {
141                 return $this->shortName;
142         }
143
144         /**
145          * Setter for short uni*-like name
146          *
147          * @param       $shortName      The application's short uni*-like name
148          * @return      void
149          */
150         public final function setAppShortName ($shortName) {
151                 // Cast and set it
152                 $shortName = (string) $shortName;
153                 $this->shortName = $shortName;
154         }
155
156         /**
157          * Builds the master template's name
158          *
159          * @return      $masterTemplateName             Name of the master template
160          */
161         public function buildMasterTemplateName () {
162                 // Get short name and add suffix
163                 $masterTemplateName = str_replace('-', '', $this->getAppShortName()) . '_main';
164
165                 // Return it
166                 return $masterTemplateName;
167         }
168
169         /**
170          * Launches the admin area
171          *
172          * @return      void
173          */
174         public final function entryPoint () {
175                 // Analyze the environment for type of response/request
176                 $type = ConsoleTools::analyzeEnvironmentForType();
177                 $classType = ConsoleTools::analyzeEnvironmentForClassType();
178
179                 // Create a new request object
180                 $requestInstance = ObjectFactory::createObjectByName(ucfirst($type) . 'Request');
181
182                 // Remember request instance here
183                 $this->setRequestInstance($requestInstance);
184
185                 // Default response is HTTP (HTML page) and type is 'Web'
186                 $response = $type;
187                 $responseType = $classType;
188
189                 // Do we have another response?
190                 if ($requestInstance->isRequestElementSet('request')) {
191                         // Then use it
192                         $response = strtolower($requestInstance->getRequestElement('request'));
193                         $responseType = $response;
194                 } // END - if
195
196                 // ... and a new response object
197                 $responseClass = sprintf("%sResponse", $this->convertToClassName($response));
198                 $responseInstance = ObjectFactory::createObjectByName($responseClass, array($this));
199
200                 // Remember response instance here
201                 $this->setResponseInstance($responseInstance);
202
203                 // Get the parameter from the request
204                 $commandName = $requestInstance->getRequestElement('page');
205
206                 // If it is null then get default command
207                 if (is_null($commandName)) {
208                         // Get the default command
209                         $commandName = $responseInstance->getDefaultCommand();
210
211                         // Set it in request
212                         $requestInstance->setRequestElement('page', $commandName);
213                 } // END - if
214
215                 // Get a resolver
216                 $resolverClass = sprintf("%sControllerResolver", $this->convertToClassName($responseType));
217                 $resolverInstance = ObjectFactory::createObjectByName($resolverClass, array($commandName, $this));
218
219                 // Get a controller instance as well
220                 $this->controllerInstance = $resolverInstance->resolveController();
221
222                 // Get a web output class
223                 $outputInstance = ObjectFactory::createObjectByConfiguredName('output_class', array($this));
224
225                 // Set it in this application
226                 $this->setWebOutputInstance($outputInstance);
227
228                 // Handle the request
229                 $this->controllerInstance->handleRequest($requestInstance, $responseInstance);
230         }
231
232         /**
233          * Handle the indexed array of fatal messages and puts them out in an
234          * acceptable fasion
235          *
236          * @param       $messageList    An array of fatal messages
237          * @return      void
238          */
239         public function handleFatalMessages (array $messageList) {
240                 // Walk through all messages
241                 foreach ($messageList as $message) {
242                         print("MSG:".$message."<br />\n");
243                 } // END - if
244         }
245
246         /**
247          * Assigns application-depending data
248          *
249          * @param       $templateInstance       An instance of a template engine
250          * @return      void
251          */
252         public function assignExtraTemplateData (CompileableTemplate $templateInstance) {
253                 // Assign charset
254                 $templateInstance->assignConfigVariable('header_charset');
255         }
256 }
257
258 // [EOF]
259 ?>