<?php
/*
* 21.09.2007 see detail date
* 20.09.2007 added addDatePicker
* FIXME: see detail addTable to see potential bugs
*
* */
Class XMLGenerator{
private $xml;
private $response;
private $form;
public function __construct(){
$this->xml = new DOMDocument();
$this->response = $this->xml->createElement('response');
$this->xml->appendChild($this->response);
}
public function __destruct(){
$this->xml = null;
$this->response = null;
$this->form = null;
}
//overloading function
public function __call($method,$argument){
//single select put select form on whatever place will be
if($method=='addSingleSelect'){
if(sizeof($argument)==4){
return $this->addSingleSelectNoAction($argument[0],$argument[1],$argument[2],$argument[3]);
}
else
if(sizeof($argument)==5){
return $this->addSingleSelectWithAction($argument[0],$argument[1],$argument[2],$argument[3],$argument[4]);
}
else
if(sizeof($argument)==6){
return $this->addSingleSelectWithPOSTAction($argument[0],$argument[1],$argument[2],$argument[3],$argument[4],$argument[5]);
}
else{
die('invalid argument for addSingleSelect');
}
}
else{
die('method '.$method.' does\'nt exists.');
}
}
public function addBreak($place,$count){
$chunknode = $this->xml->createElement('chunk');
$placenode = $this->xml->createElement('place');
$placetext = $this->xml->createTextNode(htmlspecialchars($place));
$placenode->appendChild($placetext);
$textnode = $this->xml->createElement('break');
$text = $this->xml->createTextNode($count);
$textnode->appendChild($text);
$this->response->appendChild($chunknode);
$chunknode->appendChild($placenode);
$chunknode->appendChild($textnode);
}
public function replaceOnChange($id,$value){
$redir = $this->xml->createElement('onchange');
$urlnode = $this->xml->createElement('url');
$urlnode->setAttribute('id',$id);
$urlnode->setAttribute('onchange',$value);
$redir->appendChild($urlnode);
$this->response->appendChild($redir);
}
//graph is placed in site_path/tmp
public function addGraph($graph,$place,$append){
$chunknode = $this->xml->createElement('chunk');
$placenode = $this->xml->createElement('place');
$chunknode->setAttribute('append',$append);
$placegraph = $this->xml->createTextNode(htmlspecialchars($place));
$placenode->appendChild($placegraph);
$graphnode = $this->xml->createElement('graph');
$graph = $this->xml->createTextNode('tmp/'.$graph);
$graphnode->appendChild($graph);
$this->response->appendChild($chunknode);
$chunknode->appendChild($placenode);
$chunknode->appendChild($graphnode);
}
//flash object is placed in site_path/repo
public function addFlashObject($flashobject,$place,$append){
$chunknode = $this->xml->createElement('chunk');
$placenode = $this->xml->createElement('place');
$chunknode->setAttribute('append',$append);
$placeflashobject = $this->xml->createTextNode(htmlspecialchars($place));
$placenode->appendChild($placeflashobject);
$flashobjectnode = $this->xml->createElement('flashobject');
$flashobject = $this->xml->createTextNode('repo/'.$flashobject);
$flashobjectnode->appendChild($flashobject);
$this->response->appendChild($chunknode);
$chunknode->appendChild($placenode);
$chunknode->appendChild($flashobjectnode);
}
public function addText($text,$place,$append){
$chunknode = $this->xml->createElement('chunk');
$placenode = $this->xml->createElement('place');
$chunknode->setAttribute('append',$append);
$placetext = $this->xml->createTextNode(htmlspecialchars($place));
$placenode->appendChild($placetext);
$textnode = $this->xml->createElement('text');
$text = $this->xml->createTextNode($text);
$textnode->appendChild($text);
$this->response->appendChild($chunknode);
$chunknode->appendChild($placenode);
$chunknode->appendChild($textnode);
}
public function addURL($alias,$href,$place,$append){
$chunknode = $this->xml->createElement('chunk');
$placenode = $this->xml->createElement('place');
$chunknode->setAttribute('append',$append);
$placetext = $this->xml->createTextNode(htmlspecialchars($place));
$placenode->appendChild($placetext);
$urlnode = $this->xml->createElement('a');
$text = $this->xml->createTextNode($alias);
$urlnode->appendChild($text);
$urlnode->setAttribute('href',$href);
$this->response->appendChild($chunknode);
$chunknode->appendChild($placenode);
$chunknode->appendChild($urlnode);
}
public function buildMenu($arraymenu){
$this->addMenu($arraymenu,'menuholder');
}
public function buildSubMenu($arraysubmenu){
//clear the placeholder
$this->addText(null,'submenu',0);
foreach($arraysubmenu as $key => $value){
if($value=='break'){
}else{
$this->addURL($value,$key,'submenu',1);
}
$this->addBreak('submenu',2);
}
}
public function addMenu($array,$place){
$chunknode = $this->xml->createElement('chunk');
$placenode = $this->xml->createElement('place');
$placetext = $this->xml->createTextNode(htmlspecialchars($place));
$placenode->appendChild($placetext);
$menunode = $this->xml->createElement('menuitem');
foreach($array as $key => $value){
$itemnode = $this->xml->createElement('item');
$aliasnode = $this->xml->createElement('alias');
$aliastext = $this->xml->createTextNode(htmlspecialchars($value));
$aliasnode->appendChild($aliastext);
$itemnode->appendChild($aliasnode);
$urlnode = $this->xml->createElement('url');
$urltext = $this->xml->createTextNode(htmlspecialchars($key));
$urlnode->appendChild($urltext);
$itemnode->appendChild($urlnode);
$menunode->appendChild($itemnode);
}
$this->response->appendChild($chunknode);
$chunknode->appendChild($placenode);
$chunknode->appendChild($menunode);
}
//id should be filled with URL that build the form
//if form is located on panel, the url->href will change to closePanel and if panel closed,
//the URL back to initial state
public function addForm($id,$place,$append){
$this->form = $this->xml->createElement('form');
$chunknode = $this->xml->createElement('chunk');
$chunknode->setAttribute('append',$append);
$placenode = $this->xml->createElement('place');
$placetext = $this->xml->createTextNode(htmlspecialchars($place));
$placenode->appendChild($placetext);
$this->response->appendChild($chunknode);
$chunknode->appendChild($placenode);
$this->form->setAttribute('id',$id);
$chunknode->appendChild($this->form);
}
//FIXME: when type is file, see handleresponse about how to catch the variable
public function addTextField($label,$value,$id,$type){
$tf = $this->xml->createElement('textfield');
$tf->setAttribute('id',htmlspecialchars($id));
$tf->setAttribute('name',htmlspecialchars($id));
$tf->setAttribute('type',htmlspecialchars($type));
$tf->setAttribute('value',htmlspecialchars($value));
$tflabel = $this->xml->createElement('label');
$tflabelcontent = $this->xml->createTextNode(htmlspecialchars($label));
$tflabel->appendChild($tflabelcontent);
$tf->appendChild($tflabel);
$this->form->appendChild($tf);
}
//build date picker
public function addDatePicker($label,$value,$id){
$tf = $this->xml->createElement('datepicker');
$tf->setAttribute('id',htmlspecialchars($id));
$tf->setAttribute('name',htmlspecialchars($id));
$tflabel = $this->xml->createElement('label');
$tavalue = $this->xml->createElement('value');
$tflabelcontent = $this->xml->createTextNode(htmlspecialchars($label));
$tavaluecontent = $this->xml->createTextNode(htmlspecialchars($value));
$tflabel->appendChild($tflabelcontent);
$tavalue->appendChild($tavaluecontent);
$tf->appendChild($tflabel);
$tf->appendChild($tavalue);
$this->form->appendChild($tf);
}
//catch the post variable using html+$id
public function addHTMLArea($label,$value,$id){
$tf = $this->xml->createElement('htmlarea');
$tf->setAttribute('id',htmlspecialchars('html'.$id));
$tf->setAttribute('name',htmlspecialchars('html'.$id));
$tflabel = $this->xml->createElement('label');
$tavalue = $this->xml->createElement('value');
$tflabelcontent = $this->xml->createTextNode(htmlspecialchars($label));
$tavaluecontent = $this->xml->createTextNode(htmlspecialchars($value));
$tflabel->appendChild($tflabelcontent);
$tavalue->appendChild($tavaluecontent);
$tf->appendChild($tflabel);
$tf->appendChild($tavalue);
$this->form->appendChild($tf);
}
public function addSelect($label,$array,$selected,$id){
$sl = $this->xml->createElement('select');
$sl->setAttribute('name',htmlspecialchars($id));
$sl->setAttribute('id',htmlspecialchars($id));
$sllabel = $this->xml->createElement('label');
$sllabelcontent = $this->xml->createTextNode(htmlspecialchars($label));
$sllabel->appendChild($sllabelcontent);
$sl->appendChild($sllabel);
foreach($array as $key => $value){
$optionnode = $this->xml->createElement('option');
$optionnode->setAttribute('value',$key);
//NOTE: selected == value is different from common idea. [14-08-07]
if($selected==$key){
$optionnode->setAttribute('selected','selected');
}
$optiontext = $this->xml->createTextNode($value);
$optionnode->appendChild($optiontext);
$sl->appendChild($optionnode);
}
$this->form->appendChild($sl);
}
//single select independent with form tag without jump onChange action
public function addSingleSelectNoAction($array,$selected,$id,$place){
$chunknode = $this->xml->createElement('chunk');
$placenode = $this->xml->createElement('place');
$placetext = $this->xml->createTextNode(htmlspecialchars($place));
$placenode->appendChild($placetext);
$sl = $this->xml->createElement('select');
$sl->setAttribute('name',htmlspecialchars($id));
$sl->setAttribute('id',htmlspecialchars($id));
foreach($array as $key => $value){
$optionnode = $this->xml->createElement('option');
$optionnode->setAttribute('value',$key);
if($selected==$key){
$optionnode->setAttribute('selected','selected');
}
$optiontext = $this->xml->createTextNode($value);
$optionnode->appendChild($optiontext);
$sl->appendChild($optionnode);
}
$chunknode->appendChild($placenode);
$chunknode->appendChild($sl);
$this->response->appendChild($chunknode);
}
//single select independent with form tag with jump onChange action
public function addSingleSelectWithAction($array,$selected,$id,$place,$action){
$chunknode = $this->xml->createElement('chunk');
$placenode = $this->xml->createElement('place');
$placetext = $this->xml->createTextNode(htmlspecialchars($place));
$placenode->appendChild($placetext);
$sl = $this->xml->createElement('select');
$sl->setAttribute('name',htmlspecialchars($id));
$sl->setAttribute('id',htmlspecialchars($id));
$sl->setAttribute('onChange',$action);
foreach($array as $key => $value){
$optionnode = $this->xml->createElement('option');
$optionnode->setAttribute('value',$key);
if($selected==$key){
$optionnode->setAttribute('selected','selected');
}
$optiontext = $this->xml->createTextNode($value);
$optionnode->appendChild($optiontext);
$sl->appendChild($optionnode);
}
$chunknode->appendChild($placenode);
$chunknode->appendChild($sl);
$this->response->appendChild($chunknode);
}
//single select independent with form tag with jump onChange action
public function addSingleSelectWithPOSTAction($array,$selected,$id,$place,$action,$post){
$chunknode = $this->xml->createElement('chunk');
$placenode = $this->xml->createElement('place');
$placetext = $this->xml->createTextNode(htmlspecialchars($place));
$placenode->appendChild($placetext);
$sl = $this->xml->createElement('select');
$sl->setAttribute('name',htmlspecialchars($id));
$sl->setAttribute('id',htmlspecialchars($id));
$sl->setAttribute('onChange',$action);
$sl->setAttribute('POST',$post);
foreach($array as $key => $value){
$optionnode = $this->xml->createElement('option');
$optionnode->setAttribute('value',$key);
if($selected==$key){
$optionnode->setAttribute('selected','selected');
}
$optiontext = $this->xml->createTextNode($value);
$optionnode->appendChild($optiontext);
$sl->appendChild($optionnode);
}
$chunknode->appendChild($placenode);
$chunknode->appendChild($sl);
$this->response->appendChild($chunknode);
}
public function addSelectAction($label,$array,$selected,$id,$action){
$sl = $this->xml->createElement('select');
$sl->setAttribute('name',htmlspecialchars($id));
$sl->setAttribute('id',htmlspecialchars($id));
$sl->setAttribute('onChange',$action);
$sllabel = $this->xml->createElement('label');
$sllabelcontent = $this->xml->createTextNode(htmlspecialchars($label));
$sllabel->appendChild($sllabelcontent);
$sl->appendChild($sllabel);
foreach($array as $key => $value){
$optionnode = $this->xml->createElement('option');
$optionnode->setAttribute('value',$key);
if($selected==$key){
$optionnode->setAttribute('selected','selected');
}
$optiontext = $this->xml->createTextNode($value);
$optionnode->appendChild($optiontext);
$sl->appendChild($optionnode);
}
$this->form->appendChild($sl);
}
public function printOffset($offset,$numrows,$url,$pagelimit){
$place = 'offsettable';
if($offset>0){
$href = 'javascript:sndReqPOST(\''.$url.'\',\'ofs='.($offset-$pagelimit).htmlspecialchars('&').'pglimit='.$pagelimit.'\')';
$this->addURL(htmlspecialchars('<<prev'),$href,$place,1);
$this->addText(' ',$place,1);
}
$i =(int)($numrows/$pagelimit);
$mod = $numrows%$pagelimit;
if($i>1){
$count =1;
if($mod!=0){
while($i>=0){
$href = 'javascript:sndReqPOST(\''.$url.'\',\'ofs='.($count*$pagelimit-$pagelimit).htmlspecialchars('&').'pglimit='.$pagelimit.'\')';
if(($count*$pagelimit-$pagelimit)==($offset)){
$this->addURL('['.$count.']',$href,$place,1);
$this->addText(' ',$place,1);
}
else{
$this->addURL($count,$href,$place,1);
$this->addText(' ',$place,1);
}
$count++;
$i--;
}
}
else{
while($i>0){
$href = 'javascript:sndReqPOST(\''.$url.'\',\'ofs='.($count*$pagelimit-$pagelimit).htmlspecialchars('&').'pglimit='.$pagelimit.'\')';
if(($count*$pagelimit-$pagelimit)==($offset)){
$this->addURL('['.$count.']',$href,$place,1);
$this->addText(' ',$place,1);
}
else{
$this->addURL($count,$href,$place,1);
$this->addText(' ',$place,1);
}
$count++;
$i--;
}
}
}
if($offset+$pagelimit < $numrows){
$href = 'javascript:sndReqPOST(\''.$url.'\',\'ofs='.($offset+$pagelimit).htmlspecialchars('&').'pglimit='.$pagelimit.'\')';
$this->addURL(htmlspecialchars('next>>'),$href,$place,1);
}
}
public function addHidden($id,$value){
$hidden = $this->xml->createElement('hidden');
$hidden->setAttribute('id',htmlspecialchars($id));
$hidden->setAttribute('value',htmlspecialchars($value));
$this->form->appendChild($hidden);
}
public function addSubmitBtn($onclick,$value){
$tf = $this->xml->createElement('submit');
$tf->setAttribute('onclick',htmlspecialchars($onclick));
$tf->setAttribute('value',htmlspecialchars($value));
$this->form->appendChild($tf);
}
//table with an url on the specific column, provide jumping action
//using associate array on column
public function addTable($array_row,$array_column,$title,$action,$offset,$place,$append){
//create chunk for table
$chunknode = $this->xml->createElement('chunk');
$chunknode->setAttribute('append',$append);
//add place
$placenode = $this->xml->createElement('place');
$placetext = $this->xml->createTextNode(htmlspecialchars($place));
$placenode->appendChild($placetext);
$chunknode->appendChild($placenode);
$table = $this->xml->createElement('table');
$chunknode->appendChild($table);
//add title
$titlenode = $this->xml->createElement('title');
$titletext = $this->xml->createTextNode(htmlspecialchars($title));
$titlenode->appendChild($titletext);
$table->appendChild($titlenode);
//add action
$actionnode = $this->xml->createElement('action');
$actiontext = $this->xml->createTextNode(htmlspecialchars($action));
$actionnode->appendChild($actiontext);
$table->appendChild($actionnode);
//add offset
$offsetnode = $this->xml->createElement('offset');
$offsettext = $this->xml->createTextNode($offset);
$offsetnode->appendChild($offsettext);
$table->appendChild($offsetnode);
//add colum name
$columnnode = $this->xml->createElement('column');
foreach($array_column as $key => $value){
$columnnamenode = $this->xml->createElement('nameclm');
//attach the url if the key contain url jump action, i.e javascript:sndReq()
//FIXME: choose different condition if you have different idea :p
//normal table key column should be lower than 2
if(strlen($key)>2){
$columnnamenode->setAttribute('url',$key);
}
$columnnametext = $this->xml->createTextNode(htmlspecialchars($value));
$columnnamenode->appendChild($columnnametext);
$columnnode->appendChild($columnnamenode);
}
$table->appendChild($columnnode);
//add table item
$rownode = $this->xml->createElement('row');
foreach($array_row as $key => $value){
$array = get_object_vars($value);
$item = $this->xml->createElement('item');
foreach($array as $key2 => $value2){
$tempitem = $this->xml->createElement($key2);
$itemcontent = $this->xml->createTextNode($value2);
$tempitem->appendChild($itemcontent);
$item->appendChild($tempitem);
}
$table->appendChild($item);
}
$table->appendChild($rownode);
$this->response->appendChild($chunknode);
}
/*
* function exclude from chunk response
* this should only be executed. there is no view will be built by this function
* */
//attach clearform request
public function clearForm(){
$clearform = $this->xml->createElement('clearform');
$this->response->appendChild($clearform);
}
public function addRedir($url){
$redir = $this->xml->createElement('redirect');
$urlnode = $this->xml->createElement('url');
$urltext = $this->xml->createTextNode(htmlspecialchars($url));
$urlnode->appendChild($urltext);
$redir->appendChild($urlnode);
$this->response->appendChild($redir);
}
public function addRedirPOST($url,$postdata){
$redir = $this->xml->createElement('redirect');
$urlnode = $this->xml->createElement('url');
$urltext = $this->xml->createTextNode(htmlspecialchars($url));
$urlnode->appendChild($urltext);
$redir->appendChild($urlnode);
$urlnode->setAttribute('postdata',htmlspecialchars($postdata));
$this->response->appendChild($redir);
}
//add loop remains active until thereis another chunk contains loop tag
//destroy all loops [coz we dont want to be bothered with responsibility of each loop]
//recreate the actual loops
public function addLoop($url,$post,$time){
$loop = $this->xml->createElement('loop');
$urlnode = $this->xml->createElement('url');
$urltext = $this->xml->createTextNode(htmlspecialchars($url));
$urlnode->appendChild($urltext);
$loop->appendChild($urlnode);
$urlnode->setAttribute('time',$time);
$urlnode->setAttribute('post',$post);
$this->response->appendChild($loop);
}
public function removeAllLoop(){
$loop = $this->xml->createElement('loop');
$this->response->appendChild($loop);
}
//ensure that there is no cache
public function flush(){
header('Content-Type: text/xml');
header('Expires: Wed, 23 Dec 1980 00:30:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
echo $this->xml->saveXML();
}
}
?>