Framework  3.9
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 
74 {
75  var $options;
76  var $addEntry;
78  var $grouped;
79  var $addEntrySize= 20;
80  var $width;
81  var $height;
82  var $resizable = false;
83  var $nobr = false;
84  var $dropdown;
85  var $dropdownMaxWidth = "340px";
86  var $dropdownMessage = "Click to Select Terms";
87  var $showSelectAll = false;
88  var $separator = ", ";
89 
90  var $fixedOptions = array();
91  var $defaultOptions = array();
92 
93  function CheckListFieldRenderer(&$form, $field, $label, $options, $grouped = false)
94  {
95  $this->options = $options;
96  $this->grouped = $grouped;
97  $this->FieldRenderer($form);
98  if ($form->data->hasField($field))
99  {
100  $form->override($field, $label, $this);
101  }
102  else
103  {
104  $form->add($this, $field);
105  if ($label) $form->alias($field, $label);
106  }
107  }
108 
109  function allowAddEntry($text = "or add new", $size = 40)
110  {
111  $this->addEntry = true;
112  $this->addEntryText = $text;
113  $this->addEntrySize = $size;
114 
115  return $this;
116  }
117 
118  function setSize($width, $height, $scrollAfter = 0)
119  {
120  $this->width = $width;
121  $this->height = $height;
122  $this->scrollAfter = $scrollAfter;
123 
124  return $this;
125  }
126 
127  function addFixedOption($value)
128  {
129  $this->fixedOptions[$value] = $value;
130 
131  return $this;
132  }
133 
134  function addDefaultOption($value)
135  {
136  $this->defaultOptions[$value] = $value;
137 
138  return $this;
139  }
140 
141  function getOptionCount()
142  {
143  if (!$this->grouped) return count($this->options);
144 
145  $count = 0;
146 
147  foreach($this->options as $groupName => $group);
148  {
149  $count += count($group) + 1;
150  }
151 
152  return $count;
153  }
154 
155  function renderField($field)
156  {
157  $this->_startField($field);
158 
159  $rawValue = $this->parent->data->get($field);
160  if (is_array($rawValue))
161  {
162  $this->preProcess($field);
163  $rawValue = $this->parent->data->get($field);
164  }
165 
166  $values = explode(",", $rawValue);
167 
168  $divider = $this->horizontal ? " " : "";
169  $layout = $this->horizontal ? " style='display: inline'" : "";
170 
171  if ($this->showSelectAll)
172  {
173  echo "<a class='select_toggle' href='#' onclick='return {$this->parent->id}_{$field}_toggleSelectAll(this);'>Select All</a><br/>";
174  }
175 
176  if ($this->width && $this->height && $this->getOptionCount() > $this->scrollAfter)
177  {
178  $width = is_numeric($this->width) ? "{$this->width}px" : $this->width;
179  $height = is_numeric($this->height) ? "{$this->height}px" : $this->height;
180  $padding = ($width == "auto") ? "padding-right: 20px; " : "";
181 
182  $resizable = ($this->resizable && !$this->dropdown) ? " resizable": "";
183  $dropdown = $this->dropdown ? " multi_select_dropdown_list" : "";
184  echo "<div id='{$this->parent->id}_{$field}_container' class='selectbox scrollbox{$resizable}{$dropdown}' style='width: {$width}; height: {$height}; $padding overflow: auto'>\n";
185  }
186  else
187  {
188  echo "<div class='selectbox'>";
189  }
190 
191  $idx = 0;
192 
193  if ($this->grouped)
194  {
195  echo "<dl>\n";
196  foreach($this->options as $groupName => $group)
197  {
198  echo "<dt>$groupName</dt>\n<dd>\n";
199  foreach($group as $value => $text)
200  {
201  $checked = (array_search($value, $values) !== FALSE) ? " checked" : "";
202 
203  if ($this->nobr) { $text = str_replace(" ", "&nbsp;", $text); }
204 
205  echo "<label for='{$this->parent->id}_{$field}[$idx]'$layout><input type='checkbox' class='checkbox' id='{$this->parent->id}_{$field}[$idx]' name='{$field}[$idx]' value='$value'$checked/>$text</label>$divider";
206  $idx++;
207  }
208 
209  echo "</dd>";
210  }
211  echo "</dl>";
212  }
213  else
214  {
215  if(count($this->options) > 0)
216  {
217  foreach($this->options as $value => $text)
218  {
219  $disabled = "";
220 
221  if ($this->fixedOptions[$value])
222  {
223  $disabled = " disabled";
224  $checked = " checked";
225  }
226  else if ($this->defaultOptions[$value])
227  {
228  $checked = "checked";
229  }
230  else
231  {
232  $checked = (array_search($value, $values) !== FALSE) ? " checked" : "";
233  }
234  if ($this->nobr) { $text = str_replace(" ", "&nbsp;", $text); }
235  echo "<label for='{$this->parent->id}_{$field}[$idx]'$layout><input type='checkbox' class='checkbox' id='{$this->parent->id}_{$field}[$idx]' name='{$field}[$idx]' value='$value'$checked$disabled/>$text</label>$divider";
236  $idx++;
237  }
238 
239  if($this->addEntry)
240  {
241  foreach($values as $value)
242  {
243  if(!array_key_exists($value, $this->options))
244  $addEntryValue .= $value;
245  }
246  }
247  }
248  }
249 
250  if ($this->addEntry)
251  {
252  echo "&nbsp;{$this->addEntryText}&nbsp;";
253  }
254 
255  if ($this->addEntry)
256  {
257  echo "<input type='text' name='{$field}_addEntry' value='{$addEntryValue}' size='{$this->addEntrySize}'/>";
258  }
259 
260  // Save this count to loop through checkboxes on required validation
261  echo "<input type=\"hidden\" name=\"count_{$field}\" value=\"".$idx."\">";
262 
263  echo "</div>\n";
264 
265  $this->_endField($field);
266  }
267 
268  function renderScript($field)
269  {
270  if ($this->showSelectAll)
271  {
272  ?>
273  <script type="text/javascript">
274 
275  function <?echo $this->parent->id?>_<?echo $field ?>_toggleSelectAll(link)
276  {
277  link = document.id(link);
278 
279  var value = (link.innerHTML == "Select All");
280 
281  var container = document.id('<?echo $this->parent->id?>_<?echo $field?>_container');
282 
283  container.getElements("input[type=checkbox]").each(function(cbox)
284  {
285  cbox.set('checked', value);
286  });
287 
288  link.innerHTML = (value) ? "Deselect All" : "Select All";
289  if (container.multiSelect) container.multiSelect.selectionChanged();
290  return false;
291  }
292 
293  </script>
294  <?
295  }
296 
297  if ($this->dropdown)
298  {
299  ?>
300  <script type='text/javascript'>
301  window.addEvent('domready', function()
302  {
303  new MultiSelect('<?echo $this->parent->id?>_<?echo $field?>_container', {maxWidth: '<?echo $this->dropdownMaxWidth?>', message: '<?echo jsSafe($this->dropdownMessage)?>'});
304  });
305  </script>
306  <?
307  }
308  }
309 
310  function renderReadOnly($field)
311  {
312  $this->_startField($field);
313 
314  $value = $this->parent->data->get($field);
315 
316  $items = explode(",", $value);
317 
318  if ($this->grouped)
319  {
320  foreach($this->options as $category => $options)
321  {
322  $out = "";
323 
324  foreach($items as $item)
325  {
326  if (array_key_exists($item, $options))
327  {
328  if (!$out)
329  {
330  $out = "$category: ";
331  }
332  else
333  {
334  $out .= $this->separator;
335  }
336 
337  $out .= $options[$item];
338  }
339  }
340 
341  if ($out) $answers[] = $out;
342  }
343  }
344  else
345  {
346  // JDG 3/12 - show option value, if found, instead of key
347 
348  foreach($items as $item)
349  {
350  $answers[] = array_key_exists($item, $this->options) ? $this->options[$item] : $item;
351  }
352  }
353 
354  $value = implode($this->separator, $answers);
355 
356  echo $value;
357 
358  $this->_endField($field);
359  }
360 
361  function renderSearchScript($field, $mode)
362  {
363  $this->renderScript($field);
364  }
365 
366  /*
367  * Only implemented for ungrouped option set
368  */
369  function renderSearchField($field, $mode)
370  {
371  $this->_startField($field);
372 
373  $rawValue = $this->parent->params->get($field, $mode);
374 
375  if (is_array($rawValue))
376  {
377  $rawValue = implode(",", array_values($rawValue));
378  $this->parent->params->set($field, $mode, $rawValue);
379  }
380 
381  $values = explode(",", $rawValue);
382  $values = array_combine($values, $values);
383 
384  $divider = $this->horizontal ? " " : "";
385  $layout = $this->horizontal ? " style='display: inline'" : "";
386 
387  if ($this->showSelectAll)
388  {
389  echo "<a href='#' onclick='return {$this->parent->id}_{$field}_toggleSelectAll(this);'>Select All</a><br/>";
390  }
391 
392  if ($this->width && $this->height && $this->getOptionCount() > $this->scrollAfter)
393  {
394  $width = is_numeric($this->width) ? "{$this->width}px" : $this->width;
395  $height = is_numeric($this->height) ? "{$this->height}px" : $this->height;
396  $padding = ($width == "auto") ? "padding-right: 20px; " : "";
397 
398  $resizable = ($this->resizable && !$this->dropdown) ? " resizable": "";
399  $dropdown = $this->dropdown ? " multi_select_dropdown_list" : "";
400  echo "<div id='{$this->parent->id}_{$field}_container' class='selectbox scrollbox{$resizable}{$dropdown}' style='width: {$width}; height: {$height}; $padding overflow: auto'>\n";
401  }
402  else
403  {
404  echo "<div class='selectbox'>";
405  }
406  $idx = 0;
407 
408 
409  if ($this->grouped)
410  {
411  echo "<dl>\n";
412  foreach($this->options as $groupName => $group)
413  {
414  echo "<dt>$groupName</dt>\n<dd>\n";
415  foreach($group as $value => $text)
416  {
417  $checked = (array_search($value, $values) !== FALSE) ? " checked" : "";
418 
419  if ($this->nobr) { $text = str_replace(" ", "&nbsp;", $text); }
420 
421  echo "<label for='{$this->parent->id}_{$field}_$idx'$layout><input type='checkbox' class='checkbox' id='{$this->parent->id}_{$field}_$idx' name='{$field}:{$mode}[$idx]' value='$value'$checked/>$text</label>$divider";
422 
423  $idx++;
424  }
425 
426  echo "</dd>";
427  }
428  echo "</dl>";
429  }
430  else
431  {
432  if(count($this->options) > 0)
433  {
434  foreach($this->options as $value => $text)
435  {
436  $checked = (array_key_exists($value, $values)) ? " checked" : "";
437  echo "<label for='{$this->parent->id}_{$field}_$value'$layout><input type='checkbox' class='checkbox' id='{$this->parent->id}_{$field}_$value' name='{$field}:{$mode}[$value]' value='$value'$checked/>$text</label>$divider";
438 
439  $idx++;
440  }
441  }
442  }
443 
444  if ($this->width && $this->height)
445  {
446  echo "</div>\n";
447  }
448 
449  $this->_endField($field);
450  }
451 
452  function preProcess($field = "")
453  {
454  $value = ($this->parent->method == "POST") ? $_POST[$field] : $_GET[$field];
455  $addEntry = ($this->parent->method == "POST") ? $_POST["{$field}_addEntry"] : $_GET["{$field}_addEntry"];
456 
457  if (is_array($value))
458  {
459  $this->parent->data->set($field, implode(",", array_values($value)));
460  }
461  else if (!$value)
462  {
463  $this->parent->data->set($field, "");
464  }
465  else
466  {
467  $this->parent->data->set($field, $value);
468  }
469 
470  trace("addEntry($field): $addEntry", 3);
471 
472  if ($addEntry)
473  {
474  if($this->parent->data->get($field))
475  {
476  $this->parent->data->set($field, $this->parent->data->get($field) . ",");
477  }
478 
479  $this->parent->data->set($field, $this->parent->data->get($field) . $addEntry);
480  }
481  }
482 
483 }?>
CheckListFieldRenderer: Renders the specified list of options as a list of checkboxes from which mult...
$separator
The separator between values in read only list.
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 ...
CheckListFieldRenderer(&$form, $field, $label, $options, $grouped=false)
allowAddEntry($text="or add new", $size=40)
renderSearchScript($field, $mode)
FieldRenderers can override this method to provide any Javascript that the control requires when bein...
renderField($field)
FieldRenderers must override this method to provide the HTML implementation of the control used to ed...
setSize($width, $height, $scrollAfter=0)
preProcess($field="")
FieldRenderers can override this method to provide behavior that occurs prior to the saving of the pa...
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.
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010