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.
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-basic
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class ListboxField extends DropdownField {
|
|
|
|
protected $source;
|
|
|
|
protected $size;
|
|
|
|
protected $multiple;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new dropdown field.
|
|
|
|
* @param name The field name
|
|
|
|
* @param title The field title
|
|
|
|
* @param source An map of the dropdown items
|
|
|
|
* @param value You can pass an array of values or a single value like a drop down to be selected
|
|
|
|
* @param form The parent form
|
|
|
|
*/
|
|
|
|
function __construct($name, $title = "", $source = array(), $value = array(), $size = null, $multiple = null, $form = null) {
|
|
|
|
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>";
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Sets the number of rows high this field should be
|
|
|
|
*/
|
|
|
|
function setSize($i){
|
|
|
|
$this->size = $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets this field to have a muliple select attribute
|
|
|
|
*/
|
|
|
|
function setMultiple($bool){
|
|
|
|
$this->multiple = $bool;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|