2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Dropdown field, created from a <select> tag.
|
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 {
|
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 $disabled
|
|
|
|
*/
|
|
|
|
protected $disabled;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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)
|
2008-10-15 14:39:09 +02:00
|
|
|
* Argument is deprecated in 2.3, please use {@link setHasEmptyDefault()} and {@link setEmptyString()} instead.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-08-06 05:43:48 +02:00
|
|
|
function __construct($name, $title = null, $source = array(), $value = "", $form = null, $emptyString = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->source = $source;
|
2008-10-15 14:39:09 +02:00
|
|
|
|
|
|
|
if($emptyString) $this->setHasEmptyDefault(true);
|
|
|
|
if(is_string($emptyString)) $this->setEmptyString($emptyString);
|
2007-07-19 12:40:28 +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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-09-29 12:31:06 +02:00
|
|
|
* Returns a <select> tag containing all the appropriate <option> tags.
|
|
|
|
* Makes use of {@link FormField->createTag()} to generate the <select>
|
|
|
|
* tag and option elements inside is as the content of the <select>.
|
|
|
|
*
|
|
|
|
* @return string HTML tag for this dropdown field
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function Field() {
|
|
|
|
$options = '';
|
2008-09-29 12:31:06 +02:00
|
|
|
|
2008-10-15 14:39:09 +02:00
|
|
|
$source = $this->getSource();
|
|
|
|
foreach($source as $value => $title) {
|
|
|
|
$selected = ($value == $this->value) ? 'selected' : null;
|
2008-08-26 01:32:12 +02:00
|
|
|
if($selected && $this->value != 0) {
|
|
|
|
$this->isSelected = true;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-09-29 12:31:06 +02:00
|
|
|
|
2008-09-30 22:08:17 +02:00
|
|
|
$options .= $this->createTag(
|
|
|
|
'option',
|
|
|
|
array(
|
|
|
|
'selected' => $selected,
|
|
|
|
'value' => $value
|
|
|
|
),
|
|
|
|
$title
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-09-29 12:31:06 +02:00
|
|
|
$attributes = array(
|
|
|
|
'class' => trim($this->extraClass()) ? $this->extraClass() : null,
|
|
|
|
'id' => $this->id(),
|
|
|
|
'name' => $this->name,
|
|
|
|
'disabled' => $this->disabled ? 'disabled' : null,
|
|
|
|
'tabindex' => $this->getTabIndex()
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-09-29 12:31:06 +02:00
|
|
|
return $this->createTag('select', $attributes, $options);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-10-15 14:39:09 +02:00
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function isSelected(){
|
|
|
|
return $this->isSelected;
|
|
|
|
}
|
|
|
|
|
2008-10-15 14:39:09 +02:00
|
|
|
/**
|
|
|
|
* Gets the source array including any empty default values.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function getSource() {
|
2008-10-15 14:39:09 +02:00
|
|
|
if($this->getHasEmptyDefault()) {
|
|
|
|
return array(""=>$this->emptyString) + (array)$this->source;
|
|
|
|
} else {
|
|
|
|
return (array)$this->source;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-10-15 14:39:09 +02:00
|
|
|
/**
|
|
|
|
* @param array $source
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function setSource($source) {
|
|
|
|
$this->source = $source;
|
|
|
|
}
|
2008-10-15 14:39:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param boolean $bool
|
|
|
|
*/
|
|
|
|
function setHasEmptyDefault($bool) {
|
|
|
|
$this->hasEmptyDefault = $bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function getHasEmptyDefault() {
|
|
|
|
return $this->hasEmptyDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the default selection label, e.g. "select...".
|
|
|
|
* Defaults to an empty string. Automatically sets
|
|
|
|
* {@link $hasEmptyDefault} to true.
|
|
|
|
*
|
|
|
|
* @param string $str
|
|
|
|
*/
|
|
|
|
function setEmptyString($str) {
|
|
|
|
$this->setHasEmptyDefault(true);
|
|
|
|
$this->emptyString = $str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function getEmptyString() {
|
|
|
|
return $this->emptyString;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
function performReadonlyTransformation() {
|
|
|
|
$field = new LookupField($this->name, $this->title, $this->source);
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
function extraClass(){
|
|
|
|
$ret = parent::extraClass();
|
|
|
|
if($this->extraClass) $ret .= " $this->extraClass";
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-01-10 01:34:18 +01:00
|
|
|
* Dropdown field with an add button to the right.
|
|
|
|
* The class is originally designed to be used by RelatedDataEditor
|
|
|
|
* However, it can potentially be used as a normal dropdown field with add links in a normal form
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-basic
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class DropdownField_WithAdd extends DropdownField {
|
|
|
|
|
|
|
|
protected $addText, $useExistingText, $addLink, $useExistingLink;
|
|
|
|
public $editLink;
|
|
|
|
|
|
|
|
function __construct($name, $title = "", $source = array(), $addLink=null, $useExistingLink=null, $addText="Add", $useExistingText="Use Existing", $value = "", $form = null){
|
|
|
|
parent::__construct($name, $title, $source, $value, $form);
|
|
|
|
$this->addText = $addText;
|
|
|
|
$this->useExistingText = $useExistingText;
|
|
|
|
$this->addLink = $addLink;
|
|
|
|
$this->useExistingLink = $useExistingLink;
|
|
|
|
}
|
|
|
|
|
|
|
|
function emptyString($string){
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a <select> tag containing all the appropriate <option> tags and with add/useExisting link
|
|
|
|
*/
|
|
|
|
function Field() {
|
|
|
|
|
|
|
|
//Add these js file so that the DropdownField_WithAdd can work alone (in a webpage, rather than CMS).
|
2008-09-29 20:49:55 +02:00
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/prototype.js');
|
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/behaviour.js');
|
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/prototype_improvements.js');
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
Requirements::Javascript(SAPPHIRE_DIR . "/javascript/DropdownField_WithAdd.js");
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
$dropdown = parent::Field();
|
|
|
|
if($this->addLink) $addLink = <<<HTML
|
|
|
|
<a class="addlink link" id="{$this->name}_addLink" href="$this->addLink" style="display: inline; padding-left: 1em; text-decoration: underline;">$this->addText</a>
|
|
|
|
HTML;
|
|
|
|
if($this->useExistingLink) $useExistingLink = <<<HTML
|
|
|
|
<a class="useExistinglink link" id="{$this->name}_useExistingLink" href="$this->useExistingLink" style="display: none; padding-left: 1em; text-decoration: underline;">$this->useExistingText</a>
|
|
|
|
HTML;
|
|
|
|
|
|
|
|
if($this->editLink) $editLink = <<<HTML
|
|
|
|
<a class="editlink" id="{$this->name}_editLink" href="$this->editLink" style="display: inline; padding-left: 1em; text-decoration: underline;">edit</a>
|
|
|
|
HTML;
|
|
|
|
|
|
|
|
return $dropdown . $addLink . $useExistingLink . $editLink;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a class for this special label so that
|
|
|
|
* it can have special styling
|
|
|
|
*/
|
|
|
|
function Title() {
|
|
|
|
$title = parent::Title();
|
2007-11-02 04:15:24 +01:00
|
|
|
if( $title ) {
|
|
|
|
return <<<HTML
|
2007-07-19 12:40:28 +02:00
|
|
|
<span class="keylabel">$title</span>
|
|
|
|
HTML;
|
2007-11-02 04:15:24 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return '';
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|