Framework  3.9
option_list_field_renderer.inc
Go to the documentation of this file.
1 <?php
6 /**************************************************************
7 
8 Copyright (c) 2014 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 
50 {
51  var $options = array();
52  var $cssClass = "options_list";
53  var $grouped = false;
54  var $groupTag = "h6";
55 
56  function OptionListFieldRenderer(&$form, $field, $label, $options, $grouped = false)
57  {
58  $this->options = $options;
59  $this->grouped = $grouped;
60 
61  $this->FieldRenderer($form);
62  if ($form->data->hasField($field))
63  {
64  $form->override($field, $label, $this);
65  }
66  else
67  {
68  $form->add($this, $field);
69  if ($label) $form->alias($field, $label);
70  }
71  }
72 
73 
74  function renderScript($field)
75  {
76  if ($this->parent->readOnlyForm || $this->parent->isReadOnly($field)) return;
77  $id = "{$this->parent->id}_{$field}";
78  ?>
79  <script type='text/javascript'>
80  function <?echo $id?>_update(sel)
81  {
82  value = sel.get('data-value');
83 
84  $$('#<?echo $id?>_list li[data-value]').each(function(elt)
85  {
86  var v = elt.get('data-value');
87  if (v == value)
88  {
89  elt.addClass('selected');
90  }
91  else
92  {
93  elt.removeClass('selected');
94  }
95  });
96 
97  var hidden = document.id('<?echo $id?>');
98  var v = hidden.value;
99  hidden.value = value;
100  var f = document.id('<?echo $this->parent->id?>');
101  if (v != hidden.value && f && f.manager)
102  {
103  f.manager.dirty();
104  }
105  }
106  </script>
107 <?
108  }
109 
110  function renderField($field)
111  {
112  $this->_startField($field);
113 
114  $value = $this->parent->data->get($field);
115 
116  $id = "{$this->parent->id}_{$field}";
117 
118  echo "<input type='hidden' id='{$id}' name='{$field}' value='{$value}'/>";
119  if (!$this->grouped)
120  {
121  echo "<ul id='{$id}_list' class='{$this->cssClass}'>";
122  foreach($this->options as $option => $description)
123  {
124  $selected = ($value == $option) ? " class='selected'" : "";
125  echo "<li{$selected} data-value='{$option}' onclick='{$id}_update(this); return false;'>{$description}</li>\n";
126  }
127  echo "</ul>";
128  }
129  else
130  {
131  echo "<div id='{$id}_list' class='{$this->cssClass}'>";
132 
133  foreach($this->options as $group => $options)
134  {
135  echo "<{$this->groupTag}>{$group}</{$this->groupTag}>\n";
136  echo "<ul class='{$this->cssClass}'>";
137  foreach($options as $option => $description)
138  {
139  $selected = ($value == $option) ? " class='selected'" : "";
140  echo "<li{$selected} data-value='{$option}' onclick='{$id}_update(this); return false;'>{$description}</li>\n";
141  }
142  echo "</ul>";
143  }
144 
145  echo "</div>";
146  }
147 
148  $this->_endField($field);
149  }
150 
151  function renderReadOnly($field)
152  {
153  $this->_startField($field);
154 
155  $value = $this->parent->data->get($field);
156  if ($value)
157  {
158  echo $this->options[$value];
159  }
160  $this->_endField($field);
161  }
162 }
163 
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.
Displays a control that provides a highly styleable set of options from which the user can select a s...
$groupTag
The tag to use when displaying the groups.
$options
The array of options for the control to display, as value=>description pairs.
$grouped
True if the $options array is grouped by headings, false if not.
$cssClass
CSS class for the generated list of options. Use this to override the look and feel.
renderField($field)
FieldRenderers must override this method to provide the HTML implementation of the control used to ed...
OptionListFieldRenderer(&$form, $field, $label, $options, $grouped=false)
renderScript($field)
FieldRenderers can override this method to provide any Javascript that their control requires for an ...