Hellihello
na, es ging ja eher um die Grundlagen. Das XML könnte zB. aus einer Datei via file_get_contents() eingelesen werden. Oder per simplexml_load_file().
require_once müsste innerhalb des Outputbuffers auch gehen, aber da (s.o. und s.u.) sicher nicht angebracht.
Schau mal hier ein Versuch mit strikterem MVC:
<?php
/**
* My static functions for global use
*
*/
class My
{
/**
* create nice idented xml string (from simplexml)
* @param string | object
*/
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();
}
}
/**
* expand model with php-code and csv-options
*
*/
class ModelReader
{
/**
* @var SimpleXMLElement
*/
private $_rawModel = NULL;
/**
* @var SimpleXMLElement
*/
private $_expandedModel = NULL;
/**
* Constructor
*
* instanciate $_rawModel and $_expandedModel
*
* @param mixed $xml
* @return void
*/
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);
}
}
}
/**
* check if element contains option elemens
*
* @param SimpleXMLElement $simplexmlElement
* @return boolean
*/
private function _hasOptions ($simplexmlElement) {
return isset($simplexmlElement->options);
}
/**
* blow up options (comma seperated and "-" seperated
*
* @param SimpleXMLElement $var
* @return boolean
*/
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++;
}
}
}
/**
* get full model as XML-String
*
* @param void
* @return string
*/
public function getXML()
{
return $this->_expandedModel->asXML();
}
/**
* get model definition
*
* @param mixed $xml
* @return void
*/
public static function getModel($xml)
{
if (is_string($xml) && file_exists($xml)) {
$xml = file_get_contents("ModelModel.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 representing data model
private $_model = NULL;
private $_divKey = NULL;
// will be instance of SimpleXMLElement with root element named "form"
public $form = NULL;
// count of div elments
function __construct(SimpleXMLElement $model = NULL) {
$this->form = new SimpleXMLElement("<form/>");
$this->form["method"] = "post";
$this->form["action"] = basename(__FILE__);
if (NULL !== $model) {
$this->create($model);
}
// abschicken wollen wir auch noch
$this->button();
// abschicken wollen wir auch noch
$this->button("reset");
}
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 _setDivKey()
{
if (NULL === $this->_divKey) {
$this->_divKey= 0;
} else {
$this->_divKey++;
}
}
private function _getNewFormDiv()
{
$this->_setDivKey();
$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->label["class"] = "block";
$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;
$parentDiv->label["class"] = "block";
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 getForm()
{
return $this->form->asXML();
}
function formatNeatly()
{
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->loadxml($this->form->asXML());
$dom->formatOutput = true;
return $dom->saveXML();
}
}
class Model
{
private $_model = NULL;
function __construct($modelFileName)
{
$this->_model = ModelReader::getModel($modelFileName);
}
public function getForm()
{
return new FormCreator($this->_model);
}
}
class View
{
function __construct($params)
{
include ("doctemplate.inc.php");
}
}
class Controll
{
private $_modelPath = "";
public function __construct()
{
$this->_modelPath = "ModelModel.xml";
$model = new Model("ModelModel.xml");
$form = $model->getForm();
new View(array("form"=>$form));
}
}
new Controll();
Bzw. http://html-ag.wvs-berlin.de/selfhtml/downloads/Model_SELFHTML.zip
Dank und Gruß,