PHP Class Tree\NestedTree\NestedTree

Show file Open project: nilsteampassnet/TeamPass

Public Methods

Method Description
__construct ( string $table, string $idField, string $parentField, string $sortField ) Constructor. Set the database table name and necessary field names
generateTreeData ( &$arr, integer $id, integer $level, &$n ) Generate the tree data. A single call to this generates the n-values for 1 node in the tree. This function assigns the passed in n value as the node's nleft value. It then processes all the node's children (which in turn recursively processes that node's children and so on), and when it is finally done, it takes the update n-value and assigns it as its nright value. Because it is passed as a reference, the subsequent changes in subrequests are held over to when control is returned so the nright can be assigned.
getChildren ( integer $id, boolean $includeSelf = false ) : array Fetch the children of a node, or if no node is specified, fetch the top level items.
getDescendants ( integer $id, boolean $includeSelf = false, boolean $childrenOnly = false, $unique_id_list = false ) : array Fetch the descendants of a node, or if no node is specified, fetch the entire tree. Optionally, only return child data instead of all descendant data.
getFields ( ) : array A utility function to return an array of the fields that need to be selected in SQL select queries
getImmediateFamily ( integer $id ) : array Fetch the immediately family of a node. More specifically, fetch a node's parent, siblings and children. If the node isn't valid, fetch the first level of nodes from the tree.
getNode ( integer $id ) : object Fetch the node data for the node identified by $id
getPath ( integer $id, boolean $includeSelf = false ) : array Fetch the path to a node. If an invalid node is passed, an empty array is returned.
getTreeWithChildren ( ) : array Fetch the tree data, nesting within each node references to the node's children
isChildOf ( integer $child_id, integer $parent_id ) : boolean Check if one node is a child of another node. If either node is not found, then false is returned.
isDescendantOf ( integer $descendant_id, integer $ancestor_id ) : boolean Check if one node descends from another node. If either node is not found, then false is returned.
numChildren ( integer $id ) : integer Find the number of children a node has
numDescendants ( integer $id ) : integer Find the number of descendants a node has
rebuild ( ) Rebuilds the tree data and saves it to the database

Method Details

__construct() public method

Constructor. Set the database table name and necessary field names
public __construct ( string $table, string $idField, string $parentField, string $sortField )
$table string Name of the tree database table
$idField string Name of the primary key ID field
$parentField string Name of the parent ID field
$sortField string Name of the field to sort data.

generateTreeData() public method

Generate the tree data. A single call to this generates the n-values for 1 node in the tree. This function assigns the passed in n value as the node's nleft value. It then processes all the node's children (which in turn recursively processes that node's children and so on), and when it is finally done, it takes the update n-value and assigns it as its nright value. Because it is passed as a reference, the subsequent changes in subrequests are held over to when control is returned so the nright can be assigned.
public generateTreeData ( &$arr, integer $id, integer $level, &$n )
$id integer The ID of the current node to process
$level integer The nlevel to assign to the current node

getChildren() public method

Fetch the children of a node, or if no node is specified, fetch the top level items.
public getChildren ( integer $id, boolean $includeSelf = false ) : array
$id integer The ID of the node to fetch child data for.
$includeSelf boolean Whether or not to include the passed node in the the results.
return array The children of the passed node

getDescendants() public method

Fetch the descendants of a node, or if no node is specified, fetch the entire tree. Optionally, only return child data instead of all descendant data.
public getDescendants ( integer $id, boolean $includeSelf = false, boolean $childrenOnly = false, $unique_id_list = false ) : array
$id integer The ID of the node to fetch descendant data for. Specify an invalid ID (e.g. 0) to retrieve all data.
$includeSelf boolean Whether or not to include the passed node in the the results. This has no meaning if fetching entire tree.
$childrenOnly boolean True if only returning children data. False if returning all descendant data
return array The descendants of the passed now

getFields() public method

A utility function to return an array of the fields that need to be selected in SQL select queries
public getFields ( ) : array
return array An indexed array of fields to select

getImmediateFamily() public method

Fetch the immediately family of a node. More specifically, fetch a node's parent, siblings and children. If the node isn't valid, fetch the first level of nodes from the tree.
public getImmediateFamily ( integer $id ) : array
$id integer The ID of the node to fetch child data for.
return array An array of each node in the family

getNode() public method

Fetch the node data for the node identified by $id
public getNode ( integer $id ) : object
$id integer The ID of the node to fetch
return object An object containing the node's data, or null if node not found

getPath() public method

If a top level node is passed, an array containing on that node is included (if 'includeSelf' is set to true, otherwise an empty array)
public getPath ( integer $id, boolean $includeSelf = false ) : array
$id integer The ID of the node to fetch child data for.
$includeSelf boolean Whether or not to include the passed node in the the results.
return array An array of each node to passed node

getTreeWithChildren() public method

Fetch the tree data, nesting within each node references to the node's children
public getTreeWithChildren ( ) : array
return array The tree with the node's child data

isChildOf() public method

Check if one node is a child of another node. If either node is not found, then false is returned.
public isChildOf ( integer $child_id, integer $parent_id ) : boolean
$child_id integer The node that is possibly a child
$parent_id integer The node that is possibly a parent
return boolean True if $child_id is a child of $parent_id, false otherwise

isDescendantOf() public method

Check if one node descends from another node. If either node is not found, then false is returned.
public isDescendantOf ( integer $descendant_id, integer $ancestor_id ) : boolean
$descendant_id integer The node that potentially descends
$ancestor_id integer The node that is potentially descended from
return boolean True if $descendant_id descends from $ancestor_id, false otherwise

numChildren() public method

Find the number of children a node has
public numChildren ( integer $id ) : integer
$id integer The ID of the node to search for. Pass 0 to count the first level items
return integer The number of descendants the node has, or -1 if the node isn't found.

numDescendants() public method

Find the number of descendants a node has
public numDescendants ( integer $id ) : integer
$id integer The ID of the node to search for. Pass 0 to count all nodes in the tree.
return integer The number of descendants the node has, or -1 if the node isn't found.

rebuild() public method

Rebuilds the tree data and saves it to the database
public rebuild ( )