Multiple.php
Go to the documentation of this file.
1<?php
11
13
59{
63 protected $list = array();
64
68 protected $skin = 'radio';
69
73 protected $maxItems = null;
74
83 public function __construct($name, $parameters, $form)
84 {
85 parent::__construct($name, $parameters, $form);
86
87 $this->list = (isset($parameters['list']) && is_array($parameters['list'])) ? $parameters['list'] : array();
88 $this->maxItems = isset($parameters['maxItems']) ? $parameters['maxItems'] : $this->maxItems;
89 }
90
100 protected function setDefaults()
101 {
102 parent::setDefaults();
103
104 // multiple-choice-elements have values of type array
105 $this->defaults['defaultValue'] = array();
106 $this->defaults['skin'] = 'checkbox';
107 $this->defaults['maxItems'] = null;
108 }
109
123 protected function htmlList($options = null, $value = null)
124 {
125 if ($value == null) $value = $this->htmlValue();
126 if ($options == null) $options = $this->list;
127
128 $options = $this->htmlEscape($options);
129 $list = '';
130
131 // select
132 if (in_array($this->skin, ['select', 'tags'])) {
133 foreach ($options as $index => $option) {
134 if (is_array($option)) {
135 $list .= "<optgroup label=\"{$index}\">" . $this->htmlList($option, $value) . "</optgroup>";
136 } else {
137 $selected = (in_array($index, $value)) ? ' selected' : '';
138 $list .= "<option value=\"{$index}\"{$selected}>{$option}</option>";
139 }
140 }
141 // checkbox
142 } else {
143 $inputAttributes = $this->htmlInputAttributes();
144
145 foreach ($options as $index => $option) {
146 $selected = (is_array($value) && (in_array($index, $value))) ? " checked=\"yes\"" : '';
147
148 $list .= "<span>" .
149 "<label>" .
150 "<input type=\"checkbox\" name=\"{$this->name}[]\"{$inputAttributes} value=\"{$index}\"{$selected}>" .
151 "<span>{$option}</span>" .
152 "</label>" .
153 "</span>";
154 }
155 }
156
157 return $list;
158 }
159
167 public function __toString()
168 {
169 $marker = $this->htmlMarker();
170 $label = $this->htmlLabel();
171 $list = $this->htmlList();
172 $wrapperAttributes = $this->htmlWrapperAttributes();
174 $helpMessage = $this->htmlHelpMessage();
175
176 if (in_array($this->skin, ['select', 'tags'])) {
177 // render HTML select
178
179 $inputAttributes = $this->htmlInputAttributes();
180
181 return "<p {$wrapperAttributes}>" .
182 "<label>" .
183 "<span class=\"depage-label\">{$label}{$marker}</span>" .
184 "<select multiple name=\"{$this->name}[]\"{$inputAttributes}>{$list}</select>" .
185 "</label>" .
188 "</p>\n";
189 } else {
190 // render HTML checkbox
191 return "<p {$wrapperAttributes}>" .
192 "<span class=\"depage-label\">{$label}{$marker}</span>" .
193 "<span>{$list}</span>" .
196 "</p>\n";
197 }
198 }
199
208 protected function htmlInputAttributes()
209 {
210 $attributes = '';
211
212 // HTML5 validator hack
213 if ($this->required && in_array($this->skin, ['select', 'tags'])) $attributes .= ' required="required"';
214 if ($this->maxItems) $attributes .= " data-max-items=\"$this->maxItems\"";
215 return $attributes;
216 }
217
223 protected function typeCastValue()
224 {
225 if ($this->value == "") {
226 $this->value = array();
227 } else {
228 $this->value = (array) $this->value;
229
230 if ($this->maxItems) {
231 $this->value = array_slice($this->value, 0, $this->maxItems);
232 }
233 }
234 }
235}
236
237/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
htmlEscape($options=array())
Escapes HTML in strings and arrays of strings.
Definition Element.php:267
input element base class
Definition Input.php:21
$value
Input elements's value.
Definition Input.php:123
$label
Input element - HTML label.
Definition Input.php:36
$errorMessage
Message that gets displayed in case of invalid input.
Definition Input.php:217
htmlHelpMessage()
Returns HTML-rendered helpMessage.
Definition Input.php:676
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
Definition Input.php:627
htmlMarker()
Returns elements' required-indicator.
Definition Input.php:565
$helpMessage
Extra help message.
Definition Input.php:229
htmlValue()
Returns HTML-rendered element value.
Definition Input.php:647
$marker
Input element - HTML marker text that marks required fields.
Definition Input.php:91
htmlErrorMessage()
Returns HTML-rendered error message.
Definition Input.php:657
HTML-multiple-choice input type i.e.
Definition Multiple.php:59
htmlInputAttributes()
Returns string of HTML attributes for input element.
Definition Multiple.php:208
htmlList($options=null, $value=null)
HTML option list rendering.
Definition Multiple.php:123
$list
Contains list of selectable options.
Definition Multiple.php:63
__construct($name, $parameters, $form)
multiple class constructor
Definition Multiple.php:83
$skin
HTML skin type (checkbox or select).
Definition Multiple.php:68
typeCastValue()
Converts value to element specific type.
Definition Multiple.php:223
__toString()
Renders element to HTML.
Definition Multiple.php:167
setDefaults()
collects initial values across subclasses.
Definition Multiple.php:100
Abstract element classes.
Definition Container.php:10
Classes for HTML input-elements.
Definition Address.php:9