CWIS Developer Documentation

- Home
- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

HtmlOptionList.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: HtmlOptionList.php
4 #
5 # Part of the ScoutLib application support library
6 # Copyright 2014-2017 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu
8 #
9
13 class HtmlOptionList
14 {
15
16 # ---- PUBLIC INTERFACE --------------------------------------------------
17
28 public function __construct($ResultVar, $Options, $SelectedValue = NULL)
29 {
30 $this->ResultVar = $ResultVar;
31 $this->Options = $Options;
32 $this->SelectedValue = $SelectedValue;
33 }
34
38 public function PrintHtml()
39 {
40 print $this->GetHtml();
41 }
42
47 public function GetHtml()
48 {
49 # start out with empty HTML
50 $Html = "";
51
52 # if there are options or we are supposed to print even if no options
53 if (count($this->Options) || $this->PrintIfEmpty)
54 {
55 # begin select element
56 $Html .= '<select name="'.$this->ResultVar.'"'
57 .' size="'.$this->Size.'"'
58 .' id="'.$this->ResultVar.'"';
59 if ($this->ListClasses)
60 {
61 $Html .= ' class="'.htmlspecialchars($this->ListClasses).'"';
62 }
63 if ($this->SubmitOnChange)
64 {
65 if ($this->OnChangeAction)
66 { $Html .= ' onChange="'.$this->OnChangeAction.'"'; }
67 else
68 { $Html .= ' onChange="submit()"'; }
69 }
70 if ($this->MultipleAllowed) { $Html .= ' multiple'; }
71 if ($this->Disabled) { $Html .= ' disabled'; }
72 $Html .= ">\n";
73
74 # for each option
75 foreach ($this->Options as $Value => $Label)
76 {
77 # if option is actually a group of options
78 if (is_array($Label))
79 {
80 # add group start tag
81 $Html .= " <optgroup label=\""
82 .htmlspecialchars($Value)."\">\n";
83
84 # for each option in group
85 foreach ($Label as $GValue => $GLabel)
86 {
87 # add tag for option
88 $Html .= $this->GetOptionTag($GValue, $GLabel);
89 }
90
91 # add group end tag
92 $Html .= " </optgroup>\n";
93 }
94 else
95 {
96 $Html .= $this->GetOptionTag($Value, $Label);
97 }
98 }
99
100 # end select element
101 $Html .= '</select>';
102 }
103
104 # return generated HTML to caller
105 return $Html;
106 }
107
116 public function DisabledOptions($Options = NULL)
117 {
118 if ($Options !== NULL)
119 {
120 if (is_array($Options))
121 {
122 $this->DisabledOptions = $Options;
123 }
124 else
125 {
126 $this->DisabledOptions[$Options] = "X";
127 }
128 }
129 return $this->DisabledOptions;
130 }
131
138 public function SelectedValue($NewValue = NULL)
139 {
140 if ($NewValue !== NULL)
141 {
142 $this->SelectedValue = $NewValue;
143 }
144 return $this->SelectedValue;
145 }
146
152 public function Size($NewValue = NULL)
153 {
154 if ($NewValue !== NULL)
155 {
156 $this->Size = intval($NewValue);
157 }
158 return $this->Size;
159 }
160
167 public function MultipleAllowed($NewValue = NULL)
168 {
169 if ($NewValue !== NULL)
170 {
171 $this->MultipleAllowed = $NewValue ? TRUE : FALSE;
172
173 # adjust form field name (result variable) if needed
174 if ($this->MultipleAllowed
175 && (substr($this->ResultVar, -2) != "[]"))
176 {
177 $this->ResultVar .= "[]";
178 }
179 elseif (!$this->MultipleAllowed
180 && (substr($this->ResultVar, -2) == "[]"))
181 {
182 $this->ResultVar .= substr($this->ResultVar, 0, -2);
183 }
184 }
185 return $this->MultipleAllowed;
186 }
187
196 public function SubmitOnChange($NewValue = NULL)
197 {
198 if ($NewValue !== NULL)
199 {
200 $this->SubmitOnChange = $NewValue ? TRUE : FALSE;
201 }
202 return $this->SubmitOnChange;
203 }
204
216 public function OnChangeAction($NewValue = NULL)
217 {
218 if ($NewValue !== NULL)
219 {
220 $this->OnChangeAction = $NewValue;
221 }
222 return $this->OnChangeAction;
223 }
224
234 public function PrintIfEmpty($NewValue = NULL)
235 {
236 if ($NewValue !== NULL)
237 {
238 $this->PrintIfEmpty = $NewValue ? TRUE : FALSE;
239 }
240 return $this->PrintIfEmpty;
241 }
242
250 public function Disabled($NewValue = NULL)
251 {
252 if ($NewValue !== NULL)
253 {
254 $this->Disabled = $NewValue ? TRUE : FALSE;
255 }
256 return $this->Disabled;
257 }
258
264 public function ClassForList($NewValue = NULL)
265 {
266 if ($NewValue !== NULL)
267 {
268 $this->ListClasses = $NewValue;
269 }
270 return $this->ListClasses;
271 }
272
284 public function ClassForOptions($NewValue = NULL)
285 {
286 if ($NewValue !== NULL)
287 {
288 $this->OptionClasses = $NewValue;
289 }
290 return $this->OptionClasses;
291 }
292
301 public function DataForOptions($NewValue = NULL)
302 {
303 if ($NewValue !== NULL)
304 {
305 $this->OptionData = $NewValue;
306 }
307 return $this->OptionData;
308 }
309
318 public function MaxLabelLength($NewValue = NULL)
319 {
320 if ($NewValue !== NULL)
321 {
322 $this->MaxLabelLength = $NewValue;
323 }
324 return $this->MaxLabelLength;
325 }
326
327 # ---- PRIVATE INTERFACE -------------------------------------------------
328
329 protected $Disabled = FALSE;
330 protected $DisabledOptions = array();
331 protected $ListClasses = NULL;
332 protected $MaxLabelLength = 0;
333 protected $MultipleAllowed = FALSE;
334 protected $OnChangeAction = "submit()";
335 protected $Options;
336 protected $OptionClasses = NULL;
337 protected $OptionData = array();
338 protected $PrintIfEmpty = TRUE;
339 protected $ResultVar;
340 protected $SelectedValue;
341 protected $Size = 1;
342 protected $SubmitOnChange = FALSE;
343
349 protected function GetOptionTag($Value, $Label)
350 {
351 # start option element
352 $Html = ' <option value="'.htmlspecialchars($Value).'"';
353
354 # add in selected attribute if appropriate
355 if ((is_array($this->SelectedValue)
356 && in_array($Value, $this->SelectedValue))
357 || ($Value == $this->SelectedValue))
358 {
359 $Html .= ' selected';
360 }
361
362 # add in disabled attribute if appropriate
363 if (array_key_exists($Value, $this->DisabledOptions))
364 {
365 $Html .= ' disabled';
366 }
367
368 # add in class if requested
369 if ($this->OptionClasses)
370 {
371 if (is_array($this->OptionClasses))
372 {
373 if (isset($this->OptionClasses[$Value]))
374 {
375 $Html .= ' class="'
376 .htmlspecialchars($this->OptionClasses[$Value]).'"';
377 }
378 }
379 else
380 {
381 $Html .= ' class="'
382 .htmlspecialchars($this->OptionClasses).'"';
383 }
384 }
385
386 # add in data attributes if requested
387 if (isset($this->OptionData[$Value]))
388 {
389 foreach ($this->OptionData[$Value] as $DName => $DVal)
390 {
391 $DName = preg_replace('/[^a-z0-9-]/', '', strtolower($DName));
392 $Html .= ' data-'.$DName.'="'.htmlspecialchars($DVal).'"';
393 }
394 }
395
396 # substring label to its max length if requested
397 if (is_numeric($this->MaxLabelLength) && $this->MaxLabelLength > 0)
398 {
399 $Label = substr($Label, 0, $this->MaxLabelLength);
400 }
401
402 # add label and end option element
403 $Html .= ">".htmlspecialchars($Label)."</option>\n";
404
405 return $Html;
406 }
407 }
HtmlOptionList\SubmitOnChange
SubmitOnChange($NewValue=NULL)
Get/set whether to submit the form when the list value is changed.
Definition: HtmlOptionList.php:196
HtmlOptionList\PrintIfEmpty
PrintIfEmpty($NewValue=NULL)
Get/set whether list should be output even if there are no items.
Definition: HtmlOptionList.php:234
HtmlOptionList\$Options
$Options
Definition: HtmlOptionList.php:335
HtmlOptionList\$SubmitOnChange
$SubmitOnChange
Definition: HtmlOptionList.php:342
HtmlOptionList\$OptionClasses
$OptionClasses
Definition: HtmlOptionList.php:336
HtmlOptionList\$MultipleAllowed
$MultipleAllowed
Definition: HtmlOptionList.php:333
HtmlOptionList\$SelectedValue
$SelectedValue
Definition: HtmlOptionList.php:340
HtmlOptionList\$OnChangeAction
$OnChangeAction
Definition: HtmlOptionList.php:334
HtmlOptionList\$ResultVar
$ResultVar
Definition: HtmlOptionList.php:339
HtmlOptionList\__construct
__construct($ResultVar, $Options, $SelectedValue=NULL)
Class constructor.
Definition: HtmlOptionList.php:28
PHP
PHP
Definition: RSSClient--Test.php:12
HtmlOptionList\$OptionData
$OptionData
Definition: HtmlOptionList.php:337
HtmlOptionList\ClassForOptions
ClassForOptions($NewValue=NULL)
Get/set CSS class(es) for the options.
Definition: HtmlOptionList.php:284
HtmlOptionList\GetHtml
GetHtml()
Get HTML for list.
Definition: HtmlOptionList.php:47
HtmlOptionList\PrintHtml
PrintHtml()
Print HTML for list.
Definition: HtmlOptionList.php:38
HtmlOptionList\$PrintIfEmpty
$PrintIfEmpty
Definition: HtmlOptionList.php:338
HtmlOptionList\$Disabled
$Disabled
Definition: HtmlOptionList.php:329
HtmlOptionList\MultipleAllowed
MultipleAllowed($NewValue=NULL)
Get/set whether multiple items may be selected.
Definition: HtmlOptionList.php:167
HtmlOptionList\DataForOptions
DataForOptions($NewValue=NULL)
Get/set HTML data attributes for the options.
Definition: HtmlOptionList.php:301
HtmlOptionList\Size
Size($NewValue=NULL)
Get/set the list size (number of visible items).
Definition: HtmlOptionList.php:152
HtmlOptionList\GetOptionTag
GetOptionTag($Value, $Label)
Get HTML for one option.
Definition: HtmlOptionList.php:349
HtmlOptionList\OnChangeAction
OnChangeAction($NewValue=NULL)
Get/set action to take if form is submitted on change.
Definition: HtmlOptionList.php:216
HtmlOptionList\$DisabledOptions
$DisabledOptions
Definition: HtmlOptionList.php:330
HtmlOptionList\Disabled
Disabled($NewValue=NULL)
Get/set whether the whole option list is editable.
Definition: HtmlOptionList.php:250
HtmlOptionList\SelectedValue
SelectedValue($NewValue=NULL)
Get/set currently selected value or array of currently selected values.
Definition: HtmlOptionList.php:138
HtmlOptionList\$Size
$Size
Definition: HtmlOptionList.php:341
HtmlOptionList\$MaxLabelLength
$MaxLabelLength
Definition: HtmlOptionList.php:332
HtmlOptionList\$ListClasses
$ListClasses
Definition: HtmlOptionList.php:331
HtmlOptionList\MaxLabelLength
MaxLabelLength($NewValue=NULL)
Get/set the maximum number of character a label will be displayed.
Definition: HtmlOptionList.php:318
HtmlOptionList\ClassForList
ClassForList($NewValue=NULL)
Get/set CSS class(es) for the list.
Definition: HtmlOptionList.php:264
HtmlOptionList
Convenience class for generating an HTML select/option form element.
Definition: HtmlOptionList.php:13
HtmlOptionList\DisabledOptions
DisabledOptions($Options=NULL)
Get/set disabled options.
Definition: HtmlOptionList.php:116