File: D:/HostingSpaces/SBogers59/ferrumbv.nl/wwwroot/kms/lib/form/form.class.php
<?php
/**
* m_form.php
* Created by Komma Mediadesign.
* Author: mike
* Date: 3/27/13
*/
class Form
{
/*
* Form attributes
*/
private $_formAttr;
/*
* Add / edit form
*/
private $_type;
/*
* Contains the fields
*/
private $_fields = array();
/*
* Contains the fields
*/
private $_buttons = array();
/*
* Contains a string with blockOutput
*/
private $_blockOutput = array();
/*
* Construct
*/
public function __construct($name)
{
$this->_formAttr['name'] = $name;
$this->_id = $this->_formAttr['name'];
}
/*
* Get form configuration
*/
public function get()
{
return $this->_formAttr;
}
/*
* Adds a field to the form
* @param Field object
*/
public function addField($Field)
{
$this->_fields[] = $Field;
}
/*
* Adds a block to the form
* @param string
*/
public function addBlocks($string)
{
$this->_blockOutput = $string;
}
/*
* Adds a button to the form
* @param Button object
*/
public function addButton($Button)
{
$this->_buttons[] = $Button;
}
/*
* Set attributes
*/
public function setAttr($attr, $value)
{
$this->_formAttr[$attr] = $value;
}
/*
* Set form type to add or edit
*/
public function setType($type)
{
$this->_type = $type;
}
/*
* Render the form
*/
public function render()
{
// Start form with specified attributes
$form = '<form method="post"';
foreach($this->_formAttr as $attr => $value)
{
if( isset($value) && ! empty($value)) $form .= ' ' . $attr . '="' . $value . '"';
}
$form .= '>';
if( ! empty($this->_blockOutput)) $form .= $this->_blockOutput;
// Add some buttons to the form
if( ! empty($this->_buttons))
{
foreach($this->_buttons as $Button)
{
$Button->addClasses(array('blue'));
$form .= $Button->display(FALSE);
}
}
$form .= '<input type="hidden" name="form_type" value="'. $this->_type .'" />';
if($this->_type == 'edit')
{
// Id to edit
defined('URL_SUB2') ? $id = URL_SUB2 : $id = 0;
$form .= '<input type="hidden" name="edit_id" value="'. $id .'" />';
}
$form .= '</form>';
return $form;
}
/*
* Validate this form
*/
public function validate()
{
if(Session::get($this->_formAttr['name'] . '_errors')) Session::destroy($this->_formAttr['name'] . '_errors');
$valid = true;
foreach($this->_fields as $Field)
{
$data = $Field->get();
if( ! $Field->validate())
{
Session::set(array($this->_formAttr['name'] . '_errors',$data['name']),$Field->getError());
$valid = false;
}
}
return $valid;
}
/*
* Reset $this->_fields to an empty array
*/
public function clearFields()
{
$this->_fields = array();
}
/*
* Prepare values for the database
*/
public function prepare()
{
// Data to store
$data = array();
// Get values
foreach($this->_fields as $Field)
{
$fieldData = $Field->get();
$fieldName = $fieldData['name'];
$key = array($fieldData['form_name'] . '_data',$fieldData['name']);
$value = Session::get($key);
// Check if value is equal to label
if(strip_tags($value) == $fieldData['label'])
{
$value = null;
}
// Check if to prepare values
if(isset($fieldData['object']))
{
$object = $fieldData['object'];
$file = DOCUMENT_ROOT . 'lib/form/types/' . $object . '.class.php';
if(is_file($file))
{
if( ! class_exists($object)) require $file;
$TypeObject = new $object;
if(method_exists($object,'prepareToDb'))
{
$value = $TypeObject->prepareToDb($value);
}
}
}
/* extension CSB */
if($fieldData['form_name'] == 'projects' && $fieldName == 'services')
{
// do nothing..
}
else
{
// Add value to data array
$data[$fieldName] = $value;
}
}
// add a timestamp to the data
if( ! isset($data['timest']) && $this->_type == 'add') $data['timest'] = time();
return $data;
}
/*
* Prepare values for the database
*/
public function fillStoredData($id)
{
$Dbh = new DatabaseHandler();
$Dbh->setTableName(TABLE_PREFIX . $this->_formAttr['name'] . '_items');
$Dbh->addRule('id',$id);
$result = $Dbh->select();
// Get values for each field
foreach($this->_fields as $Field)
{
$fieldData = $Field->get();
$fieldName = $fieldData['name'];
if(isset($result[$fieldName]))
{
$value = $result[$fieldName];
// prepare value from database?
if(isset($fieldData['object']))
{
$object = $fieldData['object'];
$file = DOCUMENT_ROOT . 'lib/form/types/' . $object . '.class.php';
if(is_file($file))
{
if( ! class_exists($object)) require $file;
$TypeObject = new $object;
if(method_exists($object,'prepareFromDb'))
{
$value = $TypeObject->prepareFromDb($value);
}
}
}
$sessionKey = array($this->_formAttr['name'] . '_data',$fieldName);
Session::set($sessionKey,$value);
}
$Field->defaultValue();
// Get Thumbnail
if(isset($result['thumb']))
{
$sessionKey = array($this->_formAttr['name'] . '_data','thumb');
Session::set($sessionKey,$result['thumb']);
}
if(isset($fieldData['object']))
{
if($fieldData['object'] == 'Field_Type_Blog_Image' || $fieldData['object'] == 'Field_Type_Blog_Single_Image')
{
// Fill images
$FileHandler = new File_Handler();
$FileHandler->set('_name', $this->_formAttr['name'] . '_images');
$FileHandler->set('_tableName',TABLE_PREFIX . $this->_formAttr['name'] . '_images');
$FileHandler->fillSession();
}
if($fieldData['object'] == 'Field_Type_Blog_Video')
{
// Fill videos
$VideoHandler = new Video_Handler();
$VideoHandler->set('_form_name', $this->_formAttr['name']);
$VideoHandler->fillSession();
}
}
}
/* CSB Extension */
// Get URL SUB
$LanguageHandler = new LanguageHandler();
$LanguageHandler->set(URL_LANG);
defined('URL_PAGE') ? $page = $LanguageHandler->convert(URL_PAGE) : $page = '';
if($page == 'projects' && defined('URL_SUB2'))
{
$sessionServices = array();
$itemId = URL_SUB2;
$Dbh = new DatabaseHandler();
$Dbh->setTableName(TABLE_PREFIX . 'projects_services');
$Dbh->setData(array('service_id'=>''));
$Dbh->addRule('project_id',$itemId);
$itemsInDB = $Dbh->select();
$itemsInDB = $Dbh->twoDimensional($itemsInDB);
foreach($itemsInDB as $item)
{
$sessionServices[] = $item['service_id'];
}
Session::set(array('projects_data','services'), $sessionServices);
}
}
}