silverstripe-framework/forms/DropdownField.php

355 lines
8.2 KiB
PHP
Raw Normal View History

<?php
/**
* Dropdown field, created from a <select> tag.
2014-08-15 08:53:05 +02:00
*
* <b>Setting a $has_one relation</b>
2014-08-15 08:53:05 +02:00
*
* 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',
* );
2014-08-15 08:53:05 +02:00
*
* public function getCMSFields() {
* $fields = parent::getCMSFields();
* $field = DropdownField::create('GalleryID', 'Gallery', Gallery::get()->map('ID', 'Title'))
* ->setEmptyString('(Select one)');
* $fields->addFieldToTab('Root.Content', $field, 'Content');
* </code>
2014-08-15 08:53:05 +02:00
*
* As you see, you need to put "GalleryID", rather than "Gallery" here.
2014-08-15 08:53:05 +02:00
*
* <b>Populate with Array</b>
2014-08-15 08:53:05 +02:00
*
* Example model defintion:
* <code>
* class MyObject extends DataObject {
* static $db = array(
* 'Country' => "Varchar(100)"
* );
* }
* </code>
2014-08-15 08:53:05 +02:00
*
* Example instantiation:
* <code>
* DropdownField::create(
* 'Country',
* 'Country',
* array(
* 'NZ' => 'New Zealand',
* 'US' => 'United States',
* 'GEM'=> 'Germany'
* )
* );
* </code>
2014-08-15 08:53:05 +02:00
*
* <b>Populate with Enum-Values</b>
2014-08-15 08:53:05 +02:00
*
* You can automatically create a map of possible values from an {@link Enum} database column.
2014-08-15 08:53:05 +02:00
*
* Example model definition:
* <code>
* class MyObject extends DataObject {
* static $db = array(
* 'Country' => "Enum('New Zealand,United States,Germany','New Zealand')"
* );
* }
* </code>
2014-08-15 08:53:05 +02:00
*
* Field construction:
* <code>
* DropdownField::create(
* 'Country',
* 'Country',
* singleton('MyObject')->dbObject('Country')->enumValues()
* );
* </code>
2014-08-15 08:53:05 +02:00
*
* <b>Disabling individual items</b>
2014-08-15 08:53:05 +02:00
*
* Individual items can be disabled by feeding their array keys to setDisabledItems.
2014-08-15 08:53:05 +02:00
*
* <code>
* $DrDownField->setDisabledItems( array( 'US', 'GEM' ) );
* </code>
2014-08-15 08:53:05 +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
2014-08-15 08:53:05 +02:00
*
* @package forms
* @subpackage fields-basic
*/
class DropdownField extends FormField {
/**
* @var array|ArrayAccess $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.
*/
protected $source;
2014-08-15 08:53:05 +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;
2014-08-15 08:53:05 +02:00
/**
* @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;
2014-08-15 08:53:05 +02:00
/**
* @var string $emptyString The title shown for an empty default selection,
* e.g. "Select...".
*/
protected $emptyString = '';
2014-08-15 08:53:05 +02:00
/**
* @var array $disabledItems The keys for items that should be disabled (greyed out) in the dropdown
*/
protected $disabledItems = array();
2014-08-15 08:53:05 +02:00
/**
* @param string $name The field name
* @param string $title The field title
* @param array|ArrayAccess $source A map of the dropdown items
* @param string $value The current value
* @param Form $form The parent form
*/
public function __construct($name, $title=null, $source=array(), $value='', $form=null) {
$this->setSource($source);
parent::__construct($name, ($title===null) ? $name : $title, $value, $form);
}
2014-08-15 08:53:05 +02:00
2015-06-20 12:11:08 +02:00
/**
* @param array $properties
* @return HTMLText
*/
public function Field($properties = array()) {
$source = $this->getSource();
$options = array();
if ($this->getHasEmptyDefault()) {
$selected = ($this->value === '' || $this->value === null);
$disabled = (in_array('', $this->disabledItems, true)) ? 'disabled' : false;
$options[] = new ArrayData(array(
'Value' => '',
'Title' => $this->getEmptyString(),
'Selected' => $selected,
'Disabled' => $disabled
));
}
if ($source) {
foreach($source as $value => $title) {
$selected = false;
if($value === '' && ($this->value === '' || $this->value === null)) {
$selected = true;
} else {
// check against value, fallback to a type check comparison when !value
if($value) {
$selected = ($value == $this->value);
} else {
$selected = ($value === $this->value) || (((string) $value) === ((string) $this->value));
}
$this->isSelected = $selected;
}
2014-08-15 08:53:05 +02:00
$disabled = false;
if(in_array($value, $this->disabledItems) && $title != $this->emptyString ){
$disabled = 'disabled';
}
$options[] = new ArrayData(array(
'Title' => $title,
'Value' => $value,
'Selected' => $selected,
'Disabled' => $disabled,
));
}
}
$properties = array_merge($properties, array(
'Options' => new ArrayList($options)
));
return parent::Field($properties);
}
2014-08-15 08:53:05 +02:00
/**
2014-08-15 08:53:05 +02:00
* Mark certain elements as disabled, regardless of the
* {@link setDisabled()} settings.
2014-08-15 08:53:05 +02:00
*
* @param array $items Collection of array keys, as defined in the $source array
*/
public function setDisabledItems($items) {
$this->disabledItems = $items;
return $this;
}
2014-08-15 08:53:05 +02:00
/**
* @return array
*/
public function getDisabledItems() {
return $this->disabledItems;
}
/**
* @return array
*/
public function getAttributes() {
return array_merge(
parent::getAttributes(),
array(
2014-08-15 08:53:05 +02:00
'type' => null,
'value' => null
)
);
}
/**
* @return boolean
*/
public function isSelected() {
return $this->isSelected;
}
/**
* Gets the source array including any empty default values.
2014-08-15 08:53:05 +02:00
*
* @return array|ArrayAccess
*/
public function getSource() {
return $this->source;
}
/**
* @param array|ArrayAccess $source
*/
public function setSource($source) {
$this->source = $source;
return $this;
}
2014-08-15 08:53:05 +02:00
/**
* @param boolean $bool
*/
public function setHasEmptyDefault($bool) {
$this->hasEmptyDefault = $bool;
return $this;
}
2014-08-15 08:53:05 +02:00
/**
* @return boolean
*/
public function getHasEmptyDefault() {
return $this->hasEmptyDefault;
}
/**
* Set the default selection label, e.g. "select...".
2014-08-15 08:53:05 +02:00
*
* Defaults to an empty string. Automatically sets {@link $hasEmptyDefault}
* to true.
*
* @param string $str
*/
public function setEmptyString($str) {
$this->setHasEmptyDefault(true);
$this->emptyString = $str;
return $this;
}
/**
* @return string
*/
public function getEmptyString() {
return $this->emptyString;
}
/**
* @return LookupField
*/
public function performReadonlyTransformation() {
$field = $this->castedCopy('LookupField');
$field->setSource($this->getSource());
$field->setReadonly(true);
2014-08-15 08:53:05 +02:00
return $field;
}
/**
* Get the source of this field as an array
*
* @return array
*/
public function getSourceAsArray()
{
$source = $this->getSource();
if (is_array($source)) {
return $source;
} else {
$sourceArray = array();
foreach ($source as $key => $value) {
$sourceArray[$key] = $value;
}
}
return $sourceArray;
}
/**
* Validate this field
*
* @param Validator $validator
* @return bool
*/
public function validate($validator) {
$source = $this->getSourceAsArray();
$disabled = $this->getDisabledItems();
if (!array_key_exists($this->value, $source) || in_array($this->value, $disabled)) {
if ($this->getHasEmptyDefault() && !$this->value) {
return true;
}
$validator->validationError(
$this->name,
_t(
'DropdownField.SOURCE_VALIDATION',
"Please select a value within the list provided. {value} is not a valid option",
array('value' => $this->value)
),
"validation"
);
return false;
}
return true;
}
/**
* Returns another instance of this field, but "cast" to a different class.
*
* @see FormField::castedCopy()
*
* @param String $classOrCopy
* @return FormField
*/
public function castedCopy($classOrCopy) {
$field = parent::castedCopy($classOrCopy);
$field->setHasEmptyDefault($this->getHasEmptyDefault());
return $field;
}
}