Location: PHPKode > projects > MySQL-XML > classgen.php
<?
//// class generator using
// MySQL-XML by Brandon Niemczyk

$header = "<html><head></head><body>\n";
$header .= "<a href=\"http://www.capableweb.com/\">\n";
$header .= "<img src=\"http://www.capableweb.com/images/title_whitebg.jpg\" />\n";
$header .= "</a>\n";
$header .= "<h1>PHP Class generator using the MySQL-XML library</h1>\n";
$header .= "<p>Attributes are simple object attributes there can only be one of each attribute ";
$header .= "Children are one of the advantages of XML technology, there can be an unlimited amount ";
$header .= "of children (of any type that you create now) added to each object</p>";
$footer = "\n</body></html>";

function printform() {
	global $header, $footer;
	echo $header;
	echo '<form action="#" method="POST">';
	echo '<table>';
	
	// echo class input
	echo '<tr><td>Class Name</td><td><input type="text" name="class" /></td></tr>'."\n";
	
	// echo attributes inputs
	for($i = 0; $i < 10; $i++) {
		echo "<tr>";
		echo "<td>";
		echo "Attribute #".($i + 1);
		echo "</td>";
		echo "<td>";
		echo "<input type=\"text\" name=\"attribute[$i]\" />";
		echo "</td>";
		echo "</tr>\n";
	}
	
	// echo children inputs
	for($i = 0; $i < 10; $i++) {
		echo "<tr>";
		echo "<td>";
		echo "Child type #".($i + 1)." (you need to make classes for these too)";
		echo "</td>";
		echo "<td>";
		echo "<input type=\"text\" name=\"child[$i]\" />";
		echo "</td>";
		echo "</tr>\n";
	}
	
		
	// echo submit button
	echo '<tr><td colspan="100%"><input type="submit"/></td></tr>'."\n";
	
	echo '</table>';
	echo '</form>';
	echo $footer;
}

function genclass() {
	//// print header
	global $header, $footer, $class, $attribute, $child;
	$tab = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	echo $header;
	
	$children = array();
	$atts = array();
	// set $children and $atts
	foreach($attribute as $tmp)
		if($tmp != "" && isset($tmp)) {
			$tmp = strtolower($tmp);
			$tmp = str_replace(" ", "_", $tmp);
			array_push($atts, $tmp);
		}
		
	foreach($child as $tmp)
		if($tmp != "" && isset($tmp)) {
			$tmp = strtolower($tmp);
			$tmp = str_replace(" ", "_", $tmp);
			array_push($children, $tmp);
		}
	
	// top comments and class start
	$data = "//// class $class\n";
	$data .= "// generated by classgen\n";
	$data .= "// requires MySQL-XML\n\n";
	$data .= "class $class {\n\n";
	
	// common variables
	$data .= "\tvar \$element;\n";
	$data .= "\tvar \$xmlobjtype;\n";
	$data .= "\tvar \$document;\n";
	
	// attribute variables
	$data .= "\n\t//// attribute variables\n";
	foreach($atts as $tmp)
		$data .= "\tvar \$$tmp;\n";
		
	// child type arrays
	$data .= "\n\t//// arrays of children by type\n";
	$data .= "\t// these should all be declared as classes as well\n";
	foreach($children as $tmp)
		$data .= "\tvar \$$tmp"."_children = array();\n";
	
	// constructor
	$data .= "\n\t////constructor\n";
	$data .= "\tfunction $class(\$document = null) {\n";
	$data .= "\t\t".'$this->element = new ELEMENT();'."\n";
	$data .= "\t\t\$this->element->name = \"$class\";\n";
	$data .= "\t\t\$this->document = \$document;\n";
	$data .= "\t\t\$this->xmlobjtype = new ATTRIBUTE();\n";
	$data .= "\t\t\$this->xmlobjtype->name = \"xmlobjtype\";\n";
	$data .= "\t\t\$this->xmlobjtype->value = \"$class\";\n";
	foreach($atts as $tmp) {
		$data .= "\t\t\$this->$tmp = new ATTRIBUTE();\n";
		$data .= "\t\t\$this->$tmp"."->name = \"$tmp\";\n";
		$data .= "\t\t\$this->$tmp"."->value = 0;\n";
	}
	$data .= "\t}\n";
	
	// getXML function
	$data .= "\n\tfunction getXML() {\n";
	$data .= "\t\t".'return $this->element->getXML(true);'."\n";
	$data .= "\t}\n";
	
	// update function
	$data .= "\n\t////update function\n";
	$data .= "\tfunction update() {\n";
	$data .= "\t\tif(!isset(\$this->element->id)) \$this->add();\n";
	$data .= "\t\t else {\n";
	$data .= "\t\t\t\$this->element->update();\n";
	foreach($atts as $tmp) {
		$data .= "\t\t\t\$this->".$tmp."->update();\n";
	}
	$data .= "\t\t\t".'$this->element->become($this->element->id);'."\n";
	$data .= "\t\t}\n";
	$data .= "\t}\n";
	
	// add function
	$data .= "\n\t//// add to database\n";
	$data .= "\tfunction add() {\n";
	$data .= "\t\t".'$this->element->addATTRIBUTE($this->xmlobjtype);'."\n";
	foreach($atts as $tmp) {
		$data .= "\t\t".'$this->element->addATTRIBUTE($this->'.$tmp.');'."\n";
	}
	$data .= "\t\t".'$document = new DOCUMENT();'."\n";
	$data .= "\t\t".'if(isset($this->document) && $this->document != null && $this->document != 0)'."\n";
	$data .= "\t\t\t".'$document->become($this->document);'."\n";
	$data .= "\t\t".'if(!isset($document->root)) $this->element->add();'."\n";
	$data .= "\t\t".'else $document->root->addCHILD($this->element);'."\n";
	$data .= "\t}\n";
	
	// become function
	$data .= "\n\t//// become by id\n";
	$data .= "\tfunction become(\$id) {\n";
	$data .= "\t\t\$this->element->become(\$id);\n";
	$data .= "\t\t".'foreach($this->element->attributes as $tmp) {'."\n";
	foreach($atts as $tmp) {
		$data .= "\t\t\t".'if($tmp->name == "'.$tmp.'")'."\n";
		$data .= "\t\t\t\t\$this->$tmp = \$tmp;\n";
	}
	$data .= "\t\t}\n";
	foreach($children as $tmp) {
		$data .= "\t\t".'$tmp = new ATTRIBUTE();'."\n";
		$data .= "\t\t".'$tmp->name = "xmlobjtype";'."\n";
		$data .= "\t\t".'$tmp->value = "'.$tmp.'";'."\n";
		$data .= "\t\t".'$this->'.$tmp.'_children = $this->element->findCHILDID("'.$tmp.'", $tmp);'."\n";
	}
	$data .= "\t}\n";
	
	// set functions
	foreach($atts as $att) {
		$data .= "\n\tfunction set_$att(\$value) {\n";
		$data .= "\t\t".'$this->'.$att.'->value = $value;'."\n";
		$data .= "\t\t".'$this->'.$att.'->update();'."\n";
		$data .= "\t\t".'if(isset($this->element->id))'."\n\t\t\t".'$this->element->become($this->element->id);'."\n";
		$data .= "\t}\n";
	}
	
	// cdata funcs (set and get)
	$data .= "\n\tfunction set_cdata(\$value) {\n";
	$data .= "\t\t".'$this->element->cdata = $value;'."\n";
	$data .= "\t}\n";
	
	$data .= "\n\tfunction get_cdata(\$value) {\n";
	$data .= "\t\t".'return $this->element->cdata;'."\n";
	$data .= "\t}\n";
	
	// get id function
	$data .= "\n\tfunction get_id() {\n";
	$data .= "\t\treturn \$this->element->id;\n";
	$data .= "\t}\n";
	
	// get functions
	foreach($atts as $att) {
		$data .= "\n\tfunction get_$att() {\n";
		$data .= "\t\t".'return $this->'.$att.'->value;'."\n";
		$data .= "\t}\n";
	}
	
	// add (children) functions
	foreach($children as $child) {
		$data .= "\n\tfunction add_".$child."(\$child) {\n";
		$data .= "\t\t".'$this->element->addCHILD($child->element);'."\n";
		$data .= "\t\t".'$child->update();'."\n";
		$data .= "\t\t".'$this->become($this->element->id);'."\n";
		$data .= "\t}\n";
	}
	
	// getHTMLFORM function
	$data .= "\n\tfunction getHTMLFORM(\$action = \"#\", \$method = \"POST\") {\n";
	$data .= "\t\t".'$form = "<form action=\"$action\" method=\"$method\"><table>";'."\n";
	foreach($atts as $att) {
		$show = str_replace("_", " ", $att);
		$data .= "\t\t\$form .= '<tr>';\n";
		$data .= "\t\t".'$form .= "<td>'.$show.'</td>";'."\n";
		$data .= "\t\t".'$form .= "<td><input type=\"text\" name=\"'.$att.'\" value=\"".$this->get_'.$att.'()."\" /></td>";'."\n";
		$data .= "\t\t".'$form .= "</tr>";'."\n";
	}
	$data .= "\t\t".'$form .= \'<tr><td colspan="2"><input type="submit" value="Submit" /></td></tr>\';'."\n";
	$data .= "\t\t\$form .= '<table></form>';\n";
	$data .= "\t\treturn \$form;\n";
	$data .= "\t}\n";
	
	// processHTMLFORM function
	// to be released
	$data .= "\tfunction processHTMLFORM() {\n";
	foreach($atts as $att) {
		$data .= "\t\tglobal \$$att;\n";
		$data .= "\t\t".'$this->set_'.$att.'($'.$att.')'.";\n";
	}
	$data .= "\t\t".'$this->update();'."\n";
	$data .= "\t}\n";
	
	$data .= "}";
	
	//// print data
	$data = htmlspecialchars($data);
	$data = nl2br($data);
	$data = str_replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;", $data);
	echo $data;
	//// print footer
	echo $footer;
}
if(!$_POST)
	printform();
else
	genclass();
?>
Return current item: MySQL-XML