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