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
|
|
|
*/
|
2015-02-13 05:35:39 +01:00
|
|
|
class ListboxField extends MultiSelectField {
|
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
|
|
|
|
2012-03-05 12:27:55 +01:00
|
|
|
/**
|
|
|
|
* @var Array
|
|
|
|
*/
|
|
|
|
protected $disabledItems = array();
|
|
|
|
|
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
|
2015-02-13 05:35:39 +01:00
|
|
|
* @param string|array|null $value You can pass an array of values or a single value like a drop down to be selected
|
2009-06-02 01:37:03 +02:00
|
|
|
* @param int $size Optional size of the select element
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2015-02-13 05:35:39 +01:00
|
|
|
public function __construct($name, $title = '', $source = array(), $value = null, $size = null) {
|
|
|
|
if($size) {
|
|
|
|
$this->setSize($size);
|
|
|
|
}
|
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
|
2015-02-13 05:35:39 +01:00
|
|
|
*
|
|
|
|
* @param array $properties
|
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Field($properties = array()) {
|
2012-04-14 07:32:29 +02:00
|
|
|
$properties = array_merge($properties, array(
|
2015-02-13 05:35:39 +01:00
|
|
|
'Options' => $this->getOptions()
|
2012-04-14 07:32:29 +02:00
|
|
|
));
|
2015-02-13 05:35:39 +01:00
|
|
|
return $this
|
|
|
|
->customise($properties)
|
|
|
|
->renderWith($this->getTemplates());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the list of options to render in this formfield
|
|
|
|
*
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
public function getOptions() {
|
|
|
|
// Loop through and figure out which values were selected.
|
|
|
|
$options = array();
|
|
|
|
$selectedValue = $this->getValueArray();
|
|
|
|
foreach($this->getSource() as $itemValue => $title) {
|
|
|
|
$itemSelected = in_array($itemValue, $selectedValue)
|
|
|
|
|| in_array($itemValue, $this->getDefaultItems());
|
|
|
|
$itemDisabled = $this->isDisabled()
|
|
|
|
|| in_array($itemValue, $this->getDisabledItems());
|
|
|
|
$options[] = new ArrayData(array(
|
|
|
|
'Title' => $title,
|
|
|
|
'Value' => $itemValue,
|
|
|
|
'Selected' => $itemSelected,
|
|
|
|
'Disabled' => $itemDisabled,
|
|
|
|
));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-02-13 05:35:39 +01:00
|
|
|
$options = new ArrayList($options);
|
|
|
|
$this->extend('updateGetOptions', $options);
|
|
|
|
return $options;
|
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(
|
2015-02-13 05:35:39 +01:00
|
|
|
'multiple' => 'true',
|
|
|
|
'size' => $this->getSize(),
|
|
|
|
'name' => $this->getName() . '[]'
|
2011-12-22 13:10:57 +01:00
|
|
|
)
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
/**
|
2015-02-13 05:35:39 +01:00
|
|
|
* Get the size of this dropdown in rows.
|
2012-03-05 10:55:09 +01:00
|
|
|
*
|
2015-02-13 05:35:39 +01:00
|
|
|
* @return integer
|
2012-03-05 10:55:09 +01:00
|
|
|
*/
|
2015-02-13 05:35:39 +01:00
|
|
|
public function getSize() {
|
|
|
|
return $this->size;
|
2012-03-05 10:55:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-13 05:35:39 +01:00
|
|
|
* Sets the size of this dropdown in rows.
|
|
|
|
*
|
|
|
|
* @param int $size The height in rows (e.g. 3)
|
|
|
|
* @return $this Self reference
|
2012-03-05 10:55:09 +01:00
|
|
|
*/
|
2015-02-13 05:35:39 +01:00
|
|
|
public function setSize($size) {
|
|
|
|
$this->size = $size;
|
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
|
2015-02-13 05:35:39 +01:00
|
|
|
* @return $this Self reference
|
2012-03-05 12:27:55 +01:00
|
|
|
*/
|
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
|
|
|
/**
|
2015-02-13 05:35:39 +01:00
|
|
|
* @return array
|
2012-03-05 12:27:55 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getDisabledItems() {
|
2012-03-05 12:27:55 +01:00
|
|
|
return $this->disabledItems;
|
|
|
|
}
|
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|