DB_NestedSet
[ class tree: DB_NestedSet ] [ index: DB_NestedSet ] [ all elements ]

Source for file GraphViz.php

Documentation is available at GraphViz.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PEAR :: DB_NestedSet_TreeMenu                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Jason Rust <jrust@rustyparts.com>                           |
  17. // |          Arnaud Limbourg <arnaud@php.net>                            |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: GraphViz.php 155194 2004-04-02 00:15:06Z datenpunk $
  21. //
  22.  
  23. require_once 'Image/GraphViz.php';
  24.  
  25. /**
  26.  * A helper class to translate the data from a nested set table into a
  27.  * GraphViz diagram using PEAR::Image_GraphViz developped by Sebastian Bergmann.
  28.  *
  29.  * Based on DB_NestedSet_TreeMenu to a very large extent.
  30.  *
  31.  * @author       Jason Rust <jrust@rustyparts.com>
  32.  * @author       Arnaud Limbourg <arnaud@php.net>
  33.  * @package      DB_NestedSet
  34.  * @version      $Revision: 155194 $
  35.  */
  36. {
  37.     /**
  38.      * @var array The current menu structure
  39.      *
  40.      * @access private
  41.      */
  42.     var $_structTreeMenu = false;
  43.  
  44.     function DB_NestedSet_GraphViz($params)
  45.     {
  46.         $this->_structTreeMenu &$this->_createFromStructure($params);
  47.     }
  48.  
  49.     /**
  50.      * Creates an Image_GraphViz graph based off of the results from getAllNodes() method
  51.      * of the DB_NestedSet class.  The needed parameters are:
  52.      * <li>
  53.      * 'structure' => the result from $nestedSet->getAllNodes(true)
  54.      * 'nodeLabel' => the text to show in the box reprenting the node
  55.      * </li>
  56.      *
  57.      * @access public
  58.      * @return object An Image_GraphViz object
  59.      */
  60.     function &_createFromStructure($params)
  61.     {
  62.         // Basically we go through the array of nodes checking to see
  63.         // if each node has children and if so recursing.  The reason this
  64.         // works is because the data from getAllNodes() is ordered by level
  65.         // so a root node will always be first, and sub children will always
  66.         // be after them.
  67.         if (!isset($params['graphViz'])) {
  68.             $graph &new Image_GraphViz();
  69.         else {
  70.             $graph &$params['graphViz'];
  71.         }
  72.  
  73.         // always start at level 1
  74.         if (!isset($params['currentLevel'])) {
  75.             $params['currentLevel'= 1;
  76.         }
  77.  
  78.         // have to use a while loop here because foreach works on a copy of the array and
  79.         // the child nodes are passed by reference during the recursion so that the parent
  80.         // will know when they have been hit.
  81.         reset($params['structure']);
  82.         while (list($key$nodeeach($params['structure'])) {
  83.             // see if we've already been here before
  84.             if (isset($node['hit'])) {
  85.                 continue;
  86.             }
  87.  
  88.             // mark that we've hit this node
  89.             $params['structure'][$key]['hit'$node['hit'= true;
  90.  
  91.             $graph->addNode(
  92.                 $node['id'],
  93.                 array('label' => $node[$params['nodeLabel']],
  94.                       'shape' => 'box'
  95.                 )
  96.             );
  97.  
  98.             // if the node has a parent then we add an arrow from the parent
  99.             // to the child
  100.             if ($node['parent'!= 0{
  101.                 $graph->addEdge(
  102.                     array($node['parent'=> $node['id']),
  103.                     array('label' => $node['edgeLabel'])
  104.                 );
  105.             }
  106.  
  107.             // see if it has children
  108.             if (($node['r'- 1!= $node['l']{
  109.                 $children = array();
  110.                 // harvest all the children
  111.                 $tempStructure $params['structure'];
  112.                 foreach ($tempStructure as $childKey => $childNode{
  113.                     if (!isset($childNode['hit']&&
  114.                     $childNode['l'$node['l'&&
  115.                     $childNode['r'$node['r'&&
  116.                     $childNode['rootid'== $node['rootid']{
  117.                         // important that we assign it by reference here, so that when the child
  118.                         // marks itself 'hit' the parent loops will know
  119.                         $children[&$params['structure'][$childKey];
  120.                     }
  121.                 }
  122.  
  123.                 $recurseParams $params;
  124.                 $recurseParams['structure'$children;
  125.                 $recurseParams['graphViz']  &$graph;
  126.                 $recurseParams['currentLevel']++;
  127.                 $this->_createFromStructure($recurseParams);
  128.             }
  129.         }
  130.  
  131.         return $graph;
  132.     }
  133.  
  134.     /**
  135.      * Outputs the graph as an image
  136.      *
  137.      * @access public
  138.      * @param  string absolute path to the dot command
  139.      * @param  string output image format
  140.      * @return void 
  141.      */
  142.     function printTree($dot_command = null$output_format 'png')
  143.     {
  144.         if (!is_null($dot_command)) {
  145.             $this->_structTreeMenu->dotCommand = $dot_command;
  146.         }
  147.         $this->_structTreeMenu->image($output_format);
  148.     }
  149. }
  150. ?>

Documentation generated on Mon, 11 Mar 2019 15:37:09 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.