Updating the PrinterNavigation system to take an XML structure instead that is much...
authorRobert Sesek <rsesek@bluestatic.org>
Mon, 14 Jan 2008 21:01:38 +0000 (16:01 -0500)
committerRobert Sesek <rsesek@bluestatic.org>
Mon, 14 Jan 2008 21:01:38 +0000 (16:01 -0500)
PrinterNavigation.php

index e5dae90c9102cc643bfa45a9cf14a7fa79da67e8..74d571326c9717379bf54f941026a384d1939fa2 100644 (file)
 class BSPrinterNavigation
 {
        /**
-       * Global links that are used for admin home, logout, etc.
-       * @var  array
-       */
-       private $toplinks = array();
+        * XML document structure
+        * @var SimpleXMLElement
+        */
+       private $structure;
        
        /**
-       * Navigational tabs: array(text, url)
-       * @var  array
-       */
-       private $tabs = array();
+        * Keys to set focus on
+        * @var array
+        */
+       private $focusKeys = array();
        
        /**
-       * Sections: text
-       * @var  array
-       */
-       private $sections = array();
+        * Sections to display
+        * @var array
+        */
+       private $displaySections = array();
        
        /**
-       * Links: array(text, url)
-       * @var  array
-       */
-       private $links = array();
-       
-       /**
-       * Array of scopes to set focus to key
-       * @var  array
-       */
-       private $focus = array('tab' => null, 'link' => null);
-       
-       // ###################################################################
-       /**
-       * Adds a global link to the array; these cannot be removed once added
-       *
-       * @param        string  Link text
-       * @param        string  HREF of the URL
-       */
-       public function addTopLink($text, $href)
+        * Constructor
+        * 
+        * @param       string  Navigation XML data
+        */
+       public function __construct($xml)
        {
-               $this->toplinks["$href"] = $text;
+               $this->structure = new SimpleXMLElement($xml);
        }
        
-       // ###################################################################
        /**
-       * Adds to the structure array. The following is the global structure 
-       * of the array, but you can specify any entry point. When you add onto
-       * the structure, it will go at the bottom
-       * array(
-       *       '<tab>unique_key'       => array(
-       *               '<section>unique_key'   => array(
-       *                       '<link>unique_key'      => array('text', 'url')
-       *               )
-       *       )
-       * )
-       *
-       * Note that the portion in brackets is called the "scope"
-       *
-       * @param        string  Scope to add to
-       * @param        string  Unique key for the scope
-       * @param        string  Parent key to add to
-       * @param        string  Text to display (usually localized)
-       * @param        string  URL to go to when text is clicked
-       */
-       public function addComponent($scope, $key, $parent, $text, $url)
+        * Adds focus to a given key
+        *
+        * @param       string Key
+        */
+       public function addFocus($key)
        {
-               if ($scope == 'tab')
-               {
-                       $this->tabs["$key"] = array($text, $url);
-               }
-               else if ($scope == 'section')
-               {
-                       $this->sections["$parent"]["$key"] = $text;
-               }
-               else if ($scope == 'link')
-               {
-                       $this->links["$parent"]["$key"] = array($text, $url);
-               }
+               $this->focusKeys[] = $key;
        }
        
-       // ###################################################################
        /**
-       * Sets the focus for either a tab or link
-       *
-       * @param        string  Scope operator
-       * @param        string  Unique key in scope
-       * @param        string  Parent operator (links only)
-       */
-       public function setFocus($scope, $key, $parent)
+        * Adds a section to display. The order in which sections are added is the order
+        * in which they are painted
+        *
+        * @param       string  Key
+        */
+       public function addSection($key)
        {
-               if ($scope == 'tab')
-               {
-                       if (isset($this->tabs["$key"]))
-                       {
-                               $this->focus["$scope"] = $key;
-                       }
-                       else
-                       {
-                               throw new Exception('Invalid key for scope');
-                       }
-               }
-               else if ($scope == 'link')
-               {
-                       if (isset($this->links["$parent"]["$key"]))
-                       {
-                               $this->focus["$scope"] = $key;
-                       }
-                       else
-                       {
-                               throw new Exception('Invalid key for scope');
-                       }
-               }
-               else
-               {
-                       throw new Exception('BSPrinterNavigation::setFocus() only allows setting of focus for tab and link scopes');
-               }
+               $this->displaySections[] = $key;
        }
-       
-       // ###################################################################
+               
        /**
-       * Generates the header HTML that is called in BSPrinterRootElementPage
-       * to setup the navigation frame
-       *
-       * @return       string  Generated HTML content
-       */
+        * Generates the header HTML that is called in BSPrinterRootElementPage
+        * to setup the navigation frame
+        *
+        * @return      string  Generated HTML content
+        */
        public function constructHeaderHtml()
        {
                $output = '<!-- navigation start -->' . "\n\n";
@@ -171,9 +102,10 @@ class BSPrinterNavigation
                // -------------------------------------------------------------------
                
                $output2 = array();
-               foreach ($this->toplinks AS $href => $text)
+               foreach ($this->structure->links->link as $link)
                {
-                       $output2[] = '<a href="' . $href . '">' . $text . '</a>';
+                       $attrs = $link->attributes();
+                       $output2[] = '<a href="' . $attrs->target . '">' . $link . '</a>';
                }
                
                $output .= "\n" . '<div id="toplinks">';
@@ -188,15 +120,15 @@ class BSPrinterNavigation
                // -------------------------------------------------------------------
                
                $output .= "\n" . '<div id="tabbar">';
-               foreach ($this->tabs AS $key => $content)
+               foreach ($this->structure->tabs->tab as $tab)
                {
-                       $link = "\n\t" . '<a href="' . $content[1] . '"';
-                       if ($this->focus['tab'] == $key)
+                       $link = "\n\t" . '<a href="' . $tab['target'] . '"';
+                       if (in_array($tab['key'], $this->focusKeys))
                        {
                                $link .= ' id="focustab"';
                        }
                        
-                       $link .= '><span>' . $content[0] . '</span></a>';
+                       $link .= '><span>' . $tab . '</span></a>';
                        
                        $output .= $link;
                }
@@ -210,13 +142,46 @@ class BSPrinterNavigation
                // -------------------------------------------------------------------
                
                $output .= "\n" . '<td id="menu">';
-               foreach ((array)$this->sections[ $this->focus['tab'] ] AS $key => $text)
+               foreach ($this->structure->sections->section as $section)
                {
+                       if (!in_array($section['key'], $this->displaySections))
+                       {
+                               continue;
+                       }
+                       
+                       // handle inherited sections
+                       $links = array();
+                       if ($section['inherits'])
+                       {
+                               $keys = explode(',', $section['inherits']);
+                               
+                               foreach ($keys as $parKey)
+                               {
+                                       $parLinks = $this->structure->xpath('/navigation/sections/section[@key="' . $parKey . '"]/link');
+                                       $links = array_merge($links, $parLinks);
+                               }
+                               
+                               // add the links from the hybrid section
+                               if (is_array($section->link))
+                               {
+                                       $links = array_merge($links, $section->link);
+                               }
+                               else
+                               {
+                                       $links[] = $section->link;
+                               }
+                       }
+                       // no inheritance, so the links are straight from the section
+                       else
+                       {
+                               $links = $section->link;
+                       }
+                       
                        $output .= "\n" . '<ul>';
-                       $output .= "\n\t" . '<li class="header"><span>' . $text . '</span></li>';
-                       foreach ((array)$this->links["$key"] AS $key2 => $content)
+                       $output .= "\n\t" . '<li class="header"><span>' . $section['name'] . '</span></li>';
+                       foreach ($links as $link)
                        {
-                               $output .= "\n\t" . '<li' . ($this->focus['link'] == $key2 ? ' class="focus"' : '') . '><a href="' . $content[1] . '"><span>' . $content[0] . '</span></a></li>';
+                               $output .= "\n\t" . '<li' . (in_array($link['key'], $this->focusKeys) ? ' class="focus"' : '') . '><a href="' . $link['target'] . '"><span>' . $link . '</span></a></li>';
                        }
                        $output .= "\n" . '</ul>' . "\n";
                }
@@ -230,13 +195,12 @@ class BSPrinterNavigation
                return $output;
        }
        
-       // ###################################################################
        /**
-       * Generates the HTML that is inserted in BSPrinterRootElementPage that
-       * closes all of the navigation HTML stuff
-       *
-       * @return       string  Generated HTML content
-       */
+        * Generates the HTML that is inserted in BSPrinterRootElementPage that
+        * closes all of the navigation HTML stuff
+        *
+        * @return      string  Generated HTML content
+        */
        public function constructFooterHtml()
        {
                $output = '';