Skip to content
Snippets Groups Projects
class.dynamic_forms.php 35 KiB
Newer Older
  • Learn to ignore specific revisions
  •             return array($item->value, $item->id);
    
    Jared Hancock's avatar
    Jared Hancock committed
            return null;
        }
    
        function toString($item) {
    
            return ($item instanceof DynamicListItem)
                ? $item->toString() : (string) $item;
    
        }
    
        function validateEntry($item) {
    
            $config = $this->getConfiguration();
    
            parent::validateEntry($item);
            if ($item && !$item instanceof DynamicListItem)
                $this->_errors[] = 'Select a value from the list';
    
            elseif ($item && $config['typeahead']
                    && $this->getWidget()->getEnteredValue() != $item->get('value'))
                $this->_errors[] = 'Select a value from the list';
    
    Jared Hancock's avatar
    Jared Hancock committed
        }
    
        function getConfigurationOptions() {
            return array(
                'typeahead' => new ChoiceField(array(
                    'id'=>1, 'label'=>'Widget', 'required'=>false,
                    'default'=>false,
                    'choices'=>array(false=>'Drop Down', true=>'Typeahead'),
    
                    'hint'=>'Typeahead will work better for large lists'
                )),
                '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),
                )),
    
    
        function getChoices() {
            if (!$this->_choices) {
                $this->_choices = array();
                foreach ($this->getList()->getItems() as $i)
                    $this->_choices[$i->get('id')] = $i->get('value');
            }
            return $this->_choices;
        }
    
    
        function export($value) {
            if ($value && is_numeric($value)
                    && ($item = DynamicListItem::lookup($value)))
                return $item->toString();
            return $value;
        }
    
    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();
            $value = false;
    
            if ($this->value instanceof DynamicListItem) {
    
    Jared Hancock's avatar
    Jared Hancock committed
                // Loaded from database
                $value = $this->value->get('id');
                $name = $this->value->get('value');
            } elseif ($this->value) {
                // Loaded from POST
                $value = $this->value;
    
                $name = $this->getEnteredValue();
    
            if (!$config['typeahead'] || $mode=='search') {
    
    Jared Hancock's avatar
    Jared Hancock committed
                $this->value = $value;
    
                return parent::render($mode);
    
    Jared Hancock's avatar
    Jared Hancock committed
            }
    
            $source = array();
            foreach ($this->field->getList()->getItems() as $i)
                $source[] = array(
    
                    'value' => $i->get('value'), 'id' => $i->get('id'),
    
                    'info' => $i->get('value')." -- ".$i->get('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();
        }