aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/sidebar/SelectElement.php
blob: 33031d2e1b1a7a9713437eb2e9c8ff59d2187f70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
 * Model for a select element of the select widget.
 *
 * @author  Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @license GPL2 or any later version
 * @since   Stud.IP 3.1
 */
class SelectElement extends WidgetElement
{
    protected $id;
    protected $label;
    protected $active;
    protected $tooltip;
    protected $is_header = false;
    protected $indent_level = null;

    /**
     * Constructs the element with an id (value of the according option
     * element) and a label (text content of the according option
     * element).
     *
     * @param String      $id      Id of the element
     * @param String      $label   Label/text content of the element
     * @param bool        $active  Indicates whether the element is active
     * @param String|null $tooltip Optional Title attribute for the element
     */
    public function __construct($id, $label, $active = false, $tooltip = null)
    {
        $this->id      = $id;
        $this->label   = $label;
        $this->active  = $active;
        $this->tooltip = $tooltip;
    }

    /**
     * Sets the id of the element.
     *
     * @param String $id Id of the element
     * @return SelectElement instance to allow chaining
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    /**
     * Sets the label/text content of the element.
     *
     * @param String $label Label/text content of the element
     * @return SelectElement instance to allow chaining
     */
    public function setLabel($label)
    {
        $this->label = $label;
        return $this;
    }

    /**
     * Returns the id of the element.
     *
     * @return String Id of the element
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Returns the label/text content of the element. The label is stripped
     * of all leading whitespace.
     *
     * @return String Label/text content of the element
     * @see SelectElement::getIndentLevel
     */
    public function getLabel()
    {
        return ltrim($this->label);
    }

    /**
     * Returns the tooltip content of the element. It is stripped
     * of all leading whitespace.
     *
     * @return String tooltip content of the element
     * @see SelectElement::getIndentLevel
     */
    public function getTooltip()
    {
        return $this->tooltip !== null ? ltrim($this->tooltip) : null;
    }

    /**
     * Sets the activate of the element.
     *
     * @param bool $active Indicates whether the element is active (optional,
     *                     defaults to true)
     * @return SelectElement instance to allow chaining
     */
    public function setActive($active = true)
    {
        $this->active = $active;
        return $this;
    }

    /**
     * Returns whether the element is active.
     *
     * @return bool indicating whether the element is active
     */
    public function isActive()
    {
        return $this->active;
    }

    /**
     * Sets the flag indicating whether this element should be displayed
     * as a header element.
     *
     * @param bool $is_header "Is header" flag
     * @return SelectElement instance to allow chaining
     */
    public function setAsHeader($is_header = true)
    {
        $this->is_header = (bool)$is_header;
        return $this;
    }

    /**
     * Returns whether the elements should be displayed as a header element.
     *
     * @return bool indicating whether the element should be displayed as a
     *              header element
     */
    public function isHeader()
    {
        return $this->is_header;
    }

    /**
     * Sets the indentation level of the element.
     *
     * @param int $level Indentation level
     * @return SelectElement instance to allow chaining
     */
    public function setIndentLevel($level)
    {
        $this->indent_level = (int)$level;
        return $this;
    }

    /**
     * Returns the indentation level of the element. If the level has not
     * been set explicitely, it is calculated based on the number of
     * leading whitespace characters. This is used to indent the label in
     * the according option element.
     *
     * @return int Indentation level
     */
    public function getIndentLevel()
    {
        if ($this->indent_level !== null) {
            return $this->indent_level;
        }
        return mb_strlen($this->label) - mb_strlen(ltrim($this->label));
    }

    /**
     * Renders the element (well, not really - this returns it's label).
     *
     * @return String The label/text content of the element
     * @todo   What should this method actually do?
     */
    public function render()
    {
        return $this->getLabel();
    }
}