2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Dropdown field, created from a <select> tag.
|
2010-10-15 05:55:22 +02:00
|
|
|
*
|
|
|
|
* <b>Setting a $has_one relation</b>
|
|
|
|
*
|
|
|
|
* Using here an example of an art gallery, with Exhibition pages,
|
|
|
|
* each of which has a Gallery they belong to. The Gallery class is also user-defined.
|
|
|
|
* <code>
|
|
|
|
* static $has_one = array(
|
|
|
|
* 'Gallery' => 'Gallery',
|
|
|
|
* );
|
|
|
|
*
|
|
|
|
* public function getCMSFields() {
|
|
|
|
* $fields = parent::getCMSFields();
|
2012-05-28 11:13:42 +02:00
|
|
|
* $field = new DropdownField('GalleryID', 'Gallery', Gallery::get()->map('ID', 'Title'));
|
2012-03-20 03:47:22 +01:00
|
|
|
* $field->setEmptyString('(Select one)');
|
|
|
|
* $fields->addFieldToTab('Root.Content', $field, 'Content');
|
2010-10-15 05:55:22 +02:00
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* As you see, you need to put "GalleryID", rather than "Gallery" here.
|
|
|
|
*
|
|
|
|
* <b>Populate with Array</b>
|
|
|
|
*
|
|
|
|
* Example model defintion:
|
|
|
|
* <code>
|
|
|
|
* class MyObject extends DataObject {
|
|
|
|
* static $db = array(
|
|
|
|
* 'Country' => "Varchar(100)"
|
|
|
|
* );
|
2012-05-23 12:45:04 +02:00
|
|
|
* }
|
2010-10-15 05:55:22 +02:00
|
|
|
* </code>
|
|
|
|
*
|
2012-05-23 12:45:04 +02:00
|
|
|
* Example instantiation:
|
2010-10-15 05:55:22 +02:00
|
|
|
* <code>
|
|
|
|
* new DropdownField(
|
|
|
|
* 'Country',
|
|
|
|
* 'Country',
|
|
|
|
* array(
|
|
|
|
* 'NZ' => 'New Zealand',
|
|
|
|
* 'US' => 'United States'
|
|
|
|
* 'GEM'=> 'Germany'
|
|
|
|
* )
|
|
|
|
* );
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* <b>Populate with Enum-Values</b>
|
|
|
|
*
|
|
|
|
* You can automatically create a map of possible values from an {@link Enum} database column.
|
|
|
|
*
|
|
|
|
* Example model definition:
|
|
|
|
* <code>
|
|
|
|
* class MyObject extends DataObject {
|
|
|
|
* static $db = array(
|
|
|
|
* 'Country' => "Enum('New Zealand,United States,Germany','New Zealand')"
|
|
|
|
* );
|
2012-05-23 12:45:04 +02:00
|
|
|
* }
|
2010-10-15 05:55:22 +02:00
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* Field construction:
|
|
|
|
* <code>
|
|
|
|
* new DropdownField(
|
|
|
|
* 'Country',
|
|
|
|
* 'Country',
|
|
|
|
* singleton('MyObject')->dbObject('Country')->enumValues()
|
|
|
|
* );
|
|
|
|
* </code>
|
|
|
|
*
|
2010-10-19 00:50:48 +02:00
|
|
|
* @see CheckboxSetField for multiple selections through checkboxes instead.
|
|
|
|
* @see ListboxField for a single <select> box (with single or multiple selections).
|
|
|
|
* @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 DropdownField extends FormField {
|
2011-12-21 17:35:42 +01:00
|
|
|
|
2008-10-15 14:39:09 +02:00
|
|
|
/**
|
|
|
|
* @var boolean $source Associative or numeric array of all dropdown items,
|
|
|
|
* with array key as the submitted field value, and the array value as a
|
|
|
|
* natural language description shown in the interface element.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $source;
|
2008-10-15 14:39:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean $isSelected Determines if the field was selected
|
|
|
|
* at the time it was rendered, so if {@link $value} matches on of the array
|
|
|
|
* values specified in {@link $source}
|
|
|
|
*/
|
|
|
|
protected $isSelected;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean $hasEmptyDefault Show the first <option> element as
|
|
|
|
* empty (not having a value), with an optional label defined through
|
|
|
|
* {@link $emptyString}. By default, the <select> element will be
|
|
|
|
* rendered with the first option from {@link $source} selected.
|
|
|
|
*/
|
|
|
|
protected $hasEmptyDefault = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string $emptyString The title shown for an empty default selection,
|
|
|
|
* e.g. "Select...".
|
|
|
|
*/
|
|
|
|
protected $emptyString = '';
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new dropdown field.
|
2007-08-27 07:13:43 +02:00
|
|
|
* @param $name The field name
|
|
|
|
* @param $title The field title
|
|
|
|
* @param $source An map of the dropdown items
|
|
|
|
* @param $value The current value
|
|
|
|
* @param $form The parent form
|
2008-10-15 14:39:09 +02:00
|
|
|
* @param $emptyString mixed Add an empty selection on to of the {@link $source}-Array
|
2007-07-19 12:40:28 +02:00
|
|
|
* (can also be boolean, which results in an empty string)
|
2012-05-23 12:45:04 +02:00
|
|
|
* Argument is deprecated in 3.1, please use {@link setEmptyString()} and/or {@link setHasEmptyDefault(true)} instead.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($name, $title = null, $source = array(), $value = '', $form = null, $emptyString = null) {
|
2011-08-29 14:31:03 +02:00
|
|
|
$this->setSource($source);
|
2012-05-23 12:45:04 +02:00
|
|
|
|
|
|
|
if($emptyString === true) {
|
2012-07-13 11:37:35 +02:00
|
|
|
Deprecation::notice('3.1', 'Please use setHasEmptyDefault(true) instead of passing a boolean true $emptyString argument', Deprecation::SCOPE_GLOBAL);
|
2012-05-23 12:45:04 +02:00
|
|
|
}
|
|
|
|
if(is_string($emptyString)) {
|
2012-07-13 11:37:35 +02:00
|
|
|
Deprecation::notice('3.1', 'Please use setEmptyString() instead of passing a string $emptyString argument.', Deprecation::SCOPE_GLOBAL);
|
2012-05-23 12:45:04 +02:00
|
|
|
}
|
|
|
|
|
2008-10-15 14:39:09 +02:00
|
|
|
if($emptyString) $this->setHasEmptyDefault(true);
|
|
|
|
if(is_string($emptyString)) $this->setEmptyString($emptyString);
|
2012-05-23 12:45:04 +02:00
|
|
|
|
2008-08-06 05:43:48 +02:00
|
|
|
parent::__construct($name, ($title===null) ? $name : $title, $value, $form);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Field($properties = array()) {
|
2008-10-15 14:39:09 +02:00
|
|
|
$source = $this->getSource();
|
2011-03-23 05:12:25 +01:00
|
|
|
$options = array();
|
2008-10-16 01:25:55 +02:00
|
|
|
if($source) {
|
2011-03-23 05:12:25 +01:00
|
|
|
// SQLMap needs this to add an empty value to the options
|
2009-01-05 07:19:48 +01:00
|
|
|
if(is_object($source) && $this->emptyString) {
|
2011-03-23 05:12:25 +01:00
|
|
|
$options[] = new ArrayData(array(
|
2012-03-19 05:06:51 +01:00
|
|
|
'Value' => '',
|
|
|
|
'Title' => $this->emptyString,
|
2011-03-23 05:12:25 +01:00
|
|
|
));
|
2009-01-05 07:19:48 +01:00
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2008-10-16 01:25:55 +02:00
|
|
|
foreach($source as $value => $title) {
|
2011-03-23 05:12:25 +01:00
|
|
|
$selected = false;
|
2009-04-29 03:20:24 +02:00
|
|
|
if($value === '' && ($this->value === '' || $this->value === null)) {
|
2011-03-23 05:12:25 +01:00
|
|
|
$selected = true;
|
2009-04-29 03:20:24 +02:00
|
|
|
} else {
|
2011-03-23 05:12:25 +01:00
|
|
|
// check against value, fallback to a type check comparison when !value
|
|
|
|
$selected = ($value) ? $value == $this->value : $value === $this->value;
|
|
|
|
$this->isSelected = $selected;
|
2008-10-16 01:25:55 +02:00
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
|
|
|
$options[] = new ArrayData(array(
|
|
|
|
'Title' => $title,
|
|
|
|
'Value' => $value,
|
|
|
|
'Selected' => $selected,
|
|
|
|
));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2008-10-15 23:44:38 +02:00
|
|
|
|
2011-03-23 05:12:25 +01:00
|
|
|
$properties = array_merge($properties, array('Options' => new ArrayList($options)));
|
|
|
|
|
2012-04-14 07:32:29 +02:00
|
|
|
return parent::Field($properties);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-03-23 05:12:25 +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(),
|
2012-03-17 09:16:25 +01:00
|
|
|
array('type' => null, 'value' => null)
|
2011-12-22 13:10:57 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2008-10-15 14:39:09 +02:00
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function isSelected() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->isSelected;
|
|
|
|
}
|
2012-05-23 12:45:04 +02:00
|
|
|
|
2008-10-15 14:39:09 +02:00
|
|
|
/**
|
|
|
|
* Gets the source array including any empty default values.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getSource() {
|
2008-10-15 23:44:38 +02:00
|
|
|
if(is_array($this->source) && $this->getHasEmptyDefault()) {
|
2012-05-23 12:45:04 +02:00
|
|
|
return array('' => $this->emptyString) + (array) $this->source;
|
2008-10-15 14:39:09 +02:00
|
|
|
} else {
|
2008-10-15 23:44:38 +02:00
|
|
|
return $this->source;
|
2008-10-15 14:39:09 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-05-23 12:45:04 +02:00
|
|
|
|
2008-10-15 14:39:09 +02:00
|
|
|
/**
|
|
|
|
* @param array $source
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setSource($source) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->source = $source;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-10-15 14:39:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param boolean $bool
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setHasEmptyDefault($bool) {
|
2008-10-15 14:39:09 +02:00
|
|
|
$this->hasEmptyDefault = $bool;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2008-10-15 14:39:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getHasEmptyDefault() {
|
2008-10-15 14:39:09 +02:00
|
|
|
return $this->hasEmptyDefault;
|
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2008-10-15 14:39:09 +02:00
|
|
|
/**
|
|
|
|
* Set the default selection label, e.g. "select...".
|
|
|
|
* Defaults to an empty string. Automatically sets
|
|
|
|
* {@link $hasEmptyDefault} to true.
|
2012-05-23 12:45:04 +02:00
|
|
|
*
|
2008-10-15 14:39:09 +02:00
|
|
|
* @param string $str
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setEmptyString($str) {
|
2008-10-15 14:39:09 +02:00
|
|
|
$this->setHasEmptyDefault(true);
|
|
|
|
$this->emptyString = $str;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2008-10-15 14:39:09 +02:00
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2008-10-15 14:39:09 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getEmptyString() {
|
2008-10-15 14:39:09 +02:00
|
|
|
return $this->emptyString;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function performReadonlyTransformation() {
|
2012-09-14 14:33:59 +02:00
|
|
|
$field = new LookupField($this->name, $this->title, $this->getSource());
|
2007-07-19 12:40:28 +02:00
|
|
|
$field->setValue($this->value);
|
|
|
|
$field->setForm($this->form);
|
2008-08-12 04:58:48 +02:00
|
|
|
$field->setReadonly(true);
|
2007-07-19 12:40:28 +02:00
|
|
|
return $field;
|
|
|
|
}
|
2012-03-19 05:06:51 +01:00
|
|
|
}
|