Newer
Older
return false;
}
static function create($ht=false) {
$inst = parent::create($ht);
$inst->set('created', new SqlFunction('NOW'));
return $inst;
}
static function getSelections() {
$selections = array();
foreach (DynamicList::objects() as $list) {
$selections['list-'.$list->id] =
SelectionField, $list->get('id'));
}
return $selections;
}
}
FormField::addFieldTypes('Custom Lists', array('DynamicList', 'getSelections'));
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
/**
* Represents a single item in a dynamic list
*
* Fields:
* value - (char * 255) Actual list item content
* extra - (char * 255) Other values that represent the same item in the
* list, such as an abbreviation. In practice, should be a
* space-separated list of tokens which should hit this list item in a
* search
* sort - (int) If sorting by this field, represents the numeric sort order
* that this item should come in the dropdown list
*/
class DynamicListItem extends VerySimpleModel {
static $meta = array(
'table' => LIST_ITEM_TABLE,
'pk' => array('id'),
'joins' => array(
'list' => array(
'null' => true,
'constraint' => array('list_id' => 'DynamicList.id'),
),
),
);
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
const ENABLED = 0x0001;
protected function hasStatus($flag) {
return 0 !== ($this->get('status') & $flag);
}
protected function clearStatus($flag) {
return $this->set('status', $this->get('status') & ~$flag);
}
protected function setStatus($flag) {
return $this->set('status', $this->get('status') | $flag);
}
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 getFilterData() {
$raw = $this->getConfiguration();
$props = array();
if ($form = $this->getConfigurationForm()) {
foreach ($form->getFields() as $field) {
$tag = $field->get('id');
if (isset($raw[$tag]))
$props[".$tag"] = $field->toString($raw[$tag]);
}
}
return $props;
}
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 ($value === null && $id === null)
return null;
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;
}
function getFilterData() {
$data = array(parent::getFilterData());
if (($v = $this->getClean()) instanceof DynamicListItem) {
$data = array_merge($data, $v->getFilterData());
}
return $data;
}
}
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();
}