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.
|
2010-10-15 05:55:22 +02:00
|
|
|
*
|
|
|
|
* <b>Usage</b>
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* new ListboxField(
|
|
|
|
* $name = "pickanumber",
|
|
|
|
* $title = "Pick a number",
|
|
|
|
* $source = array(
|
|
|
|
* "1" => "one",
|
|
|
|
* "2" => "two",
|
|
|
|
* "3" => "three"
|
|
|
|
* ),
|
|
|
|
* $value = 1
|
|
|
|
* )
|
|
|
|
* </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
|
|
|
|
*
|
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?
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $multiple = false;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new dropdown field.
|
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
|
|
|
|
*/
|
2009-06-02 01:37:03 +02:00
|
|
|
function __construct($name, $title = '', $source = array(), $value = '', $size = null, $multiple = false, $form = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if($size) $this->size = $size;
|
|
|
|
if($multiple) $this->multiple = $multiple;
|
|
|
|
parent::__construct($name, $title, $source, $value, $form);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a <select> tag containing all the appropriate <option> tags
|
|
|
|
*/
|
|
|
|
function Field() {
|
2008-09-30 05:21:28 +02:00
|
|
|
$size = '';
|
|
|
|
$multiple = '';
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->size) $size = "size=\"$this->size\"";
|
2008-09-30 05:21:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->multiple) {
|
|
|
|
$multiple = "multiple=\"multiple\"";
|
|
|
|
$this->name .= '[]';
|
|
|
|
}
|
|
|
|
|
2008-10-02 12:09:29 +02:00
|
|
|
$options = "";
|
|
|
|
|
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) {
|
2007-07-19 12:40:28 +02:00
|
|
|
// Loop through the array of values to find out if this value is selected.
|
2008-10-02 12:09:29 +02:00
|
|
|
$selected = "";
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($this->value as $v){
|
2008-10-02 12:09:29 +02:00
|
|
|
if($value == $v) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$selected = " selected=\"selected\"";
|
|
|
|
break;
|
2008-10-02 12:09:29 +02:00
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
$options .= "<option$selected value=\"$value\">$title</option>\n";
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
// 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) {
|
|
|
|
$selected = $value == $this->value ? " selected=\"selected\"" : "";
|
|
|
|
$options .= "<option$selected value=\"$value\">$title</option>";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$id = $this->id();
|
|
|
|
return "<select $size $multiple name=\"$this->name\" id=\"$id\">$options</select>";
|
|
|
|
}
|
2009-06-02 01:37:03 +02:00
|
|
|
|
2007-07-19 12:40:28 +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
|
|
|
*/
|
2009-06-02 01:37:03 +02:00
|
|
|
function setSize($size) {
|
|
|
|
$this->size = $size;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-06-02 01:37:03 +02:00
|
|
|
* Sets this field to have a muliple select attribute
|
|
|
|
* @param boolean $bool
|
|
|
|
*/
|
|
|
|
function setMultiple($bool) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->multiple = $bool;
|
|
|
|
}
|
2009-06-02 01:37:03 +02:00
|
|
|
|
2011-08-29 14:31:03 +02:00
|
|
|
function setSource($source) {
|
|
|
|
if($source) {
|
|
|
|
$hasCommas = array_filter(array_keys($source), create_function('$key', 'return strpos($key, ",") !== FALSE;'));
|
|
|
|
if($hasCommas) {
|
|
|
|
throw new InvalidArgumentException('No commas allowed in $source keys');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::setSource($source);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
function dataValue() {
|
|
|
|
if($this->value && $this->multiple && is_array($this->value)) {
|
|
|
|
return implode(',', $this->value);
|
|
|
|
} else {
|
|
|
|
return parent::dataValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setValue($val) {
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
if($diff = array_diff($parts, array_keys($this->source))) {
|
|
|
|
throw new InvalidArgumentException(sprintf(
|
|
|
|
'Invalid keys "%s" in value array for multiple=true',
|
|
|
|
Convert::raw2xml(implode(',', $diff))
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::setValue($parts);
|
|
|
|
} else {
|
|
|
|
if(!in_array($val, array_keys($this->source))) {
|
|
|
|
throw new InvalidArgumentException(sprintf(
|
|
|
|
'Invalid value "%s" for multiple=true',
|
|
|
|
Convert::raw2xml($val)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::setValue($val);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
parent::setValue($val);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
?>
|