Framework  3.9
subselect_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 
47 {
48  var $super;
49  var $options;
50  var $format;
51 
52  function SubSelectFieldRenderer(&$form, $field, $label, $super, $options, $format)
53  {
54  $this->options = $options;
55  $this->super = $super;
56  $this->format = $format;
57  $this->FieldRenderer($form);
58  if ($form->data->hasField($field))
59  {
60  // JDG 5/30/11 - don't crash on additiona/psuedo fields
61  // only query if real field
62  $form->override($field, $label, $this);
63  }
64  else
65  {
66  $form->add($this, $field);
67  // JDG 7/22/2011 - allow "additional" fields to override label
68  $form->overrides[$field]['label'] = $label;
69  }
70  }
71 
77  function extraControls($field)
78  {
79 
80  }
81 
82  function renderField($field)
83  {
84  $this->_startField($field);
85 
86  if($this->width)
87  {
88  $style = "style='width: {$this->width}'";
89  }
90 
91  echo "<span class='subselect_container'>";
92  echo "<select $style name='$field' id='{$this->parent->id}_{$field}'";
93 
94  if ($this->onChange != "")
95  {
96  echo " onchange='$this->onChange(this)'";
97  }
98 
99  echo ">\n";
100  echo "</select>\n";
101 
102  $this->extraControls($field);
103 
104  echo "</span>";
105 
106  $this->_endField($field);
107  }
108 
109  function renderSearchField($field, $mode, $value = "")
110  {
111  if ($mode != "equal") $mode = "equal";
112 
113  $searchValue = (!$value) ? $this->parent->params->get($field, $mode) : $value;
114 
115  $this->_startField($field);
116 
117  echo "<select id='$field' name='$field:$mode'><option value=''></option>\n";
118  foreach($this->options as $value => $name)
119  {
120  $selected = ($searchValue == $value) ? " selected" : "";
121 
122  echo "<option value='".htmlSafe($value)."'$selected>$name</option>";
123  }
124 
125  echo "</select>";
126 
127  $this->_endField($field);
128  }
129 
130 
131  function renderReadOnly($field)
132  {
133  $super_id = $this->parent->data->get($this->super);
134 
135  if (!array_key_exists($super_id, $this->options)) return;
136 
137  $this->_startField($field);
138 
139  $item = findItem($this->options[$super_id], $field, $this->parent->data->get($field));
140 
141  if ($item)
142  {
143  echo $item->format($this->format);
144  }
145  else
146  {
147  echo "Not specified";
148  }
149 
150  $this->_endField($field);
151  }
152 
153  function renderScript($field)
154  {
155  if ($this->parent->readOnlyForm || $this->parent->isReadOnly($field)
156  || array_key_exists($field, $this->parent->hidden)) return "";
157 
158  $mgr = "{$this->parent->id}_{$field}_mgr";
159  $super_id = "{$this->parent->id}_{$this->super}";
160  $value = $this->parent->data->get($field);
161  $this->setFormatTemplate();
162 ?>
163 <script type="text/javascript">
164 window.addEvent('domready', function()
165 {
166  var <?echo $mgr?> = new SubSelectManager('<?echo $super_id?>', '<?echo $this->parent->id?>_<?echo $field?>', '<?echo $value?>');
167 <?
168  foreach($this->options as $key => $options)
169  {
170  $pk = $options[0]->getPrimaryKey();
171  $values = formatItems($options, "{'name': '{SubSelectFieldRenderer::formatOption}', 'value': {{$pk}} }", ", ");
172 ?>
173  <?echo $mgr?>.addOptions(<?echo $key?>, [{'name': '', 'value': 0}, <?echo $values?>]);
174 <?
175  }
176 ?>
177  <?echo $mgr?>.update();
178 });
179 </script>
180 <?
181  }
182 
189  function setFormatTemplate()
190  {
191  foreach($this->options as $key => $options)
192  {
193  foreach($options as $option)
194  {
195  $option->set("_formatTemplate", $this->format);
196  }
197  }
198  }
199 
200  static function formatOption($option)
201  {
202  $template = $option->get("_formatTemplate");
203  return jsSafe($option->format("{$template}"));
204  }
205 
206  function preProcess($field = "")
207  {
208  if(!$this->parent->data->hasField($this->super)) return;
209 
210  $super_id = $this->parent->data->get($this->super);
211 
212  if (!array_key_exists($super_id, $this->options))
213  {
214  $this->parent->data->set($field, 0);
215  }
216  }
217 
218 } // end class SubSelectFieldRenderer
219 ?>
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.
SubSelectFieldRenderer - provides a second-level hierarchical selection mechanism based on the result...
SubSelectFieldRenderer(&$form, $field, $label, $super, $options, $format)
renderSearchField($field, $mode, $value="")
preProcess($field="")
FieldRenderers can override this method to provide behavior that occurs prior to the saving of the pa...
extraControls($field)
Override this function to provide extra editing controls that follow the visibility rules of the main...
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...
setFormatTemplate()
We need to save the class var format into the object for use in the static call to formatOption.
jsSafe($str, $escapeEntities=false)
Utility function to escape a string correctly for use in a Javascript client-side call.
Definition: functions.inc:434
findItem($items, $field, $value)
Find the first item in the array that matches the supplied value for the specified field.
Definition: data_item.inc:2261
formatItems($items, $template, $separator="")
Format a list of DataItems using the specified templated.
Definition: data_item.inc:2176