33 |
// | Harald Radi <harald.radi@nme.at> | |
// | Harald Radi <harald.radi@nme.at> | |
34 |
// +-----------------------------------------------------------------------+ |
// +-----------------------------------------------------------------------+ |
35 |
// |
// |
36 |
// Id: TreeMenu.php,v 1.15 2003/02/21 18:19:39 richard Exp |
// Id: TreeMenu.php,v 1.18 2003/12/20 13:10:59 richard Exp |
37 |
// $Id$ |
// $Id$ |
38 |
|
|
39 |
/** |
/** |
101 |
* are Wolfram Kriesings' PEAR Tree class, and Richard Heyes' (me!) |
* are Wolfram Kriesings' PEAR Tree class, and Richard Heyes' (me!) |
102 |
* Tree class (available here: http://www.phpguru.org/). This |
* Tree class (available here: http://www.phpguru.org/). This |
103 |
* method is intended to be used statically, eg: |
* method is intended to be used statically, eg: |
104 |
* $treeMenu = &HTML_TreeMenu::import($myTreeStructureObj); |
* $treeMenu = &HTML_TreeMenu::createFromStructure($myTreeStructureObj); |
105 |
* |
* |
106 |
* @param array $params An array of parameters that determine |
* @param array $params An array of parameters that determine |
107 |
* how the import happens. This can consist of: |
* how the import happens. This can consist of: |
172 |
break; |
break; |
173 |
|
|
174 |
/** |
/** |
175 |
* Richard Heyes' (me!) Tree class |
* Richard Heyes' (me!) second (array based) Tree class |
176 |
|
*/ |
177 |
|
case 'heyes_array': |
178 |
|
// Need to create a HTML_TreeMenu object ? |
179 |
|
if (!isset($params['treeMenu'])) { |
180 |
|
$treeMenu = &new HTML_TreeMenu(); |
181 |
|
$parentID = 0; |
182 |
|
} else { |
183 |
|
$treeMenu = &$params['treeMenu']; |
184 |
|
$parentID = $params['parentID']; |
185 |
|
} |
186 |
|
|
187 |
|
// Loop thru the trees nodes |
188 |
|
foreach ($params['structure']->getChildren($parentID) as $nodeID) { |
189 |
|
$data = $params['structure']->getData($nodeID); |
190 |
|
$parentNode = &$treeMenu->addItem(new HTML_TreeNode(array_merge($params['nodeOptions'], $data))); |
191 |
|
|
192 |
|
// Recurse ? |
193 |
|
if ($params['structure']->hasChildren($nodeID)) { |
194 |
|
$recurseParams['type'] = 'heyes_array'; |
195 |
|
$recurseParams['parentID'] = $nodeID; |
196 |
|
$recurseParams['nodeOptions'] = $params['nodeOptions']; |
197 |
|
$recurseParams['structure'] = &$params['structure']; |
198 |
|
$recurseParams['treeMenu'] = &$parentNode; |
199 |
|
HTML_TreeMenu::createFromStructure($recurseParams); |
200 |
|
} |
201 |
|
} |
202 |
|
|
203 |
|
break; |
204 |
|
|
205 |
|
/** |
206 |
|
* Richard Heyes' (me!) original OO based Tree class |
207 |
*/ |
*/ |
208 |
case 'heyes': |
case 'heyes': |
209 |
default: |
default: |
233 |
|
|
234 |
return $treeMenu; |
return $treeMenu; |
235 |
} |
} |
236 |
|
|
237 |
/** |
/** |
238 |
* Creates a treeMenu from XML. The structure of your XML should be |
* Creates a treeMenu from XML. The structure of your XML should be |
239 |
* like so: |
* like so: |
240 |
* |
* |
241 |
* <treemenu> |
* <treemenu> |
242 |
* <node text="First node" icon="folder.gif" expandedIcon="folder-expanded.gif" /> |
* <node text="First node" icon="folder.gif" expandedIcon="folder-expanded.gif" /> |
243 |
* <node text="Second node" icon="folder.gif" expandedIcon="folder-expanded.gif"> |
* <node text="Second node" icon="folder.gif" expandedIcon="folder-expanded.gif"> |
244 |
* <node text="Sub node" icon="folder.gif" expandedIcon="folder-expanded.gif" /> |
* <node text="Sub node" icon="folder.gif" expandedIcon="folder-expanded.gif" /> |
245 |
* </node> |
* </node> |
246 |
* <node text="Third node" icon="folder.gif" expandedIcon="folder-expanded.gif"> |
* <node text="Third node" icon="folder.gif" expandedIcon="folder-expanded.gif"> |
247 |
* </treemenu> |
* </treemenu> |
248 |
* |
* |
249 |
* Any of the options you can supply to the HTML_TreeNode constructor can be supplied as |
* Any of the options you can supply to the HTML_TreeNode constructor can be supplied as |
250 |
* attributes to the <node> tag. If there are no subnodes for a particular node, you can |
* attributes to the <node> tag. If there are no subnodes for a particular node, you can |
251 |
* use the XML shortcut <node ... /> instead of <node ... ></node>. The $xml argument can |
* use the XML shortcut <node ... /> instead of <node ... ></node>. The $xml argument can |
252 |
* be either the XML as a string, or an pre-created XML_Tree object. Also, this method |
* be either the XML as a string, or an pre-created XML_Tree object. Also, this method |
253 |
* REQUIRES my own Tree class to work (http://phpguru.org/tree.html). If this has not |
* REQUIRES my own Tree class to work (http://phpguru.org/tree.html). If this has not |
254 |
* been include()ed or require()ed this method will die(). |
* been include()ed or require()ed this method will die(). |
255 |
* |
* |
256 |
* @param mixed $xml This can be either a string containing the XML, or an XML_Tree object |
* @param mixed $xml This can be either a string containing the XML, or an XML_Tree object |
257 |
* (the PEAR::XML_Tree package). |
* (the PEAR::XML_Tree package). |
258 |
* @return object The HTML_TreeMenu object |
* @return object The HTML_TreeMenu object |
259 |
*/ |
*/ |
260 |
function createFromXML($xml) |
function createFromXML($xml) |
261 |
{ |
{ |
262 |
if (!class_exists('Tree')) { |
if (!class_exists('Tree')) { |
263 |
die('Could not find Tree class'); |
die('Could not find Tree class'); |
264 |
} |
} |
265 |
|
|
266 |
// Supplied $xml is a string |
// Supplied $xml is a string |
267 |
if (is_string($xml)) { |
if (is_string($xml)) { |
268 |
require_once('XML/Tree.php'); |
require_once('XML/Tree.php'); |
269 |
$xmlTree = &new XML_Tree(); |
$xmlTree = &new XML_Tree(); |
270 |
$xmlTree->getTreeFromString($xml); |
$xmlTree->getTreeFromString($xml); |
271 |
|
|
272 |
// Supplied $xml is an XML_Tree object |
// Supplied $xml is an XML_Tree object |
273 |
} else { |
} else { |
274 |
$xmlTree = $xml; |
$xmlTree = $xml; |
275 |
} |
} |
276 |
|
|
277 |
// Now process the XML_Tree object, setting the XML attributes |
// Now process the XML_Tree object, setting the XML attributes |
278 |
// to be the tag data (with out the XML tag name or contents). |
// to be the tag data (with out the XML tag name or contents). |
279 |
$treeStructure = Tree::createFromXMLTree($xmlTree, true); |
$treeStructure = Tree::createFromXMLTree($xmlTree, true); |
280 |
$treeStructure->nodes->traverse(create_function('&$node', '$tagData = $node->getTag(); $node->setTag($tagData["attributes"]);')); |
$treeStructure->nodes->traverse(create_function('&$node', '$tagData = $node->getTag(); $node->setTag($tagData["attributes"]);')); |
281 |
|
|
282 |
|
|
283 |
return HTML_TreeMenu::createFromStructure(array('structure' => $treeStructure)); |
return HTML_TreeMenu::createFromStructure(array('structure' => $treeStructure)); |
284 |
} |
} |
285 |
} // HTML_TreeMenu |
} // HTML_TreeMenu |
286 |
|
|
287 |
|
|
380 |
* o link The link for the node, defaults to blank |
* o link The link for the node, defaults to blank |
381 |
* o icon The icon for the node, defaults to blank |
* o icon The icon for the node, defaults to blank |
382 |
* o expandedIcon The icon to show when the node is expanded |
* o expandedIcon The icon to show when the node is expanded |
383 |
* o class The CSS class for this node, defaults to blank |
* o cssClass The CSS class for this node, defaults to blank |
384 |
* o expanded The default expanded status of this node, defaults to false |
* o expanded The default expanded status of this node, defaults to false |
385 |
* This doesn't affect non dynamic presentation types |
* This doesn't affect non dynamic presentation types |
386 |
* o linkTarget Target for the links. Defaults to linkTarget of the |
* o linkTarget Target for the links. Defaults to linkTarget of the |
577 |
* is achieved using cookies. Default is true. |
* is achieved using cookies. Default is true. |
578 |
* o noTopLevelImages - Whether to skip displaying the first level of images if |
* o noTopLevelImages - Whether to skip displaying the first level of images if |
579 |
* there is multiple top level branches. |
* there is multiple top level branches. |
580 |
|
* o maxDepth - The maximum depth of indentation. Useful for ensuring |
581 |
|
* deeply nested trees don't go way off to the right of your |
582 |
|
* page etc. Defaults to no limit. |
583 |
* |
* |
584 |
* And also a boolean for whether the entire tree is dynamic or not. |
* And also a boolean for whether the entire tree is dynamic or not. |
585 |
* This overrides any perNode dynamic settings. |
* This overrides any perNode dynamic settings. |
595 |
|
|
596 |
// Defaults |
// Defaults |
597 |
$this->images = 'images'; |
$this->images = 'images'; |
598 |
|
$this->maxDepth = 0; // No limit |
599 |
$this->linkTarget = '_self'; |
$this->linkTarget = '_self'; |
600 |
$this->defaultClass = ''; |
$this->defaultClass = ''; |
601 |
$this->usePersistence = true; |
$this->usePersistence = true; |
617 |
function toHTML() |
function toHTML() |
618 |
{ |
{ |
619 |
static $count = 0; |
static $count = 0; |
620 |
$menuObj = 'objTreeMenu_' . ++$count; |
$menuObj = 'objTreeMenu_' . ++$count; |
621 |
|
|
622 |
$html = "\n"; |
$html = "\n"; |
623 |
$html .= '<script language="javascript" type="text/javascript">' . "\n\t"; |
$html .= '<script language="javascript" type="text/javascript">' . "\n\t"; |
641 |
} |
} |
642 |
} |
} |
643 |
|
|
644 |
$html .= sprintf("\n\t%s.drawMenu();", $menuObj); |
$html .= sprintf("\n\t%s.drawMenu();", $menuObj); |
645 |
|
$html .= sprintf("\n\t%s.writeOutput();", $menuObj); |
646 |
|
|
647 |
if ($this->usePersistence && $this->isDynamic) { |
if ($this->usePersistence && $this->isDynamic) { |
648 |
$html .= sprintf("\n\t%s.resetBranches();", $menuObj); |
$html .= sprintf("\n\t%s.resetBranches();", $menuObj); |
649 |
} |
} |
657 |
* |
* |
658 |
* @access private |
* @access private |
659 |
*/ |
*/ |
660 |
function _nodeToHTML($nodeObj, $prefix, $return = 'newNode') |
function _nodeToHTML($nodeObj, $prefix, $return = 'newNode', $currentDepth = 0, $maxDepthPrefix = null) |
661 |
{ |
{ |
662 |
|
$prefix = empty($maxDepthPrefix) ? $prefix : $maxDepthPrefix; |
663 |
|
|
664 |
$expanded = $this->isDynamic ? ($nodeObj->expanded ? 'true' : 'false') : 'true'; |
$expanded = $this->isDynamic ? ($nodeObj->expanded ? 'true' : 'false') : 'true'; |
665 |
$isDynamic = $this->isDynamic ? ($nodeObj->isDynamic ? 'true' : 'false') : 'false'; |
$isDynamic = $this->isDynamic ? ($nodeObj->isDynamic ? 'true' : 'false') : 'false'; |
666 |
$html = sprintf("\t %s = %s.addItem(new TreeNode('%s', %s, %s, %s, %s, '%s', '%s', %s));\n", |
$html = sprintf("\t %s = %s.addItem(new TreeNode('%s', %s, %s, %s, %s, '%s', '%s', %s));\n", |
667 |
$return, |
$return, |
668 |
$prefix, |
$prefix, |
669 |
$nodeObj->text, |
str_replace("'", "\\'", $nodeObj->text), |
670 |
!empty($nodeObj->icon) ? "'" . $nodeObj->icon . "'" : 'null', |
!empty($nodeObj->icon) ? "'" . $nodeObj->icon . "'" : 'null', |
671 |
!empty($nodeObj->link) ? "'" . $nodeObj->link . "'" : 'null', |
!empty($nodeObj->link) ? "'" . $nodeObj->link . "'" : 'null', |
672 |
$expanded, |
$expanded, |
682 |
str_replace(array("\r", "\n", "'"), array('\r', '\n', "\'"), $handler)); |
str_replace(array("\r", "\n", "'"), array('\r', '\n', "\'"), $handler)); |
683 |
} |
} |
684 |
|
|
685 |
|
if ($this->maxDepth > 0 AND $currentDepth == $this->maxDepth) { |
686 |
|
$maxDepthPrefix = $prefix; |
687 |
|
} |
688 |
|
|
689 |
/** |
/** |
690 |
* Loop through subnodes |
* Loop through subnodes |
691 |
*/ |
*/ |
692 |
if (!empty($nodeObj->items)) { |
if (!empty($nodeObj->items)) { |
693 |
for ($i=0; $i<count($nodeObj->items); $i++) { |
for ($i=0; $i<count($nodeObj->items); $i++) { |
694 |
$html .= $this->_nodeToHTML($nodeObj->items[$i], $return, $return . '_' . ($i + 1)); |
$html .= $this->_nodeToHTML($nodeObj->items[$i], $return, $return . '_' . ($i + 1), $currentDepth + 1, $maxDepthPrefix); |
695 |
} |
} |
696 |
} |
} |
697 |
|
|