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'),
'joins' => array(
'fields' => array(
'reverse' => 'DynamicFormField.form',
),
),
// Registered form types
static $types = array(
'T' => 'Ticket Information',
'U' => 'User Information',
function getFields($cache=true) {
if (!$cache)
$fields = false;
else
$fields = &$this->_fields;
if (!$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->getLocal('title'); }
function getInstructions() { return $this->getLocal('instructions'); }
function getForm($source=false) {
if (!$this->_form || $source) {
$fields = $this->getFields($this->_has_data);
$this->_form = new Form($fields, $source, array(
'title'=>$this->getLocal('title'), 'instructions'=>$this->getLocal('instructions')));
function isDeletable() {
return $this->get('deletable');
}
function disableFields(array $ids) {
foreach ($this->getFields() as $F) {
if (in_array($F->get('id'), $ids)) {
$F->disable();
}
}
}
function instanciate($sort=1) {
return DynamicFormEntry::create(array(
'form_id'=>$this->get('id'), 'sort'=>$sort));
function data($data) {
if ($data instanceof DynamicFormEntry) {
$this->_fields = $data->getFields();
$this->_has_data = true;
function getTranslateTag($subtag) {
return _H(sprintf('form.%s.%s', $subtag, $this->id));
}
function getLocal($subtag) {
$tag = $this->getTranslateTag($subtag);
$T = CustomDataTranslation::translate($tag);
return $T != $tag ? $T : $this->get($subtag);
}
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);
if ($rv = parent::save($refetch | $this->dirty))
return $this->saveTranslations();
return $rv;
}
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;
}
function saveTranslations($vars=false) {
Loading
Loading full blame...