treeselect :D
| From: | Marco Sottana | Date: | Mon, 30 Oct 2006 12:48:47 +0000 |
| Subject: | treeselect :D | ||
| References: | 1 2 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-25677@lists.php.net to get a copy of this message | ||
i extend select to make a treeselect
[code]
<?php
require_once('HTML/QuickForm/select.php');
class HTML_QuickForm_treexot extends HTML_QuickForm_select
{
function loadTreeQuery(&$conn, $sql, $textCol=null, $valueCol=null, $parentCol=null,$orderCol,$values=null)
{
if (is_string($conn)) {
require_once('DB.php');
$dbConn = &DB::connect($conn, true);
if (DB::isError($dbConn)) {
return $dbConn;
}
} elseif (is_subclass_of($conn, "db_common")) {
$dbConn = &$conn;
} else {
return PEAR::raiseError('Argument 1 of HTML_Select::loadQuery is not a valid type');
}
$result = $dbConn->query($sql);
if (DB::isError($result)) {
return $result;
}
if (!is_object($result) || !is_a($result, 'db_result')) {
return PEAR::raiseError('Argument 1 of HTML_Select::loadDbResult is not a valid DB_result');
}
if (isset($values)) {
$this->setValue($values);
}
$fetchMode = ($textCol && $valueCol) ? DB_FETCHMODE_ASSOC : DB_FETCHMODE_ORDERED;
$rows=array();
while (is_array($row = $result->fetchRow($fetchMode)) ) {
$rows[]=array('category_id'=>$row[$valueCol],'parent_id' =>$row[$parentCol],'category_name' =>$row[$textCol],'category_position'=>$row[$orderCol]);
/*
if ($fetchMode == DB_FETCHMODE_ASSOC) {
$this->addOption($row[$textCol], $row[$valueCol]);
} else {
$this->addOption($row[0], $row[1]);
}
*/
}
$ordered = chainX('category_id', 'parent_id', 'category_position', $rows);
foreach($ordered as $item)
{
//echo str_repeat('------', $item['indent']).$item['category_name'].'<br>';
//echo '<option value="'.$item['category_id'].'">'.str_repeat('------', $item['indent']).$item['category_name'].'</option>';
$this->addOption(str_repeat('------', $item['indent']+1).$item['category_name'],$item['category_id']);
}
//$this->loadDbResult($result, $textCol, $valueCol, $values);
$result->free();
if (is_string($conn)) {
$dbConn->disconnect();
}
return true;
} // end func loadQuery
}
function chainX($primary_field, $parent_field, $sort_field, $rows, $root_id=0, $maxlevel=25)
{
$c = new chainX($primary_field, $parent_field, $sort_field, $rows, $root_id, $maxlevel);
return $c->chainX_table;
}
class chainX
{
var $table;
var $rows;
var $chainX_table;
var $primary_field;
var $parent_field;
var $sort_field;
function chainX($primary_field, $parent_field, $sort_field, $rows, $root_id, $maxlevel)
{
$this->rows = $rows;
$this->primary_field = $primary_field;
$this->parent_field = $parent_field;
$this->sort_field = $sort_field;
$this->buildchainX($root_id,$maxlevel);
}
function buildchainX($rootcatid,$maxlevel)
{
foreach($this->rows as $row)
{
$this->table[$row[$this->parent_field]][ $row[$this->primary_field]] = $row;
}
$this->makeBranch($rootcatid,0,$maxlevel);
}
function makeBranch($parent_id,$level,$maxlevel)
{
$rows=$this->table[$parent_id];
foreach($rows as $key=>$value)
{
$rows[$key]['key'] = $this->sort_field;
}
usort($rows,'chainXCMP');
foreach($rows as $item)
{
$item['indent'] = $level;
$this->chainX_table[] = $item;
if((isset($this->table[$item[$this->primary_field]])) && (($maxlevel>$level+1) || ($maxlevel==0)))
{
$this->makeBranch($item[$this->primary_field], $level+1, $maxlevel);
}
}
}
}
function chainXCMP($a,$b)
{
if($a[$a['key']] == $b[$b['key']])
{
return 0;} return($a[$a['key']]<$b[$b['key']])?-1:1; } ?> [/code] [code] function preGenerateForm() { global $dsn,$mdb2; HTML_QuickForm::registerElementType('treexot', '/srv/www/prgs/extPear/treexot.php', 'HTML_QuickForm_treexot'); ///* $el=& HTML_QuickForm::createElement('treexot', 'id_padre', 'Padre'); $el->addOption("SUPREMO","0"); $sql=" select * from tbl_menu_sx where find_in_set(4,checkbox_intention) and id<>".$this->id; $el->loadTreeQuery($dsn,$sql,"nome","id","id_padre","posizione"); $this->fb_preDefElements['id_padre'] = $el; } [/code]