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

    var $_source = false;
Jared Hancock's avatar
Jared Hancock committed
    function Form() {
        call_user_func_array(array($this, '__construct'), func_get_args());
    }
    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 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 (!is_array($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) {
                $this->_clean[$key] = $this->_clean[$field->get('name')]
                    = $field->getClean();
            }
        }
        return $this->_clean;
    }

    function errors() {
        return $this->_errors;
    function render($staff=true, $title=false, $instructions=false) {
        if ($title)
            $this->title = $title;
        if ($instructions)
            $this->instructions = $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 $parent;
    var $presentation_only = false;
Jared Hancock's avatar
Jared Hancock committed

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

    function __construct($options=array()) {
        static $uid = 100;
        $this->ht = array_merge($this->ht, $options);
        if (!isset($this->ht['id']))
            $this->ht['id'] = $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;
Jared Hancock's avatar
Jared Hancock committed
    }

    function errors() {
        return $this->_errors;
    function addError($message, $field=false) {
        if ($field)
            $this->_errors[$field] = $message;
        else
            $this->_errors[] = $message;
    }
Jared Hancock's avatar
Jared Hancock committed

    function isValidEntry() {
        $this->validateEntry();
        return count($this->_errors) == 0;
    }

    /**
     * validateEntry
     *
Loading
Loading full blame...