MenuTemplateEngine heavily extended, but still in non-working state
[core.git] / inc / classes / main / template / menu / class_MenuTemplateEngine.php
index 9f179c0f4e8928e0d0c9f2272ea4bb5a8d159340..0dbd2cb806bcaf7799a99ab2c2ade74d3415b3eb 100644 (file)
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
+       /**
+        * Main nodes in the XML tree ('menu' is ignored)
+        */
+       private $mainNodes = array(
+               'header',
+               'entries',
+               'footer',
+       );
+
+       /**
+        * Sub nodes in the XML tree
+        */
+       private $subNodes = array(
+               'title',
+               'entry',
+               'anchor',
+               'design',
+               'text',
+               'advert',
+       );
+
+       /**
+        * Menu instance
+        */
+       private $menuInstance = null;
+
+       /**
+        * Current main node
+        */
+       private $currMainNode = '';
+
        /**
         * Protected constructor
         *
@@ -36,6 +67,7 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
         * Creates an instance of the class TemplateEngine and prepares it for usage
         *
         * @param       $appInstance    A manageable application
+        * @param       $menuInstance   A RenderableMenu instance
         * @return      $tplInstance    An instance of TemplateEngine
         * @throws      BasePathIsEmptyException                If the provided $templateBasePath is empty
         * @throws      InvalidBasePathStringException  If $templateBasePath is no string
@@ -44,7 +76,7 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
         * @throws      BasePathReadProtectedException  If $templateBasePath is
         *                                                                                      read-protected
         */
-       public final static function createMenuTemplateEngine (ManageableApplication $appInstance) {
+       public final static function createMenuTemplateEngine (ManageableApplication $appInstance, RenderableMenu $menuInstance) {
                // Get a new instance
                $tplInstance = new MenuTemplateEngine();
 
@@ -87,6 +119,9 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
                // Absolute output path for compiled templates
                $tplInstance->setCompileOutputPath($configInstance->getConfigEntry('base_path') . $configInstance->getConfigEntry('compile_output_path'));
 
+               // Set the menu instance
+               $tplInstance->setMenuInstance($menuInstance);
+
                // Return the prepared instance
                return $tplInstance;
        }
@@ -105,6 +140,154 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
                // Load the special template
                $this->loadTemplate($template);
        }
+
+       /**
+        * Getter for current main node
+        *
+        * @return      $currMainNode   Current main node
+        */
+       public final function getCurrMainNode () {
+               return $this->currMainNode;
+       }
+
+       /**
+        * Getter for main node array
+        *
+        * @return      $mainNodes      Array with valid main node names
+        */
+       public final function getMainNodes () {
+               return $this->mainNodes;
+       }
+
+       /**
+        * Getter for sub node array
+        *
+        * @return      $subNodes       Array with valid sub node names
+        */
+       public final function getSubNodes () {
+               return $this->subNodes;
+       }
+
+       /**
+        * Handles the start element of an XML resource
+        *
+        * @param       $resource               XML parser resource (currently ignored)
+        * @param       $element                The element we shall handle
+        * @param       $attributes             All attributes
+        * @return      void
+        * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
+        */
+       public function startElement ($resource, $element, array $attributes) {
+               // Initial method name which will never be called...
+               $methodName = 'initMenu';
+
+               // Make the element name lower-case
+               $element = strtolower($element);
+
+               // Is the element a main node?
+               /* DEBUG: */ echo "START: &gt;".$element."&lt;<br />\n";
+               if (in_array($element, $this->getMainNodes())) {
+                       // Okay, main node found!
+                       $methodName = 'startMenu' . $this->convertToClassName($element);
+
+                       // Set it
+                       $this->currMainNode = $element;
+               } elseif (in_array($element, $this->getSubNodes())) {
+                       // Sub node found
+                       $methodName = 'setMenuProperty' . $this->convertToClassName($element);
+               } elseif ($element != 'menu') {
+                       // Invalid node name found
+                       throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
+               }
+
+               // Call method
+               /* DEBUG: */ echo "call: ".$methodName."<br />\n";
+               call_user_func_array(array($this, $methodName), $attributes);
+       }
+
+       /**
+        * Ends the main or sub node by sending out the gathered data
+        *
+        * @param       $resource       An XML resource pointer (currently ignored)
+        * @param       $nodeName       Name of the node we want to finish
+        * @return      void
+        * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
+        */
+       public function endElement ($resource, $nodeName) {
+               // Make all lower-case
+               $nodeName = strtolower($nodeName);
+
+               // Does this match with current main node?
+               /* DEBUG: */ echo "END: &gt;".$nodeName."&lt;<br />\n";
+               if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
+                       // Did not match!
+                       throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
+               } elseif (in_array($nodeName, $this->getSubNodes())) {
+                       // Silently ignore sub nodes
+                       return;
+               }
+
+               // Construct method name
+               $methodName = 'finish' . $this->convertToClassName($nodeName);
+
+               // Call the corresponding method
+               /* DEBUG: */ echo "call: ".$methodName."<br />\n";
+               call_user_func_array(array($this->getMenuInstance(), $methodName), array());
+       }
+
+       /**
+        * Currently not used
+        *
+        * @param       $resource               XML parser resource (currently ignored)
+        * @param       $characters             Characters to handle
+        * @return      void
+        * @todo        Find something usefull with this!
+        */
+       public function characterHandler ($resource, $characters) {
+               // Trim all spaces away
+               $characters = trim($characters);
+
+               // Is this string empty?
+               if (empty($characters)) {
+                       // Then skip it silently
+                       return false;
+               } // END - if
+
+               // Unfinished work!
+               $this->partialStub("Handling extra characters is not yet supported!");
+       }
+
+       /**
+        * Intializes the menu
+        *
+        * @return      void
+        * @todo        Add cache creation here
+        */
+       private function initMenu () {
+               // Unfinished work!
+               $this->partialStub('Please implement this method.');
+       }
+
+       /**
+        * Getter for menu cache file (FQFN)
+        *
+        * @return      $fqfn   Full-qualified file name of the menu cache
+        */
+       public function getMenuCacheFqfn () {
+               // Get the FQFN ready
+               $fqfn = sprintf("%s%s%s/%s.%s",
+                       $this->getConfigInstance()->getConfigEntry('base_path'),
+                       $this->getGenericBasePath(),
+                       'menus/_cache',
+                       md5(
+                               $this->getMenuInstance()->getMenuName() . ':' . $this->__toString() . ':' . $this->getMenuInstance()->__toString()
+                       ),
+                       $this->getMenuInstance()->getMenuType()
+               );
+
+               // Return it
+               return $fqfn;
+       }
 }
 
 // [EOF]