Skip to content
Snippets Groups Projects
class.forms.php 142 KiB
Newer Older
  • Learn to ignore specific revisions
  •                 __('changed from <strong>%1$s</strong> to <strong>%2$s</strong>'),
                    $this->display($before), $this->display($after));
            }
            return $desc;
        }
    
    }
    
    class FileFieldAttachments {
        var $files;
    
        function __construct($files) {
            $this->files = $files;
        }
    
        function __toString() {
            $files = array();
            foreach ($this->files as $f) {
                $files[] = $f->file->name;
            }
            return implode(', ', $files);
        }
    
        function getVar($tag) {
            switch ($tag) {
    
            case 'names':
                return $this->__toString();
    
            case 'files':
                throw new OOBContent(OOBContent::FILES, $this->files->all());
            }
        }
    
        static function getVarScope() {
            return array(
    
                'names' => __('List of file names'),
    
                'files' => __('Attached files'),
            );
        }
    
    class InlineFormData extends ArrayObject {
        var $_form;
    
        function __construct($form, array $data=array()) {
            parent::__construct($data);
            $this->_form = $form;
        }
    
        function getVar($tag) {
            foreach ($this->_form->getFields() as $f) {
                if ($f->get('name') == $tag)
                    return $this[$f->get('id')];
            }
        }
    }
    
    
    class InlineFormField extends FormField {
        static $widget = 'InlineFormWidget';
    
        var $_iform = null;
    
        function validateEntry($value) {
            if (!$this->getInlineForm()->isValid()) {
    
                $this->_errors[] = __('Correct any errors below and try again.');
    
            }
        }
    
        function parse($value) {
            // The InlineFieldWidget returns an array of cleaned data
            return $value;
        }
    
        function to_database($value) {
            return JsonDataEncoder::encode($value);
        }
    
        function to_php($value) {
            $data = JsonDataParser::decode($value);
            // The InlineFormData helps with the variable replacer API
            return new InlineFormData($this->getInlineForm(), $data);
        }
    
        function display($data) {
            $form = $this->getInlineForm();
            ob_start(); ?>
            <div><?php
            foreach ($form->getFields() as $field) { ?>
                <span style="display:inline-block;padding:0 5px;vertical-align:top">
                    <strong><?php echo Format::htmlchars($field->get('label')); ?></strong>
                    <div><?php
                        $value = $data[$field->get('id')];
                        echo $field->display($value); ?></div>
                </span><?php
            } ?>
            </div><?php
            return ob_get_clean();
        }
    
    
        function getInlineForm($data=false) {
    
            $form = $this->get('form');
            if (is_array($form)) {
    
                $form = new SimpleForm($form, $data ?: $this->value ?: $this->getSource());
    
            }
            return $form;
    
    class InlineDynamicFormField extends FormField {
        function getInlineForm($data=false) {
            if (!isset($this->_iform) || $data) {
                $config = $this->getConfiguration();
                $this->_iform = DynamicForm::lookup($config['form']);
                if ($data)
                    $this->_iform = $this->_iform->getForm($data);
    
            return $this->_iform;
    
        function getConfigurationOptions() {
            $forms = DynamicForm::objects()->filter(array('type'=>'G'))
                ->values_flat('id', 'title');
            $choices = array();
            foreach ($forms as $row) {
                list($id, $title) = $row;
                $choices[$id] = $title;
    
            return array(
                'form' => new ChoiceField(array(
                    'id'=>2, 'label'=>'Inline Form', 'required'=>true,
                    'default'=>'', 'choices'=>$choices
                )),
            );
    
    class InlineFormWidget extends Widget {
        function render($mode=false) {
            $form = $this->field->getInlineForm();
            if (!$form)
                return;
            // Handle first-step edits -- load data from $this->value
            if ($form instanceof DynamicForm && !$form->getSource())
                $form = $form->getForm($this->value);
            $inc = ($mode == 'client') ? CLIENTINC_DIR : STAFFINC_DIR;
            include $inc . 'templates/inline-form.tmpl.php';
        }
    
        function getValue() {
            $data = $this->field->getSource();
            if (!$data)
                return null;
            $form = $this->field->getInlineForm($data);
            if (!$form)
                return null;
            return $form->getClean();
    
    Jared Hancock's avatar
    Jared Hancock committed
    class Widget {
    
        static $media = null;
    
    Jared Hancock's avatar
    Jared Hancock committed
    
        function __construct($field) {
            $this->field = $field;
            $this->name = $field->getFormName();
    
            $this->id = '_' . $this->name;
    
        }
    
        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')];
    
            elseif (isset($data[$this->field->get('id')]))
                return $data[$this->field->get('id')];
    
            return null;
    
    
        /**
         * getJsValueGetter
         *
         * Used with the dependent fields feature, this function should return a
         * single javascript expression which can be used in a larger expression
         * (<> == true, where <> is the result of this function). The %s token
         * will be replaced with a jQuery variable representing this widget.
         */
        function getJsValueGetter() {
            return '%s.val()';
        }
    
    Jared Hancock's avatar
    Jared Hancock committed
    }
    
    class TextboxWidget extends Widget {
    
    Jared Hancock's avatar
    Jared Hancock committed
        static $input_type = 'text';
    
    
        function render($options=array(), $extraConfig=false) {
    
    Jared Hancock's avatar
    Jared Hancock committed
            $config = $this->field->getConfiguration();
    
            if (is_array($extraConfig)) {
                foreach ($extraConfig as $k=>$v)
                    if (!isset($config[$k]) || !$config[$k])
                        $config[$k] = $v;
            }
    
    Jared Hancock's avatar
    Jared Hancock committed
            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['autofocus']))
                $autofocus = 'autofocus';
    
            if (isset($config['disabled']))
                $disabled = 'disabled="disabled"';
    
            if (isset($config['translatable']) && $config['translatable'])
                $translatable = 'data-translate-tag="'.$config['translatable'].'"';
    
            $type = static::$input_type;
            $types = array(
                'email' => 'email',
                'phone' => 'tel',
            );
            if ($type == 'text' && isset($types[$config['validator']]))
                $type = $types[$config['validator']];
    
            $placeholder = sprintf('placeholder="%s"', $this->field->getLocal('placeholder',
                $config['placeholder']));
    
            <input type="<?php echo $type; ?>"
    
                id="<?php echo $this->id; ?>"
    
                <?php echo implode(' ', array_filter(array(
    
                    $size, $maxlength, $classes, $autocomplete, $disabled,
    
                    $translatable, $placeholder, $autofocus))); ?>
    
    Jared Hancock's avatar
    Jared Hancock committed
                name="<?php echo $this->name; ?>"
                value="<?php echo Format::htmlchars($this->value); ?>"/>
            <?php
        }
    }
    
    
    
    class TextboxSelectionWidget extends TextboxWidget {
        //TODO: Support multi-input e.g comma separated inputs
    
        function render($options=array(), $extraConfig=array()) {
    
    
            if ($this->value && is_array($this->value))
                $this->value = current($this->value);
    
            parent::render($options);
        }
    
        function getValue() {
    
            $value = parent::getValue();
    
            if ($value && ($item=$this->field->lookupChoice((string) $value)))
                $value = $item;
    
    Jared Hancock's avatar
    Jared Hancock committed
    class PasswordWidget extends TextboxWidget {
        static $input_type = 'password';
    
    
        function render($mode=false, $extra=false) {
            $extra = array();
            if ($this->field->value) {
                $extra['placeholder'] = '••••••••••••';
            }
            return parent::render($mode, $extra);
        }
    
    
    Jared Hancock's avatar
    Jared Hancock committed
        function parseValue() {
    
    Jared Hancock's avatar
    Jared Hancock committed
            // Show empty box unless failed POST
    
            if ($_SERVER['REQUEST_METHOD'] != 'POST'
    
                    || !$this->field->getForm()->isValid())
    
    Jared Hancock's avatar
    Jared Hancock committed
                $this->value = '';
        }
    }
    
    
    Jared Hancock's avatar
    Jared Hancock committed
    class TextareaWidget extends Widget {
    
        function render($options=array()) {
    
    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 = array('richtext', 'no-bar');
                $class[] = @$config['size'] ?: 'small';
                $class = sprintf('class="%s"', implode(' ', $class));
    
                $this->value = Format::viewableImages($this->value);
            }
    
            if (isset($config['context']))
                $attrs['data-root-context'] = '"'.$config['context'].'"';
    
            <span style="display:inline-block;width:100%">
    
            <textarea <?php echo $rows." ".$cols." ".$maxlength." ".$class
    
                    .' '.Format::array_implode('=', ' ', $attrs)
    
                    .' placeholder="'.$config['placeholder'].'"'; ?>
    
                id="<?php echo $this->id; ?>"
    
    Jared Hancock's avatar
    Jared Hancock committed
                name="<?php echo $this->name; ?>"><?php
                    echo Format::htmlchars($this->value);
                ?></textarea>
            </span>
            <?php
        }
    
    Peter Rotich's avatar
    Peter Rotich committed
    
        function parseValue() {
            parent::parseValue();
            if (isset($this->value)) {
                $value = $this->value;
                $config = $this->field->getConfiguration();
                // Trim empty spaces based on text input type.
                // Preserve original input if not empty.
                if ($config['html'])
                    $this->value = trim($value, " <>br/\t\n\r") ? $value : '';
                else
                    $this->value = trim($value) ? $value : '';
            }
        }
    
    
    Jared Hancock's avatar
    Jared Hancock committed
    }
    
    class PhoneNumberWidget extends Widget {
    
        function render($options=array()) {
    
            $config = $this->field->getConfiguration();
    
    Jared Hancock's avatar
    Jared Hancock committed
            list($phone, $ext) = explode("X", $this->value);
            ?>
    
            <input id="<?php echo $this->id; ?>" type="tel" 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']) { ?> <?php echo __('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($options=array()) {
    
    Peter Rotich's avatar
    Peter Rotich committed
            $mode = null;
            if (isset($options['mode']))
                $mode = $options['mode'];
            elseif (isset($this->field->options['render_mode']))
                $mode = $this->field->options['render_mode'];
    
                if (!($val = (string) $this->field))
    
                    $val = sprintf('<span class="faded">%s</span>', __('None'));
    
    Jared Hancock's avatar
    Jared Hancock committed
            $config = $this->field->getConfiguration();
    
            if ($mode == 'search') {
                $config['multiselect'] = true;
            }
    
    
    Jared Hancock's avatar
    Jared Hancock committed
            // 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'])
                ? $this->field->getLocal('prompt', $config['prompt'])
                : __('Select'
                /* Used as a default prompt for a custom drop-down list */);
    
            $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'];
    
                if (is_array($def_key))
                    $def_key = key($def_key);
    
                $have_def = isset($choices[$def_key]);
    
                $def_val = $have_def ? $choices[$def_key] : $prompt;
    
            $values = $this->value;
    
            if (!is_array($values) && isset($values)) {
    
                $values = array($values => $this->field->getChoice($values));
            }
    
            if (!is_array($values))
    
    Peter Rotich's avatar
    Peter Rotich committed
                $values = $have_def ? array($def_key => $choices[$def_key]) : array();
    
    
    Jared Hancock's avatar
    Jared Hancock committed
            if (isset($config['classes']))
                $classes = 'class="'.$config['classes'].'"';
    
            ?>
            <select name="<?php echo $this->name; ?>[]"
    
    Jared Hancock's avatar
    Jared Hancock committed
                <?php echo implode(' ', array_filter(array($classes))); ?>
    
                id="<?php echo $this->id; ?>"
    
                <?php if (isset($config['data']))
                  foreach ($config['data'] as $D=>$V)
                    echo ' data-'.$D.'="'.Format::htmlchars($V).'"';
                ?>
    
                data-placeholder="<?php echo $prompt; ?>"
    
                <?php if ($config['multiselect'])
    
    Jared Hancock's avatar
    Jared Hancock committed
                    echo ' multiple="multiple"'; ?>>
    
                <?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>
    
            $this->emitChoices($choices, $values, $have_def, $def_key); ?>
    
    Jared Hancock's avatar
    Jared Hancock committed
            </select>
            <?php
    
            if ($config['multiselect']) {
             ?>
            <script type="text/javascript">
            $(function() {
    
                $("#<?php echo $this->id; ?>")
    
    Jared Hancock's avatar
    Jared Hancock committed
                .select2({'minimumResultsForSearch':10, 'width': '350px'});
    
        function emitChoices($choices, $values=array(), $have_def=false, $def_key=null) {
    
            reset($choices);
            if (is_array(current($choices)) || current($choices) instanceof Traversable)
    
                return $this->emitComplexChoices($choices, $values, $have_def, $def_key);
    
    
            foreach ($choices as $key => $name) {
                if (!$have_def && $key == $def_key)
                    continue; ?>
                <option value="<?php echo $key; ?>" <?php
                    if (isset($values[$key])) echo 'selected="selected"';
    
                ?>><?php echo Format::htmlchars($name); ?></option>
    
        function emitComplexChoices($choices, $values=array(), $have_def=false, $def_key=null) {
    
            foreach ($choices as $label => $group) {
                if (!count($group)) continue;
                ?>
    
                <optgroup label="<?php echo $label; ?>"><?php
                foreach ($group as $key => $name) {
                    if (!$have_def && $key == $def_key)
                        continue; ?>
                <option value="<?php echo $key; ?>" <?php
                    if (isset($values[$key])) echo 'selected="selected"';
    
                ?>><?php echo Format::htmlchars($name); ?></option>
    
            if (!($value = parent::getValue()))
                return null;
    
            if ($value && !is_array($value))
                $value = array($value);
    
    
            // Assume multiselect
            $values = array();
            $choices = $this->field->getChoices();
    
    
            if ($choices && is_array($value)) {
                // Complex choices
                if (is_array(current($choices))
                        || current($choices) instanceof Traversable) {
                    foreach ($choices as $label => $group) {
                         foreach ($group as $k => $v)
                            if (in_array($k, $value))
                                $values[$k] = $v;
                    }
                } else {
                    foreach($value as $k => $v) {
                        if (isset($choices[$v]))
                            $values[$v] = $choices[$v];
                        elseif (($i=$this->field->lookupChoice($v)))
                            $values += $i;
                    }
    
    
        function getJsValueGetter() {
            return '%s.find(":selected").val()';
        }
    
    /**
     * A widget for the ChoiceField which will render a list of radio boxes or
     * checkboxes depending on the value of $config['multiple']. Complex choices
     * are also supported and will be rendered as divs.
     */
    class BoxChoicesWidget extends Widget {
        function render($options=array()) {
            $this->emitChoices($this->field->getChoices());
        }
    
        function emitChoices($choices) {
    
          static $uid = 1;
    
    
          if (!isset($this->value))
              $this->value = $this->field->get('default');
          $config = $this->field->getConfiguration();
          $type = $config['multiple'] ? 'checkbox' : 'radio';
    
    
          $classes = array('checkbox');
    
          if (isset($config['classes']))
    
              $classes = array_merge($classes, (array) $config['classes']);
    
    
          foreach ($choices as $k => $v) {
              if (is_array($v)) {
                  $this->renderSectionBreak($k);
                  $this->emitChoices($v);
                  continue;
              }
    
              $id = sprintf("%s-%s", $this->id, $uid++);
    
            <label class="<?php echo implode(' ', $classes); ?>"
                for="<?php echo $id; ?>">
            <input id="<?php echo $id; ?>" type="<?php echo $type; ?>"
                name="<?php echo $this->name; ?>[]" <?php
    
                if ($this->value[$k]) echo 'checked="checked"'; ?> value="<?php
                echo Format::htmlchars($k); ?>"/>
            <?php
    
            if ($v) {
                echo Format::viewableImages($v);
            } ?>
    
            </label>
    <?php   }
        }
    
        function renderSectionBreak($label) { ?>
            <div><?php echo Format::htmlchars($label); ?></div>
    <?php
        }
    
        function getValue() {
            $data = $this->field->getSource();
            if (count($data)) {
                if (!isset($data[$this->name]))
                    return array();
                return $this->collectValues($data[$this->name], $this->field->getChoices());
            }
            return parent::getValue();
        }
    
        function collectValues($data, $choices) {
            $value = array();
            foreach ($choices as $k => $v) {
                if (is_array($v))
                    $value = array_merge($value, $this->collectValues($data, $v));
                elseif (@in_array($k, $data))
                    $value[$k] = $v;
            }
            return $value;
        }
    }
    
    /**
     * An extension to the BoxChoicesWidget which will render complex choices in
     * tabs.
     */
    class TabbedBoxChoicesWidget extends BoxChoicesWidget {
        function render($options=array()) {
            $tabs = array();
            foreach ($this->field->getChoices() as $label=>$group) {
                if (is_array($group)) {
                    $tabs[$label] = $group;
                }
                else {
                    $this->emitChoices(array($label=>$group));
                }
            }
            if ($tabs) {
                ?>
                <div>
                <ul class="alt tabs">
    <?php       $i = 0;
                foreach ($tabs as $label => $group) {
                    $active = $i++ == 0; ?>
                    <li <?php if ($active) echo 'class="active"';
                      ?>><a href="#<?php echo sprintf('%s-%s', $this->name, Format::slugify($label));
                      ?>"><?php echo Format::htmlchars($label); ?></a></li>
    <?php       } ?>
                </ul>
    <?php       $i = 0;
                foreach ($tabs as $label => $group) {
                    $first = $i++ == 0; ?>
                    <div class="tab_content <?php if (!$first) echo 'hidden'; ?>" id="<?php
                      echo sprintf('%s-%s', $this->name, Format::slugify($label));?>">
    <?php           $this->emitChoices($group); ?>
                    </div>
    <?php       } ?>
                </div>
    <?php   }
        }
    }
    
    
    Peter Rotich's avatar
    Peter Rotich committed
    /**
    * TimezoneWidget extends ChoicesWidget to add auto-detect and select2 search
    * options
    *
    **/
    class TimezoneWidget extends ChoicesWidget {
    
        function render($options=array()) {
            parent::render($options);
            $config = $this->field->getConfiguration();
            if (@$config['autodetect']) {
            ?>
            <button type="button" class="action-button" onclick="javascript:
                $('head').append($('<script>').attr('src', '<?php
                echo ROOT_PATH; ?>js/jstz.min.js'));
                var recheck = setInterval(function() {
                    if (window.jstz !== undefined) {
                        clearInterval(recheck);
                        var zone = jstz.determine();
                        $('#<?php echo $this->id; ?>').val(zone.name()).trigger('change');
    
                    }
                }, 100);
                return false;"
                style="vertical-align:middle">
                <i class="icon-map-marker"></i> <?php echo __('Auto Detect'); ?>
            </button>
            <?php
            } ?>
            <script type="text/javascript">
                $(function() {
                    $('#<?php echo $this->id; ?>').select2({
                        allowClear: true,
                        width: '300px'
                    });
                });
            </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($options=array()) {
    
    Jared Hancock's avatar
    Jared Hancock committed
            $config = $this->field->getConfiguration();
    
            if (!isset($this->value))
                $this->value = $this->field->get('default');
    
            $classes = array('checkbox');
    
    Jared Hancock's avatar
    Jared Hancock committed
            if (isset($config['classes']))
    
                $classes = array_merge($classes, (array) $config['classes']);
    
            <label class="<?php echo implode(' ', $classes); ?>">
    
            <input id="<?php echo $this->id; ?>"
    
                type="checkbox" name="<?php echo $this->name; ?>[]" <?php
    
    Jared Hancock's avatar
    Jared Hancock committed
                if ($this->value) echo 'checked="checked"'; ?> value="<?php
                echo $this->field->get('id'); ?>"/>
            <?php
    
            if ($config['desc']) {
                echo Format::viewableImages($config['desc']);
    
    Jared Hancock's avatar
    Jared Hancock committed
            } ?>
    
            </label>
    
    Jared Hancock's avatar
    Jared Hancock committed
    <?php
    
    Jared Hancock's avatar
    Jared Hancock committed
        }
    
        function getValue() {
    
            $data = $this->field->getSource();
    
            if (count($data)) {
                if (!isset($data[$this->name]))
                    return false;
    
                return @in_array($this->field->get('id'), $data[$this->name]);
    
    Jared Hancock's avatar
    Jared Hancock committed
            return parent::getValue();
        }
    
    
        function getJsValueGetter() {
    
            return '%s.is(":checked")';
    
    Jared Hancock's avatar
    Jared Hancock committed
    }
    
    class DatetimePickerWidget extends Widget {
    
        function render($options=array()) {
    
    Jared Hancock's avatar
    Jared Hancock committed
            $config = $this->field->getConfiguration();
    
            $timezone = $this->field->getTimezone();
    
    Jared Hancock's avatar
    Jared Hancock committed
            if ($this->value) {
    
    Peter Rotich's avatar
    Peter Rotich committed
    
                if (is_int($this->value))
                    // Assuming UTC timezone.
                    $datetime = DateTime::createFromFormat('U', $this->value);
                else {
                    $datetime = Format::parseDateTime($this->value);
    
    Peter Rotich's avatar
    Peter Rotich committed
    
                if ($config['time']) {
                    // Convert to user's timezone for update.
                    $datetime->setTimezone($timezone);
                }
    
                $this->value = Format::date($datetime->getTimestamp(), false,
                        false, $timezone ? $timezone->getName() : 'UTC');
            } else {
                $datetime = new DateTime('now');
                $datetime->setTimezone($timezone);
    
    Jared Hancock's avatar
    Jared Hancock committed
            }
            ?>
            <input type="text" name="<?php echo $this->name; ?>"
    
    Peter Rotich's avatar
    Peter Rotich committed
                id="<?php echo $this->id; ?>" style="display:inline-block;width:auto"
    
    Peter Rotich's avatar
    Peter Rotich committed
                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
    
    Peter Rotich's avatar
    Peter Rotich committed
                        if ($dt=$this->field->getMinDateTime())
                            echo sprintf("minDate: new Date(%s),\n", $dt->format('U')*1000);
                        if ($dt=$this->field->getMaxDateTime())
                            echo sprintf("maxDate: new Date(%s),\n", $dt->format('U')*1000);
    
    Jared Hancock's avatar
    Jared Hancock committed
                        elseif (!$config['future'])
    
    Peter Rotich's avatar
    Peter Rotich committed
                            echo "maxDate: new Date().getTime(),\n";
    
    Jared Hancock's avatar
    Jared Hancock committed
                        ?>
                        numberOfMonths: 2,
                        showButtonPanel: true,
                        buttonImage: './images/cal.png',
    
                        dateFormat: $.translate_format('<?php echo $cfg->getDateFormat(true); ?>')
    
    Jared Hancock's avatar
    Jared Hancock committed
                    });
                });
            </script>
            <?php
    
    Peter Rotich's avatar
    Peter Rotich committed
            if ($config['time']) {
                list($hr, $min) = explode(':', $datetime ?
                        $datetime->format('H:i') : '');
    
    Jared Hancock's avatar
    Jared Hancock committed
                // TODO: Add time picker -- requires time picker or selection with
                //       Misc::timeDropdown
                echo '&nbsp;' . Misc::timeDropdown($hr, $min, $this->name . ':time');
    
                echo sprintf('&nbsp;<span class="faded">(<a href="#"
                            data-placement="top" data-toggle="tooltip"
                            title="%s">%s</a>)</span>',
                        $datetime->getTimezone()->getName(),
    
    Peter Rotich's avatar
    Peter Rotich committed
                        $datetime->format('T'));
            }
    
    Jared Hancock's avatar
    Jared Hancock committed
        }
    
        /**
         * Function: getValue
         * Combines the datepicker date value and the time dropdown selected
    
    Peter Rotich's avatar
    Peter Rotich committed
         * time value into a single date and time string value in DateTime::W3C
    
    Jared Hancock's avatar
    Jared Hancock committed
         */
        function getValue() {
    
    Peter Rotich's avatar
    Peter Rotich committed
            if ($value = parent::getValue()) {
                // Effective timezone for the selection
    
                $timezone = $this->field->getTimezone();
    
    Peter Rotich's avatar
    Peter Rotich committed
                // See if we have time
                $data = $this->field->getSource();
                if ($value && isset($data[$this->name . ':time']))
                    $value .=' '.$data[$this->name . ':time'];
    
    
                $dt = new DateTime($value, $timezone);
    
    Peter Rotich's avatar
    Peter Rotich committed
                $value = $dt->format('Y-m-d H:i:s T');
    
    Peter Rotich's avatar
    Peter Rotich committed
    
            return $value;
    
    class SectionBreakWidget extends Widget {
    
        function render($options=array()) {
    
            ?><div class="form-header section-break"><h3><?php
    
            echo Format::htmlchars($this->field->getLocal('label'));
            ?></h3><em><?php echo Format::htmlchars($this->field->getLocal('hint'));
    
            ?></em></div>
            <?php
        }
    }
    
    class ThreadEntryWidget extends Widget {
    
        function render($options=array()) {
    
    Peter Rotich's avatar
    Peter Rotich committed
            $config = $this->field->getConfiguration();
    
            if ($options['client']) {
                $namespace = $options['draft-namespace']
                    ?: 'ticket.client';
                 $object_id = substr(session_id(), -12);
            } else {
                $namespace = $options['draft-namespace'] ?: 'ticket.staff';
    
            list($draft, $attrs) = Draft::getDraftAndDataAttrs($namespace, $object_id, $this->value);
    
            <textarea style="width:100%;" name="<?php echo $this->field->get('name'); ?>"
    
    Peter Rotich's avatar
    Peter Rotich committed
                placeholder="<?php echo Format::htmlchars($this->field->get('placeholder')); ?>"
    
    Peter Rotich's avatar
    Peter Rotich committed
                class="<?php if ($config['html']) echo 'richtext';
    
                    ?> draft draft-delete" <?php echo $attrs; ?>
    
                cols="21" rows="8" style="width:80%;"><?php echo
    
                Format::htmlchars($this->value) ?: $draft; ?></textarea>
    
            if (!$config['attachments'])
                return;
    
            $attachments = $this->getAttachments($config);
    
            print $attachments->render($options);
    
            foreach ($attachments->getMedia() as $type=>$urls) {
                foreach ($urls as $url)
                    Form::emitMedia($url, $type);
    
    
        function getAttachments($config=false) {
            if (!$config)
                $config = $this->field->getConfiguration();
    
    
            $field = new FileUploadField(array(
    
                'name'=>'attach:' . $this->field->get('id'),
    
            $field->setForm($this->field->getForm());
            return $field;
    
    Peter Rotich's avatar
    Peter Rotich committed
    
        function parseValue() {
            parent::parseValue();
            if (isset($this->value)) {
                $value = $this->value;
                $config = $this->field->getConfiguration();
                // Trim spaces based on text input type.
                // Preserve original input if not empty.
                if ($config['html'])
                    $this->value = trim($value, " <>br/\t\n\r") ? $value : '';
                else
                    $this->value = trim($value) ? $value : '';
            }
        }
    
    
    class FileUploadWidget extends Widget {
        static $media = array(
            'css' => array(
                '/css/filedrop.css',
            ),
        );
    
    
        function render($options) {
    
            $config = $this->field->getConfiguration();
            $name = $this->field->getFormName();
    
            $id = substr(md5(spl_object_hash($this)), 10);
    
            $attachments = $this->field->getFiles();
    
            $mimetypes = array_filter($config['__mimetypes'],
                function($t) { return strpos($t, '/') !== false; }
            );
    
            $maxfilesize = ($config['size'] ?: 1048576) / 1048576;
    
            $files = $F = array();
            $new = array_fill_keys($this->field->getClean(), 1);
    
            foreach ($attachments as $a) {
                $F[] = $a->file;
                unset($new[$a->file_id]);
    
            }
            // Add in newly added files not yet saved (if redisplaying after an
            // error)
            if ($new) {
    
                $F = array_merge($F, AttachmentFile::objects()
                    ->filter(array('id__in' => array_keys($new)))
    
            foreach ($F as $file) {
                $files[] = array(
                    'id' => $file->getId(),
                    'name' => $file->getName(),
                    'type' => $file->getType(),
                    'size' => $file->getSize(),
                    'download_url' => $file->getDownloadUrl(),
                );
    
                ?>" class="filedrop"><div class="files"></div>
                <div class="dropzone"><i class="icon-upload"></i>
    
    Jared Hancock's avatar
    Jared Hancock committed
                <?php echo sprintf(
                    __('Drop files here or %s choose them %s'),
                    '<a href="#" class="manual">', '</a>'); ?>
    
            <input type="file" multiple="multiple"
                id="file-<?php echo $id; ?>" style="display:none;"
    
                accept="<?php echo implode(',', $config['__mimetypes']); ?>"/>
    
            <script type="text/javascript">
    
            $(function(){$('#<?php echo $id; ?> .dropzone').filedropbox({
    
              url: 'ajax.php/form/upload/<?php echo $this->field->get('id') ?>',
    
              link: $('#<?php echo $id; ?>').find('a.manual'),
    
              paramname: 'upload[]',
    
              fallback_id: 'file-<?php echo $id; ?>',
    
              allowedfileextensions: <?php echo JsonDataEncoder::encode(
    
                $config['__extensions'] ?: array()); ?>,
    
              allowedfiletypes: <?php echo JsonDataEncoder::encode(
    
              maxfiles: <?php echo $config['max'] ?: 20; ?>,
    
              maxfilesize: <?php echo $maxfilesize; ?>,
    
              name: '<?php echo $name; ?>[]',
              files: <?php echo JsonDataEncoder::encode($files); ?>
            });});
            </script>
    <?php
        }
    
        function getValue() {
    
            $ids = array();
            // Handle manual uploads (IE<10)
            if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES[$this->name])) {
                foreach (AttachmentFile::format($_FILES[$this->name]) as $file) {
    
                        $F = $this->field->uploadFile($file);
                        $ids[] = $F->getId();
    
                    }
                    catch (FileUploadError $ex) {}
    
            // Files uploaded here MUST have been uploaded by this user and
            // identified in the session
            //
    
            // If no value was sent, assume an empty list
    
            if (!($files = parent::getValue()))
    
                return array();
    
            $allowed = array();
            // Files already attached to the field are allowed
            foreach ($this->field->getFiles() as $F) {
                // FIXME: This will need special porting in v1.10
                $allowed[$F->id] = 1;
    
    
            // New files uploaded in this session are allowed
            if (isset($_SESSION[':uploadedFiles']))
                $allowed += $_SESSION[':uploadedFiles'];
    
    
            // Canned attachments initiated by this session
            if (isset($_SESSION[':cannedFiles']))
               $allowed += $_SESSION[':cannedFiles'];
    
    
            // Parse the files and make sure it's allowed.
            foreach ($files as $info) {
                @list($id, $name) = explode(',', $info, 2);
                if (!isset($allowed[$id]))
                    continue;
    
                // Keep the values as the IDs
                if ($name)
                    $ids[$name] = $id;
                else
                    $ids[] = $id;
    
    class FileUploadError extends Exception {}
    
    
    class FreeTextField extends FormField {
        static $widget = 'FreeTextWidget';
    
        protected $attachments;