<?
class ELEMENT {
/** element name(type)
* @var name
*/
var $name;
/** id used for internal purposes */
var $id;
/** array of elements under encapsulated
with-in this element id only*/
var $children = array();
/** array of attributes */
var $attributes = array();
/** cdata */
var $cdata;
/** parents id number */
var $parent;
/** document */
var $document;
/** boolean value indicating if
this is a root node or not */
var $isroot;
/** return the xml data of elements *
* that have an attribute of $name with *
* value similar to $value, this is outdated *
* you should use: *
* search_attribute_with_depth_getXML instead */
function search_attribute_get_XML($name, $value, $recursive = false) {
$ids = array();
$data = "";
$query = "SELECT ATTRIBUTES.ELEMENT FROM ATTRIBUTES, PARENTCHILD
WHERE ATTRIBUTES.NAME = '$name' AND
ATTRIBUTES.VALUE LIKE '%$value%' AND
ATTRIBUTES.ELEMENT = PARENTCHILD.CHILD AND
PARENTCHILD.PARENT = $this->id";
$results = mysql_query($query);
$tmp = new ELEMENT();
for($i = 0; $i < mysql_num_rows($results); $i++) {
$row = mysql_fetch_row($results);
$tmp->become($row[0]);
$data .= $tmp->getXML();
}
// if recursive then do children (recursivly of course)
if($recursive != false) {
foreach($this->children as $i) {
$tmp->become($i);
$data .= $tmp->search_attribute_get_XML($name, $value, true);
}
}
// return data
return $data;
}
function internal_addresultstoarray($results, &$array) {
for($i=0; $i < mysql_num_rows($results); $i++) {
$row = mysql_fetch_row($results);
array_push($array, $row[0]);
}
}
/** update to search_attribute_get_XML() *
* with faster logorithm but needs the *
* depth parameter */
function search_attribute_with_depth_get_XML($name, $value, $depth) {
$buf = array();
$tmp = array();
$children = array();
$children = null;
// do first search looking for top-level children
$query = "SELECT ATTRIBUTES.ELEMENT FROM ATTRIBUTES, PARENTCHILD
WHERE ATTRIBUTES.NAME= '$name' AND
ATTRIBUTES.VALUE LIKE '%$value%' AND
ATTRIBUTES.ELEMENT = PARENTCHILD.CHILD AND
PARENTCHILD.PARENT = $this->id";
$results = mysql_query($query);
$array_results = array();
for($i=0; $i < mysql_num_rows($results); $i++) {
$row = mysql_fetch_row($results);
array_push($array_results, $row[0]);
}
// get list of all children up to depth
if($depth > 0) $buf = $children = $this->children;
if($depth > 1) {
for($i = 1; $i < $depth; $i++) {
// get children of $buf
$query2 = "SELECT CHILD FROM PARENTCHILD WHERE
PARENT = $buf[0]";
for($i = 1; $i < count($buf); $i++) {
$query2 .= " OR PARENT = " . $buf[$i];
}
$results = mysql_query($query2);
// add results to children and remake buf[]
$buf = null;
$buf = array();
for($i=0; $i < mysql_num_rows($results); $i++) {
$row = mysql_fetch_row($results);
array_push($buf, $row[0]);
}
foreach($buf as $cur) array_push($children, $cur);
}
}
// query for elements against entire list of children
if($children != null) {
$query = "SELECT ATTRIBUTES.ELEMENT FROM ATTRIBUTES, PARENTCHILD
WHERE ATTRIBUTES.NAME = '$name' AND
ATTRIBUTES.VALUE LIKE '%$value%' AND
ATTRIBUTES.ELEMENT = PARENTCHILD.CHILD
AND PARENTCHILD.PARENT = " . $children[0];
for($i = 1; $i < count($children); $i++)
$query .= " OR ATTRIBUTES.NAME = '$name' AND
ATTRIBUTES.VALUE LIKE '%$value%' AND
ATTRIBUTES.ELEMENT = PARENTCHILD.CHILD
AND PARENTCHILD.PARENT = " . $children[$i];
$results = mysql_query($query);
for($i=0; $i < mysql_num_rows($results); $i++) {
$row = mysql_fetch_row($results);
array_push($array_results, $row[0]);
}
}
// get XML
$data = "";
foreach($array_results as $cur) {
$tmp = new ELEMENT();
$tmp->become($cur);
$data .= $tmp->getXML();
}
// return
return $data;
}
/** become the element with id $id */
function become($id) {
$this->id = $id;
/** get info from table ELEMENTS */
$query = "SELECT * FROM ELEMENTS
WHERE ID = $this->id";
$results = mysql_query($query);
$results = mysql_fetch_array($results);
$this->name = strtolower($results["ETYPE"]);
$this->cdata = $results["CDATA"];
$this->document = $results["DOCUMENT"];
/** get parent id number */
$query = "SELECT PARENT FROM PARENTCHILD
WHERE CHILD = $this->id";
$results = mysql_query($query);
$results = mysql_fetch_array($results);
$this->parent = $results["PARENT"];
/** get the attributes */
$query = "SELECT NAME, ID, VALUE FROM ATTRIBUTES
WHERE ELEMENT = $this->id";
$results = mysql_query($query);
$nrows = mysql_num_rows($results);
for($i = 0; $i < $nrows; $i++) {
$row = mysql_fetch_array($results);
$tmp = new ATTRIBUTE();
$tmp->name = strtolower($row["NAME"]);
$tmp->value = $row["VALUE"];
$tmp->id = $row["ID"];
$tmp->element = $this->id;
array_push($this->attributes, $tmp);
}
/** get the list of children id's */
$query = "SELECT CHILD FROM PARENTCHILD
WHERE PARENT = $this->id";
$results = mysql_query($query);
$nrows = mysql_num_rows($results);
for($i = 0; $i < $nrows; $i++) {
$row = mysql_fetch_array($results);
$tmp = $row["CHILD"];
array_push($this->children, $tmp);
}
}
/** get element XML data with element_id
* and parent_id as an attribute */
function getXML($recursive = false) {
$data = null;
/** display starting tag with attributes */
$data = "<$this->name elementid=\"$this->id\" ";
if($this->parent)
$data .= "parent_id=\"$this->parent\" ";
foreach($this->attributes as $i) {
$data .= "$i->name=\"$i->value\" ";
}
$data .= ">\n";
/** display cdata */
if($this->cdata)
$data .= $this->cdata . "\n";
/** display child elements */
foreach($this->children as $i) {
$tmp = new ELEMENT();
$tmp->become($i);
if($recursive == false && $tmp->id != 0 && $tmp->id != null) $data .= $tmp->getXMLWithoutChildren();
else $data .= $tmp->getXML(true);
}
/** display finishing tag */
$data .= "</$this->name >\n";
/** return the XML */
return $data;
}
/** update the element this deals only with
the ELEMENTS and ATTRIBUTES tables */
function update() {
/** update ELEMENTS table */
$query = "UPDATE ELEMENTS SET
ETYPE = '$this->name',
CDATA = '$this->cdata'
WHERE ID = $this->id";
$results = mysql_query($query);
/** update ATTRIBUTES table */
foreach($this->attributes as $i) {
if(isset($i->id) && $i->id != null) $i->update();
else $i->add();
}
}
/** add child element */
function addCHILD(&$child) {
if(!$this->id) $this->add();
$tmp = new ELEMENT();
$tmp =& $child;
$tmp->parent = $this->id;
$tmp->document = $this->document;
$tmp->add();
array_push($this->children, $tmp->id);
}
function getPARENT() {
$tmp = new ELEMENT();
$tmp->become($this->id);
return $tmp;
}
function addATTRIBUTE($attribute) {
$tmp = new ATTRIBUTE();
$tmp = $attribute;
$tmp->element = $this->id;
array_push($this->attributes, $tmp);
}
function delete() {
/** delete from ATTRIBUTES table */
$query = "DELETE FROM ATTRIBUTES WHERE
ELEMENT = $this->id";
$results = mysql_query($query);
/** delete children */
$tmp = new ELEMENT();
foreach ($this->children as $i) {
$tmp->become($i);
$tmp->delete();
}
/** delete from PARENTCHILD table */
$query = "DELETE FROM PARENTCHILD WHERE
CHILD = $this->id";
$results = mysql_query($query);
/** delete from ELEMENTS table */
$query = "DELETE FROM ELEMENTS WHERE
ID = $this->id";
$results = mysql_query($query);
/** delete from DOCUMENTS table if it
is a root node i may decide not to do this*/
}
function add() {
/** check document */
if (!isset($this->document))
$this->document = 0;
/** globalize the db connection */
global $db;
$this->name = strtolower($this->name);
/** add ETYPE(name), document and cdata */
$query = "INSERT INTO ELEMENTS SET
ETYPE = '$this->name',
CDATA = '$this->cdata',
DOCUMENT = $this->document";
$results = mysql_query($query);
/** set the id */
$this->id = mysql_insert_id($db);
/** add the attributes */
if($this->attributes)
foreach ($this->attributes as $i) {
$i->element = $this->id;
$i->add();
}
/** add the PARENTCHILD relationship
entry */
$query = "INSERT INTO PARENTCHILD SET
PARENT = $this->parent,
CHILD = $this->id";
$results = mysql_query($query);
if(!$results) {
$query = "INSERT INTO PARENTCHILD SET
CHILD = $this->id";
$results = mysql_query($query);
}
// clean up db
$query = "DELETE FROM PARENTCHILD WHERE CHILD = 0";
mysql_query($query);
}
/** returns xml of children meeting the right reqs */
function findCHILDXML($element_type, $attributes = null) {
$tmp = $this->findCHILDID($element_type, $attributes);
$rvalue = null;
if($tmp) {
$i = new ELEMENT();
foreach($tmp as $int) {
$i->become($int);
$rvalue .= $i->getXML();
}
}
return $rvalue;
}
/** returns xml of sub-elements of
* type $name only */
function getSubElementsByType($name) {
// perform recursivly
$tmp = new ELEMENT();
foreach ($this->children as $child) {
$tmp->become($child);
$tmp->getSubElementsByType($name);
if($tmp->name == $name) {
$tmp->getXMLWithoutChildren();
}
}
}
/** get XML tag without children */
function getXMLWithoutChildren() {
$tmp = new ATTRIBUTE();
$data = "<" . $this->name . " ";
$data .= "elementid=\"$this->id\" ";
$data .= "parent_id=\"".$this->parent."\" ";
foreach($this->attributes as $att) {
$tmp =& $att;
$data .= $tmp->name . "=\"";
$data .= $tmp->value . "\" ";
}
$data .= ">\n";
$data .= $this->cdata . "\n";
$data .= "</$this->name >";
return $data;
}
/** returns an array of ID's for
elements of type $element_type with
* attributes similar to the array
$attributes */
function findCHILDID($element_type, $attributes = null) {
$query = "SELECT ELEMENTS.ID FROM ELEMENTS, ATTRIBUTES
WHERE ELEMENTS.ETYPE = '$element_type' ";
if(count($attributes) > 0 && $attributes != null) {
if(count($attributes) > 1) $tmp = array_pop($attributes);
else $tmp = $attributes;
$query .= "AND ATTRIBUTES.ELEMENT = ELEMENTS.ID
AND ATTRIBUTES.VALUE = '$tmp->value'
AND ATTRIBUTES.NAME = '$tmp->name' ";
}
if(count($attributes) > 0) {
foreach($attributes as $tmp) {
$query .= "OR ELEMENTS.ETYPE = '$element_type'
AND ATTRIBUTES.ELEMENT = ELEMENTS.ID
AND ATTRIBUTES.VALUE = '$tmp->value'
AND ATTRIBUTES.NAME = '$tmp->name' ";
}
}
$results = mysql_query($query);
$tmp = null;
$tmp = array();
$nrows = mysql_num_rows($results);
for($i = 0; $i < $nrows; $i++) {
$row = mysql_fetch_row($results);
array_push($tmp, $row[0]);
}
$tmp = array_unique($tmp);
return $tmp;
}
}
?>