multiple.php
Go to the documentation of this file.
1 <?php
10 namespace depage\htmlform\elements;
11 
12 use depage\htmlform\abstracts;
13 
59 {
63  protected $list = array();
64 
73  public function __construct($name, $parameters, $form)
74  {
75  parent::__construct($name, $parameters, $form);
76 
77  $this->list = (isset($parameters['list']) && is_array($parameters['list'])) ? $parameters['list'] : array();
78  }
79 
89  protected function setDefaults()
90  {
91  parent::setDefaults();
92 
93  // multiple-choice-elements have values of type array
94  $this->defaults['defaultValue'] = array();
95  $this->defaults['skin'] = 'checkbox';
96  }
97 
111  protected function htmlList($options = null, $value = null)
112  {
113  if ($value == null) $value = $this->htmlValue();
114  if ($options == null) $options = $this->list;
115 
116  $options = $this->htmlEscape($options);
117  $list = '';
118 
119  // select
120  if ($this->skin === "select") {
121  foreach ($options as $index => $option) {
122  if (is_array($option)) {
123  $list .= "<optgroup label=\"{$index}\">" . $this->htmlList($option, $value) . "</optgroup>";
124  } else {
125  $selected = (in_array($index, $value)) ? ' selected' : '';
126  $list .= "<option value=\"{$index}\"{$selected}>{$option}</option>";
127  }
128  }
129  // checkbox
130  } else {
131  $inputAttributes = $this->htmlInputAttributes();
132 
133  foreach ($options as $index => $option) {
134  $selected = (is_array($value) && (in_array($index, $value))) ? " checked=\"yes\"" : '';
135 
136  $list .= "<span>" .
137  "<label>" .
138  "<input type=\"checkbox\" name=\"{$this->name}[]\"{$inputAttributes} value=\"{$index}\"{$selected}>" .
139  "<span>{$option}</span>" .
140  "</label>" .
141  "</span>";
142  }
143  }
144 
145  return $list;
146  }
147 
155  public function __toString()
156  {
157  $marker = $this->htmlMarker();
158  $label = $this->htmlLabel();
159  $list = $this->htmlList();
160  $wrapperAttributes = $this->htmlWrapperAttributes();
161  $errorMessage = $this->htmlErrorMessage();
162  $helpMessage = $this->htmlHelpMessage();
163 
164  if ($this->skin === 'select') {
165  // render HTML select
166 
167  $inputAttributes = $this->htmlInputAttributes();
168 
169  return "<p {$wrapperAttributes}>" .
170  "<label>" .
171  "<span class=\"depage-label\">{$label}{$marker}</span>" .
172  "<select multiple name=\"{$this->name}[]\"{$inputAttributes}>{$list}</select>" .
173  "</label>" .
174  $errorMessage .
175  $helpMessage .
176  "</p>\n";
177  } else {
178  // render HTML checkbox
179  return "<p {$wrapperAttributes}>" .
180  "<span class=\"depage-label\">{$label}{$marker}</span>" .
181  "<span>{$list}</span>" .
182  $errorMessage .
183  $helpMessage .
184  "</p>\n";
185  }
186  }
187 
196  protected function htmlInputAttributes()
197  {
198  $attributes = '';
199 
200  // HTML5 validator hack
201  if ($this->required && $this->skin === 'select') $attributes .= ' required="required"';
202  if ($this->autofocus) $attributes .= ' autofocus="autofocus"';
203  return $attributes;
204  }
205 
211  protected function typeCastValue()
212  {
213  if ($this->value == "") {
214  $this->value = array();
215  } else {
216  $this->value = (array) $this->value;
217  }
218  }
219 }
220 
221 /* 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:193
setDefaults()
collects initial values across subclasses.
Definition: multiple.php:89
input element base class
Definition: input.php:20
typeCastValue()
Converts value to element specific type.
Definition: multiple.php:211
$list
Contains list of selectable options.
Definition: multiple.php:63
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
Definition: input.php:486
htmlMarker()
Returns elements&#39; required-indicator.
Definition: input.php:445
__construct($name, $parameters, $form)
multiple class constructor
Definition: multiple.php:73
htmlErrorMessage()
Returns HTML-rendered error message.
Definition: input.php:514
__toString()
Renders element to HTML.
Definition: multiple.php:155
htmlValue()
Returns HTML-rendered element value.
Definition: input.php:504
htmlHelpMessage()
Returns HTML-rendered helpMessage.
Definition: input.php:533
$value
Input elements&#39;s value.
Definition: input.php:79
$label
Input element - HTML label.
Definition: input.php:36
htmlList($options=null, $value=null)
HTML option list rendering.
Definition: multiple.php:111
htmlInputAttributes()
Returns string of HTML attributes for input element.
Definition: multiple.php:196
$errorMessage
Message that gets displayed in case of invalid input.
Definition: input.php:173
$helpMessage
Extra help message.
Definition: input.php:185
$marker
Input element - HTML marker text that marks required fields.
Definition: input.php:47
HTML-multiple-choice input type i.e. checkbox and select.
Definition: multiple.php:58