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 .= '<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 
310  # ---- PRIVATE INTERFACE -------------------------------------------------
311 
312  protected $Options;
313  protected $ResultVar;
314 
315  protected $Disabled = FALSE;
316  protected $DisabledOptions = array();
317  protected $ListClasses = NULL;
318  protected $MultipleAllowed = FALSE;
319  protected $OnChangeAction = "submit()";
320  protected $OptionClasses = NULL;
321  protected $OptionData = array();
322  protected $PrintIfEmpty = TRUE;
323  protected $SelectedValue;
324  protected $Size = 1;
325  protected $SubmitOnChange = FALSE;
326 
332  protected function GetOptionTag($Value, $Label)
333  {
334  # start option element
335  $Html = ' <option value="'.htmlspecialchars($Value).'"';
336 
337  # add in selected attribute if appropriate
338  if ((is_array($this->SelectedValue)
339  && in_array($Value, $this->SelectedValue))
340  || ($Value == $this->SelectedValue))
341  {
342  $Html .= ' selected';
343  }
344 
345  # add in disabled attribute if appropriate
346  if (array_key_exists($Value, $this->DisabledOptions))
347  {
348  $Html .= ' disabled';
349  }
350 
351  # add in class if requested
352  if ($this->OptionClasses)
353  {
354  if (is_array($this->OptionClasses))
355  {
356  if (isset($this->OptionClasses[$Value]))
357  {
358  $Html .= ' class="'
359  .htmlspecialchars($this->OptionClasses[$Value]).'"';
360  }
361  }
362  else
363  {
364  $Html .= ' class="'
365  .htmlspecialchars($this->OptionClasses).'"';
366  }
367  }
368 
369  # add in data attributes if requested
370  if (isset($this->OptionData[$Value]))
371  {
372  foreach ($this->OptionData[$Value] as $DName => $DVal)
373  {
374  $DName = preg_replace('/[^a-z0-9-]/', '', strtolower($DName));
375  $Html .= ' data-'.$DName.'="'.htmlspecialchars($DVal).'"';
376  }
377  }
378 
379  # add label and end option element
380  $Html .= ">".htmlspecialchars($Label)."</option>\n";
381 
382  return $Html;
383  }
384 }
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.
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.
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.