2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-01-09 05:18:36 +01:00
|
|
|
/**
|
|
|
|
* Multi-line listbox field, created from a <select> tag.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <b>Usage</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <code>
|
|
|
|
* new ListboxField(
|
|
|
|
* $name = "pickanumber",
|
|
|
|
* $title = "Pick a number",
|
|
|
|
* $source = array(
|
|
|
|
* "1" => "one",
|
|
|
|
* "2" => "two",
|
|
|
|
* "3" => "three"
|
|
|
|
* ),
|
|
|
|
* $value = 1
|
|
|
|
* )
|
2014-08-15 08:53:05 +02:00
|
|
|
* </code>
|
|
|
|
*
|
2010-10-19 00:50:48 +02:00
|
|
|
* @see DropdownField for a simple <select> field with a single element.
|
|
|
|
* @see CheckboxSetField for multiple selections through checkboxes.
|
|
|
|
* @see OptionsetField for single selections via radiobuttons.
|
|
|
|
* @see TreeDropdownField for a rich and customizeable UI that can visualize a tree of selectable elements
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-basic
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class ListboxField extends DropdownField {
|
2009-06-02 01:37:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The size of the field in rows.
|
|
|
|
* @var int
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $size;
|
2009-06-02 01:37:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Should the user be able to select multiple
|
|
|
|
* items on this dropdown field?
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-06-02 01:37:03 +02:00
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $multiple = false;
|
2012-03-05 12:27:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Array
|
|
|
|
*/
|
|
|
|
protected $disabledItems = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Array
|
|
|
|
*/
|
|
|
|
protected $defaultItems = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Creates a new dropdown field.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-06-02 01:37:03 +02:00
|
|
|
* @param string $name The field name
|
|
|
|
* @param string $title The field title
|
|
|
|
* @param array $source An map of the dropdown items
|
|
|
|
* @param string|array $value You can pass an array of values or a single value like a drop down to be selected
|
|
|
|
* @param int $size Optional size of the select element
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param form The parent form
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($name, $title = '', $source = array(), $value = '', $size = null, $multiple = false) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if($size) $this->size = $size;
|
|
|
|
if($multiple) $this->multiple = $multiple;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-02 17:09:30 +01:00
|
|
|
parent::__construct($name, $title, $source, $value);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns a <select> tag containing all the appropriate <option> tags
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Field($properties = array()) {
|
2012-03-05 12:27:55 +01:00
|
|
|
if($this->multiple) $this->name .= '[]';
|
2011-12-22 13:10:57 +01:00
|
|
|
$options = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// We have an array of values
|
|
|
|
if(is_array($this->value)){
|
|
|
|
// Loop through and figure out which values were selected.
|
2008-10-15 14:39:09 +02:00
|
|
|
foreach($this->getSource() as $value => $title) {
|
2011-12-22 13:10:57 +01:00
|
|
|
$options[] = new ArrayData(array(
|
|
|
|
'Title' => $title,
|
|
|
|
'Value' => $value,
|
2012-03-05 12:27:55 +01:00
|
|
|
'Selected' => (in_array($value, $this->value) || in_array($value, $this->defaultItems)),
|
|
|
|
'Disabled' => $this->disabled || in_array($value, $this->disabledItems),
|
2011-12-22 13:10:57 +01:00
|
|
|
));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-12-22 13:10:57 +01:00
|
|
|
} else {
|
2007-07-19 12:40:28 +02:00
|
|
|
// Listbox was based a singlular value, so treat it like a dropdown.
|
2008-10-15 14:39:09 +02:00
|
|
|
foreach($this->getSource() as $value => $title) {
|
2011-12-22 13:10:57 +01:00
|
|
|
$options[] = new ArrayData(array(
|
|
|
|
'Title' => $title,
|
|
|
|
'Value' => $value,
|
2012-03-05 12:27:55 +01:00
|
|
|
'Selected' => ($value == $this->value || in_array($value, $this->defaultItems)),
|
|
|
|
'Disabled' => $this->disabled || in_array($value, $this->disabledItems),
|
2011-12-22 13:10:57 +01:00
|
|
|
));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-14 07:32:29 +02:00
|
|
|
$properties = array_merge($properties, array(
|
|
|
|
'Options' => new ArrayList($options)
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-14 07:32:29 +02:00
|
|
|
return $this->customise($properties)->renderWith($this->getTemplates());
|
2011-12-22 13:10:57 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getAttributes() {
|
2011-12-22 13:10:57 +01:00
|
|
|
return array_merge(
|
|
|
|
parent::getAttributes(),
|
|
|
|
array(
|
|
|
|
'multiple' => $this->multiple,
|
|
|
|
'size' => $this->size
|
|
|
|
)
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
/**
|
2009-06-02 01:37:03 +02:00
|
|
|
* Sets the size of this dropdown in rows.
|
|
|
|
* @param int $size The height in rows (e.g. 3)
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setSize($size) {
|
2009-06-02 01:37:03 +02:00
|
|
|
$this->size = $size;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
/**
|
2009-06-02 01:37:03 +02:00
|
|
|
* Sets this field to have a muliple select attribute
|
|
|
|
* @param boolean $bool
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setMultiple($bool) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->multiple = $bool;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setSource($source) {
|
2011-08-29 14:31:03 +02:00
|
|
|
if($source) {
|
2012-09-26 23:34:00 +02:00
|
|
|
$hasCommas = array_filter(array_keys($source),
|
|
|
|
create_function('$key', 'return strpos($key, ",") !== FALSE;'));
|
2011-08-29 14:31:03 +02:00
|
|
|
if($hasCommas) {
|
|
|
|
throw new InvalidArgumentException('No commas allowed in $source keys');
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-29 14:31:03 +02:00
|
|
|
parent::setSource($source);
|
2012-02-17 13:35:26 +01:00
|
|
|
|
|
|
|
return $this;
|
2011-08-29 14:31:03 +02:00
|
|
|
}
|
2012-03-05 10:55:09 +01:00
|
|
|
|
2011-08-29 14:31:03 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Return the CheckboxSetField value as a string
|
2012-03-05 10:55:09 +01:00
|
|
|
* selected item keys.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-03-05 10:55:09 +01:00
|
|
|
* @return string
|
2011-08-29 14:31:03 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function dataValue() {
|
2012-03-05 10:55:09 +01:00
|
|
|
if($this->value && is_array($this->value) && $this->multiple) {
|
|
|
|
$filtered = array();
|
|
|
|
foreach($this->value as $item) {
|
|
|
|
if($item) {
|
|
|
|
$filtered[] = str_replace(",", "{comma}", $item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return implode(',', $filtered);
|
2011-08-29 14:31:03 +02:00
|
|
|
} else {
|
|
|
|
return parent::dataValue();
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-05 10:55:09 +01:00
|
|
|
/**
|
|
|
|
* Save the current value of this field into a DataObject.
|
|
|
|
* If the field it is saving to is a has_many or many_many relationship,
|
|
|
|
* it is saved by setByIDList(), otherwise it creates a comma separated
|
|
|
|
* list for a standard DB text/varchar field.
|
|
|
|
*
|
|
|
|
* @param DataObject $record The record to save into
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function saveInto(DataObjectInterface $record) {
|
2012-03-05 10:55:09 +01:00
|
|
|
if($this->multiple) {
|
|
|
|
$fieldname = $this->name;
|
2012-03-06 23:47:09 +01:00
|
|
|
$relation = ($fieldname && $record && $record->hasMethod($fieldname)) ? $record->$fieldname() : null;
|
2013-08-21 08:54:05 +02:00
|
|
|
if($fieldname && $record && $relation &&
|
|
|
|
($relation instanceof RelationList || $relation instanceof UnsavedRelationList)) {
|
2012-03-05 10:55:09 +01:00
|
|
|
$idList = (is_array($this->value)) ? array_values($this->value) : array();
|
2012-03-09 22:32:12 +01:00
|
|
|
if(!$record->ID) {
|
|
|
|
$record->write(); // record needs to have an ID in order to set relationships
|
2012-09-26 23:34:00 +02:00
|
|
|
$relation = ($fieldname && $record && $record->hasMethod($fieldname))
|
|
|
|
? $record->$fieldname()
|
|
|
|
: null;
|
2012-03-09 22:32:12 +01:00
|
|
|
}
|
2012-03-06 23:47:09 +01:00
|
|
|
$relation->setByIDList($idList);
|
2012-03-05 10:55:09 +01:00
|
|
|
} elseif($fieldname && $record) {
|
|
|
|
if($this->value) {
|
|
|
|
$this->value = str_replace(',', '{comma}', $this->value);
|
|
|
|
$record->$fieldname = implode(",", $this->value);
|
|
|
|
} else {
|
|
|
|
$record->$fieldname = null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2012-03-05 10:55:09 +01:00
|
|
|
} else {
|
|
|
|
parent::saveInto($record);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a value into this CheckboxSetField
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setValue($val, $obj = null) {
|
2014-08-15 08:53:05 +02:00
|
|
|
// If we're not passed a value directly,
|
2012-03-05 10:55:09 +01:00
|
|
|
// we can look for it in a relation method on the object passed as a second arg
|
|
|
|
if(!$val && $obj && $obj instanceof DataObject && $obj->hasMethod($this->name)) {
|
|
|
|
$funcName = $this->name;
|
|
|
|
$val = array_values($obj->$funcName()->getIDList());
|
|
|
|
}
|
|
|
|
|
2011-08-29 14:31:03 +02:00
|
|
|
if($val) {
|
|
|
|
if(!$this->multiple && is_array($val)) {
|
|
|
|
throw new InvalidArgumentException('No array values allowed with multiple=false');
|
|
|
|
}
|
|
|
|
|
|
|
|
if($this->multiple) {
|
|
|
|
$parts = (is_array($val)) ? $val : preg_split("/ *, */", trim($val));
|
|
|
|
if(ArrayLib::is_associative($parts)) {
|
|
|
|
throw new InvalidArgumentException('No associative arrays allowed multiple=true');
|
|
|
|
}
|
|
|
|
|
2012-01-19 11:33:49 +01:00
|
|
|
// Doesn't check against unknown values in order to allow for less rigid data handling.
|
|
|
|
// They're silently ignored and overwritten the next time the field is saved.
|
2011-08-29 14:31:03 +02:00
|
|
|
parent::setValue($parts);
|
|
|
|
} else {
|
2012-09-14 14:33:59 +02:00
|
|
|
if(!in_array($val, array_keys($this->getSource()))) {
|
2011-08-29 14:31:03 +02:00
|
|
|
throw new InvalidArgumentException(sprintf(
|
2014-08-15 08:53:05 +02:00
|
|
|
'Invalid value "%s" for multiple=false',
|
2011-08-29 14:31:03 +02:00
|
|
|
Convert::raw2xml($val)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::setValue($val);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
parent::setValue($val);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2011-08-29 14:31:03 +02:00
|
|
|
}
|
2012-03-05 12:27:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark certain elements as disabled,
|
|
|
|
* regardless of the {@link setDisabled()} settings.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-03-05 12:27:55 +01:00
|
|
|
* @param array $items Collection of array keys, as defined in the $source array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setDisabledItems($items) {
|
2012-03-05 12:27:55 +01:00
|
|
|
$this->disabledItems = $items;
|
|
|
|
return $this;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-05 12:27:55 +01:00
|
|
|
/**
|
|
|
|
* @return Array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getDisabledItems() {
|
2012-03-05 12:27:55 +01:00
|
|
|
return $this->disabledItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default selections, regardless of the {@link setValue()} settings.
|
|
|
|
* Note: Items marked as disabled through {@link setDisabledItems()} can still be
|
|
|
|
* selected by default through this method.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-03-05 12:27:55 +01:00
|
|
|
* @param Array $items Collection of array keys, as defined in the $source array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setDefaultItems($items) {
|
2012-03-05 12:27:55 +01:00
|
|
|
$this->defaultItems = $items;
|
|
|
|
return $this;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-05 12:27:55 +01:00
|
|
|
/**
|
|
|
|
* @return Array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getDefaultItems() {
|
2012-03-05 12:27:55 +01:00
|
|
|
return $this->defaultItems;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|