Skip to content
Snippets Groups Projects
class.forms.php 162 KiB
Newer Older
  • Learn to ignore specific revisions
  •                 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 @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();
    
    Peter Rotich's avatar
    Peter Rotich committed
    
            if (!isset($this->value) && ($default=$this->field->get('default')))
                $this->value = $default;
    
    
    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'));
    
    JediKev's avatar
    JediKev committed
            ?></h3><em><?php echo Format::display($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;
    
    
        function getConfigurationOptions() {
            return array(
                'content' => new TextareaField(array(
    
                    'configuration' => array('html' => true, 'size'=>'large'),
    
                    'label'=>__('Content'), 'required'=>true, 'default'=>'',
                    'hint'=>__('Free text shown in the form, such as a disclaimer'),
                )),
    
                'attachments' => new FileUploadField(array(
                    'id'=>'attach',
    
                    'label' => __('Attachments'),
    
                    'name'=>'files',
                    'configuration' => array('extensions'=>'')
                )),
    
            );
        }
    
        function hasData() {
            return false;
        }
    
        function isBlockLevel() {
            return true;
        }
    
    
        /* utils */
    
        function to_config($config) {
            if ($config && isset($config['attachments']))
    
                $keepers = $config['attachments'] = array_values($config['attachments']);
    
            $this->getAttachments()->keepOnlyFileIds($keepers);
    
            return $config;
        }
    
    
        function db_cleanup($field=false) {
    
            if ($field && $this->getFiles())
    
                $this->getAttachments()->deleteAll();
        }
    
        function getAttachments() {
    
            if (!isset($this->attachments))
                $this->attachments = GenericAttachments::forIdAndType($this->get('id'), 'I');
    
            return $this->attachments;
        }
    
        function getFiles() {
    
            if (!($attachments = $this->getAttachments()))
                return array();
    
            return $attachments->all();
        }
    
    
    }
    
    class FreeTextWidget extends Widget {
    
        function render($options=array()) {
    
            $config = $this->field->getConfiguration();
    
            $class = $config['classes'] ?: 'thread-body bleed';
            ?><div class="<?php echo $class; ?>"><?php
    
            if ($label = $this->field->getLocal('label')) { ?>
                <h3><?php
                echo Format::htmlchars($label);
            ?></h3><?php
            }
            if ($hint = $this->field->getLocal('hint')) { ?>
            <em><?php
                echo Format::htmlchars($hint);
            ?></em><?php
            } ?>
            <div><?php
    
                echo Format::viewableImages($config['content']); ?></div>
            </div>
            <?php
    
            if (($attachments = $this->field->getFiles()) && count($attachments)) { ?>
    
                <section class="freetext-files">
                <div class="title"><?php echo __('Related Resources'); ?></div>
    
                <?php foreach ($attachments as $attach) {
                    $filename = Format::htmlchars($attach->getFilename());
                    ?>
    
                    <div class="file">
    
                    <a href="<?php echo $attach->file->getDownloadUrl(); ?>"
    
                        target="_blank" download="<?php echo $filename; ?>"
                        class="truncate no-pjax">
    
                        <i class="icon-file"></i>
    
                        <?php echo $filename; ?>
    
                    </a>
                    </div>
                <?php } ?>
            </section>
            <?php }
    
    class ColorPickerWidget extends Widget {
        static $media = array(
            'css' => array(
                'css/spectrum.css',
            ),
            'js' => array(
                'js/spectrum.js',
            ),
        );
    
        function render($options=array()) {
            ?><input type="color"
                id="<?php echo $this->id; ?>"
                name="<?php echo $this->name; ?>"
                value="<?php echo Format::htmlchars($this->value); ?>"/><?php
        }
    }
    
    
    class VisibilityConstraint {
    
        static $operators = array(
            'eq' => 1,
        );
    
    
        const HIDDEN =      0x0001;
        const VISIBLE =     0x0002;
    
        var $initial;
        var $constraint;
    
        function __construct($constraint, $initial=self::VISIBLE) {
            $this->constraint = $constraint;
            $this->initial = $initial;
        }
    
        function emitJavascript($field) {
    
    
            if (!$this->constraint->constraints)
                return;
    
    
            $func = 'recheck';
            $form = $field->getForm();
    ?>
        <script type="text/javascript">
          !(function() {
            var <?php echo $func; ?> = function() {
    
              var target = $('#field<?php echo $field->getWidget()->id; ?>');
    
    
    <?php   $fields = $this->getAllFields($this->constraint);
            foreach ($fields as $f) {
                $field = $form->getField($f);
                echo sprintf('var %1$s = $("#%1$s");',
    
                    $field->getWidget()->id);
    
            }
            $expression = $this->compileQ($this->constraint, $form);
    ?>
    
              if (<?php echo $expression; ?>)
    
    Peter Rotich's avatar
    Peter Rotich committed
                target.slideDown('fast', function (){
    
                    $(this).trigger('show');
                    });
              else
                target.slideUp('fast', function (){
                    $(this).trigger('hide');
                    });
    
            };
    
    <?php   foreach ($fields as $f) {
                $w = $form->getField($f)->getWidget();
    ?>
    
            $('#<?php echo $w->id; ?>').on('change', <?php echo $func; ?>);
    
            $('#field<?php echo $w->id; ?>').on('show hide', <?php
                    echo $func; ?>);
    
    <?php   } ?>
          })();
        </script><?php
        }
    
        /**
         * Determines if the field was visible when the form was submitted
         */
        function isVisible($field) {
    
    
            // Assume initial visibility if constraint is not provided.
            if (!$this->constraint->constraints)
                return $this->initial == self::VISIBLE;
    
    
    
            return $this->compileQPhp($this->constraint, $field);
        }
    
    
        static function splitFieldAndOp($field) {
    
            if (false !== ($last = strrpos($field, '__'))) {
                $op = substr($field, $last + 2);
                if (isset(static::$operators[$op]))
                    $field = substr($field, 0, strrpos($field, '__'));
            }
    
            return array($field, $op);
        }
    
    
        function compileQPhp(Q $Q, $field) {
    
            if (!($form = $field->getForm())) {
                return $this->initial == self::VISIBLE;
            }
    
            $expr = array();
            foreach ($Q->constraints as $c=>$value) {
                if ($value instanceof Q) {
                    $expr[] = $this->compileQPhp($value, $field);
                }
                else {
    
                    @list($f, $op) = self::splitFieldAndOp($c);
    
                    $field = $form->getField($f);
    
                    $wval = $field ? $field->getClean() : null;
    
                    switch ($op) {
                    case 'eq':
                    case null:
    
                        $expr[] = ($wval == $value && $field->isVisible());
    
                    }
                }
            }
            $glue = $Q->isOred()
                ? function($a, $b) { return $a || $b; }
                : function($a, $b) { return $a && $b; };
            $initial = !$Q->isOred();
            $expression = array_reduce($expr, $glue, $initial);
            if ($Q->isNegated)
                $expression = !$expression;
            return $expression;
        }
    
        function getAllFields(Q $Q, &$fields=array()) {
            foreach ($Q->constraints as $c=>$value) {
                if ($c instanceof Q) {
                    $this->getAllFields($c, $fields);
                }
                else {
    
                    @list($f) = self::splitFieldAndOp($c);
    
                    $fields[$f] = true;
                }
            }
            return array_keys($fields);
        }
    
        function compileQ($Q, $form) {
            $expr = array();
            foreach ($Q->constraints as $c=>$value) {
                if ($value instanceof Q) {
                    $expr[] = $this->compileQ($value, $form);
                }
                else {
    
                    list($f, $op) = self::splitFieldAndOp($c);
    
                    $widget = $form->getField($f)->getWidget();
                    $id = $widget->id;
    
                    switch ($op) {
                    case 'eq':
    
                        $expr[] = sprintf('(%s.is(":visible") && %s)',
                                $id,
                                sprintf('%s == %s',
                                    sprintf($widget->getJsValueGetter(), $id),
                                    JsonDataEncoder::encode($value))
                                );
    
                    }
                }
            }
            $glue = $Q->isOred() ? ' || ' : ' && ';
            $expression = implode($glue, $expr);
            if (count($expr) > 1)
                $expression = '('.$expression.')';
            if ($Q->isNegated)
                $expression = '!'.$expression;
            return $expression;
        }
    }
    
    
    Peter Rotich's avatar
    Peter Rotich committed
    class AssignmentForm extends Form {
    
        static $id = 'assign';
        var $_assignee = null;
    
        var $_assignees = null;
    
    Peter Rotich's avatar
    Peter Rotich committed
    
    
        function getFields() {
    
            if ($this->fields)
                return $this->fields;
    
            $fields = array(
                'assignee' => new AssigneeField(array(
    
                        'id'=>1, 'label' => __('Assignee'),
                        'flags' => hexdec(0X450F3), 'required' => true,
    
    Peter Rotich's avatar
    Peter Rotich committed
                        'validator-error' => __('Assignee selection required'),
    
    Peter Rotich's avatar
    Peter Rotich committed
                        'configuration' => array(
                            'criteria' => array(
                                'available' => true,
                                ),
                           ),
    
                'refer' => new BooleanField(array(
                        'id'=>2, 'label'=>'', 'required'=>false,
                        'default'=>false,
                        'configuration'=>array(
                            'desc' => 'Maintain referral access to current assignees')
                        )
                    ),
    
    Peter Rotich's avatar
    Peter Rotich committed
                'comments' => new TextareaField(array(
    
                        'id' => 3, 'label'=> '', 'required'=>false, 'default'=>'',
    
    Peter Rotich's avatar
    Peter Rotich committed
                        'configuration' => array(
                            'html' => true,
    
                            'size' => 'small',
    
    Peter Rotich's avatar
    Peter Rotich committed
                            'placeholder' => __('Optional reason for the assignment'),
                            ),
                        )
                    ),
                );
    
    
            if (isset($this->_assignees))
    
                $fields['assignee']->setChoices($this->_assignees);
    
    
    
    Peter Rotich's avatar
    Peter Rotich committed
            $this->setFields($fields);
    
            return $this->fields;
        }
    
    
        function getField($name) {
    
            if (($fields = $this->getFields())
                    && isset($fields[$name]))
                return $fields[$name];
        }
    
    
        function isValid($include=false) {
    
            if (!parent::isValid($include) || !($f=$this->getField('assignee')))
    
    Peter Rotich's avatar
    Peter Rotich committed
                return false;
    
            // Do additional assignment validation
            if (!($assignee = $this->getAssignee())) {
    
                $f->addError(__('Unknown assignee'));
    
    Peter Rotich's avatar
    Peter Rotich committed
            } elseif ($assignee instanceof Staff) {
                // Make sure the agent is available
                if (!$assignee->isAvailable())
    
                    $f->addError(__('Agent is unavailable for assignment'));
            } elseif ($assignee instanceof Team) {
                // Make sure the team is active and has members
                if (!$assignee->isActive())
                    $f->addError(__('Team is disabled'));
                elseif (!$assignee->getNumMembers())
                    $f->addError(__('Team does not have members'));
    
    Peter Rotich's avatar
    Peter Rotich committed
            }
    
            return !$this->errors();
        }
    
        function render($options) {
    
            switch(strtolower($options['template'])) {
            case 'simple':
                $inc = STAFFINC_DIR . 'templates/dynamic-form-simple.tmpl.php';
                break;
            default:
                throw new Exception(sprintf(__('%s: Unknown template style %s'),
                            'FormUtils', $options['template']));
            }
    
            $form = $this;
            include $inc;
        }
    
    
        function setAssignees($assignees) {
            $this->_assignees = $assignees;
            $this->_fields = array();
        }
    
        function getAssignees() {
            return $this->_assignees;
        }
    
    
    Peter Rotich's avatar
    Peter Rotich committed
        function getAssignee() {
    
    
    Peter Rotich's avatar
    Peter Rotich committed
            if (!isset($this->_assignee))
                $this->_assignee = $this->getField('assignee')->getClean();
    
    Peter Rotich's avatar
    Peter Rotich committed
    
            return $this->_assignee;
        }
    
    
        function getComments() {
            return $this->getField('comments')->getClean();
    
    Peter Rotich's avatar
    Peter Rotich committed
        }
    
    
        function refer() {
            return $this->getField('refer')->getClean();
        }
    
    Peter Rotich's avatar
    Peter Rotich committed
    }
    
    class ClaimForm extends AssignmentForm {
    
        var $_fields;
    
        function setFields($fields) {
            $this->_fields = $fields;
            parent::setFields($fields);
        }
    
        function getFields() {
    
            if ($this->_fields)
                return $this->_fields;
    
    Peter Rotich's avatar
    Peter Rotich committed
            $fields = parent::getFields();
    
    Peter Rotich's avatar
    Peter Rotich committed
            // Disable && hide assignee field selection
            if (isset($fields['assignee'])) {
                $visibility = new VisibilityConstraint(
                        new Q(array()), VisibilityConstraint::HIDDEN);
    
    Peter Rotich's avatar
    Peter Rotich committed
                $fields['assignee']->set('visibility', $visibility);
            }
    
            // Change coments placeholder to reflect claim
            if (isset($fields['comments'])) {
                $fields['comments']->configure('placeholder',
                        __('Optional reason for the claim'));
            }
    
    
            $this->setFields($fields);
    
            return $this->fields;
    
    Peter Rotich's avatar
    Peter Rotich committed
    class ReferralForm extends Form {
    
        static $id = 'refer';
        var $_target = null;
        var $_choices = null;
        var $_prompt = '';
    
        function getFields() {
    
            if ($this->fields)
                return $this->fields;
    
            $fields = array(
    
                'target' => new ChoiceField(array(
    
    Peter Rotich's avatar
    Peter Rotich committed
                        'id'=>1,
                        'label' => __('Referee'),
                        'flags' => hexdec(0X450F3),
                        'required' => true,
                        'validator-error' => __('Selection required'),
    
                        'choices' => array(
                        'agent' => __('Agent'),
                        'team'  => __('Team'),
                                    'dept'  => __('Department'),
                                   ),
                                )
                    ),
                'agent' => new ChoiceField(array(
                        'id'=>2,
                        'label' => '',
                        'flags' => hexdec(0X450F3),
                        'required' => true,
                        'configuration'=>array('prompt'=>__('Select Agent')),
                                'validator-error' => __('Agent selection required'),
                        'visibility' => new VisibilityConstraint(
                            new Q(array('target__eq'=>'agent')),
                            VisibilityConstraint::HIDDEN
                          ),
                                )
                    ),
                'team' => new ChoiceField(array(
                        'id'=>3,
                        'label' => '',
                        'flags' => hexdec(0X450F3),
                        'required' => true,
                        'validator-error' => __('Team selection required'),
                        'configuration'=>array('prompt'=>__('Select Team')),
                                'visibility' => new VisibilityConstraint(
                                        new Q(array('target__eq'=>'team')),
                                        VisibilityConstraint::HIDDEN
                                  ),
                                )
                    ),
                'dept' => new ChoiceField(array(
                        'id'=>4,
                        'label' => '',
                        'flags' => hexdec(0X450F3),
                        'required' => true,
                        'validator-error' => __('Dept. selection required'),
                        'configuration'=>array('prompt'=>__('Select Department')),
                                'visibility' => new VisibilityConstraint(
                                        new Q(array('target__eq'=>'dept')),
                                        VisibilityConstraint::HIDDEN
                                  ),
                                )
    
    Peter Rotich's avatar
    Peter Rotich committed
                    ),
                'comments' => new TextareaField(array(
    
    Peter Rotich's avatar
    Peter Rotich committed
                        'label'=> '',
                        'required'=>false,
                        'default'=>'',
                        'configuration' => array(
                            'html' => true,
                            'size' => 'small',
                            'placeholder' => __('Optional reason for the referral'),
                            ),
                        )
                    ),
                );
    
            $this->setFields($fields);
    
            return $this->fields;
        }
    
        function getField($name) {
    
            if (($fields = $this->getFields())
                    && isset($fields[$name]))
                return $fields[$name];
        }
    
    
    Peter Rotich's avatar
    Peter Rotich committed
        function isValid($include=false) {
    
            if (!parent::isValid($include) || !($f=$this->getField('target')))
                return false;
    
            // Do additional assignment validation
    
            $referee = $this->getReferee();
    
    Peter Rotich's avatar
    Peter Rotich committed
            switch (true) {
    
            case $referee instanceof Staff:
    
    Peter Rotich's avatar
    Peter Rotich committed
                // Make sure the agent is available
    
                if (!$referee->isAvailable())
    
    Peter Rotich's avatar
    Peter Rotich committed
                    $f->addError(__('Agent is unavailable for assignment'));
            break;
    
            case $referee instanceof Team:
    
    Peter Rotich's avatar
    Peter Rotich committed
                // Make sure the team is active and has members
    
                if (!$referee->isActive())
    
    Peter Rotich's avatar
    Peter Rotich committed
                    $f->addError(__('Team is disabled'));
    
                elseif (!$referee->getNumMembers())
    
    Peter Rotich's avatar
    Peter Rotich committed
                    $f->addError(__('Team does not have members'));
            break;
    
            case $referee instanceof Dept:
    
    Peter Rotich's avatar
    Peter Rotich committed
            break;
            default:
                $f->addError(__('Unknown selection'));
            }
    
            return !$this->errors();
        }
    
        function render($options) {
    
            switch(strtolower($options['template'])) {
            case 'simple':
                $inc = STAFFINC_DIR . 'templates/dynamic-form-simple.tmpl.php';
                break;
            default:
                throw new Exception(sprintf(__('%s: Unknown template style %s'),
                            'FormUtils', $options['template']));
            }
    
            $form = $this;