CWIS Developer Documentation
HtmlOptionList.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: HtmlOptionList.php
4 #
5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2014 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
8 #
9 
14 
15  # ---- PUBLIC INTERFACE --------------------------------------------------
16 
25  function __construct($ResultVar, $Options, $SelectedValue = NULL)
26  {
27  $this->ResultVar = $ResultVar;
28  $this->Options = $Options;
29  $this->SelectedValue = $SelectedValue;
30  }
31 
35  function PrintHtml()
36  {
37  print $this->GetHtml();
38  }
39 
44  function GetHtml()
45  {
46  # start out with empty HTML
47  $Html = "";
48 
49  # if there are options or we are supposed to print even if no options
50  if (count($this->Options) || $this->PrintIfEmpty)
51  {
52  # begin select element
53  $Html .= '<select name="'.$this->ResultVar.'"'
54  .' size="'.$this->Size.'"'
55  .' id="'.$this->ResultVar.'"';
56  if ($this->SubmitOnChange)
57  {
58  if ($this->OnChangeAction)
59  { $Html .= ' onChange="'.$this->OnChangeAction.'"'; }
60  else
61  { $Html .= ' onChange="submit()"'; }
62  }
63  if ($this->MultipleAllowed) { $Html .= ' multiple'; }
64  $Html .= ">\n";
65 
66  # for each option
67  foreach ($this->Options as $Value => $Label)
68  {
69  # start option element
70  $Html .= ' <option value="'.htmlspecialchars($Value).'"';
71 
72  # add in selected attribute if appropriate
73  if ((is_array($this->SelectedValue)
74  && in_array($Value, $this->SelectedValue))
75  || ($Value == $this->SelectedValue))
76  {
77  $Html .= ' selected';
78  }
79 
80  # add in disabled attribute if appropriate
81  if (array_key_exists($Value, $this->DisabledOptions))
82  {
83  $Html .= ' disabled';
84  }
85 
86  # add label and end option element
87  $Html .= ">".htmlspecialchars($Label)."</option>\n";
88  }
89 
90  # end select element
91  $Html .= '</select>';
92  }
93 
94  # return generated HTML to caller
95  return $Html;
96  }
97 
106  function DisabledOptions($Options = NULL)
107  {
108  if ($Options !== NULL)
109  {
110  if (is_array($Options))
111  {
112  $this->DisabledOptions = $Options;
113  }
114  else
115  {
116  $this->DisabledOptions[$Options] = "X";
117  }
118  }
119  return $this->DisabledOptions;
120  }
121 
128  function SelectedValue($NewValue = NULL)
129  {
130  if ($NewValue !== NULL)
131  {
132  $this->SelectedValue = $NewValue;
133  }
134  return $this->SelectedValue;
135  }
136 
142  function Size($NewValue = NULL)
143  {
144  if ($NewValue !== NULL)
145  {
146  $this->Size = intval($NewValue);
147  }
148  return $this->Size;
149  }
150 
157  function MultipleAllowed($NewValue = NULL)
158  {
159  if ($NewValue !== NULL)
160  {
161  $this->MultipleAllowed = $NewValue ? TRUE : FALSE;
162  }
163  return $this->MultipleAllowed;
164  }
165 
174  function SubmitOnChange($NewValue = NULL)
175  {
176  if ($NewValue !== NULL)
177  {
178  $this->SubmitOnChange = $NewValue ? TRUE : FALSE;
179  }
180  return $this->SubmitOnChange;
181  }
182 
194  function OnChangeAction($NewValue = NULL)
195  {
196  if ($NewValue !== NULL)
197  {
198  $this->OnChangeAction = $NewValue;
199  }
200  return $this->OnChangeAction;
201  }
202 
212  function PrintIfEmpty($NewValue = NULL)
213  {
214  if ($NewValue !== NULL)
215  {
216  $this->PrintIfEmpty = $NewValue ? TRUE : FALSE;
217  }
218  return $this->PrintIfEmpty;
219  }
220 
221 
222  # ---- PRIVATE INTERFACE -------------------------------------------------
223 
224  private $Options;
225  private $ResultVar;
226 
227  private $DisabledOptions = array();
228  private $MultipleAllowed = FALSE;
229  private $OnChangeAction = "submit()";
230  private $PrintIfEmpty = TRUE;
231  private $SelectedValue;
232  private $Size = 1;
233  private $SubmitOnChange = FALSE;
234 }
235 
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.
PHP
Definition: OAIClient.php:39
__construct($ResultVar, $Options, $SelectedValue=NULL)
Class constructor.
GetHtml()
Get HTML for list.
PrintHtml()
Print HTML for list.
MultipleAllowed($NewValue=NULL)
Get/set whether multiple items may be selected.
Size($NewValue=NULL)
Get/set the list size (number of visible items).
OnChangeAction($NewValue=NULL)
Get/set action to take if form is submitted on change.
SelectedValue($NewValue=NULL)
Get/set currently selected value or array of currently selected values.
Convenience class for generating an HTML select/option form element.
DisabledOptions($Options=NULL)
Get/set disabled options.