<?php
// ----------------------------------------------------------------------
// Copyright (C) 2006 by Ahmad Alyan.
// http://www.alhasebat.com/
// ----------------------------------------------------------------------
// LICENSE
// This program is open source product; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------
// Filename: visitation.adodb.class.php
// Author(s): Ahmad Alyan <hide@address.com>
// Original Author(s): Khaled Al-Sham'aa <hide@address.com>
// Purpose: Utility class to implement visitation model developed by Joe Celko
// to enhance trees functions over SQL platform using ADOdb library
// ----------------------------------------------------------------------
class Visitation {
var $treeTable;
var $leftBranch;
var $rightBranch;
var $leafID;
var $leafParent;
/**
* @return returns value of variable $treeTable
* @desc getTreeTable : Getting value for variable $treeTable
*/
function getTreeTable ()
{
return $this->treeTable ;
}
/**
* @param param : value to be saved in variable $treeTable
* @desc setTreeTable : Setting value for $treeTable
*/
function setTreeTable ($value)
{
$this->treeTable = $value;
}
/**
* @return returns value of variable $leftBranch
* @desc getLeftBranch : Getting value for variable $leftBranch
*/
function getLeftBranch ()
{
return $this->leftBranch ;
}
/**
* @param param : value to be saved in variable $leftBranch
* @desc setLeftBranch : Setting value for $leftBranch
*/
function setLeftBranch ($value)
{
$this->leftBranch = $value;
}
/**
* @return returns value of variable $rightBranch
* @desc getRightBranch : Getting value for variable $rightBranch
*/
function getRightBranch ()
{
return $this->rightBranch ;
}
/**
* @param param : value to be saved in variable $rightBranch
* @desc setRightBranch : Setting value for $rightBranch
*/
function setRightBranch ($value)
{
$this->rightBranch = $value;
}
/**
* @return returns value of variable $leafID
* @desc getLeafID : Getting value for variable $leafID
*/
function getLeafID ()
{
return $this->leafID ;
}
/**
* @param param : value to be saved in variable $leafID
* @desc setLeafID : Setting value for $leafID
*/
function setLeafID ($value)
{
$this->leafID = $value;
}
/**
* @return returns value of variable $leafParent
* @desc getLeafParent : Getting value for variable $leafParent
*/
function getLeafParent()
{
return $this->leafParent;
}
/**
* @param param : value to be saved in variable $leafParent
* @desc setLeafParent : Setting value for $leafParent
*/
function setLeafParent($value)
{
$this->leafParent = $value;
}
// This is the constructor for this class
// Initialize all your default variables here
function Visitation()
{
$this->setTreeTable ("");
$this->setLeftBranch ("");
$this->setRightBranch ("");
$this->setLeafID ("");
$this->setLeafParent("");
}
// This function will clear all the values of variables in this class
function emptyInfo()
{
$this->setTreeTable ("");
$this->setLeftBranch ("");
$this->setRightBranch ("");
$this->setLeafID ("");
$this->setLeafParent("");
}
/**
* @param resource $db ADOdb object
* @desc Update left/right fields in tree records after ADD/EDIT/DELETE nodes
* @author Ahamad Alyan
* @original author Khaled Al-Shamaa
*/
function updateTree($db){
global $i;
$i = 0;
$strsql = "UPDATE `".$this->treeTable."` SET `".$this->leftBranch."`=1 WHERE `".$this->leafID."`=1";
$db->Execute($strsql) or die("Error in query: $strsql. " .$db->ErrorMsg());
$this->child(1,0,$db);
$i++;
$strsql = "UPDATE `".$this->treeTable."` SET `".$this->rightBranch."`=$i WHERE `".$this->leafID."`=1";
$db->Execute($strsql) or die("Error in query: $strsql. " .$db->ErrorMsg());
}
/**
* @desc Private method for local use by updateTree method
* @author Ahamad Alyan
* @original author Khaled Al-Shamaa
*/
function child($x_id, $x_depth, $db)
{
global $i;
$x_depth++;
$strsql = "SELECT `".$this->leafID."` FROM `".$this->treeTable."` WHERE `".$this->leafParent."`=" . $x_id . " ORDER BY `".$this->leafID."`";
$rs = $db->Execute($strsql) or die("Error in query: $strsql. " .$db->ErrorMsg());
while (!$rs->EOF)
{
$i++;
$link_id=$rs->fields[$this->leafID] ;
if ($x_id != $link_id)
{
$strsql = "UPDATE `".$this->treeTable."` SET `".$this->leftBranch."`=$i WHERE `".$this->leafID."`=$link_id";
$db->Execute($strsql) or die("Error in query: $strsql. " .$db->ErrorMsg());
$this->child($link_id, $x_depth, $db);
$i++;
$strsql = "UPDATE `".$this->treeTable."` SET `".$this->rightBranch."`=$i WHERE `".$this->leafID."`=$link_id";
$db->Execute($strsql) or die("Error in query: $strsql. " .$db->ErrorMsg());
}
$rs->MoveNext();
}
}
/**
* @return Array Leaf nodes
* @param Resource $db ADOdb object
* @desc Find out leaf nodes in current tree (nodes have no child)
* @author Ahamad Alyan
* @original author Khaled Al-Shamaa
*/
function findLeaf($db)
{
$pages = array();
$strsql = "SELECT `".$this->leafID."` FROM `".$this->treeTable."` WHERE `".$this->rightBranch."`-`".$this->leftBranch."`=1 ORDER BY `".$this->leftBranch."`";
$rs = $db->Execute($strsql) or die("Error in query: $strsql. " .$db->ErrorMsg());
while (!$rs ->EOF)
{
array_push($pages, $rs->fields[$this->leafID]);
$rs->MoveNext();
}
return $pages;
}
/**
* @return Array Members nodes
* @param Resource $db ADOdb object
* @param Integer $id Node id
* @desc Find out members of sub tree for given node
* @author Ahamad Alyan
* @original author Khaled Al-Shamaa
*/
function findMembers($db, $id){
$pages = array();
$strsql = "SELECT `".$this->leftBranch."`,`".$this->rightBranch."` FROM `".$this->treeTable."` WHERE `".$this->leafID."`=" . $id;
$rs = $db->Execute($strsql) or die("Error in query: $strsql. " .$db->ErrorMsg());
$left = $rs->fields[$this->leftBranch];
$right = $rs->fields[$this->rightBranch];
$strsql = "SELECT `".$this->leafID."` FROM `".$this->treeTable."` WHERE `".$this->leftBranch."`>=$left AND `".$this->leftBranch."`<=$right ORDER BY `".$this->leftBranch."`";
$rs = $db->Execute($strsql) or die("Error in query: $strsql. " .$db->ErrorMsg());
while (!$rs ->EOF)
{
array_push($pages, $rs->fields[$this->leafID]);
$rs->MoveNext();
}
return $pages;
}
/**
* @return Array Context/path nodes
* @param Resource $db ADOdb object
* @param Integer $id Node id
* @desc Find out context (the path to a specific node) of given node
* @author Ahamad Alyan
* @original author Khaled Al-Shamaa
*/
function fetchContext($db, $id){
$pages = array();
$strsql = "SELECT `".$this->leftBranch."`,`".$this->rightBranch."` FROM `".$this->treeTable."` WHERE `".$this->leafID."`=" . $id;
$rs = $db->Execute($strsql) or die("Error in query: $strsql. " .$db->ErrorMsg());
$left = $rs->fields[$this->leftBranch];
$right = $rs->fields[$this->rightBranch];
$strsql = "SELECT `".$this->leafID."` FROM `".$this->treeTable."` WHERE `".$this->leftBranch."`<=$left AND `".$this->rightBranch."`>=$right ORDER BY `".$this->leftBranch."`";
$rs = $db->Execute($strsql) or die("Error in query: $strsql. " .$db->ErrorMsg());
while (!$rs ->EOF)
{
array_push($pages, $rs->fields[$this->leafID]);
$rs->MoveNext();
}
return $pages;
}
/**
* @return Integer Level/deepth of the node
* @param Resource $db ADOdb object
* @param Integer $id Node id
* @desc Find out level/deepth of given node
* @author Ahamad Alyan
* @original author Khaled Al-Shamaa
*/
function findLevel($db, $id){
$pages = array();
$strsql = "SELECT `".$this->leftBranch."`,`".$this->rightBranch."` FROM `".$this->treeTable."` WHERE `".$this->leafID."`=" . $id;
$rs = $db->Execute($strsql) or die("Error in query: $strsql. " .$db->ErrorMsg());
$left = $rs->fields[$this->leftBranch];
$right = $rs->fields[$this->rightBranch];
$strsql = "SELECT COUNT(".$this->leafID.") as `levels` FROM `".$this->treeTable."` WHERE `".$this->leftBranch."`<=$left AND `".$this->rightBranch."`>=$right ORDER BY `".$this->leftBranch."`";
$rs = $db->Execute($strsql) or die("Error in query: $strsql. " .$db->ErrorMsg());
$levels = $rs->fields['levels'];
return $levels;
}
/**
* @return Integer Descendants number of the node
* @param resource $db ADOdb object
* @param integer $id Node id
* @desc Find out how many descendants given node has
* @author Ahamad Alyan
* @original author Khaled Al-Shamaa
*/
function descendants($db, $id){
$pages = array();
$strsql = "SELECT `".$this->leftBranch."`,`".$this->rightBranch."` FROM `".$this->treeTable."` WHERE `".$this->leafID."`=" . $id;
$rs = $db->Execute($strsql) or die("Error in query: $strsql. " .$db->ErrorMsg());
$left = $rs->fields[$this->leftBranch];
$right = $rs->fields[$this->rightBranch];
return (($right-$left)-1)/2;
}
/**
* @desc __construct : Inside constructor
*/
function __construct() {
}
/**
* @desc __destruct : Destroying object
*/
function __destruct() {
}
/**
* @desc __clone : Object is being cloned
*/
function __clone() {
}
}
?>