Copyright updated
[core.git] / inc / classes / main / template / menu / class_MenuTemplateEngine.php
1 <?php
2 /**
3  * A Menu 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 - 2011 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 MenuTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
25         /**
26          * Main nodes in the XML tree ('menu' is ignored)
27          */
28         private $mainNodes = array(
29                 'block-list',
30         );
31
32         /**
33          * Sub nodes in the XML tree
34          */
35         private $subNodes = array(
36                 'entry-list',
37                 'entry',
38                 'entry-id',
39                 'entries-content',
40                 'block-header',
41                 'block-footer',
42                 'footer-id',
43                 'footer-class',
44                 'footer-text',
45                 'block',
46                 'title',
47                 'title-id',
48                 'title-class',
49                 'title-text',
50                 'design',
51                 'text',
52                 'advert',
53                 'anchor',
54                 'anchor-id',
55                 'anchor-text',
56                 'anchor-title',
57                 'anchor-href',
58         );
59
60         /**
61          * Menu instance
62          */
63         private $menuInstance = null;
64
65         /**
66          * Current main node
67          */
68         private $curr = array();
69
70         /**
71          * Content from dependency
72          */
73         private $dependencyContent = array();
74
75         /**
76          * Protected constructor
77          *
78          * @return      void
79          */
80         protected function __construct () {
81                 // Call parent constructor
82                 parent::__construct(__CLASS__);
83         }
84
85         /**
86          * Creates an instance of the class TemplateEngine and prepares it for usage
87          *
88          * @param       $appInstance    A manageable application
89          * @param       $menuInstance   A RenderableMenu instance
90          * @return      $templateInstance       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
96          *                                                                                      read-protected
97          */
98         public static final function createMenuTemplateEngine (ManageableApplication $appInstance, RenderableMenu $menuInstance) {
99                 // Get a new instance
100                 $templateInstance = new MenuTemplateEngine();
101
102                 // Get language and file I/O instances from application
103                 $langInstance = $appInstance->getLanguageInstance();
104                 $ioInstance = $appInstance->getFileIoInstance();
105
106                 // Determine base path
107                 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $appInstance->getRequestInstance()->getRequestElement('app') . '/';
108
109                 // Is the base path valid?
110                 if (empty($templateBasePath)) {
111                         // Base path is empty
112                         throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
113                 } elseif (!is_string($templateBasePath)) {
114                         // Is not a string
115                         throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
116                 } elseif (!is_dir($templateBasePath)) {
117                         // Is not a path
118                         throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
119                 } elseif (!is_readable($templateBasePath)) {
120                         // Is not readable
121                         throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
122                 }
123
124                 // Set the base path
125                 $templateInstance->setTemplateBasePath($templateBasePath);
126
127                 // Set the language and IO instances
128                 $templateInstance->setLanguageInstance($langInstance);
129                 $templateInstance->setFileIoInstance($ioInstance);
130
131                 // Set template extensions
132                 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
133                 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('menu_template_extension'));
134
135                 // Absolute output path for compiled templates
136                 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
137
138                 // Set the menu instance
139                 $templateInstance->setMenuInstance($menuInstance);
140
141                 // Init a variable stacker
142                 $stackerInstance = ObjectFactory::createObjectByConfiguredName('menu_stacker_class');
143
144                 // Set it
145                 $templateInstance->setStackerInstance($stackerInstance);
146
147                 // Return the prepared instance
148                 return $templateInstance;
149         }
150
151         /**
152          * Load a specified menu template into the engine
153          *
154          * @param       $template       The menu template we shall load which is
155          *                                              located in 'menu' by default
156          * @return      void
157          */
158         public function loadMenuTemplate ($template) {
159                 // Set template type
160                 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('menu_template_type'));
161
162                 // Load the special template
163                 $this->loadTemplate($template);
164         }
165
166         /**
167          * Getter for current main node
168          *
169          * @return      $currMainNode   Current main node
170          */
171         public final function getCurrMainNode () {
172                 return $this->curr['main_node'];
173         }
174
175         /**
176          * Setter for current main node
177          *
178          * @param       $element                Element name to set as current main node
179          * @return      $currMainNode   Current main node
180          */
181         private final function setCurrMainNode ($element) {
182                 $this->curr['main_node'] = (string) $element;
183         }
184
185         /**
186          * Getter for main node array
187          *
188          * @return      $mainNodes      Array with valid main node names
189          */
190         public final function getMainNodes () {
191                 return $this->mainNodes;
192         }
193
194         /**
195          * Getter for sub node array
196          *
197          * @return      $subNodes       Array with valid sub node names
198          */
199         public final function getSubNodes () {
200                 return $this->subNodes;
201         }
202
203         /**
204          * Handles the start element of an XML resource
205          *
206          * @param       $resource               XML parser resource (currently ignored)
207          * @param       $element                The element we shall handle
208          * @param       $attributes             All attributes
209          * @return      void
210          * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
211          */
212         public function startElement ($resource, $element, array $attributes) {
213                 // Initial method name which will never be called...
214                 $methodName = 'initMenu';
215
216                 // Make the element name lower-case
217                 $element = strtolower($element);
218
219                 // Is the element a main node?
220                 //* DEBUG: */ echo "START: &gt;".$element."&lt;<br />\n";
221                 if (in_array($element, $this->getMainNodes())) {
222                         // Okay, main node found!
223                         $methodName = 'start' . $this->convertToClassName($element);
224
225                         // Set it
226                         $this->setCurrMainNode($element);
227                 } elseif (in_array($element, $this->getSubNodes())) {
228                         // Sub node found
229                         $methodName = 'start' . $this->convertToClassName($element);
230                 } elseif ($element != 'menu') {
231                         // Invalid node name found
232                         throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
233                 }
234
235                 // Call method
236                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
237                 call_user_func_array(array($this, $methodName), $attributes);
238         }
239
240         /**
241          * Ends the main or sub node by sending out the gathered data
242          *
243          * @param       $resource       An XML resource pointer (currently ignored)
244          * @param       $nodeName       Name of the node we want to finish
245          * @return      void
246          * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
247          */
248         public function endElement ($resource, $nodeName) {
249                 // Make all lower-case
250                 $nodeName = strtolower($nodeName);
251
252                 // Does this match with current main node?
253                 //* DEBUG: */ echo "END: &gt;".$nodeName."&lt;<br />\n";
254                 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
255                         // Did not match!
256                         throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
257                 } // END - if
258
259                 // Construct method name
260                 $methodName = 'finish' . $this->convertToClassName($nodeName);
261
262                 // Call the corresponding method
263                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
264                 call_user_func_array(array($this, $methodName), array());
265         }
266
267         /**
268          * Currently not used
269          *
270          * @param       $resource               XML parser resource (currently ignored)
271          * @param       $characters             Characters to handle
272          * @return      void
273          * @todo        Find something useful with this!
274          */
275         public function characterHandler ($resource, $characters) {
276                 // Trim all spaces away
277                 $characters = trim($characters);
278
279                 // Is this string empty?
280                 if (empty($characters)) {
281                         // Then skip it silently
282                         return false;
283                 } // END - if
284
285                 // Assign the found characters to variable and use the last entry from
286                 // stack as the name
287                 parent::assignVariable($this->getStackerInstance()->getNamed('current_node'), $characters);
288         }
289
290         /**
291          * Handles the template dependency for given node
292          *
293          * @param       $node                                   The node we should load a dependency template
294          * @param       $templateDependency             A template to load to satisfy dependencies
295          * @return      void
296          */
297         private function handleTemplateDependency ($node, $templateDependency) {
298                 // Is the template dependency set?
299                 if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) {
300                         // Get a temporay menu template instance
301                         $templateInstance = ObjectFactory::createObjectByConfiguredName('menu_template_class', array($this->getApplicationInstance(), $this->getMenuInstance()));
302
303                         // Then load it
304                         $templateInstance->loadMenuTemplate($templateDependency);
305
306                         // Get an XmlParser instance
307                         $templateInstance->renderXmlContent();
308
309                         // Parse the template's content contents
310                         $this->dependencyContent[$node] = $templateInstance->getRawTemplateData();
311                 } // END - if
312         }
313
314         /**
315          * Intializes the menu
316          *
317          * @param       $templateDependency             A template to load to satisfy dependencies
318          * @return      void
319          * @todo        Add cache creation here
320          */
321         private function initMenu ($templateDependency = '') {
322                 // Get web template engine
323                 $this->setTemplateInstance(ObjectFactory::createObjectByConfiguredName('web_template_class', array($this->getApplicationInstance())));
324
325                 // Handle the dependency template
326                 $this->handleTemplateDependency('menu', $templateDependency);
327
328                 // Push the node name on the stacker
329                 $this->getStackerInstance()->pushNamed('current_node', 'menu');
330         }
331
332         /**
333          * Starts the menu entries
334          *
335          * @param       $templateDependency     A template to load to satisfy dependencies
336          * @return      void
337          */
338         private function startEntryList () {
339                 // Push the node name on the stacker
340                 $this->getStackerInstance()->pushNamed('current_node', 'entry-list');
341         }
342
343         /**
344          * Starts the menu block header
345          *
346          * @return      void
347          */
348         private function startBlockHeader () {
349                 // Push the node name on the stacker
350                 $this->getStackerInstance()->pushNamed('current_node', 'block-header');
351         }
352
353         /**
354          * Starts the menu block footer
355          *
356          * @return      void
357          */
358         private function startBlockFooter () {
359                 // Push the node name on the stacker
360                 $this->getStackerInstance()->pushNamed('current_node', 'block-footer');
361         }
362
363         /**
364          * Starts the menu property 'block-list'
365          *
366          * @return      void
367          */
368         private function startBlockList () {
369                 // Push the node name on the stacker
370                 $this->getStackerInstance()->pushNamed('current_node', 'block-list');
371         }
372
373         /**
374          * Starts the menu property 'block'
375          *
376          * @return      void
377          */
378         private function startBlock () {
379                 // Push the node name on the stacker
380                 $this->getStackerInstance()->pushNamed('current_node', 'block');
381         }
382
383         /**
384          * Starts the menu property 'title'
385          *
386          * @return      void
387          */
388         private function startTitle () {
389                 // Push the node name on the stacker
390                 $this->getStackerInstance()->pushNamed('current_node', 'title');
391         }
392
393         /**
394          * Starts the menu property 'title-id'
395          *
396          * @return      void
397          */
398         private function startTitleId () {
399                 // Push the node name on the stacker
400                 $this->getStackerInstance()->pushNamed('current_node', 'title-id');
401         }
402
403         /**
404          * Starts the menu property 'title-class'
405          *
406          * @return      void
407          */
408         private function startTitleClass () {
409                 // Push the node name on the stacker
410                 $this->getStackerInstance()->pushNamed('current_node', 'title-class');
411         }
412
413         /**
414          * Starts the menu property 'title-text'
415          *
416          * @return      void
417          */
418         private function startTitleText () {
419                 // Push the node name on the stacker
420                 $this->getStackerInstance()->pushNamed('current_node', 'title-text');
421         }
422
423         /**
424          * Starts the menu property 'entry'
425          *
426          * @return      void
427          */
428         private function startEntry () {
429                 // Push the node name on the stacker
430                 $this->getStackerInstance()->pushNamed('current_node', 'entry');
431         }
432
433         /**
434          * Starts the menu property 'entry-id'
435          *
436          * @return      void
437          */
438         private function startEntryId () {
439                 // Push the node name on the stacker
440                 $this->getStackerInstance()->pushNamed('current_node', 'entry-id');
441         }
442
443         /**
444          * Starts the menu property 'anchor'
445          *
446          * @return      void
447          */
448         private function startAnchor () {
449                 // Push the node name on the stacker
450                 $this->getStackerInstance()->pushNamed('current_node', 'anchor');
451         }
452
453         /**
454          * Starts the menu property 'anchor-id'
455          *
456          * @return      void
457          */
458         private function startAnchorId () {
459                 // Push the node name on the stacker
460                 $this->getStackerInstance()->pushNamed('current_node', 'anchor-id');
461         }
462
463         /**
464          * Starts the menu property 'anchor-text'
465          *
466          * @return      void
467          */
468         private function startAnchorText () {
469                 // Push the node name on the stacker
470                 $this->getStackerInstance()->pushNamed('current_node', 'anchor-text');
471         }
472
473         /**
474          * Starts the menu property 'anchor-title'
475          *
476          * @return      void
477          */
478         private function startAnchorTitle () {
479                 // Push the node name on the stacker
480                 $this->getStackerInstance()->pushNamed('current_node', 'anchor-title');
481         }
482
483         /**
484          * Starts the menu property 'anchor-href'
485          *
486          * @return      void
487          */
488         private function startAnchorHref () {
489                 // Push the node name on the stacker
490                 $this->getStackerInstance()->pushNamed('current_node', 'anchor-href');
491         }
492
493         /**
494          * Starts the menu property 'footer-id'
495          *
496          * @return      void
497          */
498         private function startFooterId () {
499                 // Push the node name on the stacker
500                 $this->getStackerInstance()->pushNamed('current_node', 'footer-id');
501         }
502
503         /**
504          * Starts the menu property 'footer-class'
505          *
506          * @return      void
507          */
508         private function startFooterClass () {
509                 // Push the node name on the stacker
510                 $this->getStackerInstance()->pushNamed('current_node', 'footer-class');
511         }
512
513         /**
514          * Starts the menu property 'footer-text'
515          *
516          * @return      void
517          */
518         private function startFooterText () {
519                 // Push the node name on the stacker
520                 $this->getStackerInstance()->pushNamed('current_node', 'footer-text');
521         }
522
523         /**
524          * Finishes the title node by added another template to the menu
525          *
526          * @return      void
527          */
528         private function finishTitle () {
529                 // Pop the last entry
530                 $this->getStackerInstance()->popNamed('current_node');
531         }
532
533         /**
534          * Finishes the title-id node by
535          *
536          * @return      void
537          */
538         private function finishTitleId () {
539                 // Pop the last entry
540                 $this->getStackerInstance()->popNamed('current_node');
541         }
542
543         /**
544          * Finishes the title-class node
545          *
546          * @return      void
547          */
548         private function finishTitleClass () {
549                 // Pop the last entry
550                 $this->getStackerInstance()->popNamed('current_node');
551         }
552
553         /**
554          * Finishes the title-class node
555          *
556          * @return      void
557          */
558         private function finishTitleText () {
559                 // Pop the last entry
560                 $this->getStackerInstance()->popNamed('current_node');
561         }
562
563         /**
564          * Finishes the footer-text node
565          *
566          * @return      void
567          */
568         private function finishFooterText () {
569                 // Pop the last entry
570                 $this->getStackerInstance()->popNamed('current_node');
571         }
572
573         /**
574          * Finishes the footer-class node
575          *
576          * @return      void
577          */
578         private function finishFooterClass () {
579                 // Pop the last entry
580                 $this->getStackerInstance()->popNamed('current_node');
581         }
582
583         /**
584          * Finishes the footer-id node
585          *
586          * @return      void
587          */
588         private function finishFooterId () {
589                 // Pop the last entry
590                 $this->getStackerInstance()->popNamed('current_node');
591         }
592
593         /**
594          * Finishes the anchor-href node
595          *
596          * @return      void
597          */
598         private function finishAnchorHref () {
599                 // Pop the last entry
600                 $this->getStackerInstance()->popNamed('current_node');
601         }
602
603         /**
604          * Finishes the anchor-title node
605          *
606          * @return      void
607          */
608         private function finishAnchorTitle () {
609                 // Pop the last entry
610                 $this->getStackerInstance()->popNamed('current_node');
611         }
612
613         /**
614          * Finishes the anchor-text node
615          *
616          * @return      void
617          */
618         private function finishAnchorText () {
619                 // Pop the last entry
620                 $this->getStackerInstance()->popNamed('current_node');
621         }
622
623         /**
624          * Finishes the anchor-id node
625          *
626          * @return      void
627          */
628         private function finishAnchorId () {
629                 // Pop the last entry
630                 $this->getStackerInstance()->popNamed('current_node');
631         }
632
633         /**
634          * Finishes the anchor node
635          *
636          * @return      void
637          */
638         private function finishAnchor () {
639                 // Pop the last entry
640                 $this->getStackerInstance()->popNamed('current_node');
641         }
642
643         /**
644          * Finishes the entry-id node
645          *
646          * @return      void
647          */
648         private function finishEntryId () {
649                 // Pop the last entry
650                 $this->getStackerInstance()->popNamed('current_node');
651         }
652
653         /**
654          * Finishes the entry node
655          *
656          * @return      void
657          */
658         private function finishEntry () {
659                 // Pop the last entry
660                 $this->getStackerInstance()->popNamed('current_node');
661         }
662
663         /**
664          * Finishes the block node
665          *
666          * @return      void
667          */
668         private function finishBlock () {
669                 // Pop the last entry
670                 $this->getStackerInstance()->popNamed('current_node');
671         }
672
673         /**
674          * Finishes the block-list node
675          *
676          * @return      void
677          */
678         private function finishBlockList () {
679                 // Pop the last entry
680                 $this->getStackerInstance()->popNamed('current_node');
681         }
682
683         /**
684          * Finishes the menu entries
685          *
686          * @return      void
687          */
688         private function finishEntryList () {
689                 // Pop the last entry
690                 $this->getStackerInstance()->popNamed('current_node');
691         }
692
693         /**
694          * Finishes the menu block header
695          *
696          * @return      void
697          */
698         private function finishBlockHeader () {
699                 // Pop the last entry
700                 $this->getStackerInstance()->popNamed('current_node');
701         }
702
703         /**
704          * Finishes the menu block footer
705          *
706          * @return      void
707          */
708         private function finishBlockFooter () {
709                 // Pop the last entry
710                 $this->getStackerInstance()->popNamed('current_node');
711         }
712
713         /**
714          * Finishes the menu
715          *
716          * @return      void
717          */
718         private function finishMenu () {
719                 // Pop the last entry
720                 $this->getStackerInstance()->popNamed('current_node');
721         }
722
723         /**
724          * Getter for menu cache file (FQFN)
725          *
726          * @return      $fqfn   Full-qualified file name of the menu cache
727          */
728         public function getMenuCacheFqfn () {
729                 // Get the FQFN ready
730                 $fqfn = sprintf("%s%s%s/%s.%s",
731                         $this->getConfigInstance()->getConfigEntry('base_path'),
732                         $this->getGenericBasePath(),
733                         'menus/_cache',
734                         md5(
735                                 $this->getMenuInstance()->getMenuName() . ':' .
736                                 $this->__toString() . ':' .
737                                 $this->getMenuInstance()->__toString()
738                         ),
739                         $this->getMenuInstance()->getMenuType()
740                 );
741
742                 // Return it
743                 return $fqfn;
744         }
745 }
746
747 // [EOF]
748 ?>