Hellihello
Wahrscheinlich macht es keinen Sinn, so ein kleines Script in OOP zu schreiben, wie gesagt, es geht nur um das Verständnis.
Die Länge ist doch unerheblich. Ich wollte es grad mal anpassen, aber ich denke, bei u.g. kannst Du Dir das vielleicht selbst abschauen. Kommentare fehlen übrigens...;
<?php
// todo: outsource to file
ob_start()?><modelexample>
<name type="text"/>
<mailaddy type="text"/>
<url type="text"/>
<AGB type="checkbox"/>
<Wochentage type="radio">
<options>Mo,Di,Mi,Do,Fr</options>
</Wochentage>
<checkies type="checkbox">
<options>XML,HTML,PHP,JS</options>
</checkies>
<Abijahrgang type="select">
<options>k.A., 1950 - 2020</options>
</Abijahrgang>
<kommentar type="textarea"/>
</modelexample>
<?php
$rawModel = ob_get_clean();
class My
{
public static function formatNeatly($xml)
{
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
if (is_object($xml)) {
$xml = $xml->asXML();
}
$dom->loadxml($xml);
$dom->formatOutput = true;
return $dom->saveXML();
}
}
$model = ModelReader::getModel($rawModel);
// expand model with php-code and csv-options
class ModelReader
{
private $_rawModel = NULL;
private $_expandedModel = NULL;
private function __construct($xml) {
$this->_rawModel = new SimpleXMLElement($xml);
$this->_expandedModel = clone ($this->_rawModel);
foreach ($this->_expandedModel as $var) {
if ($this->_hasOptions($var)) {
$this->_explodeOptions($var);
}
}
}
private function _hasOptions ($simplexmlElement) {
return isset($simplexmlElement->options);
}
private function _explodeOptions ($var) {
$optionArray = NULL;
$optionKey = 0;
if ( strpos($var->options, ",") !== false ) {
$optionArray = explode(",",$var->options);
} elseif (strpos($option, "-") !== false) {
for($i = (int)$intervallArray[0]; $i < (int)$intervallArray[1]; $i++) {
$var->option[$optionKey] = $option;
$optionKey++;
}
return true;
} else {
return false;
}
unset($var->options);
foreach ($optionArray as $option) {
if (strpos($option, "-") !== false) {
$intervallArray = explode("-", $option);
for($i = (int)$intervallArray[0]; $i < (int)$intervallArray[1]; $i++) {
$var->option[$optionKey] = $i;
$optionKey++;
}
} else {
$var->option[$optionKey] = $option;
$optionKey++;
}
}
}
public function getXML()
{
return $this->_expandedModel->asXML();
}
public static function getModel($xml)
{
$modelReader = new self($xml);
return new SimpleXMLElement(
My::formatNeatly(
$modelReader->getXML()
)
);
}
}
/** 19.11.2008
* converts given model to form if given with presets using simplexml functions
*/
class FormCreator
{
// will be instance of SimpleXMLElement represneting data model
private $_model = NULL;
// will be instance of SimpleXMLElement with root element named "form"
public $form = NULL;
// count of div elments
private $_divKey = -1;
function __construct(SimpleXMLElement $model = NULL) {
$this->form = new SimpleXMLElement("<form/>");
$this->form["method"] = "post";
$this->form["action"] = basename(__FILE__);
}
private function _isPreset($name) {
return isset($_REQUEST["form"][$name]);
}
private function _getPresetValue($name) {
return $_REQUEST["form"][$name];
}
private function _isChecked($name, $value)
{
if (is_array($_REQUEST["form"][$name]) &&
in_array($value , $_REQUEST["form"][$name]) ||
$_REQUEST["form"][$name] == $value) {
return true;
}
return false;
}
private function _getNewFormDiv()
{
$this->_divKey++;
$this->form->div[$this->_divKey]["class"] = "label_control_set";
return $this->form->div[$this->_divKey];
}
private function _getNewLabelControllSpan($key)
{
$this->form->div[$this->_divKey]->span[$key]["class"] = "label_control_set";
return $this->form->div[$this->_divKey]->span[$key];
}
function textInput($name)
{
$parentDiv = $this->_getNewFormDiv();
$parentDiv->label = $name;
$parentDiv->label["class"] = "block";
$parentDiv->input["type"] = "text";
$parentDiv->input["name"] = "form[".$name."]";
$parentDiv->input["value"] = "preset";
if($this->_isPreset($name)) {
$parentDiv->input["value"] = $this->_getPresetValue($name);
}
}
function singleCheckbox($name)
{
$parentDiv = $this->_getNewFormDiv();
$parentDiv->label = $name;
$parentDiv->input["type"] = "checkbox";
$parentDiv->input["name"] = "form[".$name."]";
if($this->_isPreset($name)) {
$parentDiv->input["checked"] = "checked";
}
}
function optionSet($name, $values, $type = "radio") {
$valueKey = 0;
$parentDiv = $this->_getNewFormDiv();
$parentDiv->label = $name;
if ($type == "select") {
foreach ($values as $value) {
$parentDiv->select["name"] = "form[".$name."]";
$parentDiv->select->option[$valueKey] = $value;
if ($this->_isPreset($name) && $this->_isChecked($name, $value)) {
$parentDiv->select->option[$valueKey]["selected"] = "selected";
}
$valueKey++;
}
return true;
}
foreach ($values as $value) {
$parentSpan = $this->_getNewLabelControllSpan($valueKey);
$this->_optionElement($parentSpan, $name, $value, $type);
$valueKey++;
}
}
private function _optionElement($parentSpan, $name, $value, $type)
{
if ($type == "checkbox") {
$arrayBrackets = "[]";
}
$parentSpan->label = $value;
$parentSpan->input["type"] = $type;
$parentSpan->input["name"] = "form[".$name."]".$arrayBrackets;
$parentSpan->input["value"] = $value;
if ($this->_isPreset($name) && $this->_isChecked($name, $value)) {
$parentSpan->input["checked"] = "checked";
}
}
function textArea($name)
{
$parentDiv = $this->_getNewFormDiv();
$parentDiv->label = $name;
$parentDiv->label["class"] = "block";
$parentDiv->textarea["name"] = "form[".$name."]";
if(trim($this->_getPresetValue($name))) {
$parentDiv->textarea = $this->_getPresetValue($name) ;
} else {
$parentDiv->textarea = " ";
}
}
function create($model) {
$fc = $this;
foreach ($model as $var) {
$name = $var->getName();
switch ($var["type"]) {
case "text":
$fc->textInput($name);
break;
case "checkbox":
if (!isset($var->option)) {
$fc->singleCheckbox($name);
}else {
$fc->optionSet($name, $var->option, "checkbox");
}
break;
case "select":
$fc->optionSet($name, $var->option, "select");
break;
case "radio":
$fc->optionSet($name, $var->option, "radio");
break;
case "textarea":
$fc->textArea($name);
break;
}
}
}
function button($value = "submit") {
$parentDiv = $this->_getNewFormDiv();
if ($value == "reset") {
$parentDiv->input["type"] = "reset";
return;
}
$parentDiv->button["value"] = $value;
$parentDiv->button["type"] = $value;
$parentDiv->button = $value;
}
function formatNeatly()
{
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->loadxml($this->form->asXML());
$dom->formatOutput = true;
return $dom->saveXML();
}
}
///////////////// model:
$fc = new FormCreator();
$fc->create($model);
function hide() {
// two text-inputs
}
// abschicken wollen wir auch noch
$fc->button();
// abschicken wollen wir auch noch
$fc->button("reset");
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8"/>
<title>sxe-form-data</title>
<style type="text/css">
div, label, input, button {padding:.2em}
label.block {display: block; float:left; width:5em;text-align:right}
textarea {width:80%; height:20em;}
</style>
</head>
<body>
<?= $fc->formatNeatly();?>
<hr/>
<pre>
<?//~ = var_dump(get_object_vars($_form))?>
</pre>
<pre>
<?= var_dump($_REQUEST)?>
</pre>
</body>
</html>
Dank und Gruß,