Class TreeItem Class to create tree-based items

The treeitem class allows you to logically store tree-based structures.

Example:

Let's have a tree with 3 nodes. It's important that we always have a "root" key.

$root = new TreeItem("root", 1); $item1 = new TreeItem("node1",2); $item2 = new TreeItem("node2",3); $item3 = new TreeItem("node3",4);

$root->addItem($item1); $root->addItem($item2); $root->addItem($item3);

This represents the tree we described above.

If you know the ID of the item you want to add to, there's no need to have a specific item handy, but rather you can use the "addItemToID" function.

 Methods

Constructor Function Creates a new, independant tree item.

TreeItem(string $name, string $id, boolean $collapsed) 

Parameters

$name

string

The name of that item

$id

string

The unique ID of that item

$collapsed

boolean

Is this item collapsed by default

Adds a new subitem to this item.

addItem(object $item) 

Parameters

$item

object

the item to add

Adds a new subitem to a specific item with an ID.

addItemToID(object $item, string $id) 

Traverses all subitems to find the correct item.

Parameters

$item

object

the item to add

$id

string

the ID to add the item to

Collapses all items, starting from the $start item.

collapseAll(string $start) 

Parameters

$start

string

the ID to start collapsing from

Expands all items, starting from the $start item.

expandAll(string $start) 

Parameters

$start

string

the ID to start expanding from

Returns all items (as ID array) which are collapsed.

getCollapsedList(array $list) 

Parameters

$list

array

Contains the list with all collapsed items

Returns all items (as ID array) which are expanded.

getExpandedList(array $list) 

Parameters

$list

array

Contains the list with all expanded items

Starts iterating at root node and flattens the tree into an array

getFlatTree($item, $flat_tree) 

Parameters

$item

$flat_tree

Retrieves a specific item by its ID.

getItemByID(string $id) : object

Note that this function traverses all subitems to find the correct item.

Parameters

$id

string

the ID to find

Returns

objectThe item, or false if nothing was found

Returns a list of the id of all parent nodes of the given node Not using the nodes of hierarchical tree, but flat tree !!

getParentNodes($parentNodes, $stop_id) 

Parameters

$parentNodes

$stop_id

getCollapsedList thinks if a node has no subnodes it is collapsed I don't think so

getRealCollapsedList($list) 

Parameters

$list

Returns a list of the id of all parent nodes of the given node

getTreeParentNodes($parentNodes, $id) 

Parameters

$parentNodes

$id

hasCollapsedNode()

hasCollapsedNode($item_id) 

Parameters

$item_id

Checks if a specific custom attribute is set

isCustomAttributeSet(string $item) 

Parameters

$item

string

the attribute name to find

Marks an item as collpased.

markCollapsed(string $id) 

Traverses all subitems to find the ID. Note that only the item with $id is collapsed, but not its childs.

Parameters

$id

string

the ID to collapse

Marks an item as expanded.

markExpanded(string $id) 

Traverses all subitems to find the ID. Note that only the item with $id is expanded, but not its childs.

Parameters

$id

string

the ID to expand, or an array with all id's

Removes an item with a specific ID.

removeItem(string $id) 

Parameters

$id

string

the ID to find

Traverses the tree starting from this item, and returning all objects as $objects.

traverse(object $objects, integer $level) 

Parameters

$objects

object

all found objects

$level

integer

Level to start on

 Properties

 

$collapsed : boolean
 

$collapsed_icon : string
 

$custom : array
 

$expanded_icon : string
 

$id : string
 

$lastnode_icon : string
 

$level : integer
 

$name : string
 

$parent : array
 

$subitems : array