Newer
Older
<?php
/*********************************************************************
class.forms.php
osTicket forms framework
Jared Hancock <jared@osticket.com>
Copyright (c) 2006-2013 osTicket
http://www.osticket.com
Released under the GNU General Public License WITHOUT ANY WARRANTY.
See LICENSE.TXT for details.
vim: expandtab sw=4 ts=4 sts=4:
**********************************************************************/
/**
* Form template, used for designing the custom form and for entering custom
* data for a ticket
*/
class Form {
var $fields = array();
var $title = 'Unnamed';
function Form() {
call_user_func_array(array($this, '__construct'), func_get_args());
}
function __construct($fields=array(), $source=null, $options=array()) {
foreach ($fields as $f)
$f->setForm($this);
if (isset($options['title']))
$this->title = $options['title'];
if (isset($options['instructions']))
$this->instructions = $options['instructions'];
// Use POST data if source was not specified
$this->_source = ($source) ? $source : $_POST;
function data($source) {
foreach ($this->fields as $name=>$f)
if (isset($source[$name]))
$f->value = $source[$name];
}
function getFields() {
return $this->fields;
}
function getTitle() { return $this->title; }
function getInstructions() { return $this->instructions; }
function getSource() { return $this->_source; }
/**
* Validate the form and indicate if there no errors.
*
* Parameters:
* $filter - (callback) function to receive each field and return
* boolean true if the field's errors are significant
*/
function isValid($include=false) {
if (!is_array($this->_errors)) {
$this->_errors = array();
$this->getClean();
foreach ($this->getFields() as $field)
if ($field->errors() && (!$include || $include($field)))
$this->_errors[$field->get('id')] = $field->errors();
}
return !$this->_errors;
}
function getClean() {
if (!$this->_clean) {
$this->_clean = array();
foreach ($this->getFields() as $key=>$field) {
$this->_clean[$key] = $this->_clean[$field->get('name')]
= $field->getClean();
}
}
return $this->_clean;
}
function errors() {
return $this->_errors;
function render($staff=true, $title=false, $instructions=false) {
if ($title)
$this->title = $title;
if ($instructions)
$this->instructions = $instructions;
$form = $this;
if ($staff)
include(STAFFINC_DIR . 'templates/dynamic-form.tmpl.php');
else
include(CLIENTINC_DIR . 'templates/dynamic-form.tmpl.php');
}
}
require_once(INCLUDE_DIR . "class.json.php");
class FormField {
static $widget = false;
var $ht = array(
'label' => 'Unlabeled',
'required' => false,
'default' => false,
'configuration' => array(),
);
var $_clean;
var $_errors = array();
var $presentation_only = false;
'Basic Fields' => array(
'text' => array('Short Answer', TextboxField),
'memo' => array('Long Answer', TextareaField),
'thread' => array('Thread Entry', ThreadEntryField, false),
'datetime' => array('Date and Time', DatetimeField),
'phone' => array('Phone Number', PhoneField),
'bool' => array('Checkbox', BooleanField),
'choices' => array('Choices', ChoiceField),
'break' => array('Section Break', SectionBreakField),
),
);
static $more_types = array();
function __construct($options=array()) {
static $uid = 100;
$this->ht = array_merge($this->ht, $options);
if (!isset($this->ht['id']))
$this->ht['id'] = $uid++;
}
static function addFieldTypes($group, $callable) {
static::$more_types[$group] = $callable;
}
static function allTypes() {
if (static::$more_types) {
foreach (static::$more_types as $group=>$c)
static::$types[$group] = call_user_func($c);
static::$more_types = array();
}
return static::$types;
}
static function getFieldType($type) {
foreach (static::allTypes() as $group=>$types)
if (isset($types[$type]))
return $types[$type];
}
function get($what) {
return $this->ht[$what];
}
/**
* getClean
*
* Validates and cleans inputs from POST request. This is performed on a
* field instance, after a DynamicFormSet / DynamicFormSection is
* submitted via POST, in order to kick off parsing and validation of
* user-entered data.
*/
function getClean() {
$this->_clean = (isset($this->value))
? $this->value : $this->parse($this->getWidget()->value);
$this->validateEntry($this->_clean);
}
return $this->_clean;
function addError($message, $field=false) {
if ($field)
$this->_errors[$field] = $message;
else
$this->_errors[] = $message;
}
Loading
Loading full blame...