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