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;
}
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
function saveTranslations($vars=false) {
global $thisstaff;
$vars = $vars ?: $_POST;
$tags = array(
'title' => $this->getTranslateTag('title'),
'instructions' => $this->getTranslateTag('instructions'),
);
$rtags = array_flip($tags);
$translations = CustomDataTranslation::allTranslations($tags, 'phrase');
foreach ($translations as $t) {
$T = $rtags[$t->object_hash];
$content = @$vars['trans'][$t->lang][$T];
if (!isset($content))
continue;
// Content is not new and shouldn't be added below
unset($vars['trans'][$t->lang][$T]);
$t->text = $content;
$t->agent_id = $thisstaff->getId();
$t->updated = SqlFunction::NOW();
if (!$t->save())
return false;
}
// New translations (?)
foreach ($vars['trans'] as $lang=>$parts) {
if (!Internationalization::isLanguageInstalled($lang))
continue;
foreach ($parts as $T => $content) {
$content = trim($content);
if (!$content)
continue;
$t = CustomDataTranslation::create(array(
'type' => 'phrase',
'object_hash' => $tags[$T],
'lang' => $lang,
'text' => $content,
'agent_id' => $thisstaff->getId(),
'updated' => SqlFunction::NOW(),
));
if (!$t->save())
return false;
}
}
return true;
}
static function getCrossTabQuery($object_type, $object_id='object_id', $exclude=array()) {
$fields = static::getDynamicDataViewFields($exclude);
return "SELECT entry.`object_id` as `$object_id`, ".implode(',', $fields)
.' FROM '.FORM_ENTRY_TABLE.' entry
JOIN '.FORM_ANSWER_TABLE.' ans ON ans.entry_id = entry.id
JOIN '.FORM_FIELD_TABLE." field ON field.id=ans.field_id
WHERE entry.object_type='$object_type' GROUP BY entry.object_id";
}
// Materialized View for Ticket custom data (MySQL FlexViews would be
// nice)
//
// @see http://code.google.com/p/flexviews/
static function getDynamicDataViewFields($exclude) {
$fields = array();
foreach (static::getInstance()->getFields() as $f) {
if ($exclude && in_array($f->get('name'), $exclude))
continue;
$impl = $f->getImpl();
if (!$impl->hasData() || $impl->isPresentationOnly())
continue;
$id = $f->get('id');
$name = ($f->get('name')) ? $f->get('name')
: 'field_'.$id;
if ($impl instanceof ChoiceField || $impl instanceof SelectionField) {
$fields[] = sprintf(
'MAX(CASE WHEN field.id=\'%1$s\' THEN REPLACE(REPLACE(REPLACE(REPLACE(coalesce(ans.value_id, ans.value), \'{\', \'\'), \'}\', \'\'), \'"\', \'\'), \':\', \',\') ELSE NULL END) as `%2$s`',
$id, $name);
}
else {
$fields[] = sprintf(
'MAX(IF(field.id=\'%1$s\',coalesce(ans.value_id, ans.value),NULL)) as `%2$s`',
$id, $name);
}
}
return $fields;
}
}
class UserForm extends DynamicForm {
static function objects() {
$os = parent::objects();
return $os->filter(array('type'=>'U'));
}
static function getUserForm() {
if (!isset(static::$form)) {
static::$form = static::objects()->one();
return static::$form;
}
static function getInstance() {
if (!isset(static::$instance))
static::$instance = static::getUserForm()->instanciate();
static function getNewInstance() {
$o = static::objects()->one();
static::$instance = $o->instanciate();
return static::$instance;
}
Filter::addSupportedMatches(/* @trans */ 'User Data', function() {
$matches = array();
foreach (UserForm::getInstance()->getFields() as $f) {
if (!$f->hasData())
continue;
$matches['field.'.$f->get('id')] = __('User').' / '.$f->getLabel();
if (($fi = $f->getImpl()) && $fi->hasSubFields()) {
foreach ($fi->getSubFields() as $p) {
$matches['field.'.$f->get('id').'.'.$p->get('id')]
= __('User').' / '.$f->getLabel().' / '.$p->getLabel();
}
return $matches;
class TicketForm extends DynamicForm {
static $instance;
static function objects() {
$os = parent::objects();
return $os->filter(array('type'=>'T'));
}
static function getInstance() {
if (!isset(static::$instance))
self::getNewInstance();
static function getNewInstance() {
$o = static::objects()->one();
static::$instance = $o->instanciate();
return static::$instance;
static function ensureDynamicDataView() {
$sql = 'SHOW TABLES LIKE \''.TABLE_PREFIX.'ticket__cdata\'';
if (!db_num_rows(db_query($sql)))
return static::buildDynamicDataView();
}
static function buildDynamicDataView() {
// create table __cdata (primary key (ticket_id)) as select
// entry.object_id as ticket_id, MAX(IF(field.name = 'subject',
// ans.value, NULL)) as `subject`,MAX(IF(field.name = 'priority',
// ans.value, NULL)) as `priority_desc`,MAX(IF(field.name =
// 'priority', ans.value_id, NULL)) as `priority_id`
// FROM ost_form_entry entry LEFT JOIN ost_form_entry_values ans ON
// ans.entry_id = entry.id LEFT JOIN ost_form_field field ON
// field.id=ans.field_id
// where entry.object_type='T' group by entry.object_id;
$sql = 'CREATE TABLE `'.TABLE_PREFIX.'ticket__cdata` (PRIMARY KEY
(ticket_id)) AS ' . static::getCrossTabQuery('T', 'ticket_id');
db_query($sql);
}
static function dropDynamicDataView() {
db_query('DROP TABLE IF EXISTS `'.TABLE_PREFIX.'ticket__cdata`');
}
static function updateDynamicDataView($answer, $data) {
// TODO: Detect $data['dirty'] for value and value_id
// We're chiefly concerned with Ticket form answers
if (!($e = $answer->getEntry()) || $e->getForm()->get('type') != 'T')
return;
// $record = array();
// $record[$f] = $answer->value'
// TicketFormData::objects()->filter(array('ticket_id'=>$a))
// ->merge($record);
$sql = 'SHOW TABLES LIKE \''.TABLE_PREFIX.'ticket__cdata\'';
if (!db_num_rows(db_query($sql)))
return;
$name = $f->get('name') ? $f->get('name')
: 'field_'.$f->get('id');
$fields = sprintf('`%s`=', $name) . db_input(
implode(',', $answer->getSearchKeys()));
$sql = 'INSERT INTO `'.TABLE_PREFIX.'ticket__cdata` SET '.$fields
.', `ticket_id`='.db_input($answer->getEntry()->get('object_id'))
.' ON DUPLICATE KEY UPDATE '.$fields;
if (!db_query($sql) || !db_affected_rows())
return self::dropDynamicDataView();
}
// Add fields from the standard ticket form to the ticket filterable fields
Filter::addSupportedMatches(/* @trans */ 'Ticket Data', function() {
$matches = array();
foreach (TicketForm::getInstance()->getFields() as $f) {
if (!$f->hasData())
continue;
$matches['field.'.$f->get('id')] = __('Ticket').' / '.$f->getLabel();
if (($fi = $f->getImpl()) && $fi->hasSubFields()) {
foreach ($fi->getSubFields() as $p) {
$matches['field.'.$f->get('id').'.'.$p->get('id')]
= __('Ticket').' / '.$f->getLabel().' / '.$p->getLabel();
// Manage materialized view on custom data updates
Signal::connect('model.created',
array('TicketForm', 'updateDynamicDataView'),
'DynamicFormEntryAnswer');
Signal::connect('model.updated',
array('TicketForm', 'updateDynamicDataView'),
'DynamicFormEntryAnswer');
// Recreate the dynamic view after new or removed fields to the ticket
// details form
Signal::connect('model.created',
array('TicketForm', 'dropDynamicDataView'),
'DynamicFormField',
function($o) { return $o->getForm()->get('type') == 'T'; });
Signal::connect('model.deleted',
array('TicketForm', 'dropDynamicDataView'),
'DynamicFormField',
function($o) { return $o->getForm()->get('type') == 'T'; });
// If the `name` column is in the dirty list, we would be renaming a
// column. Delete the view instead.
Signal::connect('model.updated',
array('TicketForm', 'dropDynamicDataView'),
'DynamicFormField',
// TODO: Lookup the dynamic form to verify {type == 'T'}
function($o, $d) { return isset($d['dirty'])
&& (isset($d['dirty']['name']) || isset($d['dirty']['type'])); });
Filter::addSupportedMatches(/* trans */ 'Custom Forms', function() {
$matches = array();
foreach (DynamicForm::objects()->filter(array('type'=>'G')) as $form) {
foreach ($form->getFields() as $f) {
if (!$f->hasData())
continue;
$matches['field.'.$f->get('id')] = $form->getTitle().' / '.$f->getLabel();
if (($fi = $f->getImpl()) && $fi->hasSubFields()) {
foreach ($fi->getSubFields() as $p) {
$matches['field.'.$f->get('id').'.'.$p->get('id')]
= $form->getTitle().' / '.$f->getLabel().' / '.$p->getLabel();
}
}
}
}
return $matches;
}, 9900);
require_once(INCLUDE_DIR . "class.json.php");
class DynamicFormField extends VerySimpleModel {
static $meta = array(
'table' => FORM_FIELD_TABLE,
'ordering' => array('sort'),
'pk' => array('id'),
'select_related' => array('form'),
'joins' => array(
'form' => array(
'null' => true,
'constraint' => array('form_id' => 'DynamicForm.id'),
var $_disabled = false;
const FLAG_ENABLED = 0x00001;
const FLAG_EXT_STORED = 0x00002; // Value stored outside of form_entry_value
const FLAG_CLOSE_REQUIRED = 0x00004;
const FLAG_MASK_CHANGE = 0x00010;
const FLAG_MASK_DELETE = 0x00020;
const FLAG_MASK_EDIT = 0x00040;
const FLAG_MASK_DISABLE = 0x00080;
const FLAG_MASK_REQUIRE = 0x10000;
const FLAG_MASK_VIEW = 0x20000;
const FLAG_MASK_NAME = 0x40000;
const MASK_MASK_INTERNAL = 0x400B0; # !change, !delete, !disable, !edit-name
const MASK_MASK_ALL = 0x700F0;
const FLAG_CLIENT_VIEW = 0x00100;
const FLAG_CLIENT_EDIT = 0x00200;
const FLAG_CLIENT_REQUIRED = 0x00400;
const MASK_CLIENT_FULL = 0x00700;
const FLAG_AGENT_VIEW = 0x01000;
const FLAG_AGENT_EDIT = 0x02000;
const FLAG_AGENT_REQUIRED = 0x04000;
const MASK_AGENT_FULL = 0x7000;
// Multiple inheritance -- delegate to FormField
function __call($what, $args) {
return call_user_func_array(
array($this->getField(), $what), $args);
}
function getField($cache=true) {
global $thisstaff;
// Finagle the `required` flag for the FormField instance
$ht = $this->ht;
$ht['required'] = ($thisstaff) ? $this->isRequiredForStaff()
: $this->isRequiredForUsers();
return new FormField($ht);
$this->_field = new FormField($ht);
return $this->_field;
}
function getAnswer() { return $this->answer; }
/**
* setConfiguration
*
* Used in the POST request of the configuration process. The
* ::getConfigurationForm() method should be used to retrieve a
* configuration form for this field. That form should be submitted via
* a POST request, and this method should be called in that request. The
* data from the POST request will be interpreted and will adjust the
* configuration of this field
*
* Parameters:
* errors - (OUT array) receives validation errors of the parsed
* configuration form
*
* Returns:
* (bool) true if the configuration was updated, false if there were
* errors. If false, the errors were written into the received errors
* array.
*/
function setConfiguration(&$errors=array()) {
$config = array();
foreach ($this->getConfigurationForm($_POST)->getFields() as $name=>$field) {
$config[$name] = $field->to_php($field->getClean());
$errors = array_merge($errors, $field->errors());
}
if (count($errors) === 0)
$this->set('configuration', JsonDataEncoder::encode($config));
$this->set('hint', $_POST['hint']);
return count($errors) === 0;
}
return !$this->hasFlag(self::FLAG_MASK_DELETE);
return $this->hasFlag(self::FLAG_MASK_NAME);
return $this->hasFlag(self::FLAG_MASK_VIEW);
}
function isRequirementForced() {
return $this->hasFlag(self::FLAG_MASK_REQUIRE);
return $this->hasFlag(self::FLAG_MASK_CHANGE);
function isEditable() {
return $this->hasFlag(self::FLAG_MASK_EDIT);
}
function disable() {
$this->_disabled = true;
}
function isEnabled() {
return !$this->_disabled && $this->hasFlag(self::FLAG_ENABLED);
}
function hasFlag($flag) {
return (isset($this->flags) && ($this->flags & $flag) != 0);
}
function getVisibilityDescription() {
$F = $this->flags;
if (!$this->hasFlag(self::FLAG_ENABLED))
return __('Disabled');
$impl = $this->getImpl();
$hints = array();
$VIEW = self::FLAG_CLIENT_VIEW | self::FLAG_AGENT_VIEW;
if (($F & $VIEW) == 0) {
$hints[] = __('Hidden');
}
elseif (~$F & self::FLAG_CLIENT_VIEW) {
$hints[] = __('Internal');
}
elseif (~$F & self::FLAG_AGENT_VIEW) {
$hints[] = __('For EndUsers Only');
}
if ($impl->hasData()) {
if ($F & (self::FLAG_CLIENT_REQUIRED | self::FLAG_AGENT_REQUIRED)) {
$hints[] = __('Required');
$hints[] = __('Optional');
}
if (!($F & (self::FLAG_CLIENT_EDIT | self::FLAG_AGENT_EDIT))) {
$hints[] = __('Immutable');
}
}
return implode(', ', $hints);
function getTranslateTag($subtag) {
return _H(sprintf('field.%s.%s', $subtag, $this->id));
}
function getLocal($subtag, $default=false) {
$tag = $this->getTranslateTag($subtag);
$T = CustomDataTranslation::translate($tag);
return $T != $tag ? $T : ($default ?: $this->get($subtag));
}
return array(
'a' => array('desc' => __('Optional'),
'flags' => self::FLAG_CLIENT_VIEW | self::FLAG_AGENT_VIEW
| self::FLAG_CLIENT_EDIT | self::FLAG_AGENT_EDIT),
'b' => array('desc' => __('Required'),
'flags' => self::FLAG_CLIENT_VIEW | self::FLAG_AGENT_VIEW
| self::FLAG_CLIENT_EDIT | self::FLAG_AGENT_EDIT
| self::FLAG_CLIENT_REQUIRED | self::FLAG_AGENT_REQUIRED),
'c' => array('desc' => __('Required for EndUsers'),
'flags' => self::FLAG_CLIENT_VIEW | self::FLAG_AGENT_VIEW
| self::FLAG_CLIENT_EDIT | self::FLAG_AGENT_EDIT
| self::FLAG_CLIENT_REQUIRED),
'd' => array('desc' => __('Required for Agents'),
'flags' => self::FLAG_CLIENT_VIEW | self::FLAG_AGENT_VIEW
| self::FLAG_CLIENT_EDIT | self::FLAG_AGENT_EDIT
| self::FLAG_AGENT_REQUIRED),
'e' => array('desc' => __('Internal, Optional'),
'flags' => self::FLAG_AGENT_VIEW | self::FLAG_AGENT_EDIT),
'f' => array('desc' => __('Internal, Required'),
'flags' => self::FLAG_AGENT_VIEW | self::FLAG_AGENT_EDIT
| self::FLAG_AGENT_REQUIRED),
'g' => array('desc' => __('For EndUsers Only'),
'flags' => self::FLAG_CLIENT_VIEW | self::FLAG_CLIENT_EDIT
| self::FLAG_CLIENT_REQUIRED),
function getAllRequirementModes() {
$modes = static::allRequirementModes();
if ($this->isPrivacyForced()) {
// Required to be internal
foreach ($modes as $m=>$info) {
if ($info['flags'] & (self::FLAG_CLIENT_VIEW | self::FLAG_AGENT_VIEW))
unset($modes[$m]);
}
}
if ($this->isRequirementForced()) {
// Required to be required
foreach ($modes as $m=>$info) {
if ($info['flags'] & (self::FLAG_CLIENT_REQUIRED | self::FLAG_AGENT_REQUIRED))
unset($modes[$m]);
}
}
return $modes;
}
function setRequirementMode($mode) {
$modes = $this->getAllRequirementModes();
if (!isset($modes[$mode]))
return false;
$info = $modes[$mode];
$this->set('flags', $info['flags'] | self::FLAG_ENABLED);
}
function isRequiredForStaff() {
return $this->hasFlag(self::FLAG_AGENT_REQUIRED);
}
function isRequiredForUsers() {
return $this->hasFlag(self::FLAG_CLIENT_REQUIRED);
}
function isRequiredForClose() {
return $this->hasFlag(self::FLAG_CLOSE_REQUIRED);
}
function isEditableToStaff() {
return $this->isEnabled()
&& $this->hasFlag(self::FLAG_AGENT_EDIT);
}
function isVisibleToStaff() {
return $this->isEnabled()
&& $this->hasFlag(self::FLAG_AGENT_VIEW);
}
function isEditableToUsers() {
return $this->isEnabled()
&& $this->hasFlag(self::FLAG_CLIENT_EDIT);
}
function isVisibleToUsers() {
return $this->isEnabled()
&& $this->hasFlag(self::FLAG_CLIENT_VIEW);
/**
* Used when updating the form via the admin panel. This represents
* validation on the form field template, not data entered into a form
* field of a custom form. The latter would be isValidEntry()
*/
function isValid() {
if (!$this->get('label'))
$this->addError(
__("Label is required for custom form fields"), "label");
if ($this->get('required') && !$this->get('name'))
$this->addError(
__("Variable name is required for required fields"
/* `required` is a visibility setting fields */
/* `variable` is used for automation. Internally it's called `name` */
), "name");
if (preg_match('/[.{}\'"`; ]/u', $this->get('name')))
$this->addError(__(
'Invalid character in variable name. Please use letters and numbers only.'
), 'name');
return count($this->errors()) == 0;
}
function delete() {
// Don't really delete form fields as that will screw up the data
// model. Instead, just drop the association with the form which
// will give the appearance of deletion. Not deleting means that
// the field will continue to exist on form entries it may already
// have answers on, but since it isn't associated with the form, it
// won't be available for new form submittals.
$this->set('form_id', 0);
if (count($this->dirty))
$this->set('updated', new SqlFunction('NOW'));
return parent::save($this->dirty || $refetch);
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
}
static function create($ht=false) {
$inst = parent::create($ht);
$inst->set('created', new SqlFunction('NOW'));
if (isset($ht['configuration']))
$inst->configuration = JsonDataEncoder::encode($ht['configuration']);
return $inst;
}
}
/**
* Represents an entry to a dynamic form. Used to render the completed form
* in reference to the attached ticket, etc. A form is used to represent the
* template of enterable data. This represents the data entered into an
* instance of that template.
*
* The data of the entry is called 'answers' in this model. This model
* represents an instance of a form entry. The data / answers to that entry
* are represented individually in the DynamicFormEntryAnswer model.
*/
class DynamicFormEntry extends VerySimpleModel {
static $meta = array(
'table' => FORM_ENTRY_TABLE,
'ordering' => array('sort'),
'pk' => array('id'),
'select_related' => array('form'),
'fields' => array('id', 'form_id', 'object_type', 'object_id',
'sort', 'extra', 'updated', 'created'),
'joins' => array(
'form' => array(
'null' => true,
'constraint' => array('form_id' => 'DynamicForm.id'),
),
),
);
var $_values;
var $_fields;
var $_form;
var $_errors = false;
var $_clean = false;
function getId() {
return $this->get('id');
}
function getAnswers() {
if (!isset($this->_values)) {
$this->_values = DynamicFormEntryAnswer::objects()
->filter(array('entry_id'=>$this->get('id')))
->all();
foreach ($this->_values as $v)
$v->entry = $this;
}
return $this->_values;
}
function getAnswer($name) {
foreach ($this->getAnswers() as $ans)
if ($ans->getField()->get('name') == $name)
function setAnswer($name, $value, $id=false) {
foreach ($this->getAnswers() as $ans) {
$f = $ans->getField();
if ($f->isStorable() && $f->get('name') == $name) {
$f->reset();
$ans->set('value', $value);
if ($id !== false)
$ans->set('value_id', $id);
break;
}
}
}
function errors() {
return $this->_errors;
}
function getTitle() { return $this->getForm()->getTitle(); }
function getInstructions() { return $this->getForm()->getInstructions(); }
function getForm() {
$this->_form = DynamicForm::lookup($this->get('form_id'));
if ($this->_form && isset($this->id))
if (isset($this->extra)) {
$x = JsonDataParser::decode($this->extra) ?: array();
$this->_form->disableFields($x['disable'] ?: array());
}
return $this->_form;
}
function getFields() {
foreach ($this->getAnswers() as $a) {
$T = $this->_fields[] = $a->getField();
$T->setForm($this);
}
function getSource() {
return $this->_source ?: (isset($this->id) ? false : $_POST);
}
function setSource($source) {
$this->_source = $source;
}
function getField($name) {
foreach ($this->getFields() as $field)
if (!strcasecmp($field->get('name'), $name))
return $field;
return null;
}
/**
* 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($filter=false) {
if (!is_array($this->_errors)) {
$this->_errors = array();
$this->getClean();
foreach ($this->getFields() as $field) {
if ($field->errors() && (!$filter || $filter($field)))
$this->_errors[$field->get('id')] = $field->errors();
function isValidForClient() {
$filter = function($f) {
return $f->isVisibleToUsers();
return $this->isValid($filter);
}
function isValidForStaff() {
$filter = function($f) {
return $f->isVisibleToStaff();
return $this->isValid($filter);
}
function getClean() {
if (!$this->_clean) {
$this->_clean = array();
foreach ($this->getFields() as $field)
$this->_clean[$field->get('id')]
= $this->_clean[$field->get('name')] = $field->getClean();
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
/**
* Compile a list of data used by the filtering system to match dynamic
* content in this entry. This returs an array of `field.<id>` =>
* <value> pairs where the <id> is the field id and the <value> is the
* toString() value for the entered data.
*
* If the field returns an array for its ::getFilterData() method, the
* data will be added in the array with the keys prefixed with
* `field.<id>`. This is useful for properties on custom lists, for
* instance, which can contain properties usefule for matching and
* filtering.
*/
function getFilterData() {
$vars = array();
foreach ($this->getFields() as $f) {
$tag = 'field.'.$f->get('id');
if ($d = $f->getFilterData()) {
if (is_array($d)) {
foreach ($d as $k=>$v) {
if (is_string($k))
$vars["$tag$k"] = $v;
else
$vars[$tag] = $v;
}
}
else {
$vars[$tag] = $d;
}
}
}
return $vars;
}
function getSaved() {
$info = array();
foreach ($this->getAnswers() as $a) {
$field = $a->getField();
$info[$field->get('id')]
= $info[$field->get('name')] = $a->getValue();
}
return $info;
}
function forTicket($ticket_id, $force=false) {
if (!isset($entries[$ticket_id]) || $force) {
$stuff = DynamicFormEntry::objects()
->filter(array('object_id'=>$ticket_id, 'object_type'=>'T'));
// If forced, don't cache the result
if ($force)
return $stuff;
$entries[$ticket_id] = &$stuff;
}
function setTicketId($ticket_id) {
$this->object_type = 'T';
$this->object_id = $ticket_id;
}
function setClientId($user_id) {
$this->object_type = 'U';
$this->object_id = $user_id;
}