PHP InfinityTree Doc for users

How to use InfinityTree

Before you start to use this library, you need to check if you have the following:

  • MySQLi support
  • PHP > 5.4

If you have the above, you can start to implement your tree.

Include the library

To use InfinityTree, you have to include the autoloader. You can do this by adding:

include('src/autoload.php');

Using config

By default, InfinityTree uses a config file in the source directory(src/config.php) that you can modify. This config file contains the db configuration. By default, the storage used is MySQLi database. You need to set the config of a MySQL database.

You can also pass a config array during initialization to the constructor that has the following structure:

$config = array(
  'db' => array( // DB Config for DB storage
    'server' => 'localhost', // Host you want to connect to, like localhost
    'user' => 'root', // DB user
    'pass' => 'root', // DB password
    'database' => 'test', // Name of the database
),

Then:

$tree = new InfinityTree($config);

Init the tree

Infinity Tree comes with its own functionality to create the storage. By default, if the table set in config does not exist, it creates for itself with the proper structure, so you do not have to do it.

InfinityTree uses the same storage to store more than one tree. Every tree has its own id. When initializing the tree, you can set the id. If you do not pass an id, the base tree with the id of 1 will be used. When a tree gets initialized, it gets a root node automatically.

$tree = new InfinityTree();
$tree->initTree($id);

Where $id can be any integer number.

Getting the tree

To get the tree, you have multiple options. Simplest way is to use:

$tree->treeAsArray();  - this gets the whole tree from the root as associative N depth array.

$tree->treeAsArray($root_id);  - the first param is the node you want to get the subtree of, null by default.

$tree->treeAsArray($root_id, true);  - the second param is called $flat and is false by default, if you set it to true, you will get the whole subtree as a one dimension array.

Add a node

Creating a node is very easy. You just pass the parent ID and a name. If you do not pass the parent ID, the node will be the child of the root element. Also, when creating the node, the node gets a unique slug generated from the name.

$tree->addNode(null, "Child 1"); creates a node named Child 1 under root node and returns the new node.

$tree->addNode(3, "Child 2"); creates a node named Child 2 under the node with the ID of 3.

Edit a node

This functionality lets you edit the name of a node (also resets the slug)

$tree->updateNode($node_id, $new_name);  - just pass the ID of the node and the new name

Delete a node

Delete function has 2 possibilities. You can delete a node or the node and the whole subtree.

$tree->deleteNode($node_id, $delete_subtree);  - The first param is the node ID, the second is a boolean, false by default. If set to true, it deletes the whole subtree, otherwise moves the subtree into root node.

Move a node

Excluding the root node, you can move any node to any other node. Move functionality has 3 parameters:

$tree->moveNode($node_id, $to_id, $position);

The first param is the node to move. The second is the node to move under, same as parent if only order changes on given depth. The third param is the position of the node on a given level.

For example if you want to move the first node on the same level to the last position, given there are 3 nodes on that level, you do it like this:

$tree->moveNode($node->id, $node->parent, 3);

Error handling

Every function in the tree can set errors. If there is an error, you can get it by calling:

$tree->getErrors();

This returns an associative array with the event as key that triggered the error, like: array('node' => 'Node inexistent'); if you pass and node ID that does not exist.

Finding a node

InfinityTree provides a function to find a node by ID, slug or name. Simply use:

$tree->findNode($by);

This will return an array with the found nodes, empty if none found.

Custom adapters

InfinityTree uses interfaces and adapters for the following things: storage, validation. If you want to change them, for example use other type of DB, just implement the interface and pass the adapter to the constructor.

$tree = new InfinityTree($config, new MyStorageAdapter(), new MyValidationAdapter());