Skip to content
Snippets Groups Projects
class.forms.php 47.5 KiB
Newer Older
  • Learn to ignore specific revisions
  •         // Priority isn't searchable this way
            return null;
        }
    
    
        function getConfigurationOptions() {
    
            return array(
                '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),
                )),
            );
    
    FormField::addFieldTypes(/*trans*/ 'Dynamic Fields', function() {
    
        return array(
    
            'priority' => array(__('Priority Level'), PriorityField),
    
    
    class TicketStateField extends ChoiceField {
    
        static $_choices = array(
                'open' => 'Open',
                'resolved' => 'Resolved',
                'closed' => 'Closed',
                'archived' => 'Archived',
                'deleted' => 'Deleted'
                );
    
        function hasIdValue() {
            return true;
        }
    
        function isChangeable() {
            return false;
        }
    
        function getChoices() {
            $this->ht['default'] =  '';
    
            return static::$_choices;
        }
    
        function getConfigurationOptions() {
            return array(
                '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),
                )),
            );
        }
    }
    FormField::addFieldTypes('Dynamic Fields', function() {
        return array(
            'state' => array('Ticket State', TicketStateField, false),
        );
    });
    
    class TicketFlagField extends ChoiceField {
    
        // Supported flags (TODO: move to configurable custom list)
        static $_flags = array(
                'onhold' => array(
                    'flag' => 1,
                    'name' => 'Onhold',
                    'states' => array('open'),
                    ),
                'overdue' => array(
                    'flag' => 2,
                    'name' => 'Overdue',
                    'states' => array('open'),
                    ),
                'answered' => array(
                    'flag' => 4,
                    'name' => 'Answered',
                    'states' => array('open'),
                    )
                );
    
        var $_choices;
    
        function hasIdValue() {
            return true;
        }
    
        function isChangeable() {
            return true;
        }
    
        function getChoices() {
            $this->ht['default'] =  '';
    
            if (!$this->_choices) {
                foreach (static::$_flags as $k => $v)
                    $this->_choices[$k] = $v['name'];
            }
    
            return $this->_choices;
        }
    
        function getConfigurationOptions() {
            return array(
                '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),
                )),
            );
        }
    }
    
    FormField::addFieldTypes('Dynamic Fields', function() {
        return array(
            'flags' => array('Ticket Flags', TicketFlagField, false),
        );
    });
    
    
    
    Jared Hancock's avatar
    Jared Hancock committed
    class Widget {
    
        function __construct($field) {
            $this->field = $field;
            $this->name = $field->getFormName();
    
        }
    
        function parseValue() {
    
            $this->value = $this->getValue();
    
            if (!isset($this->value) && is_object($this->field->getAnswer()))
                $this->value = $this->field->getAnswer()->getValue();
    
            if (!isset($this->value) && isset($this->field->value))
    
                $this->value = $this->field->value;
    
    Jared Hancock's avatar
    Jared Hancock committed
        }
    
        function getValue() {
    
            $data = $this->field->getSource();
    
            // Search for HTML form name first
            if (isset($data[$this->name]))
                return $data[$this->name];
            elseif (isset($data[$this->field->get('name')]))
                return $data[$this->field->get('name')];
            return null;
    
    Jared Hancock's avatar
    Jared Hancock committed
        }
    }
    
    class TextboxWidget extends Widget {
    
    Jared Hancock's avatar
    Jared Hancock committed
        static $input_type = 'text';
    
    
        function render($mode=false) {
    
    Jared Hancock's avatar
    Jared Hancock committed
            $config = $this->field->getConfiguration();
            if (isset($config['size']))
                $size = "size=\"{$config['size']}\"";
    
            if (isset($config['length']) && $config['length'])
    
    Jared Hancock's avatar
    Jared Hancock committed
                $maxlength = "maxlength=\"{$config['length']}\"";
    
            if (isset($config['classes']))
    
                $classes = 'class="'.$config['classes'].'"';
    
            if (isset($config['autocomplete']))
                $autocomplete = 'autocomplete="'.($config['autocomplete']?'on':'off').'"';
    
            if (isset($config['disabled']))
                $disabled = 'disabled="disabled"';
    
    Jared Hancock's avatar
    Jared Hancock committed
            ?>
            <span style="display:inline-block">
    
    Jared Hancock's avatar
    Jared Hancock committed
            <input type="<?php echo static::$input_type; ?>"
                id="<?php echo $this->name; ?>"
    
                <?php echo implode(' ', array_filter(array(
                    $size, $maxlength, $classes, $autocomplete, $disabled)))
    
                    .' placeholder="'.$config['placeholder'].'"'; ?>
    
    Jared Hancock's avatar
    Jared Hancock committed
                name="<?php echo $this->name; ?>"
                value="<?php echo Format::htmlchars($this->value); ?>"/>
            </span>
            <?php
        }
    }
    
    
    Jared Hancock's avatar
    Jared Hancock committed
    class PasswordWidget extends TextboxWidget {
        static $input_type = 'password';
    
        function parseValue() {
            // Show empty box unless failed POST
            if ($_SERVER['REQUEST_METHOD'] == 'POST'
                    && $this->field->getForm()->isValid())
                parent::parseValue();
            else
                $this->value = '';
        }
    }
    
    
    Jared Hancock's avatar
    Jared Hancock committed
    class TextareaWidget extends Widget {
    
        function render($mode=false) {
    
    Jared Hancock's avatar
    Jared Hancock committed
            $config = $this->field->getConfiguration();
    
            $class = $cols = $rows = $maxlength = "";
    
    Jared Hancock's avatar
    Jared Hancock committed
            if (isset($config['rows']))
                $rows = "rows=\"{$config['rows']}\"";
            if (isset($config['cols']))
                $cols = "cols=\"{$config['cols']}\"";
    
            if (isset($config['length']) && $config['length'])
    
    Jared Hancock's avatar
    Jared Hancock committed
                $maxlength = "maxlength=\"{$config['length']}\"";
    
            if (isset($config['html']) && $config['html'])
                $class = 'class="richtext no-bar small"';
    
            <span style="display:inline-block;width:100%">
    
            <textarea <?php echo $rows." ".$cols." ".$maxlength." ".$class
                    .' placeholder="'.$config['placeholder'].'"'; ?>
    
    Jared Hancock's avatar
    Jared Hancock committed
                name="<?php echo $this->name; ?>"><?php
                    echo Format::htmlchars($this->value);
                ?></textarea>
            </span>
            <?php
        }
    }
    
    class PhoneNumberWidget extends Widget {
    
        function render($mode=false) {
    
            $config = $this->field->getConfiguration();
    
    Jared Hancock's avatar
    Jared Hancock committed
            list($phone, $ext) = explode("X", $this->value);
            ?>
            <input type="text" name="<?php echo $this->name; ?>" value="<?php
    
            echo Format::htmlchars($phone); ?>"/><?php
    
            // Allow display of extension field even if disabled if the phone
            // number being edited has an extension
            if ($ext || $config['ext']) { ?> Ext:
                <input type="text" name="<?php
    
                echo $this->name; ?>-ext" value="<?php echo Format::htmlchars($ext);
                    ?>" size="5"/>
    
    Jared Hancock's avatar
    Jared Hancock committed
        }
    
        function getValue() {
    
            $data = $this->field->getSource();
            $base = parent::getValue();
            if ($base === null)
                return $base;
            $ext = $data["{$this->name}-ext"];
    
            // NOTE: 'X' is significant. Don't change it
    
    Jared Hancock's avatar
    Jared Hancock committed
            if ($ext) $ext = 'X'.$ext;
    
            return $base . $ext;
    
    Jared Hancock's avatar
    Jared Hancock committed
        }
    }
    
    class ChoicesWidget extends Widget {
    
        function render($mode=false) {
    
            if ($mode && $mode == 'view') {
    
                if (!($val = (string) $this->field))
    
                    $val = sprintf('<span class="faded">%s</span>', __('None'));
    
    Jared Hancock's avatar
    Jared Hancock committed
            $config = $this->field->getConfiguration();
            // Determine the value for the default (the one listed if nothing is
            // selected)
    
    Peter Rotich's avatar
    Peter Rotich committed
            $choices = $this->field->getChoices(true);
    
            $prompt = $config['prompt'] ?: __('Select');
    
            $have_def = false;
    
            // We don't consider the 'default' when rendering in 'search' mode
            if (!strcasecmp($mode, 'search')) {
                $def_val = $prompt;
            } else {
    
                $def_key = $this->field->get('default');
                if (!$def_key && $config['default'])
                    $def_key = $config['default'];
                $have_def = isset($choices[$def_key]);
    
                $def_val = $have_def ? $choices[$def_key] : $prompt;
    
    Peter Rotich's avatar
    Peter Rotich committed
            if (($value=$this->getValue()))
                $values = $this->field->parse($value);
            elseif ($this->value)
                 $values = $this->value;
    
            if ($values === null)
                $values = $have_def ? array($def_key => $choices[$def_key]) : array();
    
    
            ?>
            <select name="<?php echo $this->name; ?>[]"
                id="<?php echo $this->name; ?>"
                data-prompt="<?php echo $prompt; ?>"
                <?php if ($config['multiselect'])
                    echo ' multiple="multiple" class="multiselect"'; ?>>
                <?php if (!$have_def && !$config['multiselect']) { ?>
    
    Jared Hancock's avatar
    Jared Hancock committed
                <option value="<?php echo $def_key; ?>">&mdash; <?php
                    echo $def_val; ?> &mdash;</option>
                <?php }
    
    Peter Rotich's avatar
    Peter Rotich committed
                foreach ($choices as $key => $name) {
    
    Jared Hancock's avatar
    Jared Hancock committed
                    if (!$have_def && $key == $def_key)
                        continue; ?>
    
                    <option value="<?php echo $key; ?>" <?php
    
    Peter Rotich's avatar
    Peter Rotich committed
                        if (isset($values[$key])) echo 'selected="selected"';
    
    Jared Hancock's avatar
    Jared Hancock committed
                    ?>><?php echo $name; ?></option>
                <?php } ?>
            </select>
            <?php
    
            if ($config['multiselect']) {
             ?>
            <script type="text/javascript" src="<?php echo ROOT_PATH; ?>js/jquery.multiselect.min.js"></script>
            <link rel="stylesheet" href="<?php echo ROOT_PATH; ?>css/jquery.multiselect.css"/>
            <script type="text/javascript">
            $(function() {
                $("#<?php echo $this->name; ?>")
                .multiselect({'noneSelectedText':'<?php echo $prompt; ?>'});
            });
            </script>
           <?php
            }
    
    Jared Hancock's avatar
    Jared Hancock committed
        }
    }
    
    class CheckboxWidget extends Widget {
        function __construct($field) {
            parent::__construct($field);
            $this->name = '_field-checkboxes';
        }
    
    
        function render($mode=false) {
    
    Jared Hancock's avatar
    Jared Hancock committed
            $config = $this->field->getConfiguration();
    
            if (!isset($this->value))
                $this->value = $this->field->get('default');
    
    Jared Hancock's avatar
    Jared Hancock committed
            ?>
            <input type="checkbox" name="<?php echo $this->name; ?>[]" <?php
                if ($this->value) echo 'checked="checked"'; ?> value="<?php
                echo $this->field->get('id'); ?>"/>
            <?php
            if ($config['desc']) { ?>
                <em style="display:inline-block"><?php
                    echo Format::htmlchars($config['desc']); ?></em>
            <?php }
        }
    
        function getValue() {
    
            $data = $this->field->getSource();
            if (count($data))
                return @in_array($this->field->get('id'), $data[$this->name]);
    
    Jared Hancock's avatar
    Jared Hancock committed
            return parent::getValue();
        }
    }
    
    class DatetimePickerWidget extends Widget {
    
        function render($mode=false) {
    
    Jared Hancock's avatar
    Jared Hancock committed
            $config = $this->field->getConfiguration();
            if ($this->value) {
    
                $this->value = is_int($this->value) ? $this->value :
                    strtotime($this->value);
    
    Jared Hancock's avatar
    Jared Hancock committed
                if ($config['gmt'])
                    $this->value += 3600 *
    
                        $_SESSION['TZ_OFFSET']+($_SESSION['TZ_DST']?date('I',$this->value):0);
    
    Jared Hancock's avatar
    Jared Hancock committed
    
                list($hr, $min) = explode(':', date('H:i', $this->value));
    
                $this->value = Format::date($cfg->getDateFormat(), $this->value);
    
    Jared Hancock's avatar
    Jared Hancock committed
            }
            ?>
            <input type="text" name="<?php echo $this->name; ?>"
                value="<?php echo Format::htmlchars($this->value); ?>" size="12"
    
                autocomplete="off" class="dp" />
    
    Jared Hancock's avatar
    Jared Hancock committed
            <script type="text/javascript">
                $(function() {
                    $('input[name="<?php echo $this->name; ?>"]').datepicker({
                        <?php
                        if ($config['min'])
                            echo "minDate: new Date({$config['min']}000),";
                        if ($config['max'])
                            echo "maxDate: new Date({$config['max']}000),";
                        elseif (!$config['future'])
                            echo "maxDate: new Date().getTime(),";
                        ?>
                        numberOfMonths: 2,
                        showButtonPanel: true,
                        buttonImage: './images/cal.png',
    
                        dateFormat: $.translate_format('<?php echo $cfg->getDateFormat(); ?>')
    
    Jared Hancock's avatar
    Jared Hancock committed
                    });
                });
            </script>
            <?php
            if ($config['time'])
                // TODO: Add time picker -- requires time picker or selection with
                //       Misc::timeDropdown
                echo '&nbsp;' . Misc::timeDropdown($hr, $min, $this->name . ':time');
        }
    
        /**
         * Function: getValue
         * Combines the datepicker date value and the time dropdown selected
         * time value into a single date and time string value.
         */
        function getValue() {
    
            $data = $this->field->getSource();
    
            $config = $this->field->getConfiguration();
            if ($datetime = parent::getValue()) {
    
                $datetime = is_int($datetime) ? $datetime :
                    strtotime($datetime);
    
                if ($datetime && isset($data[$this->name . ':time'])) {
    
                    list($hr, $min) = explode(':', $data[$this->name . ':time']);
                    $datetime += $hr * 3600 + $min * 60;
                }
    
                if ($datetime && $config['gmt'])
    
                    $datetime -= (int) (3600 * $_SESSION['TZ_OFFSET'] +
                        ($_SESSION['TZ_DST'] ? date('I',$datetime) : 0));
            }
    
    Jared Hancock's avatar
    Jared Hancock committed
            return $datetime;
        }
    }
    
    
    class SectionBreakWidget extends Widget {
    
        function render($mode=false) {
    
            ?><div class="form-header section-break"><h3><?php
            echo Format::htmlchars($this->field->get('label'));
            ?></h3><em><?php echo Format::htmlchars($this->field->get('hint'));
            ?></em></div>
            <?php
        }
    }
    
    class ThreadEntryWidget extends Widget {
    
        function render($client=null) {
            global $cfg;
    
            ?><div style="margin-bottom:0.5em;margin-top:0.5em"><strong><?php
            echo Format::htmlchars($this->field->get('label'));
            ?></strong>:</div>
    
            <textarea style="width:100%;" name="<?php echo $this->field->get('name'); ?>"
    
                placeholder="<?php echo Format::htmlchars($this->field->get('hint')); ?>"
                <?php if (!$client) { ?>
                    data-draft-namespace="ticket.staff"
                <?php } else { ?>
                    data-draft-namespace="ticket.client"
                    data-draft-object-id="<?php echo substr(session_id(), -12); ?>"
                <?php } ?>
                class="richtext draft draft-delete ifhtml"
    
                cols="21" rows="8" style="width:80%;"><?php echo
    
                Format::htmlchars($this->value); ?></textarea>
    
        function showAttachments($errors=array()) {
    
            global $cfg, $thisclient;
    
            if(($cfg->allowOnlineAttachments()
                && !$cfg->allowAttachmentsOnlogin())
                || ($cfg->allowAttachmentsOnlogin()
                    && ($thisclient && $thisclient->isValid()))) { ?>
    
            <div class="clear"></div>
    
            <div><strong style="padding-right:1em;vertical-align:top"><?php
            echo __('Attachments'); ?>: </strong>
    
            <div style="display:inline-block">
            <div class="uploads" style="display:block"></div>
    
            <input type="file" class="multifile" name="attachments[]" id="attachments" size="30" value="" />
    
            <font class="error">&nbsp;<?php echo $errors['attachments']; ?></font>