Framework  3.9
compound_select_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 
48 {
49  var $options;
50  var $onChange;
51  var $width;
53 
54  var $defaultBlank = true;
55  var $defaultText = "Choose One";
56  var $emptyValue = 0;
57 
58  function CompoundSelectFieldRenderer(&$form, $field, $label, $onChange)
59  {
60  $this->options = array();
61  $this->onChange = $onChange;
62 
63  $this->FieldRenderer($form);
64  if ($form->data->hasField($field))
65  {
66  $form->override($field, $label, $this);
67  if ($this->options == null)
68  {
69  $opt = $form->data->distinctValues($field, true);
70  if (count($opt) > 0)
71  {
72  $this->options = array_combine($opt, $opt);
73  }
74  else
75  {
76  $this->options = array();
77  }
78  }
79  }
80  else
81  {
82  $form->add($this, $field);
83  // JDG 7/22/2011 - allow "additional" fields to override label
84  $form->overrides[$field]['label'] = $label;
85  }
86  }
87 
88  function add($title, $field, $items, $format)
89  {
90  $this->options[$field] = array("title" => $title,
91  "items" => $items,
92  "format" => $format);
93 
94  $this->parent->hide($field);
95 
96  return $this;
97  }
98 
99  function renderField($field)
100  {
101  $this->_startField($field);
102 
103  $current = $this->getValue();
104 
105  if($this->width)
106  $style = "style='width: {$this->width}'";
107  echo "<select $style name='$field' id='{$this->parent->id}_{$field}'";
108 
109  if ($this->onChange != "")
110  echo " onchange='$this->onChange(this)'";
111  elseif($this->addEntry)
112  echo " onchange='onChange_{$this->parent->id}_{$field}(this)'";
113 
114  echo ">\n";
115 
116  if ($this->defaultBlank)
117  {
118  echo "<option value=''></option>";
119  }
120 
121  foreach($this->options as $subField => $group)
122  {
123 
124  $groupName = $group["title"];
125 
126  echo "<optgroup label=\"$groupName\">\n";
127 
128  foreach($group["items"] as $item)
129  {
130  $value = "$subField:".$item->get($subField);
131  $name = $item->format($group["format"]);
132 
133  $name = htmlSafe($name);
134  $trunced = $this->max_chars ? ellipsis($name, $this->max_chars, true) : $name;
135 
136  if($current == $value)
137  {
138  $selected = " selected";
139  }
140  else
141  $selected = "";
142 
143  $optionTitle = ($name != $trunced) ? " title='$name'" : "";
144 
145  echo "<option$optionTitle value='".htmlSafe($value)."'$selected>$trunced</option>\n";
146  }
147 
148  echo "</optgroup>\n";
149  }
150 
151  echo "</select>\n";
152 
153  $this->_endField($field);
154  }
155 
156 
157  function getValue()
158  {
159  $obj = $this->parent->data;
160 
161  foreach(array_keys($this->options) as $field)
162  {
163  $value = $obj->get($field);
164  if ($value)
165  {
166  return "$field:$value";
167  }
168  }
169 
170  return "";
171  }
172 
173  function preProcess($field = "")
174  {
175  $obj = $this->parent->data;
176 
177  $value = ($this->parent->method == "POST") ? $_POST[$field] : $_GET[$field];
178 
179  list($targetField, $value) = explode(":", $value);
180 
181  foreach(array_keys($this->options) as $f)
182  {
183  if ($targetField == $f)
184  {
185  $obj->set($f, $value);
186  }
187  else
188  {
189  $obj->set($f, $this->emptyValue);
190  }
191  }
192  }
193 }
194 ?>
CompoundSelectFieldRenderer allows you to provide a consolidated grouped dropdown selector that proce...
renderField($field)
FieldRenderers must override this method to provide the HTML implementation of the control used to ed...
CompoundSelectFieldRenderer(&$form, $field, $label, $onChange)
$defaultBlank
whether the drop down includes a blank as the default
add($title, $field, $items, $format)
$onChange
optional selection change handler
preProcess($field="")
FieldRenderers can override this method to provide behavior that occurs prior to the saving of the pa...
$emptyValue
The value to which the non-selected fields should be set.
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.
ellipsis($txt, $max, $wholeWord=false)
Truncate the supplied text at the given maximum length.
Definition: functions.inc:779