Menu engine heavily extended, misc rewrites:
[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                 'header',
30                 'entries',
31                 'footer',
32         );
33
34         /**
35          * Sub nodes in the XML tree
36          */
37         private $subNodes = array(
38                 'entries-content',
39                 'title',
40                 'entry',
41                 'anchor',
42                 'design',
43                 'text',
44                 'advert',
45         );
46
47         /**
48          * Menu instance
49          */
50         private $menuInstance = null;
51
52         /**
53          * Current main node
54          */
55         private $curr = array();
56
57         /**
58          * Content from depency
59          */
60         private $depencyContent = array();
61
62         /**
63          * Protected constructor
64          *
65          * @return      void
66          */
67         protected function __construct () {
68                 // Call parent constructor
69                 parent::__construct(__CLASS__);
70         }
71
72         /**
73          * Creates an instance of the class TemplateEngine and prepares it for usage
74          *
75          * @param       $appInstance    A manageable application
76          * @param       $menuInstance   A RenderableMenu instance
77          * @return      $tplInstance    An instance of TemplateEngine
78          * @throws      BasePathIsEmptyException                If the provided $templateBasePath is empty
79          * @throws      InvalidBasePathStringException  If $templateBasePath is no string
80          * @throws      BasePathIsNoDirectoryException  If $templateBasePath is no
81          *                                                                                      directory or not found
82          * @throws      BasePathReadProtectedException  If $templateBasePath is
83          *                                                                                      read-protected
84          */
85         public final static function createMenuTemplateEngine (ManageableApplication $appInstance, RenderableMenu $menuInstance) {
86                 // Get a new instance
87                 $tplInstance = new MenuTemplateEngine();
88
89                 // Get language and file I/O instances from application
90                 $langInstance = $appInstance->getLanguageInstance();
91                 $ioInstance = $appInstance->getFileIoInstance();
92
93                 // Determine base path
94                 $templateBasePath = $tplInstance->getConfigInstance()->getConfigEntry('application_base_path') . $appInstance->getRequestInstance()->getRequestElement('app') . '/';
95
96                 // Is the base path valid?
97                 if (empty($templateBasePath)) {
98                         // Base path is empty
99                         throw new BasePathIsEmptyException($tplInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
100                 } elseif (!is_string($templateBasePath)) {
101                         // Is not a string
102                         throw new InvalidBasePathStringException(array($tplInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
103                 } elseif (!is_dir($templateBasePath)) {
104                         // Is not a path
105                         throw new BasePathIsNoDirectoryException(array($tplInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
106                 } elseif (!is_readable($templateBasePath)) {
107                         // Is not readable
108                         throw new BasePathReadProtectedException(array($tplInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
109                 }
110
111                 // Get configuration instance
112                 $configInstance = FrameworkConfiguration::getInstance();
113
114                 // Set the base path
115                 $tplInstance->setTemplateBasePath($templateBasePath);
116
117                 // Set the language and IO instances
118                 $tplInstance->setLanguageInstance($langInstance);
119                 $tplInstance->setFileIoInstance($ioInstance);
120
121                 // Set template extensions
122                 $tplInstance->setRawTemplateExtension($configInstance->getConfigEntry('raw_template_extension'));
123                 $tplInstance->setCodeTemplateExtension($configInstance->getConfigEntry('menu_template_extension'));
124
125                 // Absolute output path for compiled templates
126                 $tplInstance->setCompileOutputPath($configInstance->getConfigEntry('base_path') . $configInstance->getConfigEntry('compile_output_path'));
127
128                 // Set the menu instance
129                 $tplInstance->setMenuInstance($menuInstance);
130
131                 // Return the prepared instance
132                 return $tplInstance;
133         }
134
135         /**
136          * Load a specified menu template into the engine
137          *
138          * @param       $template       The menu template we shall load which is
139          *                                              located in 'menu' by default
140          * @return      void
141          */
142         public function loadMenuTemplate ($template) {
143                 // Set template type
144                 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('menu_template_type'));
145
146                 // Load the special template
147                 $this->loadTemplate($template);
148         }
149
150         /**
151          * Getter for current main node
152          *
153          * @return      $currMainNode   Current main node
154          */
155         public final function getCurrMainNode () {
156                 return $this->curr['main_node'];
157         }
158
159         /**
160          * Setter for current main node
161          *
162          * @param       $element                Element name to set as current main node
163          * @return      $currMainNode   Current main node
164          */
165         private final function setCurrMainNode ($element) {
166                 $this->curr['main_node'] = (string) $element;
167         }
168
169         /**
170          * Getter for main node array
171          *
172          * @return      $mainNodes      Array with valid main node names
173          */
174         public final function getMainNodes () {
175                 return $this->mainNodes;
176         }
177
178         /**
179          * Getter for sub node array
180          *
181          * @return      $subNodes       Array with valid sub node names
182          */
183         public final function getSubNodes () {
184                 return $this->subNodes;
185         }
186
187         /**
188          * Handles the start element of an XML resource
189          *
190          * @param       $resource               XML parser resource (currently ignored)
191          * @param       $element                The element we shall handle
192          * @param       $attributes             All attributes
193          * @return      void
194          * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
195          */
196         public function startElement ($resource, $element, array $attributes) {
197                 // Initial method name which will never be called...
198                 $methodName = 'initMenu';
199
200                 // Make the element name lower-case
201                 $element = strtolower($element);
202
203                 // Is the element a main node?
204                 //* DEBUG: */ echo "START: &gt;".$element."&lt;<br />\n";
205                 if (in_array($element, $this->getMainNodes())) {
206                         // Okay, main node found!
207                         $methodName = 'startMenu' . $this->convertToClassName($element);
208
209                         // Set it
210                         $this->setCurrMainNode($element);
211                 } elseif (in_array($element, $this->getSubNodes())) {
212                         // Sub node found
213                         $methodName = 'startMenu' . $this->convertToClassName($element);
214                 } elseif ($element != 'menu') {
215                         // Invalid node name found
216                         throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
217                 }
218
219                 // Call method
220                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
221                 call_user_func_array(array($this, $methodName), $attributes);
222         }
223
224         /**
225          * Ends the main or sub node by sending out the gathered data
226          *
227          * @param       $resource       An XML resource pointer (currently ignored)
228          * @param       $nodeName       Name of the node we want to finish
229          * @return      void
230          * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
231          */
232         public function endElement ($resource, $nodeName) {
233                 // Make all lower-case
234                 $nodeName = strtolower($nodeName);
235
236                 // Does this match with current main node?
237                 //* DEBUG: */ echo "END: &gt;".$nodeName."&lt;<br />\n";
238                 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
239                         // Did not match!
240                         throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
241                 } // END - if
242
243                 // Construct method name
244                 $methodName = 'finish' . $this->convertToClassName($nodeName);
245
246                 // Call the corresponding method
247                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
248                 call_user_func_array(array($this, $methodName), array());
249         }
250
251         /**
252          * Currently not used
253          *
254          * @param       $resource               XML parser resource (currently ignored)
255          * @param       $characters             Characters to handle
256          * @return      void
257          * @todo        Find something useful with this!
258          */
259         public function characterHandler ($resource, $characters) {
260                 // Trim all spaces away
261                 $characters = trim($characters);
262
263                 // Is this string empty?
264                 if (empty($characters)) {
265                         // Then skip it silently
266                         return false;
267                 } // END - if
268
269                 // Unfinished work!
270                 $this->partialStub('Handling extra characters is not yet supported! length='.strlen($characters));
271         }
272
273         /**
274          * Handles the template depency for given node
275          *
276          * @param       $node   The node we should load a depency template
277          * @param       $templateDepency        A template to load to satisfy depencies
278          * @return      void
279          */
280         private function handleTemplateDepency ($node, $templateDepency) {
281                 // Is the template depency set?
282                 if ((!empty($templateDepency)) && (!isset($this->depencyContent[$node]))) {
283                         // Get a temporay menu template instance
284                         $templateInstance = ObjectFactory::createObjectByConfiguredName('menu_template_class', array($this->getApplicationInstance(), $this->getMenuInstance()));
285
286                         // Then load it
287                         $templateInstance->loadMenuTemplate($templateDepency);
288
289                         // Get an XmlParser instance
290                         $templateInstance->renderXmlContent();
291
292                         // Parse the template's content contents
293                         $this->depencyContent[$node] = $templateInstance->getRawTemplateData();
294                 } // END - if
295         }
296
297         /**
298          * Intializes the menu
299          *
300          * @param       $templateDepency        A template to load to satisfy depencies
301          * @return      void
302          * @todo        Add cache creation here
303          */
304         private function initMenu ($templateDepency = '') {
305                 // Get web template engine
306                 $this->setTemplateInstance(ObjectFactory::createObjectByConfiguredName('web_template_class', array($this->getApplicationInstance())));
307
308                 // Handle the depency template
309                 $this->handleTemplateDepency('menu', $templateDepency);
310
311                 // Load the header template for this page
312                 $this->getTemplateInstance()->loadCodeTemplate('menu_global_start');
313
314                 // Set the variable group to page
315                 $this->setVariableGroup('menu');
316
317                 // Set its content in this template instance
318                 $this->assignVariable('menu_start', $this->getTemplateInstance()->getRawTemplateData());
319         }
320
321         /**
322          * Finishes the menu
323          *
324          * @return      void
325          */
326         private function finishMenu () {
327                 // Load the header template for this page
328                 $this->getTemplateInstance()->loadCodeTemplate('menu_global_end');
329
330                 // Set the variable group to page
331                 $this->setVariableGroup('menu');
332
333                 // Set its content in this template instance
334                 $this->assignVariable('menu_end', $this->getTemplateInstance()->getRawTemplateData());
335         }
336
337         /**
338          * Starts the menu entries by loading a (maybe) provided template depency
339          *
340          * @param       $templateDepency        A template to load to satisfy depencies
341          * @return      void
342          */
343         private function startMenuEntries ($templateDepency = '') {
344                 // Handle the depency template
345                 $this->handleTemplateDepency('entries', $templateDepency);
346
347                 // Load the header template for this page
348                 $this->getTemplateInstance()->loadCodeTemplate('menu_entries_start');
349
350                 // Set the variable group to page
351                 $this->setVariableGroup('menu');
352
353                 // Set its content in this template instance
354                 $this->assignVariable('entries_start', $this->getTemplateInstance()->getRawTemplateData());
355         }
356
357         /**
358          * Finishes the menu entries
359          *
360          * @return      void
361          */
362         private function finishEntries () {
363                 // Load the header template for this page
364                 $this->getTemplateInstance()->loadCodeTemplate('menu_entries_end');
365
366                 // Set the variable group to page
367                 $this->setVariableGroup('menu');
368
369                 // Set its content in this template instance
370                 $this->assignVariable('entries_end', $this->getTemplateInstance()->getRawTemplateData());
371         }
372
373         /**
374          * Starts the menu header
375          *
376          * @return      void
377          */
378         private function startMenuHeader () {
379                 // Do we have a template instance?
380                 if (is_null($this->getTemplateInstance())) {
381                         // Init template instance for underlaying web templates
382                         $templateInstance = ObjectFactory::createObjectByConfiguredName('web_template_class');
383
384                         // Set it in this template engine
385                         $this->setTemplateInstance($templateInstance);
386                 } // END - if
387
388                 // Load the header template for this page
389                 $this->getTemplateInstance()->loadCodeTemplate('menu_header_start');
390
391                 // Set the variable group to page
392                 $this->setVariableGroup('menu');
393
394                 // Set its content in this template instance
395                 $this->assignVariable('header', $this->getTemplateInstance()->getRawTemplateData());
396         }
397
398         /**
399          * Finishes the menu header
400          *
401          * @return      void
402          */
403         private function finishHeader () {
404                 // Load the header template for this page
405                 $this->getTemplateInstance()->loadCodeTemplate('menu_header_end');
406
407                 // Set the variable group to page
408                 $this->setVariableGroup('menu');
409
410                 // Set its content in this template instance
411                 $this->assignVariable('header_end', $this->getTemplateInstance()->getRawTemplateData());
412         }
413
414         /**
415          * Starts the menu footer
416          *
417          * @return      void
418          */
419         private function startMenuFooter () {
420                 // Do we have a template instance?
421                 if (is_null($this->getTemplateInstance())) {
422                         // Init template instance for underlaying web templates
423                         $templateInstance = ObjectFactory::createObjectByConfiguredName('web_template_class');
424
425                         // Set it in this template engine
426                         $this->setTemplateInstance($templateInstance);
427                 } // END - if
428
429                 // Load the footer template for this page
430                 $this->getTemplateInstance()->loadCodeTemplate('menu_footer_start');
431
432                 // Set the variable group to page
433                 $this->setVariableGroup('menu');
434
435                 // Set its content in this template instance
436                 $this->assignVariable('footer', $this->getTemplateInstance()->getRawTemplateData());
437         }
438
439         /**
440          * Finishes the menu footer
441          *
442          * @return      void
443          */
444         private function finishFooter () {
445                 // Load the footer template for this page
446                 $this->getTemplateInstance()->loadCodeTemplate('menu_footer_end');
447
448                 // Set the variable group to page
449                 $this->setVariableGroup('menu');
450
451                 // Set its content in this template instance
452                 $this->assignVariable('footer_end', $this->getTemplateInstance()->getRawTemplateData());
453         }
454
455         /**
456          * Starts the menu property 'title'
457          *
458          * @param       $id             Id of the menu
459          * @param       $class  The title to add to the menu
460          * @return      void
461          */
462         private function startMenuTitle ($id, $class) {
463                 // Set id as current
464                 $this->curr['id'] = $id;
465
466                 // Load the title template for this page
467                 $this->getTemplateInstance()->loadCodeTemplate('menu_title_' . $id . '_start');
468
469                 // Set the variable group to page
470                 $this->setVariableGroup('menu');
471
472                 // Set its content in this template instance
473                 $this->assignVariable('title_start_' . $this->curr['id'], $this->getTemplateInstance()->getRawTemplateData());
474         }
475
476         /**
477          * Finishes the title node by added another template to the menu
478          *
479          * @return      void
480          */
481         private function finishTitle () {
482                 // Load the title template for this page
483                 $this->getTemplateInstance()->loadCodeTemplate('menu_title_' . $this->curr['id'] . '_end');
484
485                 // Set the variable group to page
486                 $this->setVariableGroup('menu');
487
488                 // Set its content in this template instance
489                 $this->assignVariable('title_end_' . $this->curr['id'], $this->getTemplateInstance()->getRawTemplateData());
490         }
491
492         /**
493          * Starts the menu text
494          *
495          * @return      void
496          */
497         private function startMenuText () {
498                 // Do we have a template instance?
499                 if (is_null($this->getTemplateInstance())) {
500                         // Init template instance for underlaying web templates
501                         $templateInstance = ObjectFactory::createObjectByConfiguredName('web_template_class');
502
503                         // Set it in this template engine
504                         $this->setTemplateInstance($templateInstance);
505                 } // END - if
506
507                 // Load the text template for this page
508                 $this->getTemplateInstance()->loadCodeTemplate('menu_text_start');
509
510                 // Set the variable group to page
511                 $this->setVariableGroup('menu');
512
513                 // Set its content in this template instance
514                 $this->assignVariable('text', $this->getTemplateInstance()->getRawTemplateData());
515         }
516
517         /**
518          * Finishes the menu text
519          *
520          * @return      void
521          */
522         private function finishText () {
523                 // Load the text template for this page
524                 $this->getTemplateInstance()->loadCodeTemplate('menu_text_end');
525
526                 // Set the variable group to page
527                 $this->setVariableGroup('menu');
528
529                 // Set its content in this template instance
530                 $this->assignVariable('text_end', $this->getTemplateInstance()->getRawTemplateData());
531         }
532
533         /**
534          * Starts the menu property 'entry'
535          *
536          * @param       $id             Id of the menu
537          * @return      void
538          */
539         private function startMenuEntry ($id) {
540                 // Set id as current
541                 $this->curr['entry_id'] = $id;
542
543                 // Load the entry template for this page
544                 $this->getTemplateInstance()->loadCodeTemplate('menu_entry_' . $id . '_start');
545
546                 // Set the variable group to page
547                 $this->setVariableGroup('menu');
548
549                 // Set its content in this template instance
550                 $this->assignVariable('entry_start_' . $this->curr['id'], $this->getTemplateInstance()->getRawTemplateData());
551         }
552
553         /**
554          * Finishes the entry node by added another template to the menu
555          *
556          * @return      void
557          */
558         private function finishEntry () {
559                 // Load the entry template for this page
560                 $this->getTemplateInstance()->loadCodeTemplate('menu_entry_' . $this->curr['entry_id'] . '_end');
561
562                 // Set the variable group to page
563                 $this->setVariableGroup('menu');
564
565                 // Set its content in this template instance
566                 $this->assignVariable('entry_end_' . $this->curr['id'], $this->getTemplateInstance()->getRawTemplateData());
567         }
568         /**
569          * Starts the menu property 'anchor'
570          *
571          * @param       $id             Id of the anchor
572          * @param       $link   Link text of the anchor
573          * @param       $title  Link title of the anchor
574          * @return      void
575          */
576         private function startMenuAnchor ($id, $link, $title) {
577                 $this->partialStub('id='.$id.',link='.$link.',title='.$title);
578         }
579
580         /**
581          * Finishes the anchor node by added another template to the menu
582          *
583          * @return      void
584          */
585         private function finishAnchor () {
586                 $this->partialStub('Please implement this method.');
587         }
588
589         /**
590          * Getter for menu cache file (FQFN)
591          *
592          * @return      $fqfn   Full-qualified file name of the menu cache
593          */
594         public function getMenuCacheFqfn () {
595                 // Get the FQFN ready
596                 $fqfn = sprintf("%s%s%s/%s.%s",
597                         $this->getConfigInstance()->getConfigEntry('base_path'),
598                         $this->getGenericBasePath(),
599                         'menus/_cache',
600                         md5(
601                                 $this->getMenuInstance()->getMenuName() . ':' .
602                                 $this->__toString() . ':' .
603                                 $this->getMenuInstance()->__toString()
604                         ),
605                         $this->getMenuInstance()->getMenuType()
606                 );
607
608                 // Return it
609                 return $fqfn;
610         }
611 }
612
613 // [EOF]
614 ?>