Completed
Push — 4.6 ( 872004...5e7f0a )
by Guy
42s queued 24s
created

GroupedDropdownField::getSourceValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Forms;
4
5
use SilverStripe\ORM\ArrayLib;
6
use SilverStripe\ORM\ArrayList;
7
use SilverStripe\View\ArrayData;
8
9
/**
10
 * Grouped dropdown, using optgroup tags.
11
 *
12
 * $source parameter (from DropdownField) must be a two dimensional array.
13
 * The first level of the array is used for the optgroup, and the second
14
 * level are the <options> for each group.
15
 *
16
 * Returns a select tag containing all the appropriate option tags, with
17
 * optgroup tags around the option tags as required.
18
 *
19
 * <b>Usage</b>
20
 *
21
 * <code>
22
 * new GroupedDropdownField(
23
 *    $name = "dropdown",
24
 *    $title = "Simple Grouped Dropdown",
25
 *    $source = array(
26
 *       "numbers" => array(
27
 *              "1" => "1",
28
 *              "2" => "2",
29
 *              "3" => "3",
30
 *              "4" => "4"
31
 *          ),
32
 *       "letters" => array(
33
 *              "1" => "A",
34
 *              "2" => "B",
35
 *              "3" => "C",
36
 *              "4" => "D",
37
 *              "5" => "E",
38
 *              "6" => "F"
39
 *          )
40
 *    )
41
 * )
42
 * </code>
43
 *
44
 * <b>Disabling individual items</b>
45
 *
46
 * Unlike the source, disabled items are specified in the same way as
47
 * normal DropdownFields, using a single value list. Don't pass in grouped
48
 * values here.
49
 *
50
 * <code>
51
 * // Disables first and third option in each group
52
 * $groupedDrDownField->setDisabledItems(array("1", "3"))
53
 * </code>
54
 */
55
class GroupedDropdownField extends DropdownField
56
{
57
58
    // TODO remove this when GroupedDropdownField is implemented
59
    // This should be one of SCHEMA_DATA_TYPE_* constants instead
60
    /** @skipUpgrade */
61
    protected $schemaDataType = 'GroupedDropdownField';
62
63
    /**
64
     * Build a potentially nested fieldgroup
65
     *
66
     * @param mixed $valueOrGroup Value of item, or title of group
67
     * @param string|array $titleOrOptions Title of item, or options in grouip
68
     * @return ArrayData Data for this item
69
     */
70
    protected function getFieldOption($valueOrGroup, $titleOrOptions)
71
    {
72
        // Return flat option
73
        if (!is_array($titleOrOptions)) {
74
            return parent::getFieldOption($valueOrGroup, $titleOrOptions);
75
        }
76
77
        // Build children from options list
78
        $options = new ArrayList();
79
        foreach ($titleOrOptions as $childValue => $childTitle) {
80
            $options->push($this->getFieldOption($childValue, $childTitle));
81
        }
82
83
        return new ArrayData([
84
            'Title' => $valueOrGroup,
85
            'Options' => $options
86
        ]);
87
    }
88
89
    public function Type()
90
    {
91
        return 'groupeddropdown dropdown';
92
    }
93
94
    public function getSourceValues()
95
    {
96
        // Flatten values
97
        $values = [];
98
        $source = $this->getSource();
99
        array_walk_recursive(
100
            $source,
0 ignored issues
show
Bug introduced by
It seems like $source can also be of type ArrayAccess; however, parameter $input of array_walk_recursive() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

100
            /** @scrutinizer ignore-type */ $source,
Loading history...
101
            // Function to extract value from array key
102
            function ($title, $value) use (&$values) {
103
                $values[] = $value;
104
            }
105
        );
106
        return $values;
107
    }
108
109
    /**
110
     * @return SingleLookupField
111
     */
112
    public function performReadonlyTransformation()
113
    {
114
        $field = parent::performReadonlyTransformation();
115
        $field->setSource(ArrayLib::flatten($this->getSource()));
0 ignored issues
show
Bug introduced by
It seems like $this->getSource() can also be of type ArrayAccess; however, parameter $array of SilverStripe\ORM\ArrayLib::flatten() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

115
        $field->setSource(ArrayLib::flatten(/** @scrutinizer ignore-type */ $this->getSource()));
Loading history...
116
        return $field;
117
    }
118
}
119