Framework  3.9
related_item_radio_button_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;
54 
56  {
57  $this->relatedClass = $relatedClass;
58  $this->constraint = $constraint;
59  $this->field = $field;
60  $this->nameField = $nameField;
61  $this->valueField = $valueField;
62  $this->horizontal = true;
63 
64  $this->getRelatedItems();
65 
66  $this->FieldRenderer($form);
67  $form->override($field, $label, $this);
68  }
69 
70  function getRelatedItems()
71  {
72  $this->items = query($this->relatedClass, $this->constraint);
73  return $this->items;
74  }
75 
76  function renderField($field)
77  {
78  $this->_startField($field);
79 
80  $current = $this->parent->data->get($field);
81 
82  foreach($this->items as $item)
83  {
84  $name = $this->formatName($item, $this->nameField);
85  $valueField = ($this->valueField != "") ? $this->valueField : $item->getPrimaryKey();
86  //trace("valueField: $valueField", 3);
87 
88  echo "<input type='radio' name='{$field}' value='{$item->$valueField}'";
89  if ($item->get($valueField) == $current) echo " selected";
90  echo ">&nbsp;$name\n";
91 
92  echo ($this->horizontal) ? "&nbsp;&nbsp;&nbsp;" : "<br/>";
93  }
94 
95  $this->_endField($field);
96  }
97 
98  function renderSearchField($field, $mode)
99  {
100  if ($mode != "equal") return;
101 
102  echo "<tr>\n";
103  $this->_printLabel($field);
104  echo "<td>";
105  $valueField = ($this->valueField != "") ? $this->valueField : $item->primary_key;
106  //trace("valueField: $valueField", 3);
107 
108  $value = $this->parent->params->get($field, $mode);
109  $current = $this->parent->data->get($field);
110 
111  foreach($this->items as $item)
112  {
113  $name = $this->formatName($item, $this->nameField);
114 
115  echo "<input type='radio' name='{$field}:$mode' value='{$value}'";
116  if ($item->get($valueField) == $current) echo " selected";
117  echo ">&nbsp;$name\n";
118 
119  echo ($this->horizontal) ? "&nbsp;&nbsp;&nbsp;" : "<br/>";
120  }
121  echo "</select>";
122  }
123 
125  {
126  $this->_startField($field);
127 
128  foreach($this->items as $item)
129  {
130  $valueField = $this->valueField ? $this->valueField : $item->getPrimaryKey();
131 
132  if ($this->parent->data->get($field) == $item->get($valueField))
133  {
134  $name = $this->formatName($item, $this->nameField);
135  echo $name;
136  break;
137  }
138  }
139  $this->_endField($field);
140  }
141 }
142 ?>
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)
formatName($item, $name)
Formats the given DataItem based on the supplied format string.
_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