CWIS Developer Documentation
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 
14 {
15 
16  # ---- PUBLIC INTERFACE --------------------------------------------------
17 
28  public function __construct($ResultVar, $Options, $SelectedValue = NULL)
29  {
30  $this->ResultVar = $ResultVar;
31  $this->Options = $Options;
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 .= $this->GetSelectOpenTag();
57 
58  # for each option
59  foreach ($this->Options as $Value => $Label)
60  {
61  # if option is actually a group of options
62  if (is_array($Label))
63  {
64  # add group start tag
65  $Html .= " <optgroup label=\""
66  .htmlspecialchars($Value)."\">\n";
67 
68  # for each option in group
69  foreach ($Label as $GValue => $GLabel)
70  {
71  # add tag for option
72  $Html .= $this->GetOptionTag($GValue, $GLabel);
73  }
74 
75  # add group end tag
76  $Html .= " </optgroup>\n";
77  }
78  else
79  {
80  $Html .= $this->GetOptionTag($Value, $Label);
81  }
82  }
83 
84  # end select element
85  $Html .= '</select>';
86  }
87 
88  # return generated HTML to caller
89  return $Html;
90  }
91 
100  public function DisabledOptions($Options = NULL)
101  {
102  if ($Options !== NULL)
103  {
104  if (is_array($Options))
105  {
106  $this->DisabledOptions = $Options;
107  }
108  else
109  {
110  $this->DisabledOptions[$Options] = "X";
111  }
112  }
113  return $this->DisabledOptions;
114  }
115 
122  public function SelectedValue($NewValue = NULL)
123  {
124  if ($NewValue !== NULL)
125  {
126  $this->SelectedValue = $NewValue;
127  }
128  return $this->SelectedValue;
129  }
130 
136  public function Size($NewValue = NULL)
137  {
138  if ($NewValue !== NULL)
139  {
140  $this->Size = intval($NewValue);
141  }
142  return $this->Size;
143  }
144 
151  public function MultipleAllowed($NewValue = NULL)
152  {
153  if ($NewValue !== NULL)
154  {
155  $this->MultipleAllowed = $NewValue ? TRUE : FALSE;
156 
157  # adjust form field name (result variable) if needed
158  if ($this->MultipleAllowed
159  && (substr($this->ResultVar, -2) != "[]"))
160  {
161  $this->ResultVar .= "[]";
162  }
163  elseif (!$this->MultipleAllowed
164  && (substr($this->ResultVar, -2) == "[]"))
165  {
166  $this->ResultVar .= substr($this->ResultVar, 0, -2);
167  }
168  }
169  return $this->MultipleAllowed;
170  }
171 
180  public function SubmitOnChange($NewValue = NULL)
181  {
182  if ($NewValue !== NULL)
183  {
184  $this->SubmitOnChange = $NewValue ? TRUE : FALSE;
185  }
186  return $this->SubmitOnChange;
187  }
188 
200  public function OnChangeAction($NewValue = NULL)
201  {
202  if ($NewValue !== NULL)
203  {
204  $this->OnChangeAction = $NewValue;
205  }
206  return $this->OnChangeAction;
207  }
208 
218  public function PrintIfEmpty($NewValue = NULL)
219  {
220  if ($NewValue !== NULL)
221  {
222  $this->PrintIfEmpty = $NewValue ? TRUE : FALSE;
223  }
224  return $this->PrintIfEmpty;
225  }
226 
234  public function Disabled($NewValue = NULL)
235  {
236  if ($NewValue !== NULL)
237  {
238  $this->Disabled = $NewValue ? TRUE : FALSE;
239  }
240  return $this->Disabled;
241  }
242 
248  public function ClassForList($NewValue = NULL)
249  {
250  if ($NewValue !== NULL)
251  {
252  $this->ListClasses = $NewValue;
253  }
254  return $this->ListClasses;
255  }
256 
268  public function ClassForOptions($NewValue = NULL)
269  {
270  if ($NewValue !== NULL)
271  {
272  $this->OptionClasses = $NewValue;
273  }
274  return $this->OptionClasses;
275  }
276 
285  public function DataForOptions($NewValue = NULL)
286  {
287  if ($NewValue !== NULL)
288  {
289  $this->OptionData = $NewValue;
290  }
291  return $this->OptionData;
292  }
293 
302  public function MaxLabelLength($NewValue = NULL)
303  {
304  if ($NewValue !== NULL)
305  {
306  $this->MaxLabelLength = $NewValue;
307  }
308  return $this->MaxLabelLength;
309  }
310 
318  public function AddAttribute($Name, $Value)
319  {
320  $this->AdditionalAttributes[$Name] = $Value;
321  }
322 
323 
324  # ---- PRIVATE INTERFACE -------------------------------------------------
325 
326  protected $AdditionalAttributes = array();
327  protected $Disabled = FALSE;
328  protected $DisabledOptions = array();
329  protected $ListClasses = NULL;
330  protected $MaxLabelLength = 0;
331  protected $MultipleAllowed = FALSE;
332  protected $OnChangeAction = "submit()";
333  protected $Options;
334  protected $OptionClasses = NULL;
335  protected $OptionData = array();
336  protected $PrintIfEmpty = TRUE;
337  protected $ResultVar;
338  protected $SelectedValue;
339  protected $Size = 1;
340  protected $SubmitOnChange = FALSE;
341 
347  protected function GetOptionTag($Value, $Label)
348  {
349  # start option element
350  $Html = ' <option value="'.htmlspecialchars($Value).'"';
351 
352  # add in selected attribute if appropriate
353  if ((is_array($this->SelectedValue)
354  && in_array($Value, $this->SelectedValue))
355  || ($Value == $this->SelectedValue))
356  {
357  $Html .= ' selected';
358  }
359 
360  # add in disabled attribute if appropriate
361  if (array_key_exists($Value, $this->DisabledOptions))
362  {
363  $Html .= ' disabled';
364  }
365 
366  # add in class if requested
367  if ($this->OptionClasses)
368  {
369  if (is_array($this->OptionClasses))
370  {
371  if (isset($this->OptionClasses[$Value]))
372  {
373  $Html .= ' class="'
374  .htmlspecialchars($this->OptionClasses[$Value]).'"';
375  }
376  }
377  else
378  {
379  $Html .= ' class="'
380  .htmlspecialchars($this->OptionClasses).'"';
381  }
382  }
383 
384  # add in data attributes if requested
385  if (isset($this->OptionData[$Value]))
386  {
387  foreach ($this->OptionData[$Value] as $DName => $DVal)
388  {
389  $DName = preg_replace('/[^a-z0-9-]/', '', strtolower($DName));
390  $Html .= ' data-'.$DName.'="'.htmlspecialchars($DVal).'"';
391  }
392  }
393 
394  # substring label to its max length if requested
395  if (is_numeric($this->MaxLabelLength) && $this->MaxLabelLength > 0)
396  {
397  $Label = substr($Label, 0, $this->MaxLabelLength);
398  }
399 
400  # add label and end option element
401  $Html .= ">".htmlspecialchars($Label)."</option>\n";
402 
403  return $Html;
404  }
405 
410  protected function GetSelectOpenTag()
411  {
412  $Html = '<select name="'.$this->ResultVar.'"'
413  .' size="'.$this->Size.'"'
414  .' id="'.$this->ResultVar.'"';
415 
416  if ($this->ListClasses)
417  {
418  $Html .= ' class="'.htmlspecialchars($this->ListClasses).'"';
419  }
420 
421  if ($this->SubmitOnChange)
422  {
423  if ($this->OnChangeAction)
424  { $Html .= ' onChange="'.$this->OnChangeAction.'"'; }
425  else
426  { $Html .= ' onChange="submit()"'; }
427  }
428 
429  if ($this->MultipleAllowed) { $Html .= ' multiple'; }
430 
431  if ($this->Disabled) { $Html .= ' disabled'; }
432 
433  foreach ($this->AdditionalAttributes as $Name => $Value)
434  {
435  $Html .= " ".$Name."=\"".htmlspecialchars($Value)."\"";
436  }
437 
438  $Html .= ">\n";
439 
440  return $Html;
441  }
442 }
SubmitOnChange($NewValue=NULL)
Get/set whether to submit the form when the list value is changed.
PrintIfEmpty($NewValue=NULL)
Get/set whether list should be output even if there are no items.
__construct($ResultVar, $Options, $SelectedValue=NULL)
Class constructor.
ClassForOptions($NewValue=NULL)
Get/set CSS class(es) for the options.
GetHtml()
Get HTML for list.
PrintHtml()
Print HTML for list.
MultipleAllowed($NewValue=NULL)
Get/set whether multiple items may be selected.
DataForOptions($NewValue=NULL)
Get/set HTML data attributes for the options.
GetSelectOpenTag()
Get HTML for tag to begin list.
Size($NewValue=NULL)
Get/set the list size (number of visible items).
GetOptionTag($Value, $Label)
Get HTML for one option.
OnChangeAction($NewValue=NULL)
Get/set action to take if form is submitted on change.
Disabled($NewValue=NULL)
Get/set whether the whole option list is editable.
SelectedValue($NewValue=NULL)
Get/set currently selected value or array of currently selected values.
AddAttribute($Name, $Value)
Add an attribute for the <select> tag for the list.
MaxLabelLength($NewValue=NULL)
Get/set the maximum number of character a label will be displayed.
ClassForList($NewValue=NULL)
Get/set CSS class(es) for the list.
Convenience class for generating an HTML select/option form element.
DisabledOptions($Options=NULL)
Get/set disabled options.