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

Source for file yui.php

Documentation is available at yui.php

  1. <?php
  2. /**
  3.  * File containing the ezcTreeVisitorYUI 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, as YUI wants.
  31.  * See {@link http://developer.yahoo.com/yui/menu}.
  32.  *
  33.  * <code>
  34.  * <?php
  35.  *     $visitor = new ezcTreeVisitorYUI( 'menu' );
  36.  *     $tree->accept( $visitor );
  37.  *     echo (string) $visitor; // print the plot
  38.  * ?>
  39.  * </code>
  40.  *
  41.  * @package Tree
  42.  * @version //autogentag//
  43.  */
  44. class ezcTreeVisitorYUI implements ezcTreeVisitor
  45. {
  46.     /**
  47.      * Holds all the edges of the graph.
  48.      *
  49.      * @var array(string=>array(string)) 
  50.      */
  51.     protected $edges = array();
  52.  
  53.     /**
  54.      * Holds the root ID.
  55.      *
  56.      * @var string 
  57.      */
  58.     protected $root = null;
  59.  
  60.     /**
  61.      * Holds the XML ID.
  62.      *
  63.      * @var string 
  64.      */
  65.     protected $xmlId;
  66.  
  67.     /**
  68.      * Holds the XHTML class.
  69.      *
  70.      * @var string 
  71.      */
  72.     protected $class;
  73.  
  74.     /**
  75.      * Whether the XML ID has been set.
  76.      *
  77.      * @var bool 
  78.      */
  79.     private $treeIdSet;
  80.  
  81.     /**
  82.      * Holds the options for this class
  83.      *
  84.      * @var ezcTreeVisitorYUIOptions 
  85.      */
  86.     public $options;
  87.  
  88.     /**
  89.      * Constructs a new ezcTreeVisitorYUI visualizer.
  90.      *
  91.      * @param string $xmlId 
  92.      * @param ezcTreeVisitorYUIOptions $options 
  93.      */
  94.     public function __construct$xmlIdezcTreeVisitorYUIOptions $options null )
  95.     {
  96.         if !is_string$xmlId || strlen$xmlId === )
  97.         {
  98.             throw new ezcBaseValueException'xmlId'$xmlId'non-empty string' );
  99.         }
  100.         $this->xmlId = $xmlId;
  101.         if $options === null )
  102.         {
  103.             $this->options = new ezcTreeVisitorYUIOptions;
  104.         }
  105.         else
  106.         {
  107.             $this->options = $options;
  108.         }
  109.     }
  110.  
  111.     /**
  112.      * Formats a node's data.
  113.      *
  114.      * It is just a simple method, that provide an easy way to change the way
  115.      * on how data is formatted when this class is extended. The data is passed
  116.      * in the $data argument, and whether the node should be highlighted is
  117.      * passed in the $highlight argument.
  118.      *
  119.      * @param mixed $data 
  120.      * @param bool  $highlight 
  121.      * @return string 
  122.      */
  123.     protected function formatData$data$highlight )
  124.     {
  125.         $data htmlspecialchars$data );
  126.         return $data;
  127.     }
  128.  
  129.     /**
  130.      * Visits the node and sets the the member variables according to the node
  131.      * type and contents.
  132.      *
  133.      * @param ezcTreeVisitable $visitable 
  134.      * @return bool 
  135.      */
  136.     public function visitezcTreeVisitable $visitable )
  137.     {
  138.         if $visitable instanceof ezcTreeNode )
  139.         {
  140.             if $this->root === null )
  141.             {
  142.                 $this->root = $visitable->id;
  143.             }
  144.  
  145.             $parent $visitable->fetchParent();
  146.             if $parent )
  147.             {
  148.                 $this->edges[$parent->id][array$visitable->id$visitable->data$visitable->fetchPath() );
  149.             }
  150.         }
  151.  
  152.         return true;
  153.     }
  154.  
  155.     /**
  156.      * Loops over the children of the node with ID $id.
  157.      *
  158.      * This methods loops over all the node's children and adds the correct
  159.      * layout for each node depending on the state that is collected in the
  160.      * $level and $levelLast variables.
  161.      *
  162.      * @param string $id 
  163.      * @param int    $level 
  164.      * @param array(int=>bool) $levelLast 
  165.      *
  166.      * @return string 
  167.      */
  168.     protected function doChildren$id$level 0$levelLast array() )
  169.     {
  170.         $text '';
  171.  
  172.         $children $this->edges[$id];
  173.         $numChildren count$children );
  174.  
  175.         if $numChildren )
  176.         {
  177.             $text .= str_repeat'  '$level );
  178.  
  179.             $idPart '';
  180.             if $level !== )
  181.             {
  182.                 $text .= "<div id='{$id}' class='yuimenu'>\n";
  183.             }
  184.             $text .= str_repeat'  '$level );
  185.             $text .= "<div class='bd'>\n";
  186.             $text .= str_repeat'  '$level );
  187.             $text .= "<ul>\n";
  188.             foreach $children as $child )
  189.             {
  190.                 $path $child[2]->nodes;
  191.                 if !$this->options->displayRootNode )
  192.                 {
  193.                     array_shift$path );
  194.                 }
  195.                 if $this->options->selectedNodeLink )
  196.                 {
  197.                     $slice array_slice$path-);
  198.                     $path htmlspecialchars$this->options->basePath '/' array_pop$slice )ENT_QUOTES );
  199.                 }
  200.                 else
  201.                 {
  202.                     $path htmlspecialchars$this->options->basePath '/' join'/'$path )ENT_QUOTES );
  203.                 }
  204.                 $text .= str_repeat'  '$level );
  205.  
  206.                 $data $this->formatData$child[1]in_array$child[0]$this->options->highlightNodeIds ) );
  207.  
  208.                 $yuiItemClass =      $level == 'yuimenubaritem' 'yuimenuitem';
  209.                 $yuiItemLabelClass $level == 'yuimenubaritemlabel' 'yuimenuitemlabel';
  210.  
  211.                 $highlightPart '';
  212.                 if in_array$child[0]$this->options->highlightNodeIds ) )
  213.                 {
  214.                     $highlightPart ' highlight';
  215.                 }
  216.  
  217.                 $linkStart "<a class='{$yuiItemLabelClass}{$highlightPart}' href='{$path}'>";
  218.                 $linkEnd   "</a>";
  219.  
  220.                 if isset$this->edges[$child[0]] ) )
  221.                 {
  222.                     $text .= "<li class='{$yuiItemClass}'>{$linkStart}{$data}{$linkEnd}\n";
  223.                     $text .= $this->doChildren$child[0]$level 4$levelLast );
  224.                     $text .= str_repeat'  '$level );
  225.                     $text .= "</li>\n";
  226.                 }
  227.                 else
  228.                 {
  229.                     $text .= "<li class='{$yuiItemClass}'>{$linkStart}{$data}{$linkEnd}</li>\n";
  230.                 }
  231.             }
  232.             $text .= str_repeat'  '$level );
  233.             $text .= "</ul>\n";
  234.             $text .= str_repeat'  '$level );
  235.             if $level !== )
  236.             {
  237.                 $text .= "</div>\n";
  238.                 $text .= str_repeat'  '$level );
  239.             }
  240.             $text .= "</div>\n";
  241.         }
  242.  
  243.         return $text;
  244.     }
  245.  
  246.     /**
  247.      * Returns the XHTML representation of a tree.
  248.      *
  249.      * @return string 
  250.      * @ignore
  251.      */
  252.     public function __toString()
  253.     {
  254.         $tree '';
  255.         $this->treeIdSet false;
  256.  
  257.         $idPart " id=\"{$this->xmlId}\"";
  258.         $tree .= "<div{$idPart} class='yuimenubar yuimenubarnav'>\n";
  259.         if ( $this->options->displayRootNode )
  260.         {
  261.             $tree .= <<<END
  262.   <div class='bd'>
  263.     <ul>
  264.       <li class='yuimenubaritem'><a class='yuimenubaritemlabel' href='/Hominoidea/Hylobatidae'>{$this->root}</a>
  265.  
  266. END;
  267.         }
  268.         $tree .= $this->doChildren$this->root* (bool) $this->options->displayRootNode );
  269.         if $this->options->displayRootNode )
  270.         {
  271.             $tree .= <<<END
  272.       </li>
  273.     </ul>
  274.   </div>
  275.  
  276. END;
  277.         }
  278.         $tree .= "</div>\n";
  279.         return $tree;
  280.     }
  281. }
Documentation generated by phpDocumentor 1.4.3