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-2015 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
8 #
9 
14 {
15 
16  # ---- PUBLIC INTERFACE --------------------------------------------------
17 
26  public function __construct($ResultVar, $Options, $SelectedValue = NULL)
27  {
28  $this->ResultVar = $ResultVar;
29  $this->Options = $Options;
30  $this->SelectedValue = $SelectedValue;
31  }
32 
36  public function PrintHtml()
37  {
38  print $this->GetHtml();
39  }
40 
45  public function GetHtml()
46  {
47  # start out with empty HTML
48  $Html = "";
49 
50  # if there are options or we are supposed to print even if no options
51  if (count($this->Options) || $this->PrintIfEmpty)
52  {
53  # begin select element
54  $Html .= '<select name="'.$this->ResultVar.'"'
55  .' size="'.$this->Size.'"'
56  .' id="'.$this->ResultVar.'"';
57  if ($this->SubmitOnChange)
58  {
59  if ($this->OnChangeAction)
60  { $Html .= ' onChange="'.$this->OnChangeAction.'"'; }
61  else
62  { $Html .= ' onChange="submit()"'; }
63  }
64  if ($this->MultipleAllowed) { $Html .= ' multiple'; }
65  if ($this->Disabled) { $Html .= ' disabled'; }
66  $Html .= ">\n";
67 
68  # for each option
69  foreach ($this->Options as $Value => $Label)
70  {
71  # start option element
72  $Html .= ' <option value="'.htmlspecialchars($Value).'"';
73 
74  # add in selected attribute if appropriate
75  if ((is_array($this->SelectedValue)
76  && in_array($Value, $this->SelectedValue))
77  || ($Value == $this->SelectedValue))
78  {
79  $Html .= ' selected';
80  }
81 
82  # add in disabled attribute if appropriate
83  if (array_key_exists($Value, $this->DisabledOptions))
84  {
85  $Html .= ' disabled';
86  }
87 
88  # add label and end option element
89  $Html .= ">".htmlspecialchars($Label)."</option>\n";
90  }
91 
92  # end select element
93  $Html .= '</select>';
94  }
95 
96  # return generated HTML to caller
97  return $Html;
98  }
99 
108  public function DisabledOptions($Options = NULL)
109  {
110  if ($Options !== NULL)
111  {
112  if (is_array($Options))
113  {
114  $this->DisabledOptions = $Options;
115  }
116  else
117  {
118  $this->DisabledOptions[$Options] = "X";
119  }
120  }
121  return $this->DisabledOptions;
122  }
123 
130  public function SelectedValue($NewValue = NULL)
131  {
132  if ($NewValue !== NULL)
133  {
134  $this->SelectedValue = $NewValue;
135  }
136  return $this->SelectedValue;
137  }
138 
144  public function Size($NewValue = NULL)
145  {
146  if ($NewValue !== NULL)
147  {
148  $this->Size = intval($NewValue);
149  }
150  return $this->Size;
151  }
152 
159  public function MultipleAllowed($NewValue = NULL)
160  {
161  if ($NewValue !== NULL)
162  {
163  $this->MultipleAllowed = $NewValue ? TRUE : FALSE;
164 
165  # adjust form field name (result variable) if needed
166  if ($this->MultipleAllowed
167  && (substr($this->ResultVar, -2) != "[]"))
168  {
169  $this->ResultVar .= "[]";
170  }
171  elseif (!$this->MultipleAllowed
172  && (substr($this->ResultVar, -2) == "[]"))
173  {
174  $this->ResultVar .= substr($this->ResultVar, 0, -2);
175  }
176  }
177  return $this->MultipleAllowed;
178  }
179 
188  public function SubmitOnChange($NewValue = NULL)
189  {
190  if ($NewValue !== NULL)
191  {
192  $this->SubmitOnChange = $NewValue ? TRUE : FALSE;
193  }
194  return $this->SubmitOnChange;
195  }
196 
208  public function OnChangeAction($NewValue = NULL)
209  {
210  if ($NewValue !== NULL)
211  {
212  $this->OnChangeAction = $NewValue;
213  }
214  return $this->OnChangeAction;
215  }
216 
226  public function PrintIfEmpty($NewValue = NULL)
227  {
228  if ($NewValue !== NULL)
229  {
230  $this->PrintIfEmpty = $NewValue ? TRUE : FALSE;
231  }
232  return $this->PrintIfEmpty;
233  }
234 
242  public function Disabled($NewValue = NULL)
243  {
244  if ($NewValue !== NULL)
245  {
246  $this->Disabled = $NewValue ? TRUE : FALSE;
247  }
248  return $this->Disabled;
249  }
250 
251 
252  # ---- PRIVATE INTERFACE -------------------------------------------------
253 
254  private $Options;
255  private $ResultVar;
256 
257  private $Disabled = FALSE;
258  private $DisabledOptions = array();
259  private $MultipleAllowed = FALSE;
260  private $OnChangeAction = "submit()";
261  private $PrintIfEmpty = TRUE;
262  private $SelectedValue;
263  private $Size = 1;
264  private $SubmitOnChange = FALSE;
265 }
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.
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.
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.
Convenience class for generating an HTML select/option form element.
DisabledOptions($Options=NULL)
Get/set disabled options.