Copyright 2010-2011 Riccardo Attilio Galli <riccardo@sideralis.org> http://www.sideralis.org
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Root exception. Each exception thrown in a Sideralis Programs library derive from this class
Exception holding informations about errors occurred when using mysql
Parameters: |
|
---|
Node of a Baobab tree
Parameters: |
|
---|
id int, node id
lft int, left value
rgt int, right value
parentNode int, the parent node id
fields_values array, additional fields of the node
children array, instances of BaobabNode children of the current node
Note
this class doesn’t have database interaction, its purpose is just to have a runtime representation of a Baobab tree. The data inserted is supposed to be valid in his tree (e.g. $this->lft cant’ be -1 or major of any node right value)
Add a node as last sibling of the node’s children.
Parameters: | $child (BaobabNode) – append a node to the list of this node children |
---|
Return a representation of the tree as a string.
Parameters: |
|
---|---|
Returns: | tree or node representation |
Return type: | string |
Note
$indentLevel is meant for internal use only.
Check if the node is rightmost between his siblings.
Returns: | whether if the node is the rightmost or not |
---|---|
Return type: | boolean |
Check if the node is leftmost between his siblings.
Returns: | whether if the node is the leftmost or not |
---|---|
Return type: | boolean |
This class lets you create, populate search and destroy a tree stored using the Nested Set Model described by Joe Celko.
Parameters: |
|
---|
Apply the database schema
Returns: | TRUE if any table was added, FALSE if the build was skipped (e.g. if the tree yet exists calling build will do nothing and return FALSE). |
---|---|
Return type: | boolean |
Note
This is automatically called while instantiating the class. If the version of the library didn’t change in the meantime, nothing happens, otherwise it will throw an exception to inform you to use Baobab:upgrade()
Upgrade the database schema of all the trees to reflect the current library’s version.
Parameters: |
|
---|
Warning
To avoid any possible data loss you should backup your database’s tables first.
Warning
For each release read release notes and check what’s going to happen when you upgrade your database (see sql/upgrade/* files). Eventually stop with $untilVersion at a certain point to apply needed tweaks.
Parameters: | $removeDataTable (boolean) – unless this value is TRUE, avoid to delete the table that holds the tree data |
---|
Delete all the records about this tree. Other trees in the same table will be unaffected.
Delete all the records in $forest_name
Parameters: |
|
---|
Return the id of the first node of the tree.
Returns: | id of the root, or NULL if empty |
---|---|
Return type: | int or NULL |
Return the id of the node’s parent.
Returns: | id of the parent, or NULL if node is root |
---|---|
Return type: | int or NULL |
Retrieve the number of nodes of the subtree starting at $id_node (or at tree root if $id_node is NULL).
Parameters: | $id_node (int or NULL) – id of the node to count from (or NULL to count from root) |
---|---|
Returns: | the number of nodes in the selected subtree |
Return type: | int |
Retrieve all the descendants of a node
Parameters: | $id_node (int or NULL) – id of the node whose descendants we’re searching for, or NULL to start from the tree root. |
---|---|
Returns: | the ids of node’s descendants, in ascending order |
Return type: | array |
Find the leaves of a subtree.
Parameters: | $id_node (int or NULL) – id of a node or NULL to start from the tree root |
---|---|
Returns: | the ids of the leaves, ordered from left to right |
Return type: | array |
Find at what level of the tree each node is.
Parameters: | $id_node (int or NULL) – id of a node or NULL to start from the tree root |
---|---|
Returns: | associative arrays with id => number, level => number, unordered |
Return type: | array |
Note
tree root is at level 0
Find all the nodes between tree root and a node.
Parameters: |
|
---|---|
Returns: | sequence of associative arrays mapping for each node fieldName => value, where field names are the one present in $fields plus the field “id” (unless $squash was set), ordered from root to $id_node |
Return type: | array |
Example (considering a tree with two nodes with a field ‘name’):
php> $tree->getPath(2, array("name"))
array([0] => array([id] => 1, [name] => 'rootName'), array([id] => 2, [name] => 'secondNodeName']))
php> join("/", $tree->getPath(2, "name", TRUE))
"rootName/secondNodeName"
Find the first n node’s children starting from left or right.
Parameters: |
|
---|---|
Returns: | ids of the children nodes, ordered from left to right |
Return type: | array |
Find all node’s children
Parameters: | $id_parent (int) – id of the parent node |
---|---|
Returns: | ids of the children nodes, ordered from left to right |
Return type: | array |
Find the leftmost child of a node
Parameters: | $id_parent (int) – id of the parent node |
---|---|
Returns: | id of the leftmost child node, or 0 if not found |
Return type: | int |
Find the rightmost child of a node
Parameters: | $id_parent (int) – id of the parent node |
---|---|
Returns: | id of the rightmost child node, or 0 if not found |
Return type: | int |
Find the nth child of a parent node
Parameters: |
|
---|---|
Returns: | id of the nth child node |
Return type: | int |
Create a tree from the database data. It’s possible to use a default tree or use custom classes/functions (it must extends the class BaobabNode)
Parameters: |
|
---|---|
Returns: | a node instance |
Return type: | instance of $className |
Delete a node and all of his children. If $close_gaps is TRUE, mantains the Modified Preorder Tree consistent closing gaps.
Parameters: |
|
---|
Warning
If the gaps are not closed, you can’t use most of the API. Usually you want to avoid closing gaps when you’re deleting different subtrees and want to update the numbering just once (see Baobab.closeGaps)
Update right and left values of each node to ensure there are no gaps in the tree.
Warning
This is a really slow function, use it only if needed (e.g. to delete multiple subtrees and close gaps just once)
Calculate the height of the tree
Returns: | the height of the tree |
---|---|
Return type: | int |
Note
A tree with one node has height 1.
Update data associeted to a node
Parameters: |
|
---|
Retrieve informations about a node.
Parameters: |
|
---|---|
Returns: | the informations found |
Return type: | array |
Create and append a node as last child of a parent node. If no parent is given, the new node will become the root node.
Parameters: |
|
---|---|
Returns: | id of new node |
Return type: | int |
Create a new node and insert it as the next sibling of the node chosen (which can not be root)
Parameters: |
|
---|---|
Returns: | id of the new node |
Return type: | int |
Create a new node and insert it as the previous sibling of the node chosen (which can not be root)
Parameters: |
|
---|---|
Returns: | id of the new node |
Return type: | int |
Create a new node and insert it as the nth child of the parent node chosen
Parameters: |
|
---|---|
Returns: | id of the new node |
Return type: | int |
Move a node and all of his children as right sibling of another node.
Parameters: |
|
---|
Move a node and all of his children as left sibling of another node.
Parameters: |
|
---|
Move a node as nth child of another node.
Parameters: |
|
---|
Load data about a single tree (as generated by the export method).
Parameters: |
|
---|---|
Returns: | an array of Baobab tree instances |
Return type: | array |
$data JSON format is like the following
[
{
"tree_id": 6, //optional, it could also be one of the fields, or none at all
"fields" : ["id", "lft", "rgt"],
"values" :
[1,1,4,[
[2,2,3,[]]
]]
}
// more could follow ...
]
Note
If “id” field is used and the nodes values are not NULL, mustn’t exist in the table a record with that same value.
Note
If “tree_id” is used (as attribute or field) and not NULL, mustn’t exist in the table records belonging to that same tree.
Create a JSON dump of one or more trees
Parameters: |
|
---|---|
Returns: | a dump of the trees in JSON format |
Return type: | string |
Note
if ‘tree_id’ is passed as field, it will not appear in the field list because redundat (it will be present once in the tree_id attribute)
Example of an exported tree
[
{
"tree_id": 6,
"fields" : ["id", "lft", "rgt"], // tree_id is stripped if requested via fields because redundant
"values" :
[1,1,4,[
[2,2,3,[]]
]]
}
// more could follow ...
]