<?
class DOCUMENT {
/** the name of the document */
var $name;
/** the id of the document used
for internal purposes */
var $id;
/** the root element */
var $root = null;
/** add document to database */
function add() {
/** use global db link */
global $db;
/** add to DOCUMENTS and get ID */
$query = "INSERT INTO DOCUMENTS SET
NAME = '$this->name'";
$results = mysql_query($query);
$this->id = mysql_insert_id($db);
/** add root element if it isn't null */
if($this->root != null) {
$this->add_root_element($this->root);
$query = "UPDATE DOCUMENTS SET
ROOT_ELEMENT = ".$this->root->id."
WHERE ID = $this->id";
$results = mysql_query($query);
}
}
function become_by_name($name) {
$query = "SELECT ID FROM DOCUMENTS WHERE NAME = '$name'";
$results = mysql_query($query);
$results = mysql_fetch_row($results);
$this->become($results[0]);
}
function become($id) {
$this->id = $id;
$this->root = new ELEMENT();
$query = "SELECT ROOT_ELEMENT, NAME FROM
DOCUMENTS WHERE ID = $this->id";
$results = mysql_query($query);
$results = mysql_fetch_array($results);
$this->name = $results["NAME"];
$this->root->become($results["ROOT_ELEMENT"]);
}
function delete() {
/** delete root element */
$this->root->delete();
/** delete DOCUMENT entry */
$query = "DELETE FROM DOCUMENTS WHERE
ID = $this->id";
$results = mysql_query($query);
}
/** update DOCUMENT entry */
function update() {
$query = "UPDATE DOCUMENTS SET
NAME = '$this->name',
ROOT_ELEMENT = ".$this->root->id."
WHERE ID = $this->id";
$results = mysql_query($query);
}
/** return the XML data of the root element */
function getXML() {
$tmp = new ELEMENT();
$tmp = $this->root;
$rvalue = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
/* $rvalue .= "<?xml-stylesheet type=\"text/xsl\" href=\"ClientTrack.xsl\"?>\n"; */
/* $rvalue .= strtolower($tmp->getXML()); */
$rvalue .= $tmp->getXML();
return $rvalue;
}
/** add root element */
function add_root_element($element) {
$this->root = new ELEMENT();
$this->root = $element;
$this->root->document = $this->id;
$this->root->add();
}
/** get element ids */
function search_elements($attname, $attvalue) {
$query = "SELECT ELEMENT FROM ATTRIBUTES, ELEMENTS WHERE
ATTRIBUTES.NAME = '$attname' AND
ATTRIBUTES.VALUE = '$attvalue' AND
ELEMENTS.ID = ATTRIBUTES.ELEMENT AND
ELEMENTS.DOCUMENT = $this->id";
$result = mysql_query($query);
$rvalue = array();
for($i = 0; $i < mysql_num_rows($result); $i++) {
$row = mysql_fetch_row($result);
array_push($rvalue, $row[0]);
}
$rvalue = array_unique($rvalue);
return $rvalue;
}
}
?>