File: D:/HostingSpaces/SBogers68/resortouddorpduin.nl/wwwroot/lib/form/form.class.php
<?php
/**
* Form.class.php
* Created by Komma Mediadesign.
* Author: mike
* Date: 13/12/13
*/
require_once DOCUMENT_ROOT . 'lib/form/form_field.class.php';
require_once DOCUMENT_ROOT . 'lib/form/form_live_validation.class.php';
class Form
{
/*
* Form Name - String
*/
private $_name;
/*
* XML - XML Object
*/
private $_xml;
/*
* File to load - string
*/
private $_file;
/*
* Form Fields - Array
*/
private $_fields;
/*
* Tabs - array
*/
private $_tabs;
/*
* Current tab - XML object
*/
private $_currentTab;
/*
* Add or edit - String
*/
private $_type;
/*
* Construct
*/
public function __construct($name)
{
// Set name
$this->_name = $name;
// Set file
$this->_file = DOCUMENT_ROOT . 'config/forms/' . $this->_name . '.xml.php';
// Get XML file
$this->load();
// Set fields array
$this->setFields();
}
/*
* Get tabs menu
*/
public function getTabs()
{
// Read tabs
$tabs = get_object_vars($this->_xml->tabs);
$this->_tabs = $tabs[key($tabs)];
// Create output
$tabs = array();
// Are there more then one tab
if(count($this->_tabs) > 1)
{
// Generate output
foreach($this->_tabs as $tab)
{
// Tabs
$tab = get_object_vars($tab);
$tabs[] = $tab['label'];
}
}
return $tabs;
}
/*
G E N E R A T E
*/
/*
* Generate output
* @return string
*/
public function generate()
{
// Get fields on current tab
$this->getCurrentTab();
// Return output
return $this->showCurrentTab();
}
/*
* Load file
* @param string
* @return boolean
*/
private function load()
{
// Check if file exists
if(is_file($this->_file))
{
ob_start();
require $this->_file ;
$string = ob_get_contents();
ob_end_clean();
$this->_xml = simplexml_load_string($string);
return true;
}
return false;
}
/*
* Read fields from array
*/
private function setFields()
{
// Get ALL Fields as array
$fields = get_object_vars($this->_xml->fields);
$fields=$fields[key($fields)];
// Get Fields in current Tab as array
$this->getCurrentTab();
$currentFields = get_object_vars($this->_currentTab->fields);
$currentFields = $currentFields[key($currentFields)];
// Loop trough array
foreach($fields as $field)
{
$data = get_object_vars($field);
// Only if field is visible
if( in_array($data['name'],$currentFields))
{
// Create validation array
$temp = get_object_vars($data['validation']);
$data['validation'] = array();
foreach(array_keys($temp) as $validation )
{
$data['validation'][] = $validation;
}
// Add a new field to the global fields array
$file = DOCUMENT_ROOT . 'lib/form/types/' . strtolower($data['object']) . '.class.php';
if(is_file($file))
{
if( ! class_exists($data['object'])) require $file;
$this->_fields[$data['name']] = new $data['object'];
$this->_fields[$data['name']]->setFormName($this->_name);
$this->_fields[$data['name']]->setData($data);
}
}
}
}
/*
* Finds the current tab
*/
private function getCurrentTab()
{
// Read tabs
$tabs = get_object_vars($this->_xml->tabs);
$this->_tabs = $tabs[key($tabs)];
// Are there more then one tab
if(count($this->_tabs) > 1)
{
// If not defined we're on the first tab
if(defined('URL_SUB2'))
{
// Generate output
foreach($this->_tabs as $tab)
{
// Compare with URL
if(Fn::encodeUrl($tab->label) == URL_SUB2)
{
$this->_currentTab = $tab;
}
}
}
else
{
// Set current tab to first tab
$this->_currentTab = $this->_tabs[key($this->_tabs)];
}
}
else
{
$this->_currentTab = $this->_tabs[0];
}
}
/*
* Generates output for current tab
* @return string
*/
private function showCurrentTab()
{
$output = $jsOutput = '';
// Read fields in current tab
$fields = get_object_vars($this->_currentTab->fields);
$fields = $fields[key($fields)];
foreach($fields as $i => $key)
{
// Certain type?
$attr = get_object_vars($this->_currentTab->fields->field[$i]);
if(isset($attr['@attributes']['type']))
{
$type = $attr['@attributes']['type'];
switch($type)
{
case 'label':
$output .= ' <h3>' . $key . '</h3>';
break;
case 'spacer':
// Leave some space between the fields
$output .= '<div class="spacer"></div>';
break;
}
}
// Normal field
else if(isset($this->_fields[$key]))
{
// Get Field object from global array
$Field = $this->_fields[$key];
// Get fields
$output .= $Field->generate();
// Add field live validation
$jsOutput .= $Field->addLiveValidation();
}
}
// Add live validation javascript
if( ! empty($jsOutput))
{
$_SESSION['js_output'] .= '<script>' . $jsOutput . '</script>';
}
return $output;
}
/*
V A L I D A T E
*/
/*
* Validate output
* @return boolean
*/
public function validate()
{
// Get Fields in current Tab as array
$this->getCurrentTab();
$currentFields = get_object_vars($this->_currentTab->fields);
$currentFields = $currentFields[key($currentFields)];
// Clear error session
$_SESSION[$this->_name . '_errors'] = array();
$valid = true;
foreach($this->_fields as $name => $Field)
{
if( in_array($name,$currentFields))
{
if( ! $Field->validate())
{
// set error message
$_SESSION[$this->_name . '_errors'][$name] = $Field->getErrors();
$valid = false;
}
}
}
return $valid;
}
/*
* Save values in data session
*/
public function saveValues()
{
foreach($_POST as $field => $value)
{
if(isset($_SESSION['current_item']))
{
// In case of existing item
$_SESSION[$this->_name][$_SESSION['current_item']]['data'][$field] = $value;
}
else
{
// In case of new item
$_SESSION[$this->_name]['data'][$field] = $value;
}
}
}
/*
* Prepare values for the database
*/
public function prepare()
{
// Get All fields as array
$fields = get_object_vars($this->_xml->fields);
$fields=$fields[key($fields)];
// Data to store
$data = array();
// Get values
foreach($fields as $field)
{
$fieldData = get_object_vars($field);
// Get value
if(isset($_SESSION['current_item']))
{
// In case of existing item
$value = $_SESSION[$this->_name][$_SESSION['current_item']]['data'][$fieldData['name']];
}
else
{
// In case of new item
$value = $_SESSION[$this->_name]['data'][$fieldData['name']];
}
// Check if to prepare values
$file = DOCUMENT_ROOT . 'lib/form/types/' . $fieldData['object'] . '.class.php';
if(is_file($file))
{
if( ! class_exists($fieldData['object'])) require $file;
$TypeObject = new $fieldData['object'];
if(method_exists($fieldData['object'],'prepareForDb'))
{
$value = $TypeObject->prepareForDb($value);
}
}
// Add value to data array
$data[$fieldData['name']] = $value;
}
// add a timestamp to the data
if( ! isset($data['timest']) && $_POST['form_type'] == 'add') $data['timest'] = time();
return $data;
}
}