2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-01-09 05:18:36 +01:00
|
|
|
/**
|
2017-11-14 10:58:18 +01:00
|
|
|
* Grouped dropdown, using optgroup tags.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-01-20 09:58:04 +01:00
|
|
|
* $source parameter (from DropdownField) must be a two dimensional array.
|
2017-11-14 10:58:18 +01:00
|
|
|
* The first level of the array is used for the optgroup, and the second
|
|
|
|
* level are the options for each group.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2017-11-14 10:58:18 +01:00
|
|
|
* Returns a select tag containing all the appropriate option tags, with
|
|
|
|
* optgroup tags around the option tags as required.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <b>Usage</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <code>
|
|
|
|
* new GroupedDropdownField(
|
|
|
|
* $name = "dropdown",
|
|
|
|
* $title = "Simple Grouped Dropdown",
|
|
|
|
* $source = array(
|
|
|
|
* "numbers" => array(
|
|
|
|
* "1" => "1",
|
|
|
|
* "2" => "2",
|
|
|
|
* "3" => "3",
|
|
|
|
* "4" => "4"
|
|
|
|
* ),
|
|
|
|
* "letters" => array(
|
|
|
|
* "1" => "A",
|
|
|
|
* "2" => "B",
|
|
|
|
* "3" => "C",
|
|
|
|
* "4" => "D",
|
|
|
|
* "5" => "E",
|
|
|
|
* "6" => "F"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-10-17 11:06:02 +02:00
|
|
|
* <b>Disabling individual items</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-10-17 11:06:02 +02:00
|
|
|
* <code>
|
2014-08-15 08:53:05 +02:00
|
|
|
* $groupedDrDownField->setDisabledItems(
|
2012-10-17 11:06:02 +02:00
|
|
|
* array(
|
|
|
|
* "numbers" => array(
|
|
|
|
* "1" => "1",
|
|
|
|
* "3" => "3"
|
|
|
|
* ),
|
|
|
|
* "letters" => array(
|
|
|
|
* "3" => "C"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-basic
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class GroupedDropdownField extends DropdownField {
|
2008-01-20 09:58:04 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Field($properties = array()) {
|
2017-03-05 23:11:29 +01:00
|
|
|
$options = array();
|
|
|
|
foreach($this->getSource() as $valueOrGroupTitle => $titleOrGroup) {
|
|
|
|
// Option group
|
|
|
|
if(is_array($titleOrGroup)) {
|
|
|
|
$groupOptions = array();
|
|
|
|
foreach($titleOrGroup as $optionValue => $optionTitle) {
|
|
|
|
// Check sub-option disabled status
|
|
|
|
$disabled = false;
|
|
|
|
if( array_key_exists($valueOrGroupTitle, $this->disabledItems)
|
|
|
|
&& is_array($this->disabledItems[$valueOrGroupTitle])
|
|
|
|
&& in_array($optionValue, $this->disabledItems[$valueOrGroupTitle]) ){
|
|
|
|
$disabled = true;
|
2012-10-17 11:06:02 +02:00
|
|
|
}
|
2017-03-05 23:11:29 +01:00
|
|
|
$groupOptions[] = array(
|
|
|
|
'Value' => $optionValue,
|
|
|
|
'Disabled' => $disabled,
|
|
|
|
'Title' => $optionTitle,
|
|
|
|
'Selected' => $optionValue == $this->value
|
|
|
|
);
|
2008-08-26 01:32:12 +02:00
|
|
|
}
|
2017-03-05 23:11:29 +01:00
|
|
|
|
|
|
|
$options[] = array(
|
|
|
|
'Title' => $valueOrGroupTitle,
|
|
|
|
'Options' => new ArrayList($groupOptions),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Single option
|
|
|
|
$disabled = in_array($valueOrGroupTitle, $this->disabledItems);
|
|
|
|
|
|
|
|
$options[] = array(
|
|
|
|
'Value' => $valueOrGroupTitle,
|
|
|
|
'Disabled' => $disabled,
|
|
|
|
'Title' => $titleOrGroup,
|
|
|
|
'Selected' => $valueOrGroupTitle == $this->value
|
|
|
|
);
|
2008-08-26 01:32:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:11:29 +01:00
|
|
|
// Render
|
|
|
|
$this->extend('onBeforeRender', $this);
|
|
|
|
$properties = array_merge($properties, array(
|
|
|
|
'Options' => new ArrayList($options)
|
|
|
|
));
|
|
|
|
return $this
|
|
|
|
->customise($properties)
|
|
|
|
->renderWith($this->getTemplates());
|
2008-01-20 09:58:04 +01:00
|
|
|
}
|
2012-08-16 23:31:13 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Type() {
|
2012-08-16 23:31:13 +02:00
|
|
|
return 'groupeddropdown dropdown';
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-02-21 04:15:03 +01:00
|
|
|
/**
|
2015-05-22 21:55:54 +02:00
|
|
|
* Validate this field
|
|
|
|
*
|
|
|
|
* @param Validator $validator
|
|
|
|
* @return bool
|
2015-02-21 04:15:03 +01:00
|
|
|
*/
|
2015-05-11 02:32:00 +02:00
|
|
|
public function validate($validator) {
|
2015-05-22 21:55:54 +02:00
|
|
|
$valid = false;
|
|
|
|
$source = $this->getSourceAsArray();
|
2015-05-22 22:11:01 +02:00
|
|
|
$disabled = $this->getDisabledItems();
|
2015-05-22 21:55:54 +02:00
|
|
|
|
|
|
|
if ($this->value) {
|
|
|
|
foreach ($source as $value => $title) {
|
2015-05-22 22:11:01 +02:00
|
|
|
if (is_array($title) && array_key_exists($this->value, $title)) {
|
|
|
|
// Check that the set value is not in the list of disabled items
|
|
|
|
if (!isset($disabled[$value]) || !in_array($this->value, $disabled[$value])) {
|
|
|
|
$valid = true;
|
|
|
|
}
|
|
|
|
// Check that the value matches and is not disabled
|
|
|
|
} elseif($this->value == $value && !in_array($this->value, $disabled)) {
|
2015-05-22 21:55:54 +02:00
|
|
|
$valid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ($this->getHasEmptyDefault()) {
|
|
|
|
$valid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$valid) {
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2015-02-21 04:15:03 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|