class Tree

Build and manipulate a tree representing hierarchical relationships between objects.

Essentially this is a category tree, although collections (category analogues) are fully-fledged content objects in their own right. Pass in an array of collection objects; you can choose to pass in all collection objects or you can pass in a branch, in which case the tree will just consist of descendants of the root node.

Methods

__construct(array $objectArr, string $myId, string $parentId, string $rootId = null)

Constructor.

array
getTree()

Get a category tree.

array
getAllChild(int $key, array $ret = [])

Returns an array of all child objects of a parental object specified by its ID.

array
getAllParent(int $key, array $ret = [], int $uplevel = 1)

Returns an array of all parent objects.

object
getByKey(int $key)

returns an object from the category tree specified by its id.

array
getFirstChild(int $key)

Returns an array of all the first child objects of a parental object specified by its id.

array
makeParentSelectBox(int $selected = 0, int $key = 0)

Make a select box of parent collections from the tree.

array
makeSelBox(string $name, string $fieldName, string $prefix = '-- ', int $selected = 0, bool $addEmptyOption = false, int $key = 0)

Make select box options from the tree.

Details

at line 90
__construct(array $objectArr, string $myId, string $parentId, string $rootId = null)

Constructor.

Parameters

array $objectArr Array of collection objects that the tree will be built from.
string $myId Name of the ID field used to identify objects in the tree.
string $parentId Name of ID field used to identify an object's parent in the tree.
string $rootId Name of root object ID field. This is the object that will be used as the base node for building a tree (or subtree) from the $objectArr.

at line 127
array getTree()

Get a category tree.

Return Value

array Associative array comprising the tree.

at line 139
array getAllChild(int $key, array $ret = [])

Returns an array of all child objects of a parental object specified by its ID.

Parameters

int $key ID of the parent.
array $ret Array of child objects from previous recursions (empty if called from client).

Return Value

array Array of child nodes.

at line 168
array getAllParent(int $key, array $ret = [], int $uplevel = 1)

Returns an array of all parent objects.

The key of returned array represents how many levels up from the specified object.

Parameters

int $key ID of the child object.
array $ret Result from previous recursions (empty when called from outside).
int $uplevel Level of recursion (empty when called from outside).

Return Value

array Array of parent nodes.

at line 191
object getByKey(int $key)

returns an object from the category tree specified by its id.

Parameters

int $key ID of the object to retrieve.

Return Value

object Object (node) within the tree.

at line 202
array getFirstChild(int $key)

Returns an array of all the first child objects of a parental object specified by its id.

Parameters

int $key ID of the parent object.

Return Value

array Array of child objects.

at line 224
array makeParentSelectBox(int $selected = 0, int $key = 0)

Make a select box of parent collections from the tree.

Parameters

int $selected Currently selected option.
int $key ID of the object to display as root of the select options.

Return Value

array Array of parent options for select box.

at line 248
array makeSelBox(string $name, string $fieldName, string $prefix = '-- ', int $selected = 0, bool $addEmptyOption = false, int $key = 0)

Make select box options from the tree.

Returns an indented array of options that can be used to build a HTML select box, indented according to the relative hierarchy.

Parameters

string $name Name of the select box.
string $fieldName Name of the member variable from the node objects that should be used as the title field for the options.
string $prefix String to indent deeper levels.
int $selected Value to display as selected.
bool $addEmptyOption Set TRUE to add an empty option with value "0" at the top of the hierarchy.
int $key ID of the object to display as the root of select options.

Return Value

array Select box options as ID => title pairs.