Framework  3.9
push_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 = true;
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 PushButtonFieldRenderer(&$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 RequiredValidator($field, $label));
79  }
80 
81  }
82 
83 
84  function renderScript($field)
85  {
86  $fn = "{$this->parent->id}_{$field}_update";
87  $containerId = "{$this->parent->id}_{$field}_container";
88  $id = "{$this->parent->id}_{$field}";
89 ?>
90 <script type='text/javascript'>
91 function <?echo $fn?>(element, value)
92 {
93  var container = document.id('<?echo $containerId?>');
94  container.getElements("a.selected").each(function(elt)
95  {
96  elt.removeClass('selected');
97  });
98  document.id(element).addClass('selected');
99  var ctrl = document.id('<?echo $id?>');
100  ctrl.value = value;
101  ctrl.fireEvent('change');
102  return false;
103 }
104 </script>
105 <?
106  }
107 
108  function renderField($field)
109  {
110  if (!count($this->options)) return;
111 
112  $this->_startField($field);
113 
114 
115  $this->renderButtons($field);
116 
117 
118  $this->_endField($field);
119  }
120 
121  function renderButtons($field, $mode = null)
122  {
123  $current = $this->parent->data->get($field);
124 
125  $separator = "";
126 
127  if ($this->regular && !$this->tabular)
128  {
129  $width = 100 / count($this->options);
130  $widthStyle = " style='width: {$width}%'";
131  }
132 
133  $containerId = "{$this->parent->id}_{$field}_container";
134 
135  if ($this->tabular)
136  {
137  if ($this->regular)
138  {
139  $width = 100 / count($this->options);
140  $tdStyle = " style='width: {$width}%'";
141  }
142  else
143  {
144  $tdStyle = "";
145  }
146 
147  echo "<table id='{$containerId}' class='push_button_options'><tr><td{$tdStyle}>";
148  $separator = "</td><td{$tdStyle}>";
149  $close = "</td></tr></table>";
150  }
151  else if ($this->horizontal)
152  {
153  echo "<div id='{$containerId}' class='push_button_options horizontal'>";
154  $close = "</div>";
155  }
156  else
157  {
158  echo "<div id='{$containerId}' class='push_button_options vertical'>";
159  $close = "</div>";
160  }
161 
162  $name = $mode ? "$field:$mode" : $field;
163 
164  echo "<input type='hidden' id='{$this->parent->id}_{$field}' name='{$name}' value='$current'/>";
165 
166  $out = array();
167 
168  $max = count($this->options) - 1;
169  $disable = ($this->disable) ? "disabled='disabled'" : "";
170 
171  $count = 0;
172  foreach($this->options as $value => $name)
173  {
174  $selected = ($current == $value) ? " selected" : "";
175  $onChange = ($this->onChange) ? "{$this->onChange}(this);" : "";
176 
177  $position = "";
178  if ($count == 0) $position .= " first";
179  if ($count == $max) $position .= " last";
180 
181  // JDG 3/34/10, add border: none to correct IE8 display issue
182  $out[] = "<a class='pushbutton{$position}{$selected}'{$widthStyle} href='#' onclick='{$this->parent->id}_{$field}_update(this, \"{$value}\");{$onChange}return false' {$disable}/>{$name}</a>";
183 
184  $count++;
185  }
186 
187  $buttons = implode($separator, $out);
188  $buttons .= $close;
189 
190  echo $buttons;
191  }
192 
193  function renderSearchField($field, $mode)
194  {
195  if ($mode != "equal") return;
196 
197  $searchValue = $this->parent->params->get($field, $mode);
198 
199  $this->_startField($field);
200 
201  $this->renderButtons($field, $mode);
202 
203  $this->_endField($field);
204  }
205 
206 
207  function renderReadOnly($field)
208  {
209  $this->_startField($field);
210 
211  if (array_key_exists($this->parent->data->get($field), $this->options))
212  {
213  echo $this->options[$this->parent->data->get($field)];
214  }
215  else
216  {
217  echo "Not specified";
218  }
219  $this->_endField($field);
220  }
221 }
222 
223 ?>
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.
FieldRenderer($parent)
Constructor.
Field renderer for data fields that must be displayed as a list of choices.
PushButtonFieldRenderer(&$form, $field, $label, $options, $onChange="")
renderScript($field)
FieldRenderers can override this method to provide any Javascript that their control requires for an ...
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...
addValidatorsToForm($field, $required=false)
This method is called by the AutoForm to add any default input validators that are required by the Fi...
RequiredField Validator.
Definition: validation.inc:76