{
$this->doXmlString2Xml($string,$xpath);
}
/**
* Adds an additional pear::db_result resultset to $this->xmldoc
*
* @param Object db_result result from a DB-query
* @see doSql2Xml()
* @access public
*/
function addResult($result)
{
$this->doSql2Xml($result);
}
/**
* Adds an aditional resultset generated from an sql-statement
* to $this->xmldoc
*
* @param string sql a string containing an sql-statement.
* @access public
* @see doSql2Xml()
*/
function addSql($sql)
{
/* if there are {} expressions in the sql query, we assume it's an xpath expression to
* be evaluated.
*/
if (preg_match_all ("/\{([^\}]+)\}/i",$sql,$matches))
{
foreach ($matches[1] as $match)
{
$sql = preg_replace("#\{".preg_quote($match)."\}# ", $this->getXpathValue($match),$sql);
}
}
$result = $this->db->query($sql);
//very strange
if (PEAR::isError($result->result)) {
print "You have an SQL-Error:
".$result->result->userinfo;
print "
";
new DB_Error($result->result->code,PEAR_ERROR_DIE);
}
$this->doSql2Xml($result);
}
/**
* Adds an aditional resultset generated from an Array
* to $this->xmldoc
* TODO: more explanation, how arrays are transferred
*
* @param array multidimensional array.
* @access public
* @see doArray2Xml()
*/
function addArray ($array)
{
$parent_row = $this->insertNewResult(&$metadata);
$this->DoArray2Xml($array,$parent_row);
}
/**
* Returns an xml-string with a xml-representation of the resultsets.
*
* The resultset can be directly provided here, or if you need more than one
* in your xml, then you have to provide each of them with add() before you
* call getXML, but the last one can also be provided here.
*
* @param mixed $result result Object from a DB-query
* @return string xml
* @access public
*/
function getXML($result = Null)
{
$xmldoc = $this->getXMLObject($result);
return $xmldoc->dumpmem();
}
/**
* Returns an xml DomDocument Object with a xml-representation of the resultsets.
*
* The resultset can be directly provided here, or if you need more than one
* in your xml, then you have to provide each of them with add() before you
* call getXMLObject, but the last one can also be provided here.
*
* @param mixed $result result Object from a DB-query
* @return Object DomDocument
* @access public
*/
function getXMLObject($result = Null)
{
if ($result) {
$this->add ($result);
}
return $this->xmldoc;
}
/**
* For adding db_result-"trees" to $this->xmldoc
* @param Object db_result
* @access private
* @see addResult(),addSql()
*/
function doSql2Xml($result)
{
if (DB::IsError($result)) {
print "Error in file ".__FILE__." at line ".__LINE__."
\n";
print $result->userinfo."
\n";
new DB_Error($result->code,PEAR_ERROR_DIE);
}
// the method_exists is here, cause tableInfo is only in the cvs at the moment
// BE CAREFUL: if you have fields with the same name in different tables, you will get errors
// later, since DB_FETCHMODE_ASSOC doesn't differentiate that stuff.
$this->LastResult = &$result;
if (!method_exists($result,"tableInfo") || ! ($tableInfo = $result->tableInfo(False)))
{
//emulate tableInfo. this can go away, if every db supports tableInfo
$fetchmode = DB_FETCHMODE_ASSOC;
$res = $result->FetchRow($fetchmode);
$this->nested = False;
$i = 0;
while (list($key, $val) = each($res))
{
$tableInfo[$i]["table"]= $this->tagNameResult;
$tableInfo[$i]["name"] = $key;
$resFirstRow[$i] = $val;
$i++;
}
$res = $resFirstRow;
$FirstFetchDone = True;
$fetchmode = DB_FETCHMODE_ORDERED;
}
else
{
$FirstFetchDone = False;
$fetchmode = DB_FETCHMODE_ORDERED;
}
// initialize db hierarchy...
$parenttable = "root";
$tableInfo["parent_key"]["root"] = 0;
foreach ($tableInfo as $key => $value)
{
if (is_int($key))
{
// if the sql-query had a function the table starts with a # (only in mysql i think....), then give the field the name of the table before...
if (preg_match ("/^#/",$value["table"]) || strlen($value["table"]) == 0) {
$value["table"] = $tableInfo[($key - 1)]["table"] ;
$tableInfo[$key]["table"] = $value["table"];
}
if (!isset($tableInfo["parent_table"]) || is_null($tableInfo["parent_table"][$value]["table"]]))
{
$tableInfo["parent_key"][$value]["table"]] = $key;
$tableInfo["parent_table"][$value]["table"]] = $parenttable;
$parenttable = $value["table"] ;
}
}
//if you need more tableInfo for later use you can write a function addTableInfo..
$this->addTableInfo($key, $value, &$tableInfo);
}
// end initialize
// if user made some own tableInfo data, merge them here.
if ($this->user_tableInfo)
{
$tableInfo = $this->array_merge_clobber($tableInfo,$this->user_tableInfo);
}
$parent['root'] = $this->insertNewResult(&$tableInfo);
//initialize $resold to get rid of warning messages;
$resold[0] = "ThisValueIsImpossibleForTheFirstFieldInTheFirstRow";
while ($FirstFetchDone == True || $res = $result->FetchRow($fetchmode))
{
//FirstFetchDone is only for emulating tableInfo, as long as not all dbs support tableInfo. can go away later
$FirstFetchDone = False;
while (list($key, $val) = each($res))
{
if ($resold[$tableInfo]["parent_key"][$tableInfo][$key]["table"]]] != $res[$tableInfo]["parent_key"][$tableInfo][$key]["table"]]] || !$this->nested)
{
if ($tableInfo["parent_key"][$tableInfo][$key]["table"]] == $key )
{
if ($this->nested || $key == 0)
{
$parent[$tableInfo][$key]["table"]] = $this->insertNewRow($parent[$tableInfo]["parent_table"][$tableInfo][$key]["table"]]], $res, $key, &$tableInfo);
}
else
{
$parent[$tableInfo][$key]["table"]]= $parent[$tableInfo]["parent_table"][$tableInfo][$key]["table"]]];
}
//set all children entries to somethin stupid
foreach($tableInfo["parent_table"] as $pkey => $pvalue)
{
if ($pvalue == $tableInfo[$key]["table"])
{
$resold[$tableInfo]["parent_key"][$pkey]]= "ThisIsJustAPlaceHolder";
}
}
}
if ( $parent[$tableInfo][$key]["table"]] != Null)
{
$this->insertNewElement($parent[$tableInfo][$key]["table"]], $res, $key, &$tableInfo, &$subrow);
}
}
}
$resold = $res;
unset ($subrow);
}
return $this->xmldoc;
}
/**
* For adding whole arrays to $this->xmldoc
*
* @param array
* @param Object domNode
* @access private
* @see addArray()
*/
function DoArray2Xml ($array, $parent) {
while (list($key, $val) = each($array))
{
$tableInfo[$key]["table"]= $this->tagNameResult;
$tableInfo[$key]["name"] = $key;
}
if ($this->user_tableInfo)
{
$tableInfo = $this->array_merge_clobber($tableInfo,$this->user_tableInfo);
}
foreach ($array as $key=>$value)
{
if (is_array($value) ) {
if (is_int($key) )
{
$valuenew = array_slice($value,0,1);
$keynew = array_keys($valuenew);
$keynew = $keynew[0];
}
else
{
$valuenew = $value;
$keynew = $key;
}
$rec2 = $this->insertNewRow($parent, $valuenew, $keynew, &$tableInfo);
$this->DoArray2xml($value,$rec2);
}
else {
$this->insertNewElement($parent, $array, $key, &$tableInfo,&$subrow);
}
}
}
/**
* This method sets the options for the class
* One can only set variables, which are defined at the top of
* of this class.
*
* @param array options to be passed to the class
* @param boolean if the old suboptions should be deleted
* @access public
* @see $nested,$user_options,$user_tableInfo
*/
function setOptions($options,$delete = False) {
//set options
if (is_array($options))
{
foreach ($options as $option => $value)
{
if (isset($this->{$option}))