Newer
Older
function isEnabled() {
return $this->hasStatus(self::ENABLED);
}
function enable() {
$this->setStatus(self::ENABLED);
}
function disable() {
$this->clearStatus(self::ENABLED);
}
function getConfiguration() {
if (!$this->_config) {
$this->_config = $this->get('properties');
if (is_string($this->_config))
$this->_config = JsonDataParser::parse($this->_config);
elseif (!$this->_config)
$this->_config = array();
}
return $this->_config;
}
function setConfiguration(&$errors=array()) {
$config = array();
foreach ($this->getConfigurationForm()->getFields() as $field) {
$val = $field->to_database($field->getClean());
$config[$field->get('id')] = is_array($val) ? $val[1] : $val;
$errors = array_merge($errors, $field->errors());
}
if (count($errors) === 0)
$this->set('properties', JsonDataEncoder::encode($config));
return count($errors) === 0;
}
function getConfigurationForm() {
if (!$this->_form) {
$this->_form = DynamicForm::lookup(
array('type'=>'L'.$this->get('list_id')));
}
return $this->_form;
}
function getVar($name) {
$config = $this->getConfiguration();
$name = mb_strtolower($name);
foreach ($this->getConfigurationForm()->getFields() as $field) {
if (mb_strtolower($field->get('name')) == $name)
return $config[$field->get('id')];
}
}
function toString() {
return $this->get('value');
}
function __toString() {
return $this->toString();
}
function delete() {
# Don't really delete, just unset the list_id to un-associate it with
# the list
$this->set('list_id', null);
return $this->save();
}
}
class SelectionField extends FormField {
static $widget = 'SelectionWidget';
function getListId() {
list(,$list_id) = explode('-', $this->get('type'));
return $list_id;
}
if (!$this->_list)
$this->_list = DynamicList::lookup($this->getListId());
$config = $this->getConfiguration();
if (is_int($value))
return $this->to_php($this->getWidget()->getEnteredValue(), (int) $value);
elseif (!$config['typeahead'])
return $this->to_php(null, (int) $value);
else
return $this->to_php($value);
function to_php($value, $id=false) {
if ($id && is_int($id))
$item = DynamicListItem::lookup($id);
if (!$item || ($value !== null && $value != $item->get('value'))) {
$item = DynamicListItem::lookup(array(
'value'=>$value,
'list_id'=>$this->getListId()));
return ($item) ? $item : $value;
function hasIdValue() {
return true;
}
if ($item instanceof DynamicListItem)
return array($item->value, $item->id);
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';
}
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');
if ($this->value && !isset($this->_choices[$this->value])) {
$v = DynamicListItem::lookup($this->value);
$this->_choices[$v->get('id')] = $v->get('value').' (Disabled)';
}
}
return $this->_choices;
}
function export($value) {
if ($value && is_numeric($value)
&& ($item = DynamicListItem::lookup($value)))
return $item->toString();
return $value;
}
}
class SelectionWidget extends ChoicesWidget {
$config = $this->field->getConfiguration();
$value = false;
if ($this->value instanceof DynamicListItem) {
// 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') {
}
$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'),
);
<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; ?>"/>
<script type="text/javascript">
$(function() {
$('input#<?php echo $this->name; ?>').typeahead({
source: <?php echo JsonDataEncoder::encode($source); ?>,
$('input#<?php echo $this->name; ?>').val(item['value'])
$('input#<?php echo $this->name; ?>_id').val(item['id'])
}
});
});
</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();
}