File: D:/HostingSpaces/SBogers64/klimroosbudel.nl/wwwroot/kms/lib/form/form_field.class.php
<?php
/**
* m_form.php$_info
* Created by Komma Mediadesign.
* Author: mike
* Date: 3/27/13
*/
class Form_Field extends Functions
{
/*
* Error
*/
protected $_error;
/*
* Information about the field
*/
protected $_info = ['type'=>'text', 'classes'=>['reset_value']];
/*
* Construct
*/
public function __construct($label = null)
{
$LanguageHandler = new LanguageHandler();
$LanguageHandler->set(URL_LANG);
$this->_info['lang'] = $LanguageHandler->get();
$this->_info['label'] = $label;
$this->_info['name'] = $this->camelCase($label);
$this->_info['id'] = $this->_info['name'];
}
/*
* Configure form field
*/
public function config($key, $value)
{
if (! empty($key) && $value != null) {
if ((isset($this->_info[$key]) && is_array($this->_info[$key]) || is_array($value))) {
if (is_array($value) && key($value) != 0) {
foreach ($value as $k => $val) {
$this->_info[$key][$k] = $val;
}
}
if (is_array($value)) {
foreach ($value as $k => $val) {
$this->_info[$key][] = $val;
}
} else {
$this->_info[$key][] = $value;
}
} else {
$this->_info[$key] = $value;
}
}
}
/*
* Get form configuration
*/
public function get($key = null)
{
if (! $key == null) {
return $this->_info[$key];
}
return $this->_info;
}
/*
* Get form configuration
*/
public function set($info)
{
if (! empty($info)) {
$this->_info = $info;
}
}
/*
* Set value of the field
*/
public function defaultValue()
{
if ($value = Session::get([$this->_info['form_name'].'_data', $this->_info['name']])) {
$this->_info['value'] = $value;
} else {
$this->_info['value'] = $this->_info['label'];
}
}
/*
* Validate form field
*/
public function validate($input = null)
{
// info['validation'] has [@attributes] as first key.
$validation = [];
if (isset($this->_info['validation'])) {
foreach ($this->_info['validation'] as $info) {
if (gettype($info) == 'object') {
$validation[] = $info->getName();
}
}
}
$this->_info['validation'] = $validation;
if (count($this->_info['validation']) > 0) {
// Field is required, check if value exists (for checkboxes)
if (in_array('required_isset', $this->_info['validation'])) {
if (! isset($_POST[$this->_info['name']])) {
$this->_error = 'Please enter '.$this->_info['label'];
return false;
}
}
// Set value to validate
$input = null;
if (isset($_POST[$this->_info['name']])) {
$input = $_POST[$this->_info['name']];
}
// Field is required, check if value is not empty)
if (in_array('required', $this->_info['validation'])) {
if (empty($input) || strip_tags($input) == $this->_info['label']) {
$this->_error = 'Please enter '.$this->_info['label'];
return false;
}
}
// Check if value is a valid e-mail string
if (in_array('email', $this->_info['validation'])) {
if (! $this->checkMail($input)) {
$this->_error = 'Not a valid e-mail address';
return false;
}
}
// Specific validation for each object
if ((in_array('specified', $this->_info['validation']) && isset($this->_info['object']))) {
$object = $this->_info['object'];
$file = DOCUMENT_ROOT.'lib/form/types/'.$object.'.class.php';
if (is_file($file)) {
if (! class_exists($object)) {
require $file;
}
$TypeObject = new $object;
if ($TypeObject->validate($input)) {
return true;
}
$this->_error = $TypeObject->getError();
}
return false;
}
}
if ((isset($this->_info['object']) && $this->_info['object'] == 'Field_Type_Checkbox')) {
isset($_POST[$this->_info['name']]) ? $value = $_POST[$this->_info['name']] : $value = 0;
$key = [$this->_info['form_name'].'_data', $this->_info['name']];
Session::set($key, $value);
}
return true;
}
public function getError()
{
return $this->_error;
}
}