2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Forms;
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\ArrayData;
|
|
|
|
|
2008-01-09 05:18:36 +01:00
|
|
|
/**
|
2017-11-14 11:02:24 +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 11:02:24 +01:00
|
|
|
* The first level of the array is used for the optgroup, and the second
|
2008-01-20 09:58:04 +01:00
|
|
|
* level are the <options> for each group.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2017-11-14 11:02:24 +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(
|
2016-11-29 00:31:16 +01:00
|
|
|
* "1" => "1",
|
|
|
|
* "2" => "2",
|
|
|
|
* "3" => "3",
|
|
|
|
* "4" => "4"
|
|
|
|
* ),
|
2010-10-15 05:55:22 +02:00
|
|
|
* "letters" => array(
|
2016-11-29 00:31:16 +01:00
|
|
|
* "1" => "A",
|
|
|
|
* "2" => "B",
|
|
|
|
* "3" => "C",
|
|
|
|
* "4" => "D",
|
|
|
|
* "5" => "E",
|
|
|
|
* "6" => "F"
|
|
|
|
* )
|
2010-10-15 05:55:22 +02:00
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* </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
|
|
|
*
|
2015-02-13 05:35:39 +01:00
|
|
|
* Unlike the source, disabled items are specified in the same way as
|
|
|
|
* normal DropdownFields, using a single value list. Don't pass in grouped
|
|
|
|
* values here.
|
|
|
|
*
|
2012-10-17 11:06:02 +02:00
|
|
|
* <code>
|
2015-02-13 05:35:39 +01:00
|
|
|
* // Disables first and third option in each group
|
|
|
|
* $groupedDrDownField->setDisabledItems(array("1", "3"))
|
2012-10-17 11:06:02 +02:00
|
|
|
* </code>
|
2008-01-09 05:18:36 +01:00
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
class GroupedDropdownField extends DropdownField
|
|
|
|
{
|
2008-01-20 09:58:04 +01:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
// TODO remove this when GroupedDropdownField is implemented
|
2017-07-03 02:21:27 +02:00
|
|
|
// This should be one of SCHEMA_DATA_TYPE_* constants instead
|
|
|
|
/** @skipUpgrade */
|
2016-11-29 00:31:16 +01:00
|
|
|
protected $schemaDataType = 'GroupedDropdownField';
|
2016-09-07 05:35:47 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Build a potentially nested fieldgroup
|
|
|
|
*
|
|
|
|
* @param mixed $valueOrGroup Value of item, or title of group
|
|
|
|
* @param string|array $titleOrOptions Title of item, or options in grouip
|
|
|
|
* @return ArrayData Data for this item
|
|
|
|
*/
|
|
|
|
protected function getFieldOption($valueOrGroup, $titleOrOptions)
|
|
|
|
{
|
|
|
|
// Return flat option
|
|
|
|
if (!is_array($titleOrOptions)) {
|
|
|
|
return parent::getFieldOption($valueOrGroup, $titleOrOptions);
|
|
|
|
}
|
2015-02-13 05:35:39 +01:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
// Build children from options list
|
|
|
|
$options = new ArrayList();
|
|
|
|
foreach ($titleOrOptions as $childValue => $childTitle) {
|
|
|
|
$options->push($this->getFieldOption($childValue, $childTitle));
|
|
|
|
}
|
2008-08-26 01:32:12 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
return new ArrayData(array(
|
|
|
|
'Title' => $valueOrGroup,
|
|
|
|
'Options' => $options
|
|
|
|
));
|
|
|
|
}
|
2012-08-16 23:31:13 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
public function Type()
|
|
|
|
{
|
|
|
|
return 'groupeddropdown dropdown';
|
|
|
|
}
|
2015-02-21 04:15:03 +01:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
public function getSourceValues()
|
|
|
|
{
|
|
|
|
// Flatten values
|
|
|
|
$values = array();
|
|
|
|
$source = $this->getSource();
|
|
|
|
array_walk_recursive(
|
|
|
|
$source,
|
|
|
|
// Function to extract value from array key
|
|
|
|
function ($title, $value) use (&$values) {
|
|
|
|
$values[] = $value;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return $values;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|