Class dbtree

Description

$Id: dbtree.class.php,v 2.0 2005/09/09 19:32:45 Kuzma Exp $

Copyright (C) 2005 Kuzma Feskov <kuzma@russofile.ru>

KF_SITE_VERSION

CLASS DESCRIPTION: This class can be used to manipulate nested sets of database table records that form an hierarchical tree.

It provides means to initialize the record tree, insert record nodes in specific tree positions, retrieve node and parent records, change position of nodes and delete record nodes.

It uses ANSI SQL statements and abstract DB libraryes, such as: ADODB Provides full functionality of the class: to make it work with many database types, support transactions, and caching of SQL queries to minimize database access overhead DB_MYSQL The class-example showing variant of creation of the own engine for dialogue with a database, it's emulate some ADODB functions (ATTENTION, class only shows variant of a spelling of the driver, use it only as example)

The library works with support multilanguage interface of technology GetText (GetText autodetection).

This source file is part of the KFSITE Open Source Content Management System.

This file may be distributed and/or modified under the terms of the "GNU General Public License" version 2 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.

This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

The "GNU General Public License" (GPL) is available at http://www.gnu.org/copyleft/gpl.html.

CHANGELOG:

v2.0

[+] GetText autodetect added

[+] DB libraries abstraction added

Located in /dbtree.class.php (line 54)


        
                        
Variable Summary
object $db
array $ERRORS
array $ERRORS_MES
object $res
string $table
bigint $table_id
integer $table_left
integer $table_level
integer $table_right
Method Summary
object dbtree (string $table, string $prefix, object &$db)
array Ajar (integer $ID, array $fields, [array $condition = ''], [integer $cache = FALSE])
array Branch (integer $ID, array $fields, [array $condition = ''], [integer $cache = FALSE])
bool ChangePosition (integer $id1, integer $id2)
bool ChangePositionAll (integer $id1, integer $id2, [string $position = 'after'])
bool Clear ([array $data = array()])
bool Delete (integer $ID, [array $condition = ''])
bool DeleteAll (integer $ID, [array $condition = ''])
array Full (array $fields, [array $condition = ''], [integer $cache = FALSE])
array GetNodeInfo (integer $section_id, [integer $cache = FALSE])
array GetParentInfo (integer $section_id, [integer $cache = FALSE])
integer Insert (integer $section_id, [array $condition = ''], [array $data = array()])
integer InsertNear (integer $ID, [array $condition = ''], [array $data = array()])
bool MoveAll (integer $ID, integer $newParentId, [array $condition = ''])
array NextRow ()
array Parents (integer $ID, array $fields, [array $condition = ''], [integer $cache = FALSE])
integer RecordCount ()
string _PrepareCondition (array $condition, [bool $where = FALSE], [string $prefix = ''])
Variables
object $db (line 115)

Databse layer object.

array $ERRORS = array() (line 62)

Detailed errors of a class (for the programmer and log-files) array('error type (1 - fatal (write log), 2 - fatal (write log, send email)', 'error info string', 'function', 'info 1', 'info 2').

array $ERRORS_MES = array() (line 70)

The information on a error for the user array('string (error information)').

object $res (line 108)

DB resource object.

string $table (line 77)

Name of the table where tree is stored.

bigint $table_id (line 84)

Unique number of node.

integer $table_left (line 89)
integer $table_level (line 101)

Level of nesting.

integer $table_right (line 94)
Methods
Constructor dbtree (line 125)

The class constructor: initializes dbtree variables.

object dbtree (string $table, string $prefix, object &$db)
  • string $table: Name of the table
  • string $prefix: A prefix for fields of the table(the example, mytree_id. 'mytree' is prefix)
  • object $db
Ajar (line 730)

Returns a slightly opened tree from an element with number $ID.

  • return: - [0] => array(id, left, right, level, additional fields), [1] => array(...), etc.
array Ajar (integer $ID, array $fields, [array $condition = ''], [integer $cache = FALSE])
  • array $condition: Array structure: array('and' => array('id = 0', 'id2 >= 3'), 'or' => array('sec = \'www\'', 'sec2 <> \'erere\'')), etc where array key - condition (AND, OR, etc), value - condition string
  • array $fields: needed fields (if is): array('filed1 name', 'filed2 name', etc)
  • integer $cache: Recordset is cached for $cache microseconds
  • integer $ID: Node unique id
Branch (line 660)

Returns all elements of a branch starting from an element with number $ID.

  • return: - [0] => array(id, left, right, level, additional fields), [1] => array(...), etc.
array Branch (integer $ID, array $fields, [array $condition = ''], [integer $cache = FALSE])
  • array $condition: Array structure: array('and' => array('id = 0', 'id2 >= 3'), 'or' => array('sec = \'www\'', 'sec2 <> \'erere\'')), etc where array key - condition (AND, OR, etc), value - condition string
  • array $fields: needed fields (if is): array('filed1 name', 'filed2 name', etc)
  • integer $cache: Recordset is cached for $cache microseconds
  • integer $ID: Node unique id
ChangePosition (line 420)

Change items position.

  • return: TRUE if successful, FALSE otherwise.
bool ChangePosition (integer $id1, integer $id2)
  • integer $id1: first item ID
  • integer $id2: second item ID
ChangePositionAll (line 468)

Swapping nodes within the same level and limits of one parent with all its children: $id1 placed before or after $id2.

  • return: TRUE if successful, FALSE otherwise.
bool ChangePositionAll (integer $id1, integer $id2, [string $position = 'after'])
  • integer $id1: first item ID
  • integer $id2: second item ID
  • string $position: 'before' or 'after' $id2
Clear (line 142)

Sets initial parameters of a tree and creates root of tree ATTENTION, all previous values in table are destroyed.

  • return: TRUE if successful, FALSE otherwise.
bool Clear ([array $data = array()])
  • array $data: Contains parameters for additional fields of a tree (if is): 'filed name' => 'importance'
Delete (line 538)

Delete element with number $id from the tree wihtout deleting it's children.

  • return: TRUE if successful, FALSE otherwise.
bool Delete (integer $ID, [array $condition = ''])
  • integer $ID: Number of element
  • array $condition: Array structure: array('and' => array('id = 0', 'id2 >= 3'), 'or' => array('sec = \'www\'', 'sec2 <> \'erere\'')), etc where array key - condition (AND, OR, etc), value - condition string
DeleteAll (line 582)

Delete element with number $ID from the tree and all it childret.

  • return: TRUE if successful, FALSE otherwise.
bool DeleteAll (integer $ID, [array $condition = ''])
  • integer $ID: Number of element
  • array $condition: Array structure: array('and' => array('id = 0', 'id2 >= 3'), 'or' => array('sec = \'www\'', 'sec2 <> \'erere\'')), etc where array key - condition (AND, OR, etc), value - condition string
Full (line 625)

Returns all elements of the tree sortet by left.

  • return: needed fields
array Full (array $fields, [array $condition = ''], [integer $cache = FALSE])
  • array $condition: Array structure: array('and' => array('id = 0', 'id2 >= 3'), 'or' => array('sec = \'www\'', 'sec2 <> \'erere\'')), etc where array key - condition (AND, OR, etc), value - condition string
  • array $fields: needed fields (if is): array('filed1 name', 'filed2 name', etc)
  • integer $cache: Recordset is cached for $cache microseconds
GetNodeInfo (line 181)

Receives left, right and level for unit with number id.

  • return: - left, right, level
array GetNodeInfo (integer $section_id, [integer $cache = FALSE])
  • integer $section_id: Unique section id
  • integer $cache: Recordset is cached for $cache microseconds
GetParentInfo (line 209)

Receives parent left, right and level for unit with number $id.

  • return: - left, right, level
array GetParentInfo (integer $section_id, [integer $cache = FALSE])
  • integer $section_id
  • integer $cache: Recordset is cached for $cache microseconds
Insert (line 243)

Add a new element in the tree to element with number $section_id.

  • return: Inserted element id
integer Insert (integer $section_id, [array $condition = ''], [array $data = array()])
  • integer $section_id: Number of a parental element
  • array $condition: Array structure: array('and' => array('id = 0', 'id2 >= 3'), 'or' => array('sec = \'www\'', 'sec2 <> \'erere\'')), etc where array key - condition (AND, OR, etc), value - condition string
  • array $data: Contains parameters for additional fields of a tree (if is): array('filed name' => 'importance', etc)
InsertNear (line 299)

Add a new element in the tree near element with number id.

  • return: Inserted element id
integer InsertNear (integer $ID, [array $condition = ''], [array $data = array()])
  • integer $ID: Number of a parental element
  • array $condition: Array structure: array('and' => array('id = 0', 'id2 >= 3'), 'or' => array('sec = \'www\'', 'sec2 <> \'erere\'')), etc where array key - condition (AND, OR, etc), value - condition string
  • array $data: Contains parameters for additional fields of a tree (if is): array('filed name' => 'importance', etc)
MoveAll (line 355)

Assigns a node with all its children to another parent.

  • return: TRUE if successful, FALSE otherwise.
bool MoveAll (integer $ID, integer $newParentId, [array $condition = ''])
  • integer $ID: node ID
  • integer $newParentId: ID of new parent node
  • array $condition: Array structure: array('and' => array('id = 0', 'id2 >= 3'), 'or' => array('sec = \'www\'', 'sec2 <> \'erere\'')), etc where array key - condition (AND, OR, etc), value - condition string
NextRow (line 807)

Returns the current row.

array NextRow ()
Parents (line 695)

Returns all parents of element with number $ID.

  • return: - [0] => array(id, left, right, level, additional fields), [1] => array(...), etc.
array Parents (integer $ID, array $fields, [array $condition = ''], [integer $cache = FALSE])
  • array $condition: Array structure: array('and' => array('id = 0', 'id2 >= 3'), 'or' => array('sec = \'www\'', 'sec2 <> \'erere\'')), etc where array key - condition (AND, OR, etc), value - condition string
  • array $fields: needed fields (if is): array('filed1 name', 'filed2 name', etc)
  • integer $cache: Recordset is cached for $cache microseconds
  • integer $ID: Node unique id
RecordCount (line 798)

Returns amount of lines in result.

integer RecordCount ()
_PrepareCondition (line 822)

Transform array with conditions to SQL query Array structure: array('and' => array('id = 0', 'id2 >= 3'), 'or' => array('sec = \'www\'', 'sec2 <> \'erere\'')), etc where array key - condition (AND, OR, etc), value - condition string.

string _PrepareCondition (array $condition, [bool $where = FALSE], [string $prefix = ''])
  • array $condition
  • string $prefix
  • bool $where: - True - yes, flase - not

Documentation generated on Fri, 16 Sep 2005 16:45:04 +0400 by phpDocumentor 1.3.0RC3