Newer
Older
<?php
/*********************************************************************
class.dynamic_forms.php
Forms models built on the VerySimpleModel paradigm. Allows for arbitrary
data to be associated with tickets. Eventually this model can be
extended to associate arbitrary data with registered clients and thread
entries.
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:
**********************************************************************/
require_once(INCLUDE_DIR . 'class.orm.php');
require_once(INCLUDE_DIR . 'class.forms.php');
require_once(INCLUDE_DIR . 'class.list.php');
require_once(INCLUDE_DIR . 'class.filter.php');
require_once(INCLUDE_DIR . 'class.signal.php');
/**
* Form template, used for designing the custom form and for entering custom
* data for a ticket
*/
class DynamicForm extends VerySimpleModel {
static $meta = array(
'table' => FORM_SEC_TABLE,
'ordering' => array('title'),
'pk' => array('id'),
);
// Registered form types
static $types = array(
'T' => 'Ticket Information',
'U' => 'User Information',
function getFields($cache=true) {
if (!$cache) {
$this->_fields = null;
}
if (!$this->_fields) {
$this->_fields = new ListObject();
$this->_fields->append($f->getImpl($f));
return $this->_fields;
if (!isset($this->id))
return array();
elseif (!$this->_dfields) {
->filter(array('form_id'=>$this->id))
foreach ($this->_dfields as $f)
$f->setForm($this);
}
// Multiple inheritance -- delegate to Form
function __call($what, $args) {
$delegate = array($this->getForm(), $what);
if (!is_callable($delegate))
throw new Exception(sprintf(__('%s: Call to non-existing function'), $what));
return call_user_func_array($delegate, $args);
function getField($name, $cache=true) {
foreach ($this->getFields($cache) as $f) {
}
if ($cache)
return $this->getField($name, false);
function hasField($name) {
return ($this->getField($name));
}
function getTitle() { return $this->get('title'); }
function getInstructions() { return $this->get('instructions'); }
/**
* Drop field errors clean info etc. Useful when replacing the source
* content of the form. This is necessary because the field listing is
* cached under some circumstances.
*/
function reset() {
foreach ($this->getFields() as $f)
$f->reset();
return $this;
}
if ($source)
$this->reset();
$fields = $this->getFields();
$form = new Form($fields, $source, array(
'title'=>$this->title, 'instructions'=>$this->instructions));
return $form;
}
function addErrors(array $formErrors, $replace=false) {
$fields = array();
foreach ($this->getFields() as $f)
$fields[$f->get('id')] = $f;
foreach ($formErrors as $id => $fieldErrors) {
if (isset($fields[$id])) {
if ($replace)
$fields[$id]->_errors = $fieldErrors;
else
foreach ($fieldErrors as $E)
$fields[$id]->addError($E);
}
function isDeletable() {
return $this->get('deletable');
}
function instanciate($sort=1, $data=null) {
return DynamicFormEntry::create(
array('form_id'=>$this->get('id'), 'sort'=>$sort),
$data);
function data($data) {
if ($data instanceof DynamicFormEntry) {
$this->_fields = $data->getFields();
$this->_has_data = true;
function save($refetch=false) {
if (count($this->dirty))
$this->set('updated', new SqlFunction('NOW'));
if (isset($this->dirty['notes']))
$this->notes = Format::sanitize($this->notes);
return parent::save($refetch);
}
function delete() {
if (!$this->isDeletable())
return false;
else
return parent::delete();
function getExportableFields($exclude=array()) {
$fields = array();
foreach ($this->getFields() as $f) {
// Ignore core fields
if ($exclude && in_array($f->get('name'), $exclude))
continue;
// Ignore non-data fields
elseif (!$f->hasData() || $f->isPresentationOnly())
continue;
$fields['__field_'.$f->get('id')] = $f;
}
return $fields;
}
static function create($ht=false) {
$inst = parent::create($ht);
$inst->set('created', new SqlFunction('NOW'));
if (isset($ht['fields'])) {
$inst->save();
foreach ($ht['fields'] as $f) {
$f = DynamicFormField::create($f);
$f->form_id = $inst->id;
$f->save();
}
}
return $inst;
}
Loading
Loading full blame...