Single.php
Go to the documentation of this file.
1<?php
11
13
56{
60 protected $list = array();
61
65 protected $skin = 'radio';
66
75 public function __construct($name, $parameters, $form)
76 {
77 parent::__construct($name, $parameters, $form);
78
79 $this->list = (isset($parameters['list']) && is_array($parameters['list'])) ? $parameters['list'] : array();
80 }
81
91 protected function setDefaults()
92 {
93 parent::setDefaults();
94
95 // single-choice-elements have values of type string
96 $this->defaults['defaultValue'] = '';
97 $this->defaults['skin'] = 'radio';
98 }
99
110 protected function htmlList($options = null, $value = null)
111 {
112 if ($value == null) $value = $this->htmlValue();
113 if ($options == null) $options = $this->list;
114
115 $options = $this->htmlEscape($options);
116 $list = '';
117
118 if ($this->skin === "select") {
119 foreach ($options as $index => $option) {
120 if (is_array($option)) {
121 $list .= "<optgroup label=\"{$index}\">" . $this->htmlList($option, $value) . "</optgroup>";
122 } else {
123 $selected = ((string) $index === (string) $value) ? ' selected' : '';
124 $list .= "<option value=\"{$index}\"{$selected}>{$option}</option>";
125 }
126 }
127 } else {
128 $inputAttributes = $this->htmlInputAttributes();
129
130 foreach ($options as $index => $option) {
131 // typecasted for non-associative arrays
132 $selected = ((string) $index === (string) $value) ? " checked=\"yes\"" : '';
133 $class = htmlentities("input-single-option-" . str_replace(" ", "-", $index));
134
135 $list .= "<span>" .
136 "<label class=\"{$class}\" title=\"{$option}\">" .
137 "<input type=\"radio\" name=\"{$this->name}\"{$inputAttributes} value=\"{$index}\"{$selected}>" .
138 "<span>{$option}</span>" .
139 "</label>" .
140 "</span>";
141 }
142 }
143
144 return $list;
145 }
146
152 public function __toString()
153 {
154 $marker = $this->htmlMarker();
155 $label = $this->htmlLabel();
156 $list = $this->htmlList();
157 $wrapperAttributes = $this->htmlWrapperAttributes();
159 $helpMessage = $this->htmlHelpMessage();
160
161 if ($this->skin === "select") {
162 // render HTML select
163 $inputAttributes = $this->htmlInputAttributes();
164
165 return "<p {$wrapperAttributes}>" .
166 "<label>" .
167 "<span class=\"depage-label\">{$label}{$marker}</span>" .
168 "<select name=\"{$this->name}\"{$inputAttributes}>{$list}</select>" .
169 "</label>" .
172 "</p>\n";
173 } else {
174 // render HTML radio button list
175 return "<p {$wrapperAttributes}>" .
176 "<span class=\"depage-label\">{$label}{$marker}</span>" .
177 "<span>{$list}</span>" .
180 "</p>\n";
181 }
182 }
183
189 protected function typeCastValue()
190 {
191 // check if value is in list
192 $inList = false;
193
194 if (in_array($this->value, array_keys($this->list))) {
195 $inList = true;
196 }
197 if (!$inList) {
198 foreach($this->list as $sub) {
199 if (is_array($sub) && in_array($this->value, array_keys($sub))) {
200 $inList = true;
201 break;
202 }
203 }
204 }
205 if (!$inList) {
206 $this->value = "";
207 }
208 $this->value = (string) $this->value;
209 }
210}
211
212/* 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
htmlInputAttributes()
Returns string of HTML attributes for input element.
Definition Input.php:575
$value
Input elements's value.
Definition Input.php:123
$label
Input element - HTML label.
Definition Input.php:36
$class
class for paragraph
Definition Input.php:151
$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-single-choice input type i.e.
Definition Single.php:56
htmlList($options=null, $value=null)
Renders HTML - option list part of select/radio single element.
Definition Single.php:110
$list
Contains list of selectable options.
Definition Single.php:60
__construct($name, $parameters, $form)
single class constructor
Definition Single.php:75
$skin
HTML skin type (radio or select).
Definition Single.php:65
typeCastValue()
Converts value to element specific type.
Definition Single.php:189
__toString()
Renders element to HTML.
Definition Single.php:152
setDefaults()
collects initial values across subclasses
Definition Single.php:91
Abstract element classes.
Definition Container.php:10
Classes for HTML input-elements.
Definition Address.php:9