Skip to content
Snippets Groups Projects
class.forms.php 41.7 KiB
Newer Older
Jared Hancock's avatar
Jared Hancock committed
<?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 = '';
Jared Hancock's avatar
Jared Hancock committed
    var $instructions = '';

    var $_errors = null;
    var $_source = false;
    function __construct($fields=array(), $source=null, $options=array()) {
Jared Hancock's avatar
Jared Hancock committed
        $this->fields = $fields;
        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];
    }
Jared Hancock's avatar
Jared Hancock committed

    function getFields() {
        return $this->fields;
    }

    function getField($name) {
        $fields = $this->getFields();
        foreach($fields as $f)
            if(!strcasecmp($f->get('name'), $name))
                return $f;
        if (isset($fields[$name]))
            return $fields[$name];
Jared Hancock's avatar
Jared Hancock committed
    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 (!isset($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) {
Jared Hancock's avatar
Jared Hancock committed
                if (!$field->hasData())
                    continue;
                $this->_clean[$key] = $this->_clean[$field->get('name')]
                    = $field->getClean();
            }
            unset($this->_clean[""]);
        }
        return $this->_clean;
    }

    function errors() {
        return $this->_errors;
    function render($staff=true, $title=false, $options=array()) {
        if ($title)
            $this->title = $title;
        if (isset($options['instructions']))
            $this->instructions = $options['instructions'];
        $form = $this;
        if ($staff)
            include(STAFFINC_DIR . 'templates/dynamic-form.tmpl.php');
        else
            include(CLIENTINC_DIR . 'templates/dynamic-form.tmpl.php');
Jared Hancock's avatar
Jared Hancock committed
    }
}

require_once(INCLUDE_DIR . "class.json.php");

class FormField {
Jared Hancock's avatar
Jared Hancock committed
    var $ht = array(
        'label' => 'Unlabeled',
        'required' => false,
        'default' => false,
        'configuration' => array(),
    );

Jared Hancock's avatar
Jared Hancock committed
    var $_cform;
    var $_clean;
    var $_errors = array();
    var $answer;
    var $parent;
    var $presentation_only = false;
Jared Hancock's avatar
Jared Hancock committed

    static $types = array(
        'Basic Fields' => array(
            'text'  => array(   /* trans */ 'Short Answer', 'TextboxField'),
            'memo' => array(    /* trans */ 'Long Answer', 'TextareaField'),
            'thread' => array(  /* trans */ 'Thread Entry', 'ThreadEntryField', false),
            'datetime' => array(/* trans */ 'Date and Time', 'DatetimeField'),
            'phone' => array(   /* trans */ 'Phone Number', 'PhoneField'),
            'bool' => array(    /* trans */ 'Checkbox', 'BooleanField'),
            'choices' => array( /* trans */ 'Choices', 'ChoiceField'),
            'break' => array(   /* trans */ 'Section Break', 'SectionBreakField'),
Jared Hancock's avatar
Jared Hancock committed
    );
    static $more_types = array();
    static $uid = 100;
Jared Hancock's avatar
Jared Hancock committed

    function __construct($options=array()) {
        $this->ht = array_merge($this->ht, $options);
        if (!isset($this->ht['id']))
            $this->ht['id'] = self::$uid++;
    }

    function __clone() {
        $this->_widget = null;
        $this->ht['id'] = self::$uid++;
    static function addFieldTypes($group, $callable) {
        static::$more_types[$group] = $callable;
Jared Hancock's avatar
Jared Hancock committed
    }

    static function allTypes() {
        if (static::$more_types) {
            foreach (static::$more_types as $group=>$c)
                static::$types[$group] = call_user_func($c);
Jared Hancock's avatar
Jared Hancock committed
            static::$more_types = array();
        }
        return static::$types;
    }

    static function getFieldType($type) {
        foreach (static::allTypes() as $group=>$types)
            if (isset($types[$type]))
                return $types[$type];
    }

Jared Hancock's avatar
Jared Hancock committed
    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() {
        if (!isset($this->_clean)) {
            $this->_clean = (isset($this->value))
                ? $this->value : $this->parse($this->getWidget()->value);
            $this->validateEntry($this->_clean);
        }
        return $this->_clean;
    function reset() {
        $this->_clean = $this->_widget = null;
    }
Loading
Loading full blame...