Framework  3.9
related_image_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 
45 {
46  var $items;
47  var $default;
48  var $field;
53  var $onChange;
54 
56  {
57  $this->relatedClass = $relatedClass;
58  $this->constraint = $constraint;
59  $this->field = $field;
60  $this->imageField = $imageField;
61  $this->valueField = $valueField;
62  $this->allowAdd = $allowAdd;
63  $this->allowNone = $allowNone;
64  $this->max_chars = $maxChars;
65 
66  $this->getRelatedItems();
67 
68  $this->FieldRenderer($form);
69  $form->override($field, $label, $this);
70  }
71 
72  function renderField($field)
73  {
74 
75  $obj =& $this->parent->data;
76  $imgField = $this->imageField;
77 
78  if ($obj->$field)
79  {
80  $related = $obj->getRelated($this->relatedClass, $field);
81  $valueField = ($this->valueField != "") ? $this->valueField : $related->getPrimaryKey();
82  //trace("valueField: $valueField", 3);
83  $img = "<img src='".$related->get($imgField)."'/>";
84  }
85  else
86  {
87  $img = "Click to Select";
88  }
89 
90  $this->_startField($field);
91  echo "<input id='{$this->parent->id}_{$field}' type='hidden' name='$field' value='".$this->parent->data->get($field)."'><button name='{$field}_button' class='{$this->parent->buttonCSS}' onclick='{$field}_toggle(); return false;'>$img</button>";
92 
93  echo "<div style='display: none; position: absolute' id='{$field}_options'>";
94 
95  $items = $this->getRelatedItems();
96 
97  foreach($items as $item)
98  {
99  $valueField = ($this->valueField != "") ? $this->valueField : $item->getPrimaryKey();
100 
101  //trace("valueField: $valueField imageField: $imgField", 3);
102  echo "<img style='cursor: pointer; float: left' src='{$item->$imgField}' onclick='{$field}_select(\"{$item->$valueField}\", \"{$item->$imgField}\"); return false;'/>";
103  }
104 
105  echo "</div>";
106  $this->_endField($field);
107 
108  }
109 
111  {
112 ?>
113 <script type='text/javascript'>
114 /* <![CDATA[ */
115  function <?echo $field ?>_select(value, image)
116  {
117  var form = document.getElementById('<?echo $this->parent->id ?>');
118  var ctrl = form['<?echo $field ?>'];
119  var button = form['<?echo $field ?>_button'];
120  ctrl.value = value;
121  button.innerHTML = "<img src='" + image + "'/>";
122  <?echo $field ?>_hide();
123 <? if ($this->onChange)
124 { ?>
125  <?echo $this->onChange ?>(value, image);
126 <?
127 }
128  ?>
129  }
130 
131  function <?echo $field ?>_toggle()
132  {
133  var div = document.getElementById('<?echo $field ?>_options');
134  if (div.style.display == 'none')
135  {
136  div.style.display = 'block';
137  }
138  else
139  {
140  div.style.display = 'none';
141  }
142  }
143 
144  function <?echo $field ?>_hide()
145  {
146  var div = document.getElementById('<?echo $field ?>_options');
147  div.style.display = 'none';
148  }
149 
150 // ]]>
151 </script>
152 <?
153  }
154  function renderSearchField($field, $mode)
155  {
156  /*if ($mode != "equal") return;
157 
158  echo "<tr>\n";
159  $this->_printLabel($field);
160  echo "<td><select name='{$field}:$mode'>\n";
161  echo "<option value=''></option>\n";
162  $valueField = ($this->valueField != "") ? $this->valueField : $item->primary_key;
163  //trace("valueField: $valueField", 3);
164 
165  $value = $this->parent->params->get($field, $mode);
166 
167  foreach($this->items as $item)
168  {
169  $name = $this->formatName($item, $this->nameField);
170 
171  echo "<option value='{$item->$valueField}'";
172  if ($item->$valueField == $value) echo " selected";
173  echo ">".ellipsis($name, $this->max_chars)."</option>\n";
174  }
175  echo "</select>";*/
176  }
177 
179  {
180  echo "<tr>\n";
181  $this->_printLabel($field);
182  echo "<td";
183  if ($this->parent->valueCSS) echo " class='{$this->parent->valueCSS}'";
184  echo ">";
185 
186  /*foreach($this->items as $item)
187  {
188  $valueField = $this->valueField ? $this->valueField : $item->primary_key;
189 
190  if ($this->parent->data->$field == $item->$valueField)
191  {
192  $name = $this->formatName($item, $this->nameField);
193  echo $name;
194  break;
195  }
196  }*/
197 
198  echo "</td>\n";
199  echo "</tr>\n";
200  }
201 
202  function getRelatedItems()
203  {
204  $this->items = query($this->relatedClass, $this->constraint);
205  if ($this->allowNone)
206  {
207  $this->items = array_merge(array(new $this->relatedClass), $this->items);
208  }
209  return $this->items;
210  }
211 
212 
213  function preProcess($field = "")
214  {
215  /*trace("RelatedItemSelectFieldRenderer::process()", 3);
216 
217  $item = new $this->relatedClass;
218 
219  if (!$this->allowAdd) return;
220  $valueField = $valueField ? $this->valueField : $item->primary_key;
221  $field = "autoform_add_$valueField";
222  $val = $_POST[$field];
223 
224  trace("Checking for value '$val' in field $field", 3);
225 
226  if ($val)
227  {
228  foreach($this->items as $item)
229  {
230  if ($val == $this->formatName($item, $this->nameField))
231  {
232  $this->data->$valueField = $item->$valueField;
233  return;
234  }
235  }
236 
237  $obj = new $this->relatedClass;
238  $pk = $obj->primary_key;
239 
240  $nameField = $this->nameField;
241 
242  $obj->$nameField = $val;
243  $obj->save();
244  $this->parent->data->$valueField = $obj->$pk;
245  }*/
246  }
247 }
248 ?>
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.
_printLabel($field, $colspan=1, $styles="", $annotation="")
Internal method to generate the HTML for the field label.
FieldRenderer($parent)
Constructor.
query($class)
Performs a query against the database, returning an array of DataItem objects of the specified class.
Definition: query.inc:373