Completed
Push — master ( 1be2e7...d38097 )
by Sam
23s
created

ListboxField::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 5
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Forms;
4
5
use SilverStripe\ORM\ArrayList;
6
use SilverStripe\View\ArrayData;
7
8
/**
9
 * Multi-line listbox field, created from a <select> tag.
10
 *
11
 * <b>Usage</b>
12
 *
13
 * <code>
14
 * new ListboxField(
15
 *    $name = "pickanumber",
16
 *    $title = "Pick a number",
17
 *    $source = array(
18
 *       "1" => "one",
19
 *       "2" => "two",
20
 *       "3" => "three"
21
 *    ),
22
 *    $value = 1
23
 * )
24
 * </code>
25
 *
26
 * @see DropdownField for a simple <select> field with a single element.
27
 * @see CheckboxSetField for multiple selections through checkboxes.
28
 * @see OptionsetField for single selections via radiobuttons.
29
 * @see TreeDropdownField for a rich and customizeable UI that can visualize a tree of selectable elements
30
 */
31
class ListboxField extends MultiSelectField
32
{
33
34
    /**
35
     * The size of the field in rows.
36
     *
37
     * @var int
38
     */
39
    protected $size;
40
41
    /**
42
     * @var array
43
     */
44
    protected $disabledItems = array();
45
46
    /**
47
     * Creates a new dropdown field.
48
     *
49
     * @param string $name The field name
50
     * @param string $title The field title
51
     * @param array $source An map of the dropdown items
52
     * @param string|array|null $value You can pass an array of values or a single value like a drop down to be selected
53
     * @param int $size Optional size of the select element
54
     */
55
    public function __construct($name, $title = '', $source = array(), $value = null, $size = null)
56
    {
57
        if ($size) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $size of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
58
            $this->setSize($size);
59
        }
60
61
        parent::__construct($name, $title, $source, $value);
62
    }
63
64
    /**
65
     * Returns a <select> tag containing all the appropriate <option> tags
66
     *
67
     * @param array $properties
68
     * @return string
69
     */
70
    public function Field($properties = array())
71
    {
72
        $properties = array_merge($properties, array(
73
            'Options' => $this->getOptions(),
74
        ));
75
76
        return FormField::Field($properties);
77
    }
78
79
    /**
80
     * Gets the list of options to render in this formfield
81
     *
82
     * @return ArrayList
83
     */
84
    public function getOptions()
85
    {
86
        // Loop through and figure out which values were selected.
87
        $options = array();
88
        $selectedValue = $this->getValueArray();
89
        foreach ($this->getSource() as $itemValue => $title) {
90
            $itemSelected = in_array($itemValue, $selectedValue)
91
                || in_array($itemValue, $this->getDefaultItems());
92
            $itemDisabled = $this->isDisabled()
93
                || in_array($itemValue, $this->getDisabledItems());
94
            $options[] = new ArrayData(array(
95
                'Title' => $title,
96
                'Value' => $itemValue,
97
                'Selected' => $itemSelected,
98
                'Disabled' => $itemDisabled,
99
            ));
100
        }
101
102
        $options = new ArrayList($options);
103
        $this->extend('updateGetOptions', $options);
104
        return $options;
105
    }
106
107
    public function getAttributes()
108
    {
109
        return array_merge(
110
            parent::getAttributes(),
111
            array(
112
                'multiple' => 'true',
113
                'size' => $this->getSize(),
114
                'name' => $this->getName() . '[]'
115
            )
116
        );
117
    }
118
119
    /**
120
     * Get the size of this dropdown in rows.
121
     *
122
     * @return integer
123
     */
124
    public function getSize()
125
    {
126
        return $this->size;
127
    }
128
129
    /**
130
     * Sets the size of this dropdown in rows.
131
     *
132
     * @param int $size The height in rows (e.g. 3)
133
     * @return $this Self reference
134
     */
135
    public function setSize($size)
136
    {
137
        $this->size = $size;
138
        return $this;
139
    }
140
141
    /**
142
     * Mark certain elements as disabled,
143
     * regardless of the {@link setDisabled()} settings.
144
     *
145
     * @param array $items Collection of array keys, as defined in the $source array
146
     * @return $this Self reference
147
     */
148
    public function setDisabledItems($items)
149
    {
150
        $this->disabledItems = $items;
151
        return $this;
152
    }
153
154
    /**
155
     * @return array
156
     */
157
    public function getDisabledItems()
158
    {
159
        return $this->disabledItems;
160
    }
161
}
162