-------------------------------------------------------------------------------
Creado: jue jul 17 15:32:52 ART 2003
Autor: Gonzalo Merayo <gmeray@mecon.gov.ar>
+ Leandro Lucarella <llucar@mecon.gov.ar>
-------------------------------------------------------------------------------
$Id$
-----------------------------------------------------------------------------*/
class HTML_Arbol extends HTML_Table
{
- var $t_interna;
+ var $datos;
+ var $titulo;
+ var $link_append;
- function HTML_Arbol($dat, $titulo_str)
+ /**
+ * Constructor.
+ *
+ * @param array $datos Datos con los cuales construir el árbol.
+ * @param string $titulo Título.
+ * @param int $raiz Nodo raíz (de donde empezar a dibujar el árbol).
+ */
+ function HTML_Arbol($datos, $titulo, $link_append = '')
{
- parent::HTML_Table(array ('width'=>'132',
- 'border' => '0',
- 'cellspacing' => '0',
- 'cellpadding' => '0',
- 'bgcolor' => '#003868'));
- $this->t_interna = new HTML_Table(array ('width'=>'132',
- 'border' => '0',
- 'cellspacing' => '2',
- 'cellpadding' => '0',
- 'class' => 'bodytext'));
- $titulo = new HTML_Table(array('width'=>'132',
- 'height'=>'26',
- 'border' => '0',
- 'cellspacing' => '0',
- 'cellpadding' => '0',
- 'align'=>'center',
- 'background'=>'/MECON/images/arbol_titulo.gif'));
- $titulo->addRow(array($titulo_str), array('align'=>'center',
- 'class'=>'arboltitulo'));
- $this->addRow(array($titulo), array('bgcolor' => '#FFFFFF'));
- $this->expandir($dat, 0);
+ parent::HTML_Table(array(
+ 'width' => '132',
+ 'border' => '0',
+ 'cellspacing' => '0',
+ 'cellpadding' => '0',
+ 'bgcolor' => '#003868'));
+ $this->datos = $datos;
+ $this->titulo = $titulo;
+ $this->link_append = $link_append;
}
-
- function expandir($dat, $n)
+
+ function getCSS() {
+ return '/MECON/css/arbol';
+ }
+
+ function expandir($dat, $n, &$tabla)
{
$imagen = '';
- $bullets = array(
- '',
- '/MECON/images/arbol_bullet_1.gif',
- '/MECON/images/arbol_bullet_2.gif',
- '/MECON/images/arbol_bullet_3.gif'
- );
- $tabulados = 7;
- $classes = array('menu', 'menu1', 'menu1', 'menu2');
-
- $atr = array('border' => '0',
- 'width' => $n * $tabulados,
- 'height' => '10');
- $imagen =& new HTML_Image(@$bullets[$n] ? $bullets[$n] : '', 'bullet', $atr);
- foreach($dat as $e)
- {
- $e['titulo'] = $imagen->toHTML().$e['titulo'];
- if(isset($e['activado']) && $e['activado'] != 0) $class = 'menu_activo';
- else $class = $classes[$n];
- if(!is_null($e['link']))
- $e['titulo'] = '<a href="'.$e['link'].'" class="'.$class.'">'.$e['titulo'].'</a>';
- $this->t_interna->addRow(array($e['titulo']), array('class' => $class));
- if(isset($e['sub']))
- $this->expandir($e['sub'], $n+1);
- }
+ $bullets = array(
+ '',
+ '/MECON/images/arbol_bullet_1.gif',
+ '/MECON/images/arbol_bullet_2.gif',
+ '/MECON/images/arbol_bullet_3.gif'
+ );
+ $tabulados = 7;
+ $classes = array('menu', 'menu1', 'menu1', 'menu2');
+
+ $atr = array('border' => '0',
+ 'width' => $n * $tabulados,
+ 'height' => '10');
+ $imagen =& new HTML_Image(@$bullets[$n] ? $bullets[$n] : '', 'bullet', $atr);
+ foreach ($dat as $e) {
+ $titulo = $imagen->toHTML().$e['titulo'];
+ if(isset($e['activado']) && $e['activado'] != 0) $class = 'menu_activo';
+ else $class = $classes[$n];
+ if(!is_null($e['link'])) {
+ $link = $e['link'];
+ if ($this->link_append and @$e['id']) {
+ $link .= $this->link_append . $e['id'];
+ }
+ $titulo = '<a href="'.$link.'" class="'.$class.'">'.$titulo.'</a>';
+ }
+ $tabla->addRow(array($titulo), array('class' => $class));
+ if(isset($e['sub'])) {
+ $this->expandir($e['sub'], $n+1, $tabla);
+ }
+ }
}
function toHTML()
{
- echo '<link rel="stylesheet" href="/MECON/css/arbol.css">';
- $this->addRow(array($this->t_interna->toHTML()));
- return parent::toHTML();
+ $this->setRowCount(0);
+ $t_interna = new HTML_Table(array(
+ 'width' =>'132',
+ 'border' => '0',
+ 'cellspacing' => '2',
+ 'cellpadding' => '0',
+ 'class' => 'bodytext'));
+ $titulo = new HTML_Table(array(
+ 'width' => '132',
+ 'height' => '26',
+ 'border' => '0',
+ 'cellspacing' => '0',
+ 'cellpadding' => '0',
+ 'align' => 'center',
+ 'background' => '/MECON/images/arbol_titulo.gif'));
+ $titulo->addRow(array($this->titulo), array(
+ 'align' => 'center',
+ 'class' => 'arboltitulo'));
+ $this->addRow(array($titulo), array('bgcolor' => '#FFFFFF'));
+ $this->expandir($this->datos, 0, $t_interna);
+ $this->addRow(array($t_interna->toHTML()));
+ return parent::toHTML();
+ }
+
+ function expandirArray($dat, $n, $filtrarActivos)
+ {
+ $array = array();
+ foreach ($dat as $e) {
+ // Si no tiene ID o esta activo y se filtran los activos, se
+ // continua con el proximo item.
+ if (!@$e['id'] or $filtrarActivos and @$e['activado']) {
+ continue;
+ }
+ $array[$e['id']] = str_repeat(' ', $n) . $e['titulo'];
+ if(@$e['sub']) {
+ $array += $this->expandirArray($e['sub'], $n + 1, $filtrarActivos);
+ }
+ }
+ return $array;
+ }
+
+ function toArray($filtrarActivos = true)
+ {
+ return $this->expandirArray($this->datos, 0, $filtrarActivos);
+ }
+
+ /**
+ * Activa un nodo del árbol.
+ *
+ * @param int $id Id del nodo a modificar.
+ * @param bool $activado Nuevo valor, true si está activado, false si no.
+ *
+ * @return bool True si se pudo modificar.
+ */
+ function setActivado($id, $activado = 1) {
+ return $this->modificarNodo($this->datos, $id, 'activado', $activado);
}
+
+ /**
+ * Modifica un nodo del array.
+ *
+ * @param array $datos Datos del árbol a modificar.
+ * @param int $id Id del elemento a modificar.
+ * @param string $key Clave del dato a modificar.
+ * @param mixed $val Nuevo valor.
+ *
+ * @return bool True si se pudo modificar.
+ */
+ function modificarNodo(&$datos, $id, $key, $val) {
+ foreach (array_keys($datos) as $k) {
+ if (@$datos[$k]['id'] == $id) {
+ $datos[$k][$key] = $val;
+ return true;
+ } elseif (@$datos[$k]['sub']
+ and $this->modificarNodo($datos[$k]['sub'], $id, $key, $val)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
};
?>
-------------------------------------------------------------------------------
Creado: jue jul 17 15:33:41 ART 2003
Autor: Gonzalo Merayo <gmeray@mecon.gov.ar>
+ Leandro Lucarella <llucar@mecon.gov.ar>
-------------------------------------------------------------------------------
$Id$
-----------------------------------------------------------------------------*/
class HTML_ArbolDB extends HTML_Arbol
{
- var $padre = null;
- var $tabla;
- var $nombre;
- var $id;
- var $link = null;
- var $prepend_link = null;
- var $where = '';
- var $order = '';
- var $db;
+ var $padre = '';
+ var $tabla;
+ var $nombre;
+ var $id;
+ var $link = '';
+ var $link_append = '';
+ var $where = '';
+ var $order = '';
+ var $db;
- function HTML_ArbolDB($dbdata, $imagen)
+ function HTML_ArbolDB($dbdata, $titulo, $link_append = '')
{
if(isset($dbdata['id_padre']))
- $this->padre = $dbdata['id_padre'];
- $this->tabla = $dbdata['tabla'];
- $this->nombre = $dbdata['nombre'];
- $this->id = $dbdata['id'];
+ $this->padre = $dbdata['id_padre'];
+ $this->tabla = $dbdata['tabla'];
+ $this->nombre = $dbdata['nombre'];
+ $this->id = $dbdata['id'];
+ // FIXME - Deprecated!
if(isset($dbdata['prepend_link']))
- $this->prepend_link = $dbdata['prepend_link'];
+ $link_append = $dbdata['prepend_link'];
if(isset($dbdata['link']))
- $this->link = $dbdata['link'];
+ $this->link = $dbdata['link'];
if(isset($dbdata['where']))
- $this->where = $dbdata['where'];
+ $this->where = $dbdata['where'];
if(isset($dbdata['order']))
- $this->order = ' ORDER BY '.$dbdata['nombre'].' '.$dbdata['order'];
- $this->db = $dbdata['db'];
- $dat = $this->BuscarHijos(0);
- parent::HTML_Arbol($dat, $imagen);
+ $this->order = ' ORDER BY '.$dbdata['nombre'].' '.$dbdata['order'];
+ $this->db = $dbdata['db'];
+ parent::HTML_Arbol(array(), $titulo, $link_append);
+ $this->datos = $this->BuscarHijos(0);
}
function BuscarHijos($id)
{
$sql = "SELECT $this->nombre, $this->id ";
- if(!is_null($this->link))
- $sql .= ", $this->link ";
+ if($this->link)
+ $sql .= ", $this->link ";
$sql .= "FROM $this->tabla ";
- if(!is_null($this->padre) or $this->where)
+ if($this->padre or $this->where)
$sql .= 'WHERE ';
- if(!is_null($this->padre)) {
+ if($this->padre) {
$sql .= "$this->padre = '$id'";
if ($this->where)
$sql .= ' AND';
{
$titulo = $row[0];
$id = $row[1];
- if(is_null($this->padre)) $sub = array();
+ if(!$this->padre) $sub = array();
else $sub = $this->BuscarHijos($id);
- if(!is_null($this->link)) $link = $this->prepend_link.$row[2];
- else $link = $this->prepend_link.$id;
+ $link = strval(@$row[2]);
$dat[] = array(
- 'titulo'=> $titulo,
- 'link' => $link,
- 'sub' => $sub
+ 'titulo'=> $titulo,
+ 'link' => $link,
+ 'id' => $id,
+ 'sub' => $sub
);
}
return $dat;
--- /dev/null
+<?php /* vim: set binary expandtab tabstop=4 shiftwidth=4 tw=80 fdm=marker:
+-------------------------------------------------------------------------------
+ Ministerio de Economía
+ meconlib
+-------------------------------------------------------------------------------
+This file is part of meconlib.
+
+meconlib is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2 of the License, or (at your option)
+any later version.
+
+meconlib is distributed in the hope that it will be useful, but WITHOUT
+ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License; if not,
+write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+Boston, MA 02111-1307 USA
+-------------------------------------------------------------------------------
+Creado: jue jul 17 15:32:52 ART 2003
+Autor: Gonzalo Merayo <gmeray@mecon.gov.ar>
+-------------------------------------------------------------------------------
+$Id: Arbol.php 237 2003-07-17 22:41:48Z mmarre $
+-----------------------------------------------------------------------------*/
+
+require_once '../../lib/MECON/HTML/Arbol.php';
+
+$dat = array( // Datos {{{
+ array(
+ 'titulo' => 'Nivel0a',
+ 'link' => 'blabla',
+ 'id' => 1,
+ 'sub' => array(//sub0 {{{
+ array(
+ 'titulo' => 'Nivel1a',
+ 'link' => 'blabla',
+ 'id' => 2,
+ 'sub' => array(//sub1 {{{
+ array(
+ 'titulo' => 'Nivel3a',
+ 'link' => 'blabla',
+ 'id' => 5,
+ 'activado' => 1
+ ),
+ array(
+ 'titulo' => 'Nivel3b',
+ 'link' => 'blabla',
+ 'id' => 9,
+ 'sub' => array(//sub2 {{{
+ 5 => array(
+ 'titulo' => 'Nivel4',
+ 'link' => 'blabla',
+ 'id' => 8,
+ )
+ )//sub2 }}}
+ ),
+ array(
+ 'titulo' => 'Nivel3c',
+ 'link' => 'blabla',
+ 'id' => 7,
+ )
+ )//sub1 }}}
+ ),
+ array(
+ 'titulo' => 'Nivel1b',
+ 'link' => 'blabla',
+ 'id' => 13,
+ 'activado' => 1
+ ),
+ array(
+ 'titulo' => 'Nivel1c',
+ 'link' => 'blabla',
+ 'id' => 45,
+ )
+ )//sub0 }}}
+ ),
+ array(
+ 'titulo' => 'Nivel0b',
+ 'link' => 'blabla',
+ 'id' => 16,
+ ),
+ array(
+ 'titulo' => 'Nivel0c',
+ 'link' => 'blabla',
+ 'id' => 21,
+ )
+); // }}}
+
+$arbol = new HTML_Arbol($dat, 'MI TITULO', '?raiz=');
+echo '<link rel="stylesheet" href="'.$arbol->getCSS().'">';
+echo $arbol->toHTML();
+echo '<SELECT name="id">';
+foreach ($arbol->toArray(false) as $id => $val) {
+ echo '<OPTION value="'.$id.'">'.$val.'</OPTION>';
+}
+echo '</SELECT>';
+echo '<SELECT name="id">';
+foreach ($arbol->toArray(true) as $id => $val) {
+ echo '<OPTION value="'.$id.'">'.$val.'</OPTION>';
+}
+echo '</SELECT>';
+$arbol->setActivado(16);
+echo $arbol->toHTML();
+echo '<SELECT name="id">';
+foreach ($arbol->toArray() as $id => $val) {
+ echo '<OPTION value="'.$id.'">'.$val.'</OPTION>';
+}
+echo '</SELECT>';
+
+?>
--- /dev/null
+<?php /* vim: set binary expandtab tabstop=4 shiftwidth=4 textwidth=80:
+-------------------------------------------------------------------------------
+ Ministerio de Economía
+ meconlib
+-------------------------------------------------------------------------------
+This file is part of meconlib.
+
+meconlib is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2 of the License, or (at your option)
+any later version.
+
+meconlib is distributed in the hope that it will be useful, but WITHOUT
+ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License; if not,
+write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+Boston, MA 02111-1307 USA
+-------------------------------------------------------------------------------
+Creado: jue jul 17 15:33:41 ART 2003
+Autor: Gonzalo Merayo <gmeray@mecon.gov.ar>
+-------------------------------------------------------------------------------
+$Id$
+-----------------------------------------------------------------------------*/
+
+require_once '../../lib/MECON/HTML/Arbol/ArbolDB.php';
+require_once 'DB.php';
+
+PEAR::setErrorHandling(PEAR_ERROR_TRIGGER);
+
+$dsn = 'mysql://intranet:intranet@bal747f.mecon.ar/intranet';
+$db =& DB::connect($dsn,true);
+
+$dbdata = array(
+ 'db' => &$db,
+ 'tabla' => 'grupo_secciones',
+ 'id' => 'grupo',
+ 'nombre' => 'nombre',
+ 'id_padre' => 'grupo_padre',
+);
+
+$arbol = new HTML_ArbolDB($dbdata, 'TITULO', 'noticias.php?grupo=');
+echo '<link rel="stylesheet" href="'.$arbol->getCSS().'">';
+echo $arbol->toHTML();
+echo '<SELECT name="id">';
+foreach ($arbol->toArray(false) as $id => $val) {
+ echo '<OPTION value="'.$id.'">'.$val.'</OPTION>';
+}
+echo '</SELECT>';
+echo '<SELECT name="id">';
+foreach ($arbol->toArray(true) as $id => $val) {
+ echo '<OPTION value="'.$id.'">'.$val.'</OPTION>';
+}
+echo '</SELECT>';
+var_dump($arbol->setActivado(34));
+echo $arbol->toHTML();
+echo '<SELECT name="id">';
+foreach ($arbol->toArray() as $id => $val) {
+ echo '<OPTION value="'.$id.'">'.$val.'</OPTION>';
+}
+echo '</SELECT>';
+
+?>
+++ /dev/null
-<?\r
- require_once 'MECON/HTML/Arbol.php';\r
-\r
- $dat = array(\r
- array(\r
- 'titulo' => 'Nivel0a',\r
- 'link' => 'blabla',\r
- 'sub' => array(//sub0\r
- array(\r
- 'titulo' => 'Nivel1a',\r
- 'link' => 'blabla',\r
- 'sub' => array(//sub1\r
- array(\r
- 'titulo' => 'Nivel3a',\r
- 'link' => 'blabla',\r
- 'activado' => 1\r
- ),\r
- array(\r
- 'titulo' => 'Nivel3b',\r
- 'link' => 'blabla',\r
- 'sub' => array(//sub2\r
- array(\r
- 'titulo' => 'Nivel4',\r
- 'link' => 'blabla'\r
- )\r
- )//sub2\r
- ),\r
- array(\r
- 'titulo' => 'Nivel3c',\r
- 'link' => 'blabla'\r
- )\r
- )//sub1\r
- ),\r
- array(\r
- 'titulo' => 'Nivel1b',\r
- 'link' => 'blabla',\r
- 'activado' => 1\r
- ),\r
- array(\r
- 'titulo' => 'Nivel1c',\r
- 'link' => 'blabla'\r
- )\r
- )//sub0\r
- ),\r
- array(\r
- 'titulo' => 'Nivel0b',\r
- 'link' => 'blabla'\r
- ),\r
- array(\r
- 'titulo' => 'Nivel0c',\r
- 'link' => 'blabla'\r
- )\r
- );\r
-?>\r
-<?\r
- $arbol = new HTML_Arbol($dat, '/MECON/images/arbol_noticias.gif');\r
- echo $arbol->toHTML();\r
-?>\r
+++ /dev/null
-<?\r
- require_once 'MECON/HTML/Arbol/ArbolDB.php';\r
- require_once 'DB.php';\r
-\r
-\r
- $dsn = 'mysql://intranet:intranet@bal747f.mecon.ar/intranet';\r
- $db = DB::connect($dsn,true);\r
-\r
- $dbdata = array(\r
- 'db' => $db,\r
- 'tabla' => 'grupo_secciones',\r
- 'id' => 'grupo',\r
- 'nombre' => 'nombre',\r
- 'id_padre' => 'grupo_padre',\r
- 'prepend_link' => 'noticias.php?grupo=');\r
-\r
- $arbol = new HTML_ArbolDB($dbdata, '/MECON/images/arbol_noticias.gif');\r
- echo $arbol->toHTML();\r
-?>\r