Skip to content
Snippets Groups Projects
class.dynamic_forms.php 52.2 KiB
Newer Older
Jared Hancock's avatar
Jared Hancock committed
<?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');
Jared Hancock's avatar
Jared Hancock committed

/**
 * Form template, used for designing the custom form and for entering custom
 * data for a ticket
 */
class DynamicForm extends VerySimpleModel {
Jared Hancock's avatar
Jared Hancock committed

    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',
        'O' => 'Organization Information',
Jared Hancock's avatar
Jared Hancock committed
    var $_fields;
    var $_has_data = false;
    var $_dfields;
    function getFields($cache=true) {
        if (!$cache)
            $fields = false;
        else
            $fields = &$this->_fields;

        if (!$fields) {
            $fields = new ListObject();
Jared Hancock's avatar
Jared Hancock committed
            foreach ($this->getDynamicFields() as $f)
                $fields->append($f->getImpl($f));
        return $fields;
Jared Hancock's avatar
Jared Hancock committed
    }

    function getDynamicFields() {
        if (!isset($this->id))
            return array();
        elseif (!$this->_dfields) {
Jared Hancock's avatar
Jared Hancock committed
            $this->_dfields = DynamicFormField::objects()
                ->filter(array('form_id'=>$this->id))
Jared Hancock's avatar
Jared Hancock committed
                ->all();
            foreach ($this->_dfields as $f)
                $f->setForm($this);
        }
Jared Hancock's avatar
Jared Hancock committed
        return $this->_dfields;
    }

    // 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) {
Peter Rotich's avatar
Peter Rotich committed
            if (!strcasecmp($f->get('name'), $name))
        }
        if ($cache)
            return $this->getField($name, false);
Peter Rotich's avatar
Peter Rotich committed
    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')));
        }
        return $this->_form;
    function isDeletable() {
        return $this->get('deletable');
    }

    function disableFields(array $ids) {
        foreach ($this->getFields() as $F) {
            if (in_array($F->get('id'), $ids)) {
                $F->disable();
            }
        }
    }

Jared Hancock's avatar
Jared Hancock committed
    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) {
Jared Hancock's avatar
Jared Hancock committed
        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;
    }


Jared Hancock's avatar
Jared Hancock committed
    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->setForm($inst);
Jared Hancock's avatar
Jared Hancock committed
                $f->save();
            }
        }
        return $inst;
    }
    function saveTranslations($vars=false) {
Loading
Loading full blame...