]> git.mxchange.org Git - hub.git/blob - application/hub/main/template/announcement/class_AnnouncementTemplateEngine.php
Hub descriptor extended, template engine added:
[hub.git] / application / hub / main / template / announcement / class_AnnouncementTemplateEngine.php
1 <?php
2 /**
3  * A Announcement template engine class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.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 AnnouncementTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
25         /**
26          * Main nodes in the XML tree ('menu' is ignored)
27          */
28         private $mainNodes = array(
29                 'announcement',
30                 'annoucement-data'
31         );
32
33         /**
34          * Sub nodes in the XML tree
35          */
36         private $subNodes = array(
37                 'node-id',
38         );
39
40         /**
41          * Current main node
42          */
43         private $curr = array();
44
45         /**
46          * Content from dependency
47          */
48         private $dependencyContent = array();
49
50         /**
51          * Protected constructor
52          *
53          * @return      void
54          */
55         protected function __construct () {
56                 // Call parent constructor
57                 parent::__construct(__CLASS__);
58         }
59
60         /**
61          * Creates an instance of the class TemplateEngine and prepares it for usage
62          *
63          * @param       $appInstance    A manageable application
64          * @return      $tplInstance    An instance of TemplateEngine
65          * @throws      BasePathIsEmptyException                If the provided $templateBasePath is empty
66          * @throws      InvalidBasePathStringException  If $templateBasePath is no string
67          * @throws      BasePathIsNoDirectoryException  If $templateBasePath is no
68          *                                                                                      directory or not found
69          * @throws      BasePathReadProtectedException  If $templateBasePath is
70          *                                                                                      read-protected
71          */
72         public final static function createAnnouncementTemplateEngine (ManageableApplication $appInstance) {
73                 // Get a new instance
74                 $tplInstance = new AnnouncementTemplateEngine();
75
76                 // Get language and file I/O instances from application
77                 $langInstance = $appInstance->getLanguageInstance();
78                 $ioInstance = $appInstance->getFileIoInstance();
79
80                 // Determine base path
81                 $templateBasePath = $tplInstance->getConfigInstance()->getConfigEntry('application_base_path') . $appInstance->getRequestInstance()->getRequestElement('app') . '/';
82
83                 // Is the base path valid?
84                 if (empty($templateBasePath)) {
85                         // Base path is empty
86                         throw new BasePathIsEmptyException($tplInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
87                 } elseif (!is_string($templateBasePath)) {
88                         // Is not a string
89                         throw new InvalidBasePathStringException(array($tplInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
90                 } elseif (!is_dir($templateBasePath)) {
91                         // Is not a path
92                         throw new BasePathIsNoDirectoryException(array($tplInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
93                 } elseif (!is_readable($templateBasePath)) {
94                         // Is not readable
95                         throw new BasePathReadProtectedException(array($tplInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
96                 }
97
98                 // Get configuration instance
99                 $configInstance = FrameworkConfiguration::getInstance();
100
101                 // Set the base path
102                 $tplInstance->setTemplateBasePath($templateBasePath);
103
104                 // Set the language and IO instances
105                 $tplInstance->setLanguageInstance($langInstance);
106                 $tplInstance->setFileIoInstance($ioInstance);
107
108                 // Set template extensions
109                 $tplInstance->setRawTemplateExtension($configInstance->getConfigEntry('raw_template_extension'));
110                 $tplInstance->setCodeTemplateExtension($configInstance->getConfigEntry('announcement_template_extension'));
111
112                 // Absolute output path for compiled templates
113                 $tplInstance->setCompileOutputPath($configInstance->getConfigEntry('base_path') . $configInstance->getConfigEntry('compile_output_path'));
114
115                 // Init a variable stacker
116                 $stackerInstance = ObjectFactory::createObjectByConfiguredName('announcement_stacker_class');
117
118                 // Set it
119                 $tplInstance->setStackerInstance($stackerInstance);
120
121                 // Return the prepared instance
122                 return $tplInstance;
123         }
124
125         /**
126          * Load a specified announcement template into the engine
127          *
128          * @param       $template       The announcement template we shall load which is
129          *                                              located in 'announcement' by default
130          * @return      void
131          */
132         public function loadAnnouncementTemplate ($template) {
133                 // Set template type
134                 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('announcement_template_type'));
135
136                 // Load the special template
137                 $this->loadTemplate($template);
138         }
139
140         /**
141          * Getter for current main node
142          *
143          * @return      $currMainNode   Current main node
144          */
145         public final function getCurrMainNode () {
146                 return $this->curr['main_node'];
147         }
148
149         /**
150          * Setter for current main node
151          *
152          * @param       $element                Element name to set as current main node
153          * @return      $currMainNode   Current main node
154          */
155         private final function setCurrMainNode ($element) {
156                 $this->curr['main_node'] = (string) $element;
157         }
158
159         /**
160          * Getter for main node array
161          *
162          * @return      $mainNodes      Array with valid main node names
163          */
164         public final function getMainNodes () {
165                 return $this->mainNodes;
166         }
167
168         /**
169          * Getter for sub node array
170          *
171          * @return      $subNodes       Array with valid sub node names
172          */
173         public final function getSubNodes () {
174                 return $this->subNodes;
175         }
176
177         /**
178          * Handles the start element of an XML resource
179          *
180          * @param       $resource               XML parser resource (currently ignored)
181          * @param       $element                The element we shall handle
182          * @param       $attributes             All attributes
183          * @return      void
184          * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
185          */
186         public function startElement ($resource, $element, array $attributes) {
187                 // Initial method name which will never be called...
188                 $methodName = 'initMenu';
189
190                 // Make the element name lower-case
191                 $element = strtolower($element);
192
193                 // Is the element a main node?
194                 //* DEBUG: */ echo "START: &gt;".$element."&lt;<br />\n";
195                 if (in_array($element, $this->getMainNodes())) {
196                         // Okay, main node found!
197                         $methodName = 'start' . $this->convertToClassName($element);
198
199                         // Set it
200                         $this->setCurrMainNode($element);
201                 } elseif (in_array($element, $this->getSubNodes())) {
202                         // Sub node found
203                         $methodName = 'start' . $this->convertToClassName($element);
204                 } elseif ($element != 'menu') {
205                         // Invalid node name found
206                         throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
207                 }
208
209                 // Call method
210                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
211                 call_user_func_array(array($this, $methodName), $attributes);
212         }
213
214         /**
215          * Ends the main or sub node by sending out the gathered data
216          *
217          * @param       $resource       An XML resource pointer (currently ignored)
218          * @param       $nodeName       Name of the node we want to finish
219          * @return      void
220          * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
221          */
222         public function endElement ($resource, $nodeName) {
223                 // Make all lower-case
224                 $nodeName = strtolower($nodeName);
225
226                 // Does this match with current main node?
227                 //* DEBUG: */ echo "END: &gt;".$nodeName."&lt;<br />\n";
228                 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
229                         // Did not match!
230                         throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
231                 } // END - if
232
233                 // Construct method name
234                 $methodName = 'finish' . $this->convertToClassName($nodeName);
235
236                 // Call the corresponding method
237                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
238                 call_user_func_array(array($this, $methodName), array());
239         }
240
241         /**
242          * Currently not used
243          *
244          * @param       $resource               XML parser resource (currently ignored)
245          * @param       $characters             Characters to handle
246          * @return      void
247          * @todo        Find something useful with this!
248          */
249         public function characterHandler ($resource, $characters) {
250                 // Trim all spaces away
251                 $characters = trim($characters);
252
253                 // Is this string empty?
254                 if (empty($characters)) {
255                         // Then skip it silently
256                         return false;
257                 } // END - if
258
259                 // Assign the found characters to variable and use the last entry from
260                 // stack as the name
261                 parent::assignVariable($this->getStackerInstance()->getNamed('current_node'), $characters);
262         }
263
264         /**
265          * Handles the template dependency for given node
266          *
267          * @param       $node                                   The node we should load a dependency template
268          * @param       $templateDependency             A template to load to satisfy dependencies
269          * @return      void
270          */
271         private function handleTemplateDependency ($node, $templateDependency) {
272                 // Is the template dependency set?
273                 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
274                         // Get a temporay menu template instance
275                         $templateInstance = ObjectFactory::createObjectByConfiguredName('menu_template_class', array($this->getApplicationInstance(), $this->getMenuInstance()));
276
277                         // Then load it
278                         $templateInstance->loadMenuTemplate($templateDependency);
279
280                         // Get an XmlParser instance
281                         $templateInstance->renderXmlContent();
282
283                         // Parse the template's content contents
284                         $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
285                 } // END - if
286         }
287
288         /**
289          * Getter for menu cache file (FQFN)
290          *
291          * @return      $fqfn   Full-qualified file name of the menu cache
292          */
293         public function getMenuCacheFqfn () {
294                 // Get the FQFN ready
295                 $fqfn = sprintf("%s%s%s/%s.%s",
296                         $this->getConfigInstance()->getConfigEntry('base_path'),
297                         $this->getGenericBasePath(),
298                         'menus/_cache',
299                         md5(
300                                 $this->getMenuInstance()->getMenuName() . ':' .
301                                 $this->__toString() . ':' .
302                                 $this->getMenuInstance()->__toString()
303                         ),
304                         $this->getMenuInstance()->getMenuType()
305                 );
306
307                 // Return it
308                 return $fqfn;
309         }
310 }
311
312 // [EOF]
313 ?>