File: D:/HostingSpaces/SBogers64/klimroosbudel.nl/wwwroot/kms/lib/form/form_reader.class.php
<?php
require DOCUMENT_ROOT.'lib/form/form.class.php';
require DOCUMENT_ROOT.'lib/form/form_field.class.php';
require DOCUMENT_ROOT.'lib/form/form_block.class.php';
/**
* form_reader.php
* Created by Komma Mediadesign.
* Author: mike
* Date: 3/29/13
*/
class Form_Reader
{
/*
* XML Object
*/
private $_xml;
/*
* Add / edit form
*/
private $_type = 'add';
/*
* Fields
*/
private $_fields;
/*
* All Blocks
*/
private $_blocks;
/*
* Main block output with column structure
*/
private $_blockOutput;
/*
* Layer 1 blocks
*/
//private $_mainBlocks;
/*
* Buttons
*/
private $_buttons;
public function __construct()
{
}
/*
* Load XML file
*/
public function load($file)
{
if (is_file($file)) {
ob_start();
require $file;
$string = ob_get_contents();
ob_end_clean();
$this->_xml = simplexml_load_string($string);
}
}
/*
* Set form type to add or edit
*/
public function setType($type)
{
$this->_type = $type;
}
/*
* Generate form
*/
public function generate()
{
if (! empty($this->_xml)) {
// create form data array
$data = get_object_vars($this->_xml->data);
$fields = get_object_vars($this->_xml->fields);
$blockSection = get_object_vars($this->_xml->blocks);
$buttons = get_object_vars($this->_xml->buttons);
// Create form with attributes , convert name
$Form = new Form($data['name']);
$Form->setType($this->_type);
// Set form attributes
foreach ($data as $attr => $value) {
$Form->setAttr($attr, $value);
}
// Add fields
foreach ($fields[key($fields)] as $field) {
// add field object
$i = count($this->_fields);
$temp = get_object_vars($field);
$label = $temp['label'];
$this->_fields[$i] = new Form_Field($label);
// configure field
foreach ($field as $key => $value) {
// prepare value
$temp = get_object_vars($value);
// check for array or value
if (count($temp) > 0) {
$value = $temp;
// catch two-dimensional array (for example classes)
if (is_array($temp[key($temp)]) && count($temp) == 1) {
$value = $temp[key($temp)];
}
// if fields ! has attribute "type = associative" -> convert to indexed array.
if (! isset($temp['@attributes']) || ! in_array('associative', $temp['@attributes'])) {
$value = array_values($value);
}
} else {
$value = (string) $value;
}
$this->_fields[$i]->config($key, $value);
$this->_fields[$i]->config('form_name', $data['name']);
$this->_fields[$i]->defaultValue();
}
$name = $this->_fields[$i]->get('name');
$this->_fields[$name] = $this->_fields[$i];
// Add blocks to form
$Form->addField($this->_fields[$i]);
}
// Check if we need to fill the fields with the database data
if ($this->_type == 'edit') {
// Id to edit
defined('URL_SUB2') ? $id = URL_SUB2 : $id = 0;
// Enter data from the database into the form
if (! Session::get(['data_stored', $id])) {
$Form->fillStoredData($id);
Session::set(['data_stored', $id], true);
}
}
// define Blocks
$blockOutput = '';
if (is_array($blockSection['block'])) {
foreach ($blockSection as $blocks) {
$this->checkBlockChild($blocks);
$blockOutput .= $this->_blockOutput;
}
} else {
$this->checkBlockChild($blockSection);
$blockOutput .= $this->_blockOutput;
}
// add blocks to form
$Form->addBlocks($blockOutput);
// add buttons
$i = count($this->_fields); // ??
foreach ($buttons as $button) {
if (! is_array($button)) {
$options = get_object_vars($button);
$this->_buttons[$i] = new Button($options);
$Form->addButton($this->_buttons[$i]);
} else {
foreach ($button as $btn) {
$options = get_object_vars($btn);
$this->_buttons[$i] = new Button($options);
$Form->addButton($this->_buttons[$i]);
}
}
}
return $Form;
}
return false;
}
/*
* Check if a block has children
* Add output to the main block output
*/
private function checkBlockChild($parent, $parentName = null)
{
$hasChildren = true;
$output = '';
while ($hasChildren) {
foreach ($parent as $blockChild) {
$blockArr = get_object_vars($blockChild);
$name = $blockArr['name'];
$this->_blocks[$name] = new Form_Block();
foreach ($blockArr as $key => $value) {
if ($key != 'children' && $key != 'fields') {
$this->_blocks[$name]->setData($key, $value);
}
}
// check if this block has children
if (isset($blockArr['children'])) {
$childrenArr = get_object_vars($blockArr['children']);
$output .= $this->checkBlockChild($childrenArr['block'], $name);
// add fields
$this->checkBlockFields($blockArr, $name);
$this->_blockOutput .= $this->_blocks[$name]->display();
} else {
$hasChildren = false;
if ($parentName == null) {
// add fields
$this->checkBlockFields($blockArr, $name);
$this->_blockOutput .= $this->_blocks[$name]->display();
}
}
// add children to parent
if ($parentName != null) {
$this->checkBlockFields($blockArr, $name);
$add = $this->_blocks[$name]->display();
$this->_blocks[$parentName]->addChild($add);
}
}
}
return $output;
}
/*
* Check if a block contains fields
*/
private function checkBlockFields($block, $name)
{
if (isset($block['fields'])) {
foreach ($block['fields'] as $fieldName) {
$fieldName = (string) $fieldName;
$Field = $this->_fields[$fieldName];
$this->_blocks[$name]->addField($Field);
}
}
}
}