PHP Class FOF30\Model\TreeModel

Inheritance: extends DataModel
Show file Open project: akeeba/fof Class Usage Examples

Protected Properties

Property Type Description
$treeDepth The level (depth) of this node in the tree
$treeNestedGet Should I perform a nested get (used to query ascendants/descendants)
$treeParent The parent node of ourselves
$treeRoot The root node in the tree

Public Methods

Method Description
__construct ( Container $container = null, array $config = [] ) Public constructor. Overrides the parent constructor, making sure there are lft/rgt columns which make it compatible with nested sets.
buildQuery ( boolean $overrideLimits = false ) : JDatabaseQuery Overrides the DataModel's buildQuery to allow nested set searches using the provided scopes
check ( ) Overrides the automated table checks to handle the 'hash' column for faster searching
copy ( $data = null ) : static Makes a copy of the record, inserting it as the last child of the current node's parent.
create ( array $data ) : static Create a new record with the provided data. It is inserted as the last child of the current node's parent
equals ( TreeModel &$node ) : boolean Is $node this very node?
findByPath ( string $path ) : TreeModel | null Locate a node from a given path, e.g. "/some/other/leaf"
forceDelete ( mixed $id = null ) Delete a node, either the currently loaded one or the one specified in $id. If an $id is specified that node is loaded before trying to delete it. In the end the data model is reset. If the node has any children nodes they will be removed before the node itself is deleted.
getAncestors ( ) : Collection Get all ancestors to this node but not the node itself. In other words it gets the path to the node, without the node itself.
getAncestorsAndSelf ( ) : Collection Get all ancestors to this node and the node itself. In other words it gets the full path to the node and the node itself.
getAncestorsAndSelfWithoutRoot ( ) : Collection Get all ancestors to this node and the node itself, but not the root node. If you want to
getAncestorsWithoutRoot ( ) : Collection Get all ancestors to this node but not the node itself and its root.
getDescendants ( ) : Collection Get only our descendant (children) nodes, not ourselves.
getDescendantsAndSelf ( ) : Collection Get all descendant (children) nodes and ourselves.
getImmediateDescendants ( ) : Collection Get the immediate descendants (children). Unlike getDescendants it only goes one level deep into the tree structure. Descendants of descendant nodes will not be returned.
getLeaves ( ) : Collection Get all leaf nodes in the tree. You may want to use the scopes to narrow down the search in a specific subtree or path.
getLevel ( ) : integer | mixed Gets the level (depth) of this node in the tree. The result is cached in $this->treeDepth for faster retrieval.
getNestedList ( string $column = 'title', null $key = null, string $seperator = ' ' ) : array Returns a hashed array where each element's key is the value of the $key column (default: the ID column of the table) and its value is the value of the $column column (default: title). Each nesting level will have the value of the $column column prefixed by a number of $separator strings, as many as its nesting level (depth).
getParent ( ) : static Returns the immediate parent of the current node
getRoot ( ) : static Returns the root node of the tree this node belongs to
getSiblings ( ) : Collection Get all sibling nodes, except ourselves
getSiblingsAndSelf ( ) : Collection Get all sibling nodes, including ourselves
inSameScope ( TreeModel $otherNode ) : boolean Returns true if both this node and $otherNode are root, leaf or child (same tree scope)
insertAsChildOf ( TreeModel &$parentNode ) Alias for insertAsLastchildOf
insertAsFirstChildOf ( TreeModel &$parentNode ) Insert the current node as the first (leftmost) child of a parent node.
insertAsLastChildOf ( TreeModel &$parentNode ) Insert the current node as the last (rightmost) child of a parent node.
insertAsRoot ( ) : static Insert the current node as a tree root. It is a good idea to never use this method, instead providing a root node in your schema installation and then sticking to only one root.
insertAsSiblingOf ( TreeModel &$siblingNode ) Alias for insertRightOf
insertLeftOf ( TreeModel &$siblingNode ) Insert the current node to the left of (before) a sibling node
insertRightOf ( TreeModel &$siblingNode ) Insert the current node to the right of (after) a sibling node
insideSubtree ( TreeModel $otherNode ) : boolean Checks if our node is inside the subtree of $otherNode. This is a fast check as only lft and rgt values have to be compared.
isAncestorOf ( TreeModel $otherNode ) : boolean Returns true if we are an ancestor of $otherNode
isChild ( ) : boolean Is this a child node (not root)?
isDescendantOf ( TreeModel $otherNode ) : boolean Returns true if we are a descendant of $otherNode
isLeaf ( ) : boolean Is this a leaf node (a node without children)?
isRoot ( ) : boolean Is this a top-level root node?
isSelfOrAncestorOf ( TreeModel $otherNode ) : boolean Returns true if $otherNode is ourselves or we are an ancestor of $otherNode
isSelfOrDescendantOf ( TreeModel $otherNode ) : boolean Returns true if $otherNode is ourselves or if we are a descendant of $otherNode
makeChildOf ( TreeModel $parentNode ) Alias for makeLastChildOf
makeFirstChildOf ( TreeModel $parentNode ) Moves a node and its subtree as a the first (leftmost) child of $parentNode
makeLastChildOf ( TreeModel $parentNode ) Moves a node and its subtree as a the last (rightmost) child of $parentNode
makeNextSiblingOf ( TreeModel $siblingNode ) Alias for moveToRightOf
makePreviousSiblingOf ( TreeModel $siblingNode ) Alias for moveToLeftOf
makeRoot ( ) Makes the current node a root (and moving its entire subtree along the way). This is achieved by moving the node to the right of its root node
makeSiblingOf ( TreeModel $siblingNode ) Alias for makeNextSiblingOf
move ( integer $delta, string $where = '' ) : static Not supported in nested sets
moveLeft ( ) Move the current node (and its subtree) one position to the left in the tree, i.e. before its left-hand sibling
moveRight ( ) Move the current node (and its subtree) one position to the right in the tree, i.e. after its right-hand sibling
moveToLeftOf ( TreeModel $siblingNode ) Moves the current node (and its subtree) to the left of another node. The other node can be in a different position in the tree or even under a different root.
moveToRightOf ( TreeModel $siblingNode ) Moves the current node (and its subtree) to the right of another node. The other node can be in a different position in the tree or even under a different root.
reorder ( string $where = '' ) : static Not supported in nested sets
reset ( boolean $useDefaults = true, boolean $resetRelations = false ) : static Reset the record data and the tree cache
withoutNode ( TreeModel $node ) : void get() will not return the selected node if it's part of the query results

Protected Methods

Method Description
onAfterDelete ( $oid )
resetTreeCache ( ) : static Resets cached values used to speed up querying the tree
scopeAncestors ( ) : void get() will return all ancestor nodes but not ourselves
scopeAncestorsAndSelf ( ) : void get() will return all ancestor nodes and ourselves
scopeDescendants ( ) : void get() will return all descendants (even subtrees of subtrees!) but not ourselves
scopeDescendantsAndSelf ( ) : void get() will return all descendants (even subtrees of subtrees!) and ourselves
scopeImmediateDescendants ( ) : void get() will only return immediate descendants (first level children) of the current node
scopeLeaves ( ) : void get() will return only leaf nodes
scopeSiblings ( ) : void get() will return all sibling nodes but not ourselves
scopeSiblingsAndSelf ( ) : void get() will return all sibling nodes and ourselves
scopeWithoutRoot ( ) : void get() will not return our root if it's part of the query results
scopeWithoutSelf ( ) : void get() will not return ourselves if it's part of the query results

Method Details

__construct() public method

Public constructor. Overrides the parent constructor, making sure there are lft/rgt columns which make it compatible with nested sets.
See also: FOF30\Model\DataModel::__construct()
public __construct ( Container $container = null, array $config = [] )
$container FOF30\Container\Container The configuration variables to this model
$config array Configuration values for this model

buildQuery() public method

Overrides the DataModel's buildQuery to allow nested set searches using the provided scopes
public buildQuery ( boolean $overrideLimits = false ) : JDatabaseQuery
$overrideLimits boolean
return JDatabaseQuery

check() public method

Overrides the automated table checks to handle the 'hash' column for faster searching
public check ( )

copy() public method

Makes a copy of the record, inserting it as the last child of the current node's parent.
public copy ( $data = null ) : static
return static

create() public method

Create a new record with the provided data. It is inserted as the last child of the current node's parent
public create ( array $data ) : static
$data array The data to use in the new record
return static The new node

equals() public method

Is $node this very node?
public equals ( TreeModel &$node ) : boolean
$node TreeModel
return boolean

findByPath() public method

Notes: - This will only work when you have a "slug" and a "hash" field in your table. - If the path starts with "/" we will use the root with lft=1. Otherwise the first component of the path is supposed to be the slug of the root node. - If the root node is not found you'll get null as the return value - You will also get null if any component of the path is not found
public findByPath ( string $path ) : TreeModel | null
$path string The path to locate
return TreeModel | null The found node or null if nothing is found

forceDelete() public method

Delete a node, either the currently loaded one or the one specified in $id. If an $id is specified that node is loaded before trying to delete it. In the end the data model is reset. If the node has any children nodes they will be removed before the node itself is deleted.
public forceDelete ( mixed $id = null )
$id mixed Primary key (id field) value

getAncestors() public method

Get all ancestors to this node but not the node itself. In other words it gets the path to the node, without the node itself.
public getAncestors ( ) : Collection
return FOF30\Model\DataModel\Collection

getAncestorsAndSelf() public method

Get all ancestors to this node and the node itself. In other words it gets the full path to the node and the node itself.
public getAncestorsAndSelf ( ) : Collection
return FOF30\Model\DataModel\Collection

getAncestorsAndSelfWithoutRoot() public method

Get all ancestors to this node and the node itself, but not the root node. If you want to
public getAncestorsAndSelfWithoutRoot ( ) : Collection
return FOF30\Model\DataModel\Collection

getAncestorsWithoutRoot() public method

Get all ancestors to this node but not the node itself and its root.
public getAncestorsWithoutRoot ( ) : Collection
return FOF30\Model\DataModel\Collection

getDescendants() public method

Note: all descendant nodes, even descendants of our immediate descendants, will be returned.
public getDescendants ( ) : Collection
return FOF30\Model\DataModel\Collection

getDescendantsAndSelf() public method

Note: all descendant nodes, even descendants of our immediate descendants, will be returned.
public getDescendantsAndSelf ( ) : Collection
return FOF30\Model\DataModel\Collection

getImmediateDescendants() public method

Get the immediate descendants (children). Unlike getDescendants it only goes one level deep into the tree structure. Descendants of descendant nodes will not be returned.
public getImmediateDescendants ( ) : Collection
return FOF30\Model\DataModel\Collection

getLeaves() public method

Get all leaf nodes in the tree. You may want to use the scopes to narrow down the search in a specific subtree or path.
public getLeaves ( ) : Collection
return FOF30\Model\DataModel\Collection

getLevel() public method

Gets the level (depth) of this node in the tree. The result is cached in $this->treeDepth for faster retrieval.
public getLevel ( ) : integer | mixed
return integer | mixed

getNestedList() public method

This is useful for creating HTML select elements showing the hierarchy in a human readable format.
public getNestedList ( string $column = 'title', null $key = null, string $seperator = ' ' ) : array
$column string
$key null
$seperator string
return array

getParent() public method

Returns the immediate parent of the current node
public getParent ( ) : static
return static

getRoot() public method

Returns the root node of the tree this node belongs to
public getRoot ( ) : static
return static

getSiblings() public method

Get all sibling nodes, except ourselves
public getSiblings ( ) : Collection
return FOF30\Model\DataModel\Collection

getSiblingsAndSelf() public method

Get all sibling nodes, including ourselves
public getSiblingsAndSelf ( ) : Collection
return FOF30\Model\DataModel\Collection

inSameScope() public method

Returns true if both this node and $otherNode are root, leaf or child (same tree scope)
public inSameScope ( TreeModel $otherNode ) : boolean
$otherNode TreeModel
return boolean

insertAsChildOf() public method

Alias for insertAsLastchildOf
public insertAsChildOf ( TreeModel &$parentNode )
$parentNode TreeModel

insertAsFirstChildOf() public method

WARNING: If it's an existing node it will be COPIED, not moved.
public insertAsFirstChildOf ( TreeModel &$parentNode )
$parentNode TreeModel The node which will become our parent

insertAsLastChildOf() public method

WARNING: If it's an existing node it will be COPIED, not moved.
public insertAsLastChildOf ( TreeModel &$parentNode )
$parentNode TreeModel The node which will become our parent

insertAsRoot() public method

Insert the current node as a tree root. It is a good idea to never use this method, instead providing a root node in your schema installation and then sticking to only one root.
public insertAsRoot ( ) : static
return static

insertAsSiblingOf() public method

Alias for insertRightOf
public insertAsSiblingOf ( TreeModel &$siblingNode )
$siblingNode TreeModel

insertLeftOf() public method

WARNING: If it's an existing node it will be COPIED, not moved.
public insertLeftOf ( TreeModel &$siblingNode )
$siblingNode TreeModel We will be inserted before this node

insertRightOf() public method

WARNING: If it's an existing node it will be COPIED, not moved.
public insertRightOf ( TreeModel &$siblingNode )
$siblingNode TreeModel We will be inserted after this node

insideSubtree() public method

Checks if our node is inside the subtree of $otherNode. This is a fast check as only lft and rgt values have to be compared.
public insideSubtree ( TreeModel $otherNode ) : boolean
$otherNode TreeModel
return boolean

isAncestorOf() public method

Returns true if we are an ancestor of $otherNode
public isAncestorOf ( TreeModel $otherNode ) : boolean
$otherNode TreeModel
return boolean

isChild() public method

Is this a child node (not root)?
public isChild ( ) : boolean
return boolean

isDescendantOf() public method

Returns true if we are a descendant of $otherNode
public isDescendantOf ( TreeModel $otherNode ) : boolean
$otherNode TreeModel
return boolean

isLeaf() public method

Is this a leaf node (a node without children)?
public isLeaf ( ) : boolean
return boolean

isRoot() public method

Is this a top-level root node?
public isRoot ( ) : boolean
return boolean

isSelfOrAncestorOf() public method

Returns true if $otherNode is ourselves or we are an ancestor of $otherNode
public isSelfOrAncestorOf ( TreeModel $otherNode ) : boolean
$otherNode TreeModel
return boolean

isSelfOrDescendantOf() public method

Returns true if $otherNode is ourselves or if we are a descendant of $otherNode
public isSelfOrDescendantOf ( TreeModel $otherNode ) : boolean
$otherNode TreeModel
return boolean

makeChildOf() public method

Alias for makeLastChildOf
public makeChildOf ( TreeModel $parentNode )
$parentNode TreeModel

makeFirstChildOf() public method

Moves a node and its subtree as a the first (leftmost) child of $parentNode
public makeFirstChildOf ( TreeModel $parentNode )
$parentNode TreeModel

makeLastChildOf() public method

Moves a node and its subtree as a the last (rightmost) child of $parentNode
public makeLastChildOf ( TreeModel $parentNode )
$parentNode TreeModel

makeNextSiblingOf() public method

Alias for moveToRightOf
public makeNextSiblingOf ( TreeModel $siblingNode )
$siblingNode TreeModel

makePreviousSiblingOf() public method

Alias for moveToLeftOf
public makePreviousSiblingOf ( TreeModel $siblingNode )
$siblingNode TreeModel

makeRoot() public method

Makes the current node a root (and moving its entire subtree along the way). This is achieved by moving the node to the right of its root node
public makeRoot ( )

makeSiblingOf() public method

Alias for makeNextSiblingOf
public makeSiblingOf ( TreeModel $siblingNode )
$siblingNode TreeModel

move() public method

Not supported in nested sets
public move ( integer $delta, string $where = '' ) : static
$delta integer Ignored
$where string Ignored
return static Self, for chaining

moveLeft() public method

Move the current node (and its subtree) one position to the left in the tree, i.e. before its left-hand sibling
public moveLeft ( )

moveRight() public method

Move the current node (and its subtree) one position to the right in the tree, i.e. after its right-hand sibling
public moveRight ( )

moveToLeftOf() public method

Moves the current node (and its subtree) to the left of another node. The other node can be in a different position in the tree or even under a different root.
public moveToLeftOf ( TreeModel $siblingNode )
$siblingNode TreeModel

moveToRightOf() public method

Moves the current node (and its subtree) to the right of another node. The other node can be in a different position in the tree or even under a different root.
public moveToRightOf ( TreeModel $siblingNode )
$siblingNode TreeModel

onAfterDelete() protected method

protected onAfterDelete ( $oid )

reorder() public method

Not supported in nested sets
public reorder ( string $where = '' ) : static
$where string Ignored
return static Self, for chaining

reset() public method

Reset the record data and the tree cache
public reset ( boolean $useDefaults = true, boolean $resetRelations = false ) : static
$useDefaults boolean Should I use the default values? Default: yes
$resetRelations boolean Should I reset the relations too? Default: no
return static Self, for chaining

resetTreeCache() protected method

Resets cached values used to speed up querying the tree
protected resetTreeCache ( ) : static
return static for chaining

scopeAncestors() protected method

get() will return all ancestor nodes but not ourselves
protected scopeAncestors ( ) : void
return void

scopeAncestorsAndSelf() protected method

get() will return all ancestor nodes and ourselves
protected scopeAncestorsAndSelf ( ) : void
return void

scopeDescendants() protected method

get() will return all descendants (even subtrees of subtrees!) but not ourselves
protected scopeDescendants ( ) : void
return void

scopeDescendantsAndSelf() protected method

get() will return all descendants (even subtrees of subtrees!) and ourselves
protected scopeDescendantsAndSelf ( ) : void
return void

scopeImmediateDescendants() protected method

get() will only return immediate descendants (first level children) of the current node
protected scopeImmediateDescendants ( ) : void
return void

scopeLeaves() protected method

get() will return only leaf nodes
protected scopeLeaves ( ) : void
return void

scopeSiblings() protected method

get() will return all sibling nodes but not ourselves
protected scopeSiblings ( ) : void
return void

scopeSiblingsAndSelf() protected method

get() will return all sibling nodes and ourselves
protected scopeSiblingsAndSelf ( ) : void
return void

scopeWithoutRoot() protected method

get() will not return our root if it's part of the query results
protected scopeWithoutRoot ( ) : void
return void

scopeWithoutSelf() protected method

get() will not return ourselves if it's part of the query results
protected scopeWithoutSelf ( ) : void
return void

withoutNode() public method

get() will not return the selected node if it's part of the query results
public withoutNode ( TreeModel $node ) : void
$node TreeModel The node to exclude from the results
return void

Property Details

$treeDepth protected property

The level (depth) of this node in the tree
protected $treeDepth

$treeNestedGet protected property

Should I perform a nested get (used to query ascendants/descendants)
protected $treeNestedGet

$treeParent protected property

The parent node of ourselves
protected $treeParent

$treeRoot protected property

The root node in the tree
protected $treeRoot