Skip to content
Snippets Groups Projects
class.dynamic_forms.php 36.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • Peter Rotich's avatar
    Peter Rotich committed
        function validateEntry($entry) {
            parent::validateEntry($entry);
            if (!$this->errors()) {
                $config = $this->getConfiguration();
                if (!$entry || count($entry) == 0)
    
                    $this->_errors[] = __('Select a value from the list');
    
    Peter Rotich's avatar
    Peter Rotich committed
                elseif ($config['typeahead']
                        && !in_array($this->getWidget()->getEnteredValue(), $entry))
    
                    $this->_errors[] = __('Select a value from the list');
    
    Jared Hancock's avatar
    Jared Hancock committed
        }
    
        function getConfigurationOptions() {
            return array(
    
    Peter Rotich's avatar
    Peter Rotich committed
                'widget' => new ChoiceField(array(
    
                    'id'=>1, 'label'=>__('Widget'), 'required'=>false,
    
    Peter Rotich's avatar
    Peter Rotich committed
                    'default' => 'dropdown',
                    'choices'=>array(
    
                        'dropdown' => __('Drop Down'),
                        'typeahead' =>__('Typeahead'),
    
    Peter Rotich's avatar
    Peter Rotich committed
                    ),
                    'configuration'=>array(
                        'multiselect' => false,
                    ),
    
                    'hint'=>__('Typeahead will work better for large lists')
    
    Peter Rotich's avatar
    Peter Rotich committed
                'multiselect' => new BooleanField(array(
    
                    'id'=>1, 'label'=>__(/* Type of widget allowing multiple selections */ 'Multiselect'),
                    'required'=>false, 'default'=>false,
    
    Peter Rotich's avatar
    Peter Rotich committed
                    'configuration'=>array(
    
                        'desc'=>__('Allow multiple selections')),
                    'hint' => __('Dropdown only'),
    
    Peter Rotich's avatar
    Peter Rotich committed
                )),
    
                '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),
                )),
    
    Peter Rotich's avatar
    Peter Rotich committed
        function getConfiguration() {
    
            $config = parent::getConfiguration();
            if ($config['widget'])
                $config['typeahead'] = isset($config['widget']['typeahead']);
    
            //Typeahed doesn't support multiselect for now  TODO: Add!
            if ($config['typeahead'])
                $config['multiselect'] = false;
    
            return $config;
        }
    
        function getChoices($verbose=false) {
            if (!$this->_choices || $verbose) {
    
                $this->_choices = array();
                foreach ($this->getList()->getItems() as $i)
    
    Peter Rotich's avatar
    Peter Rotich committed
                    $this->_choices[$i->getId()] = $i->getValue();
    
                // Retired old selections
                $values = ($a=$this->getAnswer()) ? $a->getValue() : array();
                if ($values && is_array($values)) {
                    foreach ($values as $k => $v) {
                        if (!isset($this->_choices[$k])) {
    
                            if ($verbose) $v .= ' '.__('(retired)');
    
    Peter Rotich's avatar
    Peter Rotich committed
                            $this->_choices[$k] = $v;
                        }
                    }
    
            }
            return $this->_choices;
        }
    
    
        function export($value) {
            if ($value && is_numeric($value)
                    && ($item = DynamicListItem::lookup($value)))
                return $item->toString();
            return $value;
        }
    
    
        function getFilterData() {
            $data = array(parent::getFilterData());
            if (($v = $this->getClean()) instanceof DynamicListItem) {
                $data = array_merge($data, $v->getFilterData());
            }
            return $data;
        }
    
    Jared Hancock's avatar
    Jared Hancock committed
    }
    
    class SelectionWidget extends ChoicesWidget {
    
        function render($mode=false) {
    
    Jared Hancock's avatar
    Jared Hancock committed
            $config = $this->field->getConfiguration();
    
    Peter Rotich's avatar
    Peter Rotich committed
            if (($value=$this->getValue()))
                $value =  $this->field->parse($value);
            elseif ($this->value)
    
    Jared Hancock's avatar
    Jared Hancock committed
                $value = $this->value;
    
            if (!$config['typeahead'] || $mode=='search') {
    
    Jared Hancock's avatar
    Jared Hancock committed
                $this->value = $value;
    
                return parent::render($mode);
    
    Peter Rotich's avatar
    Peter Rotich committed
            if ($value && is_array($value)) {
                $name = current($value);
                $value = key($value);
            }
    
    
    Jared Hancock's avatar
    Jared Hancock committed
            $source = array();
            foreach ($this->field->getList()->getItems() as $i)
                $source[] = array(
    
    Peter Rotich's avatar
    Peter Rotich committed
                    'value' => $i->getValue(), 'id' => $i->getId(),
                    'info' => sprintf('%s %s',
                        $i->getValue(),
                        (($extra= $i->getAbbrev()) ? "-- $extra" : '')),
    
    Jared Hancock's avatar
    Jared Hancock committed
            ?>
            <span style="display:inline-block">
    
            <input type="text" size="30" name="<?php echo $this->name; ?>"
    
                id="<?php echo $this->name; ?>" value="<?php echo $name; ?>"
    
                placeholder="<?php echo $config['prompt'];
                ?>" autocomplete="off" />
    
            <input type="hidden" name="<?php echo $this->name;
                ?>_id" id="<?php echo $this->name; ?>_id" value="<?php echo $value; ?>"/>
    
    Jared Hancock's avatar
    Jared Hancock committed
            <script type="text/javascript">
            $(function() {
    
                $('input#<?php echo $this->name; ?>').typeahead({
    
    Jared Hancock's avatar
    Jared Hancock committed
                    source: <?php echo JsonDataEncoder::encode($source); ?>,
    
                    property: 'info',
    
    Jared Hancock's avatar
    Jared Hancock committed
                    onselect: function(item) {
    
                        $('input#<?php echo $this->name; ?>').val(item['value'])
    
                        $('input#<?php echo $this->name; ?>_id').val(item['id'])
    
    Jared Hancock's avatar
    Jared Hancock committed
                    }
                });
            });
            </script>
            </span>
            <?php
        }
    
    
        function getValue() {
            $data = $this->field->getSource();
            // Search for HTML form name first
            if (isset($data[$this->name.'_id']))
                return (int) $data[$this->name.'_id'];
            return parent::getValue();
        }
    
    
        function getEnteredValue() {
            // Used to verify typeahead fields
            return parent::getValue();
        }