3 * A Menu template engine class
5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
24 class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
26 * Main nodes in the XML tree ('menu' is ignored)
28 private $mainNodes = array(
33 * Sub nodes in the XML tree
35 private $subNodes = array(
63 private $menuInstance = null;
68 private $curr = array();
71 * Content from depency
73 private $depencyContent = array();
76 * Protected constructor
80 protected function __construct () {
81 // Call parent constructor
82 parent::__construct(__CLASS__);
86 * Creates an instance of the class TemplateEngine and prepares it for usage
88 * @param $appInstance A manageable application
89 * @param $menuInstance A RenderableMenu instance
90 * @return $tplInstance An instance of TemplateEngine
91 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
92 * @throws InvalidBasePathStringException If $templateBasePath is no string
93 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
94 * directory or not found
95 * @throws BasePathReadProtectedException If $templateBasePath is
98 public final static function createMenuTemplateEngine (ManageableApplication $appInstance, RenderableMenu $menuInstance) {
100 $tplInstance = new MenuTemplateEngine();
102 // Get language and file I/O instances from application
103 $langInstance = $appInstance->getLanguageInstance();
104 $ioInstance = $appInstance->getFileIoInstance();
106 // Determine base path
107 $templateBasePath = $tplInstance->getConfigInstance()->getConfigEntry('application_base_path') . $appInstance->getRequestInstance()->getRequestElement('app') . '/';
109 // Is the base path valid?
110 if (empty($templateBasePath)) {
111 // Base path is empty
112 throw new BasePathIsEmptyException($tplInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
113 } elseif (!is_string($templateBasePath)) {
115 throw new InvalidBasePathStringException(array($tplInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
116 } elseif (!is_dir($templateBasePath)) {
118 throw new BasePathIsNoDirectoryException(array($tplInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
119 } elseif (!is_readable($templateBasePath)) {
121 throw new BasePathReadProtectedException(array($tplInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
124 // Get configuration instance
125 $configInstance = FrameworkConfiguration::getInstance();
128 $tplInstance->setTemplateBasePath($templateBasePath);
130 // Set the language and IO instances
131 $tplInstance->setLanguageInstance($langInstance);
132 $tplInstance->setFileIoInstance($ioInstance);
134 // Set template extensions
135 $tplInstance->setRawTemplateExtension($configInstance->getConfigEntry('raw_template_extension'));
136 $tplInstance->setCodeTemplateExtension($configInstance->getConfigEntry('menu_template_extension'));
138 // Absolute output path for compiled templates
139 $tplInstance->setCompileOutputPath($configInstance->getConfigEntry('base_path') . $configInstance->getConfigEntry('compile_output_path'));
141 // Set the menu instance
142 $tplInstance->setMenuInstance($menuInstance);
144 // Return the prepared instance
149 * Load a specified menu template into the engine
151 * @param $template The menu template we shall load which is
152 * located in 'menu' by default
155 public function loadMenuTemplate ($template) {
157 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('menu_template_type'));
159 // Load the special template
160 $this->loadTemplate($template);
164 * Getter for current main node
166 * @return $currMainNode Current main node
168 public final function getCurrMainNode () {
169 return $this->curr['main_node'];
173 * Setter for current main node
175 * @param $element Element name to set as current main node
176 * @return $currMainNode Current main node
178 private final function setCurrMainNode ($element) {
179 $this->curr['main_node'] = (string) $element;
183 * Getter for main node array
185 * @return $mainNodes Array with valid main node names
187 public final function getMainNodes () {
188 return $this->mainNodes;
192 * Getter for sub node array
194 * @return $subNodes Array with valid sub node names
196 public final function getSubNodes () {
197 return $this->subNodes;
201 * Handles the start element of an XML resource
203 * @param $resource XML parser resource (currently ignored)
204 * @param $element The element we shall handle
205 * @param $attributes All attributes
207 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
209 public function startElement ($resource, $element, array $attributes) {
210 // Initial method name which will never be called...
211 $methodName = 'initMenu';
213 // Make the element name lower-case
214 $element = strtolower($element);
216 // Is the element a main node?
217 //* DEBUG: */ echo "START: >".$element."<<br />\n";
218 if (in_array($element, $this->getMainNodes())) {
219 // Okay, main node found!
220 $methodName = 'start' . $this->convertToClassName($element);
223 $this->setCurrMainNode($element);
224 } elseif (in_array($element, $this->getSubNodes())) {
226 $methodName = 'start' . $this->convertToClassName($element);
227 } elseif ($element != 'menu') {
228 // Invalid node name found
229 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
233 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
234 call_user_func_array(array($this, $methodName), $attributes);
238 * Ends the main or sub node by sending out the gathered data
240 * @param $resource An XML resource pointer (currently ignored)
241 * @param $nodeName Name of the node we want to finish
243 * @throws XmlNodeMismatchException If current main node mismatches the closing one
245 public function endElement ($resource, $nodeName) {
246 // Make all lower-case
247 $nodeName = strtolower($nodeName);
249 // Does this match with current main node?
250 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
251 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
253 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
256 // Construct method name
257 $methodName = 'finish' . $this->convertToClassName($nodeName);
259 // Call the corresponding method
260 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
261 call_user_func_array(array($this, $methodName), array());
267 * @param $resource XML parser resource (currently ignored)
268 * @param $characters Characters to handle
270 * @todo Find something useful with this!
272 public function characterHandler ($resource, $characters) {
273 // Trim all spaces away
274 $characters = trim($characters);
276 // Is this string empty?
277 if (empty($characters)) {
278 // Then skip it silently
283 $this->partialStub('Handling extra characters is not yet supported! length='.strlen($characters));
287 * Handles the template depency for given node
289 * @param $node The node we should load a depency template
290 * @param $templateDepency A template to load to satisfy depencies
293 private function handleTemplateDepency ($node, $templateDepency) {
294 // Is the template depency set?
295 if ((!empty($templateDepency)) && (!isset($this->depencyContent[$node]))) {
296 // Get a temporay menu template instance
297 $templateInstance = ObjectFactory::createObjectByConfiguredName('menu_template_class', array($this->getApplicationInstance(), $this->getMenuInstance()));
300 $templateInstance->loadMenuTemplate($templateDepency);
302 // Get an XmlParser instance
303 $templateInstance->renderXmlContent();
305 // Parse the template's content contents
306 $this->depencyContent[$node] = $templateInstance->getRawTemplateData();
311 * Intializes the menu
313 * @param $templateDepency A template to load to satisfy depencies
315 * @todo Add cache creation here
317 private function initMenu ($templateDepency = '') {
318 // Get web template engine
319 $this->setTemplateInstance(ObjectFactory::createObjectByConfiguredName('web_template_class', array($this->getApplicationInstance())));
321 // Handle the depency template
322 $this->handleTemplateDepency('menu', $templateDepency);
324 // Load the header template for this page
325 $this->getTemplateInstance()->loadCodeTemplate('menu_global_start');
327 // Set the variable group to page
328 $this->setVariableGroup('menu');
330 // Set its content in this template instance
331 $this->assignVariable('menu_start', $this->getTemplateInstance()->getRawTemplateData());
339 private function finishMenu () {
340 // Load the header template for this page
341 $this->getTemplateInstance()->loadCodeTemplate('menu_global_end');
343 // Set the variable group to page
344 $this->setVariableGroup('menu');
346 // Set its content in this template instance
347 $this->assignVariable('menu_end', $this->getTemplateInstance()->getRawTemplateData());
351 * Starts the menu entries by loading a (maybe) provided template depency
353 * @param $templateDepency A template to load to satisfy depencies
356 private function startEntryList ($templateDepency = '') {
357 // Handle the depency template
358 $this->handleTemplateDepency('entries', $templateDepency);
360 // Load the header template for this page
361 $this->getTemplateInstance()->loadCodeTemplate('menu_entries_start');
363 // Set the variable group to page
364 $this->setVariableGroup('menu');
366 // Set its content in this template instance
367 $this->assignVariable('entries_start', $this->getTemplateInstance()->getRawTemplateData());
371 * Finishes the menu entries
375 private function finishEntryList () {
376 // Load the header template for this page
377 $this->getTemplateInstance()->loadCodeTemplate('menu_entries_end');
379 // Set the variable group to page
380 $this->setVariableGroup('menu');
382 // Set its content in this template instance
383 $this->assignVariable('entries_end', $this->getTemplateInstance()->getRawTemplateData());
387 * Starts the menu block header
391 private function startBlockHeader () {
392 // Do we have a template instance?
393 if (is_null($this->getTemplateInstance())) {
394 // Init template instance for underlaying web templates
395 $templateInstance = ObjectFactory::createObjectByConfiguredName('web_template_class');
397 // Set it in this template engine
398 $this->setTemplateInstance($templateInstance);
401 // Load the header template for this page
402 $this->getTemplateInstance()->loadCodeTemplate('menu_header_start');
404 // Set the variable group to page
405 $this->setVariableGroup('menu');
407 // Set its content in this template instance
408 $this->assignVariable('header', $this->getTemplateInstance()->getRawTemplateData());
412 * Finishes the menu block header
416 private function finishBlockHeader () {
417 // Load the header template for this page
418 $this->getTemplateInstance()->loadCodeTemplate('menu_header_end');
420 // Set the variable group to page
421 $this->setVariableGroup('menu');
423 // Set its content in this template instance
424 $this->assignVariable('header_end', $this->getTemplateInstance()->getRawTemplateData());
428 * Starts the menu block footer
432 private function startBlockFooter () {
433 // Do we have a template instance?
434 if (is_null($this->getTemplateInstance())) {
435 // Init template instance for underlaying web templates
436 $templateInstance = ObjectFactory::createObjectByConfiguredName('web_template_class');
438 // Set it in this template engine
439 $this->setTemplateInstance($templateInstance);
442 // Load the footer template for this page
443 $this->getTemplateInstance()->loadCodeTemplate('menu_footer_start');
445 // Set the variable group to page
446 $this->setVariableGroup('menu');
448 // Set its content in this template instance
449 $this->assignVariable('footer', $this->getTemplateInstance()->getRawTemplateData());
453 * Finishes the menu block footer
457 private function finishBlockFooter () {
458 // Load the footer template for this page
459 $this->getTemplateInstance()->loadCodeTemplate('menu_footer_end');
461 // Set the variable group to page
462 $this->setVariableGroup('menu');
464 // Set its content in this template instance
465 $this->assignVariable('footer_end', $this->getTemplateInstance()->getRawTemplateData());
469 * Starts the menu property 'title'
473 private function startTitle () {
474 $this->partialStub('Cleared due to XML rewrite.');
478 * Finishes the title node by added another template to the menu
482 private function finishTitle () {
483 $this->partialStub('Cleared due to XML rewrite.');
487 * Starts the menu text
491 private function startText () {
492 // Do we have a template instance?
493 if (is_null($this->getTemplateInstance())) {
494 // Init template instance for underlaying web templates
495 $templateInstance = ObjectFactory::createObjectByConfiguredName('web_template_class');
497 // Set it in this template engine
498 $this->setTemplateInstance($templateInstance);
501 // Load the text template for this page
502 $this->getTemplateInstance()->loadCodeTemplate('menu_text_start');
504 // Set the variable group to page
505 $this->setVariableGroup('menu');
507 // Set its content in this template instance
508 $this->assignVariable('text', $this->getTemplateInstance()->getRawTemplateData());
512 * Finishes the menu text
516 private function finishText () {
517 // Load the text template for this page
518 $this->getTemplateInstance()->loadCodeTemplate('menu_text_end');
520 // Set the variable group to page
521 $this->setVariableGroup('menu');
523 // Set its content in this template instance
524 $this->assignVariable('text_end', $this->getTemplateInstance()->getRawTemplateData());
528 * Starts the menu property 'entry'
532 private function startEntry () {
533 $this->partialStub('Cleared due to XML rewrite.');
537 * Finishes the entry node by added another template to the menu
541 private function finishEntry () {
542 $this->partialStub('Cleared due to XML rewrite.');
546 * Starts the menu property 'anchor'
548 * @param $id Id of the anchor
549 * @param $link Link text of the anchor
550 * @param $title Link title of the anchor
553 private function startAnchor () {
554 $this->partialStub('Please implement this method.');
558 * Finishes the anchor node by added another template to the menu
562 private function finishAnchor () {
563 $this->partialStub('Please implement this method.');
567 * Getter for menu cache file (FQFN)
569 * @return $fqfn Full-qualified file name of the menu cache
571 public function getMenuCacheFqfn () {
572 // Get the FQFN ready
573 $fqfn = sprintf("%s%s%s/%s.%s",
574 $this->getConfigInstance()->getConfigEntry('base_path'),
575 $this->getGenericBasePath(),
578 $this->getMenuInstance()->getMenuName() . ':' .
579 $this->__toString() . ':' .
580 $this->getMenuInstance()->__toString()
582 $this->getMenuInstance()->getMenuType()