Source for file Common.php
Documentation is available at Common.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Wolfram Kriesing <wolfram@kriesing.de> |
// +----------------------------------------------------------------------+
// $Id: Common.php 320703 2011-12-08 22:08:40Z danielc $
require_once 'Tree/Tree.php';
require_once 'Tree/Error.php';
* common tree class, implements common functionality
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @var array you need to overwrite this array and give the keys/
var $_forceSetOption = false;
* put proper value-keys are given in each class, depending
* on the implementation only some options are needed or allowed,
* see the classes which extend this one
* @var array saves the options passed to the constructor
* @author Wolfram Kriesing <wolfram@kriesing.de>
$child = $this->getChild ($id);
* get the ids of the children of the given element
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @param integer ID of the element that the children shall be
* @param integer how many levels deep into the tree
* @return mixed an array of all the ids of the children of the element
* with id=$id, or false if there are no children
// returns false if no children exist
if (!($children = $this->getChildren ($id, $levels))) {
// return an empty array, if you want to know
// if there are children, use hasChildren
if ($children && sizeof($children)) {
foreach ($children as $aChild) {
$childrenIds[] = $aChild['id'];
* gets all the children and grand children etc.
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @param integer ID of the element that the children shall be
* @return mixed an array of all the children of the element with
* id=$id, or false if there are no children
// FIXXXME remove this method and replace it by getChildren($id,0)
* this method gets all the children recursively
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @param integer ID of the element that the children shall be
* @return mixed an array of all the ids of the children of the element
* with id=$id, or false if there are no children
if ($children = $this->getChildren ($id)) {
foreach ($children as $key => $aChild) {
$retChildren[] = &$children[$key];
// {{{ getAllChildrenIds()
* gets all the children-ids and grand children-ids
* @author Kriesing <wolfram@kriesing.de>
* @param integer ID of the element that the children shall
* @return mixed an array of all the ids of the children of the element
* or false if there are no children
foreach ($allChildren as $aNode) {
$childrenIds[] = $aNode['id'];
* get the id of the parent for the given element
* @param integer the id of the element for which the parentId
* @author Wolfram Kriesing <wolfram@kriesing.de>
$parent = $this->getParent ($id);
* this gets all the preceeding nodes, the parent and it's parent and so on
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @param integer the id of the element for which the parentId shall
* @return array of the parent nodes including the node with id $id
foreach($path as $aNode) {
* get the ids of the parents and all it's parents and so on
* it simply returns the ids of the elements returned by getParents()
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @param integer $id the id of the element for which the parentId
* @return array of the ids
foreach($parents as $aNode) {
$parentsIds[] = $aNode['id'];
* @author Wolfram Kriesing <wolfram@kriesing.de>
$next = $this->getNext ($id);
* @author Wolfram Kriesing <wolfram@kriesing.de>
$previous = $this->getPrevious ($id);
* @author Wolfram Kriesing <wolfram@kriesing.de>
$left = $this->getLeft ($id);
* @author Wolfram Kriesing <wolfram@kriesing.de>
$right = $this->getRight ($id);
* @author Wolfram Kriesing <wolfram@kriesing.de>
$firstRoot = $this->getFirstRoot ();
* @author Wolfram Kriesing <wolfram@kriesing.de>
$firstRoot = $this->getRoot ();
* returns the path as a string
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @param mixed $id the id of the node to get the path for
* @param integer If offset is positive, the sequence will
* start at that offset in the array . If
* offset is negative, the sequence will start that far
* from the end of the array.
* @param integer If length is given and is positive, then
* the sequence will have that many elements in it. If
* length is given and is negative then the
* sequence will stop that many elements from the end of
* the array. If it is omitted, then the sequence will
* have everything from offset up until the end
* @param string you can tell the key the path shall be used to be
* constructed with i.e. giving 'name' (=default) would
* use the value of the $element['name'] for the node-name
* (thanks to Michael Johnson).
* @return array this array contains all elements from the root
* to the element given by the id
$offset = 0 , $length = 0 , $key = 'name')
foreach ($path as $aNode) {
$pathArray[] = $aNode[$key];
$pathArray = array_slice($pathArray, $offset, $length);
$pathString = implode($seperator, $pathArray);
// abstract methods, those should be overwritten by the implementing class
* gets the path to the element given by its id
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @param mixed $id the id of the node to get the path for
* @return array this array contains all elements from the root
* to the element given by the id
* gets the path to the element given by its id
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @param mixed $id the id of the node to get the path for
* @return array this array contains the path elements and the sublevels
* to substract if no $cwd has been given.
function _preparePath ($path, $cwd = '/', $separator = '/'){
$elems = explode($separator, $path);
// beginning with a slash
if (empty ($elems[$cntElems-1 ])) {
// Get the real path, and the levels
// to substract if required
while ($elems[0 ] == '..') {
if ($down >= 0 && $cwd == '/') {
list ($_elems, $sublevel) = $this->_preparePath ($cwd);
return array ($_elems, $sublevel);
* get the level, which is how far below the root the element
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @param mixed $id the id of the node to get the level for
* returns if $childId is a child of $id
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @param int id of the element
* @param int id of the element to check if it is a child
* @param boolean if this is true the entire tree below is checked
* @return boolean true if it is a child
function isChildOf($id, $childId, $checkAll = true )
$nodeName = 'name', $seperator = '/')
* return the maximum depth of the tree
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @return int the depth of the tree
return $this->_treeDepth;
* prepare multiple results
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @param array the data to prepare
* @return array prepared results
function &_prepareResults ($results)
foreach ($results as $key => $aResult) {
$newResults[$key] = $this->_prepareResult ($aResult);
* map back the index names to get what is expected
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @return array the prepared result
function _prepareResult ($result)
foreach ($map as $key => $columnName) {
if (isset ($result[$columnName])) {
$temp = $result[$columnName]; // remember the value from the old name
unset ($result[$columnName]); // remove the old one
$result[$key] = $temp; // save it in the mapped col-name
* this method retrieves the real column name, as used in the DB
* since the internal names are fixed, to be portable between different
* DB-column namings, we map the internal name to the real column name here
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @param string the internal name used
* @return string the real name of the column
function _getColName ($internalName)
if ($map = $this->getOption('columnNameMaps')) {
if (isset ($map[$internalName])) {
return $map[$internalName];
* @author Pierre-Alain Joye <paj@pearfr.org>
* @param string the error message
* @param int the line in which the error occured
* @param mixed the error mode
* @return object a Tree_Error
function _raiseError ($errorCode, $msg = '', $line = 0 )
include_once 'Tree/Error.php';
$msg, $line, __FILE__ , $mode, $this->dbh->last_query );
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @param string the error message
* @param int the line in which the error occured
* @param mixed the error mode
* @return object a Tree_Error
function _throwError ($msg, $line, $mode = null )
include_once 'Tree/Error.php';
if ($mode === null && $this->debug > 0 ) {
$mode = PEAR_ERROR_PRINT;
$msg, $line, __FILE__ , $mode, $this->dbh->last_query );
/*******************************************************************************/
/************************ METHODS FROM Tree_Memory *****************************/
/*******************************************************************************/
* returns if the given element has any children
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @param integer $id the id of the node to check for children
* @return boolean true if the node has children
if (isset ($this->data[$id]['children']) &&
sizeof($this->data[$id]['children']) > 0 ) {
/*******************************************************************************/
/************************ METHODS FROM Tree_Options ****************************/
/*******************************************************************************/
* this constructor sets the options, since i normally need this and
* in case the constructor doesnt need to do anymore i already have
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @param array the key-value pairs of the options that shall be set
* @param boolean if set to true options are also set
* even if no key(s) was/were found in the options property
$this->_forceSetOption = $force;
foreach ($options as $key=> $value) {
* @param string the option name
* @param mixed the value for this option
* @param boolean if set to true options are also set
* even if no key(s) was/were found in the options property
function setOption($option, $value, $force = false )
// if the value is an array extract the keys
// and apply only each value that is set
// so we dont override existing options inside an array
// if an option is an array
foreach ($value as $key=> $aValue) {
$this->setOption(array ($option,$key),$aValue);
$mainOption = $option[0 ];
$options = "['". implode("']['",$option). "']";
$evalCode = "\$this->options". $options. " = \$value;";
$evalCode = "\$this->options[\$option] = \$value;";
if ($this->_forceSetOption == true ||
$force == true || isset ($this->options[$mainOption])) {
* set a number of options which are simply given in an array
* @param array the values to set
* @param boolean if set to true options are also set even if no key(s)
* was/were found in the options property
foreach ($options as $key => $value) {
* @author copied from PEAR: DB/commmon.php
* @param boolean true on success
$evalCode = "\$ret = \$this->options['".
if (isset ($this->options[$option])) {
* returns all the options
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @return string all options as an array
Documentation generated on Mon, 11 Mar 2019 15:47:03 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|