*/ //namespace XSDFormPrinter; class XSDFormPrinter { public $xml = ''; public $xml_inhalt; public $getparams=''; public function __construct() { $this->loadDefaultConfiguration(); require_once(dirname(__FILE__).'/types.php'); } /** * * Erzeugt das Formular * @param $xsd XSD File (nicht der Filename) * @param $xml XML mit den Daten die vorausgefuellt werden sollen */ public function output($xsd, $xml) { $dom = new DOMDocument(); $dom->loadXML($xsd); $this->xml_inhalt = new DOMDocument(); if($xml!='') { $this->xml_inhalt->loadXML($xml); } if($dom===false) { echo 'Failed to load XSD into DOM'; return false; } echo ''; echo '
'; echo ''; echo "\n"; $output = $this->process($dom); $this->xml.=$output['xml']; echo "\n"; echo ' '; echo '
'; echo ""; echo '
'; } private function loadDefaultConfiguration() { require_once(dirname(__FILE__).'/config.inc.php'); } /** * * Parst das DOM Document * @param $dom DOMDocument des XSD Files */ private function process($dom) { $addoutput=array('html'=>'','xml'=>''); if(!$dom->childNodes) return false; foreach($dom->childNodes as $child) { if($child != NULL) { if(get_class($child)=='DOMText') continue; $name=$child->getAttribute('name'); $maxoccurs = $child->getAttribute('maxOccurs'); $minoccurs = $child->getAttribute('minOccurs'); if($maxoccurs=='') $maxoccurs=1; if($minoccurs=='') $minoccurs=1; if($maxoccurs>1 || $maxoccurs=='unbounded') { $this->xml.=$addoutput['xml']; $addoutput=array('html'=>'','xml'=>''); $addfunc = uniqid($this->config['PREFIX'].'ADD_'); $addid = uniqid($this->config['PREFIX'].'ADDID_'); echo "$name
Add More ($maxoccurs)
"; } if($child->nodeName == "xs:element") { $type=$child->getAttribute('type'); $addoutput['xml'].="\n<".$name.'>'; if($type!='') { $output=$this->input($type, $name, $minoccurs); $addoutput['html'].=$output['html']; $addoutput['xml'].=$output['xml']; echo $output['html']; } else { $this->xml.=$addoutput['xml']; $addoutput['xml']=''; } $output=$this->process($child); $addoutput['html'].=$output['html']; $addoutput['xml'].=$output['xml']; if($type=='') $addoutput['xml'].="\n"; $addoutput['xml'].='"; } else { $output=$this->process($child); $addoutput['html'].=$output['html']; $addoutput['xml'].=$output['xml']; } if($maxoccurs>1 || $maxoccurs=='unbounded') { echo "
"; } } } return $addoutput; } /** * Erzeugt ein Eingabefeld * @param type type des Elements * @param name Name des Elements */ private function input($type, $name, $minoccurs) { $output=array('html'=>'','xml'=>''); // ElementType => (MyType, (RegexPattern=>Errormsg)) static $factory = array('xs:string'=>array('string',array()), 'xs:decimal'=>array('string',array('/^\d[.,]\d*$/'=>'Value must be a valid Number')), 'xs:integer'=>array('string',array('/^\d/$'=>'Value must be a valid Number')), 'xs:positiveInteger'=>array('string',array('/^\d$/'=>'Value must be a positive Number')), 'xs:date'=>array('date',array()), 'wysiwyg'=>array('wysiwyg',array()), 'file'=>array('file',array()), 'boolean'=>array('boolean',array()) ); if(!isset($factory[$type])) { $this->debugmsg(0,"Input Type $type not supported -> using string instead"); $type='xs:string'; } $output['html'].= "\n"; $ftype = $factory[$type][0]; $output['html'].= ''.$name.($minoccurs>0?' *':'').''; //ToDo: Create a Unique reproduceable fieldid $fieldid = $this->config['PREFIX'].'FIELD_'.$name; $validatefunction = $this->createValidation($fieldid, $factory[$type][1]); if($this->xml_inhalt->getElementsByTagName($name)->item(0)) $value = $this->xml_inhalt->getElementsByTagName($name)->item(0)->nodeValue; else $value=''; $output['html'].=sprintf($this->types[$ftype],$name, $fieldid,$validatefunction, $value); $output['html'].= ''; $output['html'].= ''; $output['xml'].=''; return $output; } /** * Erzeugt eine Javascript Funktion zum Validieren der Eingabe * @param $fieldid ID des Feldes das Validiert werden soll * @param patterns Array mit RegEx Patterns als Key und der Fehlermeldung als Value */ private function createValidation($fieldid, $patterns) { if(count($patterns)==0) return false; $functionname = uniqid($this->config['PREFIX'].'FUNC_'); $func = 'function '.$functionname.'() {'; $func .= 'var fieldvalue = document.getElementById("'.$fieldid.'").value;'; foreach($patterns as $pattern=>$errormsg) { $func.='if(!'.$pattern.'.test(fieldvalue)){alert("'.$errormsg.'"); return false;}'; } $func .= '};'; echo ''; return $functionname.'()'; } /** * Prints a Debug Message * * @param dbglevel Debug Level * @param msg Message to Output */ private function debugmsg($dbglevel, $msg) { if($dbglevel<=$this->config['debuglevel']) echo $msg; } }