%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home/nailstv/public_html/de.nails.tv/include/
Upload File :
Create Path :
Current File : /home/nailstv/public_html/de.nails.tv/include/Tree.class.php

<?php

class Tree
{
    var $db;
    var $filter = 0;
    var $cnt = 1;
    var $data = array();
    var $branch = array();
    var $branch_id;
    var $numrows;
    var $depth;


    function Tree(&$db, $depth = -1, $branch_id = 0)
    {
        $this->db = $db;
        $this->depth = $depth;
        $this->branch_id = $branch_id;
    }

    function GetDBData($sql)
    {
        $r = $this->db->Execute($sql);

        while(!$r->EOF)
        {
            $this->data[] = $r->fields;
            $r->MoveNext();
        }

        $this->numrows = count($this->data);
    }

    function FilterMethod($row)
    {
        //$this->cnt = 1;

        return $row['parent_id'] == $this->filter;
    }

    function CreateNestedArray(&$data, &$arr, $parent, $startDepth, $maxDepth, $full)
    {
        if ($maxDepth-- == 0) return;
        $index = 0;
        $startDepth++;

        $this->filter = $parent;

        $children = array_filter($data, array($this, "FilterMethod"));
        foreach ($children as $child)
        {
            $arr[$index] = $child;
            $arr[$index]['depth'] = $startDepth;
            $arr[$index]['cnt'] = $this->cnt;
            $this->cnt++;
            if($arr[$index]['depth'] < $this->depth || $arr[$index]['id'] == $this->branch_id || in_array($arr[$index]['id'], $this->branch) || $full)
            {
                $this->CreateNestedArray($data, $arr[$index]['children'], $child['id'], $startDepth, $maxDepth, $full);
            }
            $index++;
        }
    }

    function CreateResult($sql, $parent, $startDepth, $maxDepth, $full = true)
    {
        $this->GetDBData($sql);
        $this->branch = $this->GetBranch($this->branch_id);
        $arr = array();
        $this->CreateNestedArray($this->data, $arr, $parent, $startDepth, $maxDepth, $full);
        return $arr;
    }

    function GetParent($id)
    {
        for($i = 0; $i < $this->numrows; $i++)
        {
            if($this->data[$i]['id'] == $id)
            {
                $parent = $this->data[$i];
                return $parent;
            }
        }
    }

    function GetBranch($id, $count = 0)
	{
		$parent = $this->GetParent($id);
		if($parent)
        {
            $this->branch[$count] = $parent['parent_id'];
            $this->GetBranch($this->branch[$count], $count + 1);
            return array_reverse($this->branch);
		}
        return array();
	}

    function GetAncestors($id, $count = 0)
	{
		$parent = $this->GetParent($id);
		if($parent)
        {
            $this->ancestors[$count] = $parent;
            $this->GetAncestors($this->ancestors[$count]['parent_id'], $count + 1);
            return array_reverse($this->ancestors);
		}
        return array();
	}

    function GetRootId($id, $level = 0)
    {
        $a = $this->GetAncestors($id);
        return $a[$level];
    }

    function GetNav($id, $count = 0)
	{
		$parent = $this->GetParent($id);
		if($parent)
        {
            $this->nav[$count] = $parent;
            $this->GetNav($this->nav[$count]['parent_id'], $count + 1);
            return array_reverse($this->nav);
		}
        return array();
	}
}

?>

Zerion Mini Shell 1.0