Skip to content
Snippets Groups Projects
class.forms.php 38.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • Jared Hancock's avatar
    Jared Hancock committed
    
    class ChoicesWidget extends Widget {
        function render() {
            $config = $this->field->getConfiguration();
            // Determine the value for the default (the one listed if nothing is
            // selected)
    
            $choices = $this->field->getChoices();
    
            $def_key = $this->field->get('default');
    
    Jared Hancock's avatar
    Jared Hancock committed
            $have_def = isset($choices[$def_key]);
            if (!$have_def)
                $def_val = 'Select '.$this->field->get('label');
            else
                $def_val = $choices[$def_key];
    
            $value = $this->value;
            if ($value === null && $have_def)
                $value = $def_key;
    
    Jared Hancock's avatar
    Jared Hancock committed
            ?> <span style="display:inline-block">
            <select name="<?php echo $this->name; ?>">
                <?php if (!$have_def) { ?>
                <option value="<?php echo $def_key; ?>">&mdash; <?php
                    echo $def_val; ?> &mdash;</option>
                <?php }
                foreach ($choices as $key=>$name) {
                    if (!$have_def && $key == $def_key)
                        continue; ?>
    
                    <option value="<?php echo $key; ?>" <?php
    
                        if ($value == $key) echo 'selected="selected"';
    
    Jared Hancock's avatar
    Jared Hancock committed
                    ?>><?php echo $name; ?></option>
                <?php } ?>
            </select>
            </span>
            <?php
        }
    }
    
    class CheckboxWidget extends Widget {
        function __construct($field) {
            parent::__construct($field);
            $this->name = '_field-checkboxes';
        }
    
        function render() {
            $config = $this->field->getConfiguration();
            ?>
            <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() {
    
    Jared Hancock's avatar
    Jared Hancock committed
            $config = $this->field->getConfiguration();
            if ($this->value) {
                $this->value = (is_int($this->value) ? $this->value :
    
                    DateTime::createFromFormat($cfg->getDateFormat(), $this->value)
                    ->format('U'));
    
    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 = 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 :
    
                    (($dt = DateTime::createFromFormat($cfg->getDateFormat() . ' G:i',
                            $datetime . ' 00:00'))
                        ? (int) $dt->format('U') : false)
                );
                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() {
            ?><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 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
                $this->value; ?></textarea>
        <?php
        }
    
    
        function showAttachments($errors=array()) {
    
            global $cfg, $thisclient;
    
            if(($cfg->allowOnlineAttachments()
                && !$cfg->allowAttachmentsOnlogin())
                || ($cfg->allowAttachmentsOnlogin()
                    && ($thisclient && $thisclient->isValid()))) { ?>
            <hr/>
    
            <div><strong style="padding-right:1em;vertical-align:top">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>