Skip to content
Snippets Groups Projects
class.forms.php 77.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • 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 $k=>$f) {
    
                $f->setForm($this);
    
                if (!$f->get('name') && $k)
                    $f->set('name', $k);
            }
    
            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; }
    
        function setSource($source) { $this->_source = $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) {
    
                    if ($field->isPresentationOnly())
    
    Jared Hancock's avatar
    Jared Hancock committed
                        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');
    
    
        function getMedia() {
            static $dedup = array();
    
            foreach ($this->getFields() as $f) {
    
                if (($M = $f->getMedia()) && is_array($M)) {
    
                    foreach ($M as $type=>$files) {
                        foreach ($files as $url) {
                            $key = strtolower($type.$url);
                            if (isset($dedup[$key]))
                                continue;
    
    
                            self::emitMedia($url, $type);
    
    
        static function emitMedia($url, $type) {
            if ($url[0] == '/')
                $url = ROOT_PATH . substr($url, 1);
    
            switch (strtolower($type)) {
            case 'css': ?>
            <link rel="stylesheet" type="text/css" href="<?php echo $url; ?>"/><?php
                break;
            case 'js': ?>
            <script type="text/javascript" src="<?php echo $url; ?>"></script><?php
                break;
            }
        }
    
    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(
    
            /* @trans */ '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'),
    
                'files' => array(   /* @trans */ 'File Upload', 'FileUploadField'),
    
                'break' => array(   /* @trans */ 'Section Break', 'SectionBreakField'),
    
                'info' => array(    /* @trans */ 'Information', 'FreeTextField'),
    
    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 => $entries)
                    foreach ($entries as $c)
                        static::$types[$group] = array_merge(
                                static::$types[$group] ?: array(), 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];
        }
    
        function set($field, $value) {
            $this->ht[$field] = $value;
        }
    
    Jared Hancock's avatar
    Jared Hancock committed
    
        /**
         * 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);
    
                if ($vs = $this->get('cleaners')) {
                    if (is_array($vs)) {
                        foreach ($vs as $cleaner)
    
                            if (is_callable($cleaner))
                                $this->_clean = call_user_func_array(
                                        $cleaner, array($this, $this->_clean));
    
                    }
                    elseif (is_callable($vs))
    
                        $this->_clean = call_user_func_array(
                                $vs, array($this, $this->_clean));
    
                if ($this->isVisible())
                    $this->validateEntry($this->_clean);
    
            }
            return $this->_clean;
    
        function reset() {
            $this->_clean = $this->_widget = null;
        }
    
    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
         *
         * Validates user entry on an instance of the field on a dynamic form.
         * This is called when an instance of this field (like a TextboxField)
         * receives data from the user and that value should be validated.
         *
         * Parameters:
         * $value - (string) input from the user
         */
        function validateEntry($value) {
    
            if (!$value && count($this->_errors))
                return;
    
    
    Jared Hancock's avatar
    Jared Hancock committed
            # Validates a user-input into an instance of this field on a dynamic
            # form
    
            if ($this->get('required') && !$value && $this->hasData())
    
                $this->_errors[] = sprintf(__('%s is a required field'),
                    $this->getLabel());
    
    
            # Perform declared validators for the field
            if ($vs = $this->get('validators')) {
                if (is_array($vs)) {
                    foreach ($vs as $validator)
                        if (is_callable($validator))
                            $validator($this, $value);
                }
                elseif (is_callable($vs))
                    $vs($this, $value);
            }
    
        /**
         * isVisible
         *
         * If this field has visibility configuration, then it will parse the
         * constraints with the visibility configuration to determine if the
         * field is visible and should be considered for validation
         */
        function isVisible() {
            $config = $this->getConfiguration();
            if ($this->get('visibility') instanceof VisibilityConstraint) {
                return $this->get('visibility')->isVisible($this);
            }
            return true;
        }
    
    
        /**
         * FIXME: Temp
         *
         */
    
        function isEditable() {
            return (($this->get('edit_mask') & 32) == 0);
        }
    
    
    
    Jared Hancock's avatar
    Jared Hancock committed
        /**
         * parse
         *
         * Used to transform user-submitted data to a PHP value. This value is
         * not yet considered valid. The ::validateEntry() method will be called
         * on the value to determine if the entry is valid. Therefore, if the
         * data is clearly invalid, return something like NULL that can easily
         * be deemed invalid in ::validateEntry(), however, can still produce a
         * useful error message indicating what is wrong with the input.
         */
        function parse($value) {
    
            return is_string($value) ? trim($value) : $value;
    
    Jared Hancock's avatar
    Jared Hancock committed
        }
    
        /**
         * to_php
         *
         * Transforms the data from the value stored in the database to a PHP
         * value. The ::to_database() method is used to produce the database
         * valse, so this method is the compliment to ::to_database().
         *
         * Parameters:
         * $value - (string or null) database representation of the field's
         *      content
         */
        function to_php($value) {
            return $value;
        }
    
        /**
         * to_database
         *
         * Determines the value to be stored in the database. The database
         * backend for all fields is a text field, so this method should return
         * a text value or NULL to represent the value of the field. The
         * ::to_php() method will convert this value back to PHP.
         *
         * Paremeters:
         * $value - PHP value of the field's content
         */
        function to_database($value) {
            return $value;
        }
    
        /**
         * toString
         *
         * Converts the PHP value created in ::parse() or ::to_php() to a
         * pretty-printed value to show to the user. This is especially useful
         * for something like dates which are stored considerably different in
         * the database from their respective human-friendly versions.
         * Furthermore, this method allows for internationalization and
         * localization.
         *
         * Parametes:
         * $value - PHP value of the field's content
         */
        function toString($value) {
    
            return (string) $value;
    
        function __toString() {
            return $this->toString($this->value);
        }
    
    
        /**
         * Returns an HTML friendly value for the data in the field.
         */
        function display($value) {
            return Format::htmlchars($this->toString($value));
        }
    
    
        /**
         * Returns a value suitable for exporting to a foreign system. Mostly
         * useful for things like dates and phone numbers which should be
         * formatted using a standard when exported
         */
        function export($value) {
            return $this->toString($value);
        }
    
    
        /**
         * Convert the field data to something matchable by filtering. The
         * primary use of this is for ticket filtering.
         */
        function getFilterData() {
            return $this->toString($this->getClean());
        }
    
    
        function searchable($value) {
            return Format::searchable($this->toString($value));
        }
    
    
    Jared Hancock's avatar
    Jared Hancock committed
        function getLabel() { return $this->get('label'); }
    
        /**
         * getImpl
         *
         * Magic method that will return an implementation instance of this
         * field based on the simple text value of the 'type' value of this
         * field instance. The list of registered fields is determined by the
         * global get_dynamic_field_types() function. The data from this model
         * will be used to initialize the returned instance.
         *
         * For instance, if the value of this field is 'text', a TextField
         * instance will be returned.
         */
    
        function getImpl($parent=null) {
    
    Jared Hancock's avatar
    Jared Hancock committed
            // Allow registration with ::addFieldTypes and delayed calling
    
            $type = static::getFieldType($this->get('type'));
            $clazz = $type[1];
    
    Jared Hancock's avatar
    Jared Hancock committed
            $inst = new $clazz($this->ht);
            $inst->parent = $parent;
    
            $inst->setForm($this->_form);
    
    Jared Hancock's avatar
    Jared Hancock committed
            return $inst;
    
        function __call($what, $args) {
            // XXX: Throw exception if $this->parent is not set
    
            if (!$this->parent)
    
                throw new Exception(sprintf(__('%s: Call to undefined function'),
                    $what));
    
    Jared Hancock's avatar
    Jared Hancock committed
            // BEWARE: DynamicFormField has a __call() which will create a new
            //      FormField instance and invoke __call() on it or bounce
            //      immediately back
    
            return call_user_func_array(
                array($this->parent, $what), $args);
        }
    
    
    Jared Hancock's avatar
    Jared Hancock committed
        function getAnswer() { return $this->answer; }
    
        function setAnswer($ans) { $this->answer = $ans; }
    
    Jared Hancock's avatar
    Jared Hancock committed
    
        function getFormName() {
    
            if (is_numeric($this->get('id')))
    
                return substr(md5(
                    session_id() . '-field-id-'.$this->get('id')), -16);
    
                return $this->get('name') ?: $this->get('id');
    
        function setForm($form) {
            $this->_form = $form;
        }
        function getForm() {
            return $this->_form;
        }
        /**
         * Returns the data source for this field. If created from a form, the
         * data source from the form is returned. Otherwise, if the request is a
         * POST, then _POST is returned.
         */
        function getSource() {
            if ($this->_form)
                return $this->_form->getSource();
            elseif ($_SERVER['REQUEST_METHOD'] == 'POST')
                return $_POST;
            else
                return array();
        }
    
    
        function render($mode=null) {
    
            $rv = $this->getWidget()->render($mode);
            if ($v = $this->get('visibility')) {
                $v->emitJavascript($this);
            }
            return $rv;
    
        }
    
        function renderExtras($mode=null) {
            return;
    
        function getMedia() {
            $widget = $this->getWidget();
            return $widget::$media;
        }
    
    
    Jared Hancock's avatar
    Jared Hancock committed
        function getConfigurationOptions() {
            return array();
        }
    
        /**
         * getConfiguration
         *
         * Loads configuration information from database into hashtable format.
         * Also, the defaults from ::getConfigurationOptions() are integrated
         * into the database-backed options, so that if options have not yet
         * been set or a new option has been added and not saved for this field,
         * the default value will be reflected in the returned configuration.
         */
        function getConfiguration() {
            if (!$this->_config) {
                $this->_config = $this->get('configuration');
                if (is_string($this->_config))
                    $this->_config = JsonDataParser::parse($this->_config);
                elseif (!$this->_config)
                    $this->_config = array();
                foreach ($this->getConfigurationOptions() as $name=>$field)
                    if (!isset($this->_config[$name]))
                        $this->_config[$name] = $field->get('default');
            }
            return $this->_config;
        }
    
    
        /**
         * If the [Config] button should be shown to allow for the configuration
         * of this field
         */
    
    Jared Hancock's avatar
    Jared Hancock committed
        function isConfigurable() {
            return true;
        }
    
    
        /**
         * Field type is changeable in the admin interface
         */
        function isChangeable() {
            return true;
        }
    
        /**
         * Field does not contain data that should be saved to the database. Ie.
         * non data fields like section headers
         */
        function hasData() {
            return true;
        }
    
        /**
         * Returns true if the field/widget should be rendered as an entire
         * block in the target form.
         */
        function isBlockLevel() {
            return false;
        }
    
        /**
         * Fields should not be saved with the dynamic data. It is assumed that
         * some static processing will store the data elsewhere.
         */
        function isPresentationOnly() {
    
            return $this->presentation_only;
    
        /**
         * Indicates if the field places data in the `value_id` column. This
         * is currently used by the materialized view system
         */
        function hasIdValue() {
            return false;
        }
    
    
        /**
         * Indicates if the field has subfields accessible via getSubFields()
         * method. Useful for filter integration. Should connect with
         * getFilterData()
         */
        function hasSubFields() {
            return false;
        }
        function getSubFields() {
            return null;
        }
    
    
        /**
         * Indicates if the field provides for searching for something other
         * than keywords. For instance, textbox fields can have hits by keyword
         * searches alone, but selection fields should provide the option to
         * match a specific value or set of values and therefore need to
         * participate on any search builder.
         */
        function hasSpecialSearch() {
            return true;
        }
    
    
        function getConfigurationForm($source=null) {
    
    Jared Hancock's avatar
    Jared Hancock committed
            if (!$this->_cform) {
    
                $type = static::getFieldType($this->get('type'));
                $clazz = $type[1];
    
                $T = new $clazz($this->ht);
    
                $config = $this->getConfiguration();
                $this->_cform = new Form($T->getConfigurationOptions(), $source);
    
                    foreach ($this->_cform->getFields() as $name=>$f) {
    
                        if ($config && isset($config[$name]))
    
                            $f->value = $config[$name];
                        elseif ($f->get('default'))
                            $f->value = $f->get('default');
                    }
                }
    
    Jared Hancock's avatar
    Jared Hancock committed
            }
            return $this->_cform;
        }
    
    
        function configure($prop, $value) {
            $this->getConfiguration();
            $this->_config[$prop] = $value;
        }
    
    
        function getWidget($widgetClass=false) {
    
            if (!static::$widget)
    
                throw new Exception(__('Widget not defined for this field'));
    
            if (!isset($this->_widget)) {
    
                $wc = $widgetClass ?: $this->get('widget') ?: static::$widget;
    
    Jared Hancock's avatar
    Jared Hancock committed
                $this->_widget = new $wc($this);
    
                $this->_widget->parseValue();
            }
            return $this->_widget;
    
    
        function getSelectName() {
            $name = $this->get('name') ?: 'field_'.$this->get('id');
            if ($this->hasIdValue())
                $name .= '_id';
    
            return $name;
        }
    
    
        function getTranslateTag($subtag) {
            return _H(sprintf('field.%s.%s', $subtag, $this->get('id')));
        }
        function getLocal($subtag, $default=false) {
            $tag = $this->getTranslateTag($subtag);
            $T = CustomDataTranslation::translate($tag);
            return $T != $tag ? $T : ($default ?: $this->get($subtag));
        }
    
    Jared Hancock's avatar
    Jared Hancock committed
    }
    
    class TextboxField extends FormField {
    
        static $widget = 'TextboxWidget';
    
    Jared Hancock's avatar
    Jared Hancock committed
    
        function getConfigurationOptions() {
            return array(
                'size'  =>  new TextboxField(array(
    
                    'id'=>1, 'label'=>__('Size'), 'required'=>false, 'default'=>16,
    
    Jared Hancock's avatar
    Jared Hancock committed
                        'validator' => 'number')),
                'length' => new TextboxField(array(
    
                    'id'=>2, 'label'=>__('Max Length'), 'required'=>false, 'default'=>30,
    
    Jared Hancock's avatar
    Jared Hancock committed
                        'validator' => 'number')),
                'validator' => new ChoiceField(array(
    
                    'id'=>3, 'label'=>__('Validator'), 'required'=>false, 'default'=>'',
                    'choices' => array('phone'=>__('Phone Number'),'email'=>__('Email Address'),
    
                        'ip'=>__('IP Address'), 'number'=>__('Number'),
                        'regex'=>__('Custom (Regular Expression)'), ''=>__('None')))),
                'regex' => new TextboxField(array(
                    'id'=>6, 'label'=>__('Regular Expression'), 'required'=>true,
                    'configuration'=>array('size'=>40, 'length'=>100),
                    'visibility' => new VisibilityConstraint(
                        new Q(array('validator__eq'=>'regex')),
                        VisibilityConstraint::HIDDEN
                    ),
                    'cleaners' => function ($self, $value) {
                        $wrapped = "/".$value."/iu";
                        if (false === @preg_match($value, ' ')
                                && false !== @preg_match($wrapped, ' ')) {
    
                            $value = $wrapped;
    
                        if ($value == '//iu')
                            return '';
    
    
                        return $value;
                    },
                    'validators' => function($self, $v) {
                        if (false === @preg_match($v, ' '))
                            $self->addError(__('Cannot compile this regular expression'));
                    })),
    
                'validator-error' => new TextboxField(array(
    
                    'id'=>4, 'label'=>__('Validation Error'), 'default'=>'',
    
                    'configuration'=>array('size'=>40, 'length'=>60,
                        'translatable'=>$this->getTranslateTag('validator-error')
                    ),
    
                    'hint'=>__('Message shown to user if the input does not match the validator'))),
    
                'placeholder' => new TextboxField(array(
    
                    'id'=>5, 'label'=>__('Placeholder'), 'required'=>false, 'default'=>'',
                    'hint'=>__('Text shown in before any input from the user'),
    
                    'configuration'=>array('size'=>40, 'length'=>40,
                        'translatable'=>$this->getTranslateTag('placeholder')
                    ),
    
        function hasSpecialSearch() {
            return false;
        }
    
    
    Jared Hancock's avatar
    Jared Hancock committed
        function validateEntry($value) {
            parent::validateEntry($value);
    
            $config = $this->getConfiguration();
    
    Jared Hancock's avatar
    Jared Hancock committed
            $validators = array(
                '' =>       null,
                'email' =>  array(array('Validator', 'is_email'),
    
                    __('Enter a valid email address')),
    
    Jared Hancock's avatar
    Jared Hancock committed
                'phone' =>  array(array('Validator', 'is_phone'),
    
                    __('Enter a valid phone number')),
    
    Jared Hancock's avatar
    Jared Hancock committed
                'ip' =>     array(array('Validator', 'is_ip'),
    
                    __('Enter a valid IP address')),
    
                'number' => array('is_numeric', __('Enter a number')),
                'regex' => array(
                    function($v) use ($config) {
                        $regex = $config['regex'];
                        return @preg_match($regex, $v);
                    }, __('Value does not match required pattern')
                ),
    
    Jared Hancock's avatar
    Jared Hancock committed
            );
            // Support configuration forms, as well as GUI-based form fields
            $valid = $this->get('validator');
            if (!$valid) {
                $valid = $config['validator'];
            }
    
            if (!$value || !isset($validators[$valid]))
                return;
    
    Jared Hancock's avatar
    Jared Hancock committed
            $func = $validators[$valid];
    
            $error = $func[1];
            if ($config['validator-error'])
    
                $error = $this->getLocal('validator-error', $config['validator-error']);
    
    Jared Hancock's avatar
    Jared Hancock committed
            if (is_array($func) && is_callable($func[0]))
                if (!call_user_func($func[0], $value))
    
                    $this->_errors[] = $error;
    
    Jared Hancock's avatar
    Jared Hancock committed
    class PasswordField extends TextboxField {
        static $widget = 'PasswordWidget';
    
        function to_database($value) {
            return Crypto::encrypt($value, SECRET_SALT, $this->getFormName());
        }
    
        function to_php($value) {
            return Crypto::decrypt($value, SECRET_SALT, $this->getFormName());
        }
    }
    
    
    Jared Hancock's avatar
    Jared Hancock committed
    class TextareaField extends FormField {
    
        static $widget = 'TextareaWidget';
    
    
    Jared Hancock's avatar
    Jared Hancock committed
        function getConfigurationOptions() {
            return array(
                'cols'  =>  new TextboxField(array(
    
                    'id'=>1, 'label'=>__('Width').' '.__('(chars)'), 'required'=>true, 'default'=>40)),
    
    Jared Hancock's avatar
    Jared Hancock committed
                'rows'  =>  new TextboxField(array(
    
                    'id'=>2, 'label'=>__('Height').' '.__('(rows)'), 'required'=>false, 'default'=>4)),
    
    Jared Hancock's avatar
    Jared Hancock committed
                'length' => new TextboxField(array(
    
                    'id'=>3, 'label'=>__('Max Length'), 'required'=>false, 'default'=>0)),
    
                'html' => new BooleanField(array(
    
                    'id'=>4, 'label'=>__('HTML'), 'required'=>false, 'default'=>true,
                    'configuration'=>array('desc'=>__('Allow HTML input in this box')))),
    
                'placeholder' => new TextboxField(array(
    
                    'id'=>5, 'label'=>__('Placeholder'), 'required'=>false, 'default'=>'',
                    'hint'=>__('Text shown in before any input from the user'),
    
                    'configuration'=>array('size'=>40, 'length'=>40),
                )),
    
        function hasSpecialSearch() {
            return false;
        }
    
    
        function display($value) {
            $config = $this->getConfiguration();
            if ($config['html'])
                return Format::safe_html($value);
            else
    
                return nl2br(Format::htmlchars($value));
    
        function searchable($value) {
            $value = preg_replace(array('`<br(\s*)?/?>`i', '`</div>`i'), "\n", $value);
            $value = Format::htmldecode(Format::striptags($value));
            return Format::searchable($value);
        }
    
    
        function export($value) {
            return (!$value) ? $value : Format::html2text($value);
        }
    
    
        function parse($value) {
            $config = $this->getConfiguration();
            if ($config['html'])
                return Format::sanitize($value);
            else
                return $value;
        }
    
    
    Jared Hancock's avatar
    Jared Hancock committed
    }
    
    class PhoneField extends FormField {
    
        static $widget = 'PhoneNumberWidget';
    
    
        function getConfigurationOptions() {
            return array(
                'ext' => new BooleanField(array(
    
                    'label'=>__('Extension'), 'default'=>true,
    
                    'configuration'=>array(
    
                        'desc'=>__('Add a separate field for the extension'),
    
                    ),
                )),
                'digits' => new TextboxField(array(
    
                    'label'=>__('Minimum length'), 'default'=>7,
                    'hint'=>__('Fewest digits allowed in a valid phone number'),
    
                    'configuration'=>array('validator'=>'number', 'size'=>5),
                )),
                'format' => new ChoiceField(array(
    
                    'label'=>__('Display format'), 'default'=>'us',
                    'choices'=>array(''=>'-- '.__('Unformatted').' --',
                        'us'=>__('United States')),
    
        function hasSpecialSearch() {
            return false;
        }
    
    
    Jared Hancock's avatar
    Jared Hancock committed
        function validateEntry($value) {
            parent::validateEntry($value);
    
            $config = $this->getConfiguration();
    
    Jared Hancock's avatar
    Jared Hancock committed
            # Run validator against $this->value for email type
            list($phone, $ext) = explode("X", $value, 2);
    
            if ($phone && (
                    !is_numeric($phone) ||
                    strlen($phone) < $config['digits']))
    
                $this->_errors[] = __("Enter a valid phone number");
    
            if ($ext && $config['ext']) {
    
    Jared Hancock's avatar
    Jared Hancock committed
                if (!is_numeric($ext))
    
                    $this->_errors[] = __("Enter a valid phone extension");
    
    Jared Hancock's avatar
    Jared Hancock committed
                elseif (!$phone)
    
                    $this->_errors[] = __("Enter a phone number for the extension");
    
        function parse($value) {
            // NOTE: Value may have a legitimate 'X' to separate the number and
            // extension parts. Don't remove the 'X'
    
            $val = preg_replace('/[^\dX]/', '', $value);
            // Pass completely-incorrect string for validation error
            return $val ?: $value;
    
    Jared Hancock's avatar
    Jared Hancock committed
        function toString($value) {
    
            $config = $this->getConfiguration();
    
    Jared Hancock's avatar
    Jared Hancock committed
            list($phone, $ext) = explode("X", $value, 2);
    
            switch ($config['format']) {
            case 'us':
                $phone = Format::phone($phone);
                break;
            }
    
    Jared Hancock's avatar
    Jared Hancock committed
            if ($ext)
                $phone.=" x$ext";
            return $phone;
        }
    }
    
    class BooleanField extends FormField {
    
        static $widget = 'CheckboxWidget';
    
    Jared Hancock's avatar
    Jared Hancock committed
    
        function getConfigurationOptions() {
            return array(
                'desc' => new TextareaField(array(
    
                    'id'=>1, 'label'=>__('Description'), 'required'=>false, 'default'=>'',
                    'hint'=>__('Text shown inline with the widget'),
    
    Jared Hancock's avatar
    Jared Hancock committed
                    'configuration'=>array('rows'=>2)))
            );
        }
    
        function to_database($value) {
            return ($value) ? '1' : '0';
        }
    
    
        function parse($value) {
            return $this->to_php($value);
        }
    
    Jared Hancock's avatar
    Jared Hancock committed
        function to_php($value) {
    
            return $value ? true : false;
    
    Jared Hancock's avatar
    Jared Hancock committed
        }
    
        function toString($value) {
    
            return ($value) ? __('Yes') : __('No');
    
    Jared Hancock's avatar
    Jared Hancock committed
        }
    }
    
    class ChoiceField extends FormField {
    
        static $widget = 'ChoicesWidget';
    
    Jared Hancock's avatar
    Jared Hancock committed
    
        function getConfigurationOptions() {
            return array(
                'choices'  =>  new TextareaField(array(
    
                    'id'=>1, 'label'=>__('Choices'), 'required'=>false, 'default'=>'',
                    'hint'=>__('List choices, one per line. To protect against spelling changes, specify key:value names to preserve entries if the list item names change'),
    
                    'configuration'=>array('html'=>false)
                )),
    
                'default' => new TextboxField(array(
    
                    'id'=>3, 'label'=>__('Default'), 'required'=>false, 'default'=>'',
                    'hint'=>__('(Enter a key). Value selected from the list initially'),
    
                    'configuration'=>array('size'=>20, 'length'=>40),
                )),
                'prompt' => new TextboxField(array(
    
                    'id'=>2, 'label'=>__('Prompt'), 'required'=>false, 'default'=>'',
                    'hint'=>__('Leading text shown before a value is selected'),
    
                    'configuration'=>array('size'=>40, 'length'=>40,
                        'translatable'=>$this->getTranslateTag('prompt'),
                    ),
    
                'multiselect' => new BooleanField(array(
                    'id'=>1, 'label'=>'Multiselect', 'required'=>false, 'default'=>false,
                    'configuration'=>array(
                        'desc'=>'Allow multiple selections')
                )),
    
        function parse($value) {
    
            return $this->to_php($value ?: null);
    
        }
    
        function to_database($value) {
    
            if (!is_array($value)) {
                $choices = $this->getChoices();
                if (isset($choices[$value]))
                    $value = array($value => $choices[$value]);
            }
            if (is_array($value))
    
    Peter Rotich's avatar
    Peter Rotich committed
                $value = JsonDataEncoder::encode($value);
    
    
            return $value;
        }
    
        function to_php($value) {
    
            if (is_string($value))
                $array = JsonDataParser::parse($value) ?: $value;
            else
                $array = $value;
            $config = $this->getConfiguration();
    
            if (!$config['multiselect']) {
                if (is_array($array) && count($array) < 2) {
                    reset($array);
                    return key($array);
                }
                if (is_string($array) && strpos($array, ',') !== false) {
                    list($array,) = explode(',', $array, 2);
                }
    
        function toString($value) {
    
            $selection = $this->getChoice($value);
    
            return is_array($selection)
                ? (implode(', ', array_filter($selection)) ?: $value)
    
        }
    
        function getChoice($value) {
    
            $choices = $this->getChoices();
    
            $selection = array();
            if ($value && is_array($value)) {
    
    Peter Rotich's avatar
    Peter Rotich committed
                $selection = $value;
    
            } elseif (isset($choices[$value]))
                $selection[] = $choices[$value];
            elseif ($this->get('default'))
                $selection[] = $choices[$this->get('default')];