Ver Mensaje Individual
  #3 (permalink)  
Antiguo 13/06/2003, 05:27
Avatar de jama
jama
 
Fecha de Ingreso: junio-2003
Mensajes: 41
Antigüedad: 20 años, 9 meses
Puntos: 0
este es TreeMenu.php:


<?php
class HTML_TreeMenu
{
var $items;


function HTML_TreeMenu()
{
// Not much to do here :(
}


function &addItem(&$node)
{
$this->items[] = &$node;
return $this->items[count($this->items) - 1];
}


function createFromStructure($params)
{
if (!isset($params['nodeOptions'])) {
$params['nodeOptions'] = array();
}

switch (@$params['type']) {

case 'kriesing':
$className = strtolower(get_class($params['structure']->dataSourceClass));
$isXMLStruct = strpos($className,'_xml') !== false ? true : false;

$nodes = $params['structure']->getNode();
$treeMenu = new HTML_TreeMenu();
$curNode[0] = &$treeMenu;

foreach ( $nodes as $aNode ) {
$events = array();
$data = array();

if ( $isXMLStruct && sizeof($aNode['attributes']) ){
foreach ( $aNode['attributes'] as $key=>$val ) {
if ( !$aNode[$key] ) { // dont overwrite existing values
$aNode[$key] = $val;
}
}
}


foreach ( $aNode as $key=>$val ) {
if ( !is_array($val) ) {
// Dont get the recursive data in here! they are always arrays
if ( substr($key,0,2) == 'on' ){ // get the events
$events[$key] = $val;
}

// I put it in data too, so in case an options starts with 'on' its also passed to the node ... not too cool i know
$data[$key] = $val;
}
}

// Normally the text is in 'name' in the Tree class, so we check both but 'text' is used if found
$data['text'] = $aNode['text'] ? $aNode['text'] : $aNode['name'];

// Add the item to the proper node
$thisNode = &$curNode[$aNode['level']]->addItem( new HTML_TreeNode( $data , $events ) );
$curNode[$aNode['level']+1] = &$thisNode;
}
break;

case 'heyes':
default:
// Need to create a HTML_TreeMenu object ?
if (!isset($params['treeMenu'])) {
$treeMenu = &new HTML_TreeMenu();
} else {
$treeMenu = &$params['treeMenu'];
}

// Loop thru the trees nodes
foreach ($params['structure']->nodes->nodes as $node) {
$tag = $node->getTag();
$params['nodeOptions']['link'] = '?control_name=tema_ptid&tema='.$node->id;
$parentNode = &$treeMenu->addItem(new HTML_TreeNode(array_merge($params['nodeOptions'], $tag)));

// Recurse ?
if (!empty($node->nodes->nodes)) {
$recurseParams['structure'] = $node;
$recurseParams['nodeOptions'] = $params['nodeOptions'];
$recurseParams['treeMenu'] = &$parentNode;
HTML_TreeMenu::createFromStructure($recurseParams) ;
}
}
break;

}

return $treeMenu;
}

} // HTML_TreeMenu


class HTML_TreeNode
{

var $text;


var $link;

var $icon;


var $expandedIcon;


var $cssClass;


var $linkTarget;


var $items;


var $expanded;

var $isDynamic;

var $ensureVisible;


var $parent;

var $events;

function HTML_TreeNode($options = array(), $events = array())
{
$this->text = '';
$this->link = '';
$this->icon = '';
$this->expandedIcon = '';
$this->cssClass = '';
$this->expanded = false;
$this->isDynamic = true;
$this->ensureVisible = false;
$this->linkTarget = null;

$this->parent = null;
$this->events = $events;

foreach ($options as $option => $value) {
$this->$option = $value;
}
}

function setOption($option, $value)
{
$this->$option = $value;
}
function setLink($value){
$this->link = '$value';
}


function &addItem(&$node)
{
$node->parent = &$this;
$this->items[] = &$node;


if ($node->ensureVisible) {
$this->_ensureVisible();
}

return $this->items[count($this->items) - 1];
}


function _ensureVisible()
{
$this->ensureVisible = true;
$this->expanded = false;

if (!is_null($this->parent)) {
$this->parent->_ensureVisible();
}
}
}

class HTML_TreeMenu_Presentation
{
/
var $menu;


function HTML_TreeMenu_Presentation(&$structure)
{
$this->menu = &$structure;
}


function printMenu($options = array())
{
foreach ($options as $option => $value) {
$this->$option = $value;
}

echo $this->toHTML();
}
}



class HTML_TreeMenu_DHTML extends HTML_TreeMenu_Presentation
{

var $isDynamic;


var $images;


var $linkTarget;


var $userPersistence;


var $defaultClass;


var $noTopLevelImages;


function HTML_TreeMenu_DHTML(&$structure, $options = array(), $isDynamic = true)
{
$this->HTML_TreeMenu_Presentation($structure);
$this->isDynamic = $isDynamic;

// Defaults
$this->images = 'images';
$this->linkTarget = '_self';
$this->defaultClass = '';
$this->usePersistence = true;
$this->noTopLevelImages = false;

foreach ($options as $option => $value) {
$this->$option = $value;
}
}

/**
* Returns the HTML for the menu. This method can be
* used instead of printMenu() to use the menu system
* with a template system.
*
* @access public
* @return string The HTML for the menu
*/
function toHTML()
{
static $count = 0;
$menuObj = 'objTreeMenu_' . ++$count;

$html = "\n";
$html .= '<script language="javascript" type="text/javascript">' . "\n\t";
$html .= sprintf('%s = new TreeMenu("%s", "%s", "%s", "%s", %s, %s);',
$menuObj,
$this->images,
$menuObj,
$this->linkTarget,
$this->defaultClass,
$this->usePersistence ? 'true' : 'false',
$this->noTopLevelImages ? 'true' : 'false');

$html .= "\n";

/**
* Loop through subnodes
*/
if (isset($this->menu->items)) {
for ($i=0; $i<count($this->menu->items); $i++) {
$html .= $this->_nodeToHTML($this->menu->items[$i], $menuObj);
}
}

$html .= sprintf("\n\t%s.drawMenu();", $menuObj);
if ($this->usePersistence && $this->isDynamic) {
$html .= sprintf("\n\t%s.resetBranches();", $menuObj);
}
$html .= "\n</script>";

return $html;
}

/**
* Prints a node of the menu
*
* @access private
*/
function _nodeToHTML($nodeObj, $prefix, $return = 'newNode')
{
$expanded = $this->isDynamic ? ($nodeObj->expanded ? 'true' : 'false') : 'true';
$isDynamic = $this->isDynamic ? ($nodeObj->isDynamic ? 'true' : 'false') : 'false';
$html = sprintf("\t %s = %s.addItem(new TreeNode('%s', %s, %s, %s, %s, '%s', '%s', %s));\n",
$return,
$prefix,
$nodeObj->text,
!empty($nodeObj->icon) ? "'" . $nodeObj->icon . "'" : 'null',
!empty($nodeObj->link) ? "'" . $nodeObj->link . "'" : 'null',
$expanded,
$isDynamic,
$nodeObj->cssClass,
$nodeObj->linkTarget,
!empty($nodeObj->expandedIcon) ? "'" . $nodeObj->expandedIcon . "'" : 'null');

foreach ($nodeObj->events as $event => $handler) {
$html .= sprintf("\t %s.setEvent('%s', '%s');\n",
$return,
$event,
str_replace(array("\r", "\n", "'"), array('\r', '\n', "\'"), $handler));
}

/**
* Loop through subnodes
*/
if (!empty($nodeObj->items)) {
for ($i=0; $i<count($nodeObj->items); $i++) {
$html .= $this->_nodeToHTML($nodeObj->items[$i], $return, $return . '_' . ($i + 1));
}
}

return $html;
}
} // End class HTML_TreeMenu_DHTML

?>

Perdón, pero debido a lo extenso que es el código he tenido que borrar la mayoria de los comentarios explicativos.

Bueno, después de todo el tocho aquí soltado, he de decir, que la mañana ha sido productiva y he conseguido un resultado más o menos deseado, lo he arreglado introduciendo un parametro de control $cont y un if ($cont==1), añadiendo:
<input type=button onClick="location='?cont=1';" value=Temas>
y añadiendo en

// Loop thru the trees nodes
$params['nodeOptions']['link'] = '?control_name=tema_ptid&cont=0&tema='.$node->id;

así cada vez que elijo un tema $cont=0,
con el if, lo que hago es mostrar la tabla que contiene el árbol.

Supongo que la solución no es muy elegante, pero es lo que se me ha ocurrido y el resultado a mi me gusta, espero que también le guste a mi jefe.
Creí que era bueno poner que ya creo tenerlo, de todas formas, si a alguien se le ocurre una forma mejor, por favor, no dude en ponerlo.

Gracias a tod@s

Última edición por jama; 13/06/2003 a las 05:27