[ Index ] |
|
Code source de SPIP Agora 1.4 |
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,v 1.3 2004/04/02 00:15:06 datenpunk Exp $ 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: 1.3 $ 35 */ 36 class DB_NestedSet_GraphViz extends DB_NestedSet_Output 37 { 38 /** 39 * @var array The current menu structure 40 * 41 * @access private 42 */ 43 var $_structTreeMenu = false; 44 45 function DB_NestedSet_GraphViz($params) 46 { 47 $this->_structTreeMenu = &$this->_createFromStructure($params); 48 } 49 50 /** 51 * Creates an Image_GraphViz graph based off of the results from getAllNodes() method 52 * of the DB_NestedSet class. The needed parameters are: 53 * <li> 54 * 'structure' => the result from $nestedSet->getAllNodes(true) 55 * 'nodeLabel' => the text to show in the box reprenting the node 56 * </li> 57 * 58 * @access public 59 * @return object An Image_GraphViz object 60 */ 61 function &_createFromStructure($params) 62 { 63 // Basically we go through the array of nodes checking to see 64 // if each node has children and if so recursing. The reason this 65 // works is because the data from getAllNodes() is ordered by level 66 // so a root node will always be first, and sub children will always 67 // be after them. 68 if (!isset($params['graphViz'])) { 69 $graph = &new Image_GraphViz(); 70 } else { 71 $graph = &$params['graphViz']; 72 } 73 74 // always start at level 1 75 if (!isset($params['currentLevel'])) { 76 $params['currentLevel'] = 1; 77 } 78 79 // have to use a while loop here because foreach works on a copy of the array and 80 // the child nodes are passed by reference during the recursion so that the parent 81 // will know when they have been hit. 82 reset($params['structure']); 83 while (list($key, $node) = each($params['structure'])) { 84 // see if we've already been here before 85 if (isset($node['hit'])) { 86 continue; 87 } 88 89 // mark that we've hit this node 90 $params['structure'][$key]['hit'] = $node['hit'] = true; 91 92 $graph->addNode( 93 $node['id'], 94 array('label' => $node[$params['nodeLabel']], 95 'shape' => 'box' 96 ) 97 ); 98 99 // if the node has a parent then we add an arrow from the parent 100 // to the child 101 if ($node['parent'] != 0) { 102 $graph->addEdge( 103 array($node['parent'] => $node['id']), 104 array('label' => $node['edgeLabel']) 105 ); 106 } 107 108 // see if it has children 109 if (($node['r'] - 1) != $node['l']) { 110 $children = array(); 111 // harvest all the children 112 $tempStructure = $params['structure']; 113 foreach ($tempStructure as $childKey => $childNode) { 114 if (!isset($childNode['hit']) && 115 $childNode['l'] > $node['l'] && 116 $childNode['r'] < $node['r'] && 117 $childNode['rootid'] == $node['rootid']) { 118 // important that we assign it by reference here, so that when the child 119 // marks itself 'hit' the parent loops will know 120 $children[] = &$params['structure'][$childKey]; 121 } 122 } 123 124 $recurseParams = $params; 125 $recurseParams['structure'] = $children; 126 $recurseParams['graphViz'] = &$graph; 127 $recurseParams['currentLevel']++; 128 $this->_createFromStructure($recurseParams); 129 } 130 } 131 132 return $graph; 133 } 134 135 /** 136 * Outputs the graph as an image 137 * 138 * @access public 139 * @param string absolute path to the dot command 140 * @param string output image format 141 * @return void 142 */ 143 function printTree($dot_command = null, $output_format = 'png') 144 { 145 if (!is_null($dot_command)) { 146 $this->_structTreeMenu->dotCommand = $dot_command; 147 } 148 $this->_structTreeMenu->image($output_format); 149 } 150 } 151 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 14:40:03 2007 | par Balluche grâce à PHPXref 0.7 |