Apache Zeta Components Manual :: File Source for xhtml.php

Source for file xhtml.php

Documentation is available at xhtml.php

  1. <?php
  2. /**
  3.  * File containing the ezcTreeVisitorXHTML class.
  4.  *
  5.  * Licensed to the Apache Software Foundation (ASF) under one
  6.  * or more contributor license agreements.  See the NOTICE file
  7.  * distributed with this work for additional information
  8.  * regarding copyright ownership.  The ASF licenses this file
  9.  * to you under the Apache License, Version 2.0 (the
  10.  * "License"); you may not use this file except in compliance
  11.  * with the License.  You may obtain a copy of the License at
  12.  * 
  13.  *   http://www.apache.org/licenses/LICENSE-2.0
  14.  * 
  15.  * Unless required by applicable law or agreed to in writing,
  16.  * software distributed under the License is distributed on an
  17.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18.  * KIND, either express or implied.  See the License for the
  19.  * specific language governing permissions and limitations
  20.  * under the License.
  21.  *
  22.  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  23.  * @version //autogentag//
  24.  * @filesource
  25.  * @package Tree
  26.  */
  27.  
  28. /**
  29.  * An implementation of the ezcTreeVisitor interface that generates
  30.  * an XHTML representatation of a tree structure.
  31.  *
  32.  * <code>
  33.  * <?php
  34.  *     $options = new ezcTreeVisitorXHTMLOptions;
  35.  *     $options->xmlId = 'menu_tree';
  36.  *     $visitor = new ezcTreeVisitorXHTML( $options );
  37.  *     $tree->accept( $visitor );
  38.  *     echo (string) $visitor; // print the plot
  39.  * ?>
  40.  * </code>
  41.  *
  42.  * Shows (something like):
  43.  * <code>
  44.  * </code>
  45.  *
  46.  * @package Tree
  47.  * @version //autogentag//
  48.  */
  49. class ezcTreeVisitorXHTML implements ezcTreeVisitor
  50. {
  51.     /**
  52.      * Holds all the edges of the graph.
  53.      *
  54.      * @var array(string=>array(string)) 
  55.      */
  56.     protected $edges = array();
  57.  
  58.     /**
  59.      * Holds the root ID.
  60.      *
  61.      * @var string 
  62.      */
  63.     protected $root = null;
  64.  
  65.     /**
  66.      * Whether the XML ID has been set.
  67.      *
  68.      * @var bool 
  69.      */
  70.     private $treeIdSet;
  71.  
  72.     /**
  73.      * Holds the options for this class
  74.      *
  75.      * @var ezcTreeVisitorXHTMLOptions 
  76.      */
  77.     public $options;
  78.  
  79.     /**
  80.      * Constructs a new ezcTreeVisitorXHTML visualizer.
  81.      *
  82.      * @param ezcTreeVisitorXHTMLOptions $options 
  83.      */
  84.     public function __constructezcTreeVisitorXHTMLOptions $options null )
  85.     {
  86.         if $options === null )
  87.         {
  88.             $this->options = new ezcTreeVisitorXHTMLOptions;
  89.         }
  90.         else
  91.         {
  92.             $this->options = $options;
  93.         }
  94.     }
  95.  
  96.     /**
  97.      * Formats a node's data.
  98.      *
  99.      * It is just a simple method, that provide an easy way to change the way
  100.      * on how data is formatted when this class is extended. The data is passed
  101.      * in the $data argument, and whether the node should be highlighted is
  102.      * passed in the $highlight argument.
  103.      *
  104.      * @param mixed $data 
  105.      * @param bool  $highlight 
  106.      * @return string 
  107.      */
  108.     protected function formatData$data$highlight )
  109.     {
  110.         $data htmlspecialchars$data );
  111.         return $highlight "<div class=\"highlight\">$data</div>$data;
  112.     }
  113.  
  114.     /**
  115.      * Visits the node and sets the the member variables according to the node
  116.      * type and contents.
  117.      *
  118.      * @param ezcTreeVisitable $visitable 
  119.      * @return bool 
  120.      */
  121.     public function visitezcTreeVisitable $visitable )
  122.     {
  123.         if $visitable instanceof ezcTree )
  124.         {
  125.         }
  126.  
  127.         if $visitable instanceof ezcTreeNode )
  128.         {
  129.             if $this->root === null )
  130.             {
  131.                 $this->root = $visitable->id;
  132.             }
  133.  
  134.             $parent $visitable->fetchParent();
  135.             if $parent )
  136.             {
  137.                 $this->edges[$parent->id][array$visitable->id$visitable->data$visitable->fetchPath() );
  138.             }
  139.         }
  140.  
  141.         return true;
  142.     }
  143.  
  144.     /**
  145.      * Formats the path to the node
  146.      *
  147.      * @param array $child 
  148.      */
  149.     protected function formatPath$child )
  150.     {
  151.         $path $child[2]->nodes;
  152.         if !$this->options->displayRootNode )
  153.         {
  154.             array_shift$path );
  155.         }
  156.         if $this->options->selectedNodeLink )
  157.         {
  158.             $slice array_slice$path-);
  159.             $path htmlspecialchars$this->options->basePath '/' array_pop$slice ) );
  160.         }
  161.         else
  162.         {
  163.             $path htmlspecialchars$this->options->basePath '/' join'/'$path ) );
  164.         }
  165.         return $path;
  166.     }
  167.  
  168.     /**
  169.      * Loops over the children of the node with ID $id.
  170.      *
  171.      * This methods loops over all the node's children and adds the correct
  172.      * layout for each node depending on the state that is collected in the
  173.      * $level and $levelLast variables.
  174.      *
  175.      * @param string $id 
  176.      * @param int    $level 
  177.      * @param array(int=>bool) $levelLast 
  178.      *
  179.      * @return string 
  180.      */
  181.     protected function doChildren$id$level 0$levelLast array() )
  182.     {
  183.         $text '';
  184.  
  185.         $children $this->edges[$id];
  186.         $numChildren count$children );
  187.  
  188.         if $numChildren )
  189.         {
  190.             $text .= str_repeat'  '$level );
  191.  
  192.             $idPart '';
  193.             if !$this->treeIdSet )
  194.             {
  195.                 $idPart $this->options->xmlId " id=\"{$this->options->xmlId}\"" : '';
  196.                 $this->treeIdSet true;
  197.             }
  198.             $text .= "<ul{$idPart}>\n";
  199.             foreach ( $children as $child )
  200.             {
  201.                 $text .= str_repeat( '  ', $level + 2 );
  202.  
  203.                 $path = $this->formatPath$child );
  204.                 $data $this->formatData$child[1]in_array$child[0]$this->options->highlightNodeIds ) );
  205.  
  206.                 $linkStart $linkEnd '';
  207.                 if $this->options->addLinks )
  208.                 {
  209.                     $linkStart = "<a href=\"{$path}\">";
  210.                     $linkEnd   = "</a>";
  211.                 }
  212.  
  213.                 $highlightPart = '';
  214.                 if ( in_array( $child[0], $this->options->subtreeHighlightNodeIds ) )
  215.                 {
  216.                     $highlightPart = ' class="highlight"';
  217.                 }
  218.  
  219.                 if ( isset( $this->edges[$child[0]] ) )
  220.                 {
  221.                     $text .= "<li{$highlightPart}>{$linkStart}{$data}{$linkEnd}\n";
  222.                     $text .= $this->doChildren$child[0]$level 2$levelLast );
  223.                     $text .= str_repeat'  '$level );
  224.                     $text .= "</li>\n";
  225.                 }
  226.                 else
  227.                 {
  228.                     $text .= "<li{$highlightPart}>{$linkStart}{$data}{$linkEnd}</li>\n";
  229.                 }
  230.             }
  231.             $text .= str_repeat( '  ', $level + 1 );
  232.             $text .= "</ul>\n";
  233.         }
  234.  
  235.         return $text;
  236.     }
  237.  
  238.     /**
  239.      * Returns the XHTML representation of a tree.
  240.      *
  241.      * @return string
  242.      * @ignore
  243.      */
  244.     public function __toString()
  245.     {
  246.         $tree = '';
  247.         $this->treeIdSet false;
  248.  
  249.         if $this->options->displayRootNode )
  250.         {
  251.             $idPart = $this->options->xmlId " id=\"{$this->options->xmlId}\"" : '';
  252.             $tree .= "<ul{$idPart}>\n";
  253.             $tree .= "<li>{$this->root}</li>\n";
  254.             $this->treeIdSet true;
  255.         }
  256.         $tree .= $this->doChildren$this->root );
  257.         if $this->options->displayRootNode )
  258.         {
  259.             $tree .= "</ul>\n";
  260.         }
  261.         return $tree;
  262.     }
  263. }
Documentation generated by phpDocumentor 1.4.3