CWIS Developer Documentation
HtmlRadioButtonSet.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: HtmlRadioButtonSet.php
4 #
5 # Part of the ScoutLib application support library
6 # Copyright 2017 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu
8 #
9 
14 {
15 
16  # ---- PUBLIC INTERFACE --------------------------------------------------
17 
26  public function __construct(
27  $ResultVar,
28  array $Options,
29  $SelectedValue = NULL)
30  {
31  $this->ResultVar = $ResultVar;
32  $this->Options = $Options;
33  $this->SelectedValue = $SelectedValue;
34  }
35 
39  public function PrintHtml()
40  {
41  print $this->GetHtml();
42  }
43 
48  public function GetHtml()
49  {
50  # start out with empty HTML
51  $Html = "";
52 
53  # if there are options or we are supposed to print even if no options
54  if (count($this->Options) || $this->PrintIfEmpty)
55  {
56  # for each option
57  foreach ($this->Options as $Value => $Label)
58  {
59  # generate ID for elements
60  $Id = $this->ResultVar."_"
61  .preg_replace("%[^a-z0-9]%i", "", $Value);
62 
63  # start input element
64  $Html .= " <input type=\"radio\""
65  ." id=\"".htmlspecialchars($Id)."\""
66  ." name=\"".htmlspecialchars($this->ResultVar)."\""
67  ." value=\"".htmlspecialchars($Value)."\"";
68 
69  # add in selected attribute if appropriate
70  if ((is_array($this->SelectedValue)
71  && in_array($Value, $this->SelectedValue))
72  || ($Value == $this->SelectedValue))
73  {
74  $Html .= " checked";
75  }
76 
77  # add in disabled attribute if appropriate
78  if ($this->Disabled
79  || array_key_exists($Value, $this->DisabledOptions))
80  {
81  $Html .= " disabled";
82  }
83 
84  # end input element
85  $Html .= ">";
86 
87  # add label
88  $Html .= " <label for=\"".$Id."\">"
89  .htmlspecialchars($Label)."</label>\n";
90  }
91  }
92 
93  # return generated HTML to caller
94  return $Html;
95  }
96 
105  public function DisabledOptions($Options = NULL)
106  {
107  if ($Options !== NULL)
108  {
109  if (is_array($Options))
110  {
111  $this->DisabledOptions = $Options;
112  }
113  else
114  {
115  $this->DisabledOptions[$Options] = "X";
116  }
117  }
118  return $this->DisabledOptions;
119  }
120 
127  public function SelectedValue($NewValue = NULL)
128  {
129  if ($NewValue !== NULL)
130  {
131  $this->SelectedValue = $NewValue;
132  }
133  return $this->SelectedValue;
134  }
135 
145  public function PrintIfEmpty($NewValue = NULL)
146  {
147  if ($NewValue !== NULL)
148  {
149  $this->PrintIfEmpty = $NewValue ? TRUE : FALSE;
150  }
151  return $this->PrintIfEmpty;
152  }
153 
161  public function Disabled($NewValue = NULL)
162  {
163  if ($NewValue !== NULL)
164  {
165  $this->Disabled = $NewValue ? TRUE : FALSE;
166  }
167  return $this->Disabled;
168  }
169 
170 
171  # ---- PRIVATE INTERFACE -------------------------------------------------
172 
173  private $Options;
174  private $ResultVar;
175 
176  private $Disabled = FALSE;
177  private $DisabledOptions = array();
178  private $PrintIfEmpty = TRUE;
179  private $SelectedValue;
180 }
DisabledOptions($Options=NULL)
Get/set disabled options.
SelectedValue($NewValue=NULL)
Get/set currently selected value or array of currently selected values.
__construct($ResultVar, array $Options, $SelectedValue=NULL)
Class constructor.
Convenience class for generating a set of HTML radio button form elements.
PrintIfEmpty($NewValue=NULL)
Get/set whether list should be output even if there are no items.
GetHtml()
Get HTML for set.
Disabled($NewValue=NULL)
Get/set whether the whole option list is editable.
PrintHtml()
Print HTML for set.