Framework  3.9
radio_button_field_renderer.inc
Go to the documentation of this file.
1 <?php
6 /**************************************************************
7 
8  Copyright (c) 2007-2010 Sonjara, Inc
9 
10  Permission is hereby granted, free of charge, to any person
11  obtaining a copy of this software and associated documentation
12  files (the "Software"), to deal in the Software without
13  restriction, including without limitation the rights to use,
14  copy, modify, merge, publish, distribute, sublicense, and/or sell
15  copies of the Software, and to permit persons to whom the
16  Software is furnished to do so, subject to the following
17  conditions:
18 
19  The above copyright notice and this permission notice shall be
20  included in all copies or substantial portions of the Software.
21 
22  Except as contained in this notice, the name(s) of the above
23  copyright holders shall not be used in advertising or otherwise
24  to promote the sale, use or other dealings in this Software
25  without prior written authorization.
26 
27  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
29  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
31  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
32  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34  OTHER DEALINGS IN THE SOFTWARE.
35 
36 *****************************************************************/
37 
38 require_once realpath(dirname(__FILE__))."/../field_renderers.inc";
39 
46 {
47  var $options;
48  var $horizontal = false;
49  var $onChange = ""; // optional callback on data input change
50  var $disable = false; // alternative to readonly; field can be reenabled thru javascript
51  var $tabular = false; // Horizontal table-based layout
52  var $regular = false;
53 
54  function RadioButtonFieldRenderer(&$form, $field, $label, $options, $onchange = "")
55  {
56  $this->options = $options;
57  $this->onChange = $onchange;
58 
59  $this->FieldRenderer($form);
60  if ($form->data->hasField($field))
61  {
62  $form->override($field, $label, $this);
63  }
64  else
65  {
66  $form->add($this, $field);
67  $form->alias($field, $label);
68  }
69  }
70 
71  function addValidatorsToForm($field, $required = false)
72  {
73  $label = isset($this->label) ? $this->label : $this->parent->prettifyFieldName($field);
74 
75  // JDG 2/2012 - changed from tree validator expects a div and does not match the rendering of this type
76  if($required)
77  {
78  $this->parent->validator->add(new RequiredRadioButtonValidator($field, $label));
79  }
80 
81  }
82 
83 
84  function renderField($field)
85  {
86  if (!count($this->options)) return;
87 
88  if(($this->horizontal || $this->tabular) && count($this->options) > 2)
89  $this->colspan = 2;
90  $this->_startField($field);
91 
92  $current = $this->parent->data->get($field);
93 
94  $separator = "<br/>";
95 
96  if ($this->tabular)
97  {
98  if ($this->regular)
99  {
100  if ($this->horizontal)
101  {
102  $width = 100 / count($this->options);
103  }
104  else
105  {
106  $width = 100;
107  }
108  $tdStyle = " style='width: {$width}%'";
109  }
110  else
111  {
112  $tdStyle = "";
113  }
114 
115  echo "<table class='radio_button_options'><tr><td{$tdStyle}>";
116  $separator = $this->horizontal ? "</td><td>" : "</td></tr><tr><td>";
117  }
118  else if ($this->horizontal)
119  {
120  $separator = "&nbsp;&nbsp;";
121  }
122 
123  $out = array();
124 
125  foreach($this->options as $value => $name)
126  {
127  $selected = ($current == $value) ? " checked" : "";
128  $onchange = ($this->onChange) ? "onclick='$this->onChange(this)'" : "";
129  $disable = ($this->disable) ? "disabled='disabled'" : "";
130 
131  // JDG 3/34/10, add border: none to correct IE8 display issue
132  $out[] = "<label class='radio' for='{$this->parent->id}_{$field}_{$value}'><input id='{$this->parent->id}_{$field}_{$value}' style='border: none' type='radio' name='$field' value='$value'$selected $onchange $disable/>&nbsp;$name</label>";
133  }
134 
135  $radios = implode($separator, $out);
136  if ($this->tabular)
137  {
138  $radios .= "</td></tr></table>";
139  }
140 
141  echo $radios;
142 
143  $this->_endField($field);
144  }
145 
146  /*
147  * JDG, 3/12/10, fixed rendering style from select field
148  * to radio button.
149  */
150  function renderSearchField($field, $mode)
151  {
152  if ($mode != "equal") return;
153 
154  $searchValue = $this->parent->params->get($field, $mode);
155  echo "<tr>\n";
156  $this->_printLabel($field);
157  echo "<td>\n";
158  foreach($this->options as $value => $name)
159  {
160  $selected = ($searchValue == $value) ? " checked" : "";
161  echo "<input style='border: none' type='radio' name='$field:$mode' value='$value'$selected>$name</option>";
162  echo ($this->horizontal) ? "&nbsp;&nbsp;" : "<br/>";
163  }
164  echo "</td></tr>\n";
165  }
166 
167 
168  function renderReadOnly($field)
169  {
170  $this->_startField($field);
171 
172  if (array_key_exists($this->parent->data->get($field), $this->options))
173  {
174  echo $this->options[$this->parent->data->get($field)];
175  }
176  else
177  {
178  echo "Not specified";
179  }
180  $this->_endField($field);
181  }
182 }
183 
184 ?>
FieldRenderer is the abstract base class for all FieldRenderers.
_startField($field, $styles="")
Internal method to generate the starting HTML for the field (including the label)
_endField($field)
Internal method to generate the closing HTML for the field.
_printLabel($field, $colspan=1, $styles="", $annotation="")
Internal method to generate the HTML for the field label.
FieldRenderer($parent)
Constructor.
Field renderer for data fields that must be displayed as a list of choices.
renderSearchField($field, $mode)
FieldRenderers must override this method to provide the HTML implementation of the control displayed ...
renderField($field)
FieldRenderers must override this method to provide the HTML implementation of the control used to ed...
RadioButtonFieldRenderer(&$form, $field, $label, $options, $onchange="")
addValidatorsToForm($field, $required=false)
This method is called by the AutoForm to add any default input validators that are required by the Fi...