Framework  3.9
RelatedItemCheckListFieldRenderer Class Reference

Field renderer for handling one-to-many relationships that are implemented through a back-reference table. More...

+ Inheritance diagram for RelatedItemCheckListFieldRenderer:
+ Collaboration diagram for RelatedItemCheckListFieldRenderer:

Public Member Functions

 __construct (&$form, $field, $label, $relatedClass, $constraint, $nameField)
 
 setSize ($width, $height)
 
 groupBy ($groupBy, $format="")
 
 getMatchingPK ($relatedClass, $obj)
 Find the field in the DataItem obj displayed in the AutoForm that exists in the back-reference class e.g., if ProductCategory AutoForm's obj, and products are the option items displayed, then product_category_id would exist in the Product table. More...
 
 renderField ($field="")
 FieldRenderers must override this method to provide the HTML implementation of the control used to edit the field. More...
 
 getRelatedItems ()
 
 getSelected ($obj, $itempk, $pk)
 
 renderCheckboxes ($field, $items, $pk, $selected)
 
 renderReadOnly ($field="")
 
 postProcess ($field="")
 FieldRenderers can override this method to provide behavior that occurs after the parent form's target object has been saved to the database. More...
 
- Public Member Functions inherited from FieldRenderer
 FieldRenderer ($parent)
 Constructor. More...
 
 _printLabel ($field, $colspan=1, $styles="", $annotation="")
 Internal method to generate the HTML for the field label. More...
 
 _getLabel ($field, $addSuffix=true)
 
 _startField ($field, $styles="")
 Internal method to generate the starting HTML for the field (including the label) More...
 
 _endField ($field)
 Internal method to generate the closing HTML for the field. More...
 
 addValidatorsToForm ($field, $required=false)
 This method is called by the AutoForm to add any default input validators that are required by the FieldRenderer. More...
 
 addSearchValidatorsToForm ($field, $mode, $required=false)
 For SearchForm, the validator field needs to match the name tag in the form which is in the format field:mode. More...
 
 formatName ($item, $name)
 Formats the given DataItem based on the supplied format string. More...
 
 renderScript ($field)
 FieldRenderers can override this method to provide any Javascript that their control requires for an edit form. More...
 
 renderSearchScript ($field, $mode)
 FieldRenderers can override this method to provide any Javascript that the control requires when being used in a search form. More...
 
 renderSearchField ($field, $mode)
 FieldRenderers must override this method to provide the HTML implementation of the control displayed for the field in a search form. More...
 
 renderOnSubmitHandler ($field)
 FieldRenderers can override this method to provide any Javascript that must be executed when the form is submitted on the client. More...
 
 preProcess ($field="")
 FieldRenderers can override this method to provide behavior that occurs prior to the saving of the parent form's target object to the database. More...
 

Public Attributes

 $relatedClass
 
 $constraint
 
 $nameField
 
 $field
 
 $label
 
 $options
 
 $groupBy
 
- Public Attributes inherited from FieldRenderer
 $parent = null
 
 $labelSuffix = ""
 
 $colspan = 1
 
 $annotateBefore = false
 
 $annotateNextLine = true
 
 $hideLabel = false
 
 $onPreProcess = null
 callback hook for processing prior to saving the form's data object - individual renderers may override with custom processing More...
 
 $onPostProcess = null
 callback hook for processing after saving the form's data object - individual renderers may override with custom processing More...
 

Detailed Description

Field renderer for handling one-to-many relationships that are implemented through a back-reference table.

The postProcess() method is used to automatically update the table records when the form is submitted.

e.g., AutoForm renders instance of ProductCategory containing a RelatedItemCheckListFieldRenderer additional field "products" to select the products that belong to the category. Selections are saved to the product table's field product_category_id.

Definition at line 51 of file related_item_checklist_field_renderer.inc.

Constructor & Destructor Documentation

◆ __construct()

RelatedItemCheckListFieldRenderer::__construct ( $form,
  $field,
  $label,
  $relatedClass,
  $constraint,
  $nameField 
)

Definition at line 61 of file related_item_checklist_field_renderer.inc.

62  {
63  $this->field = $field;
64  $this->label = $label;
65  $this->nameField = $nameField;
66  $this->relatedClass = $relatedClass;
67  $this->constraint = $constraint;
68  $this->options = $this->getRelatedItems();
69 
70  $this->FieldRenderer($form);
71 
72  if ($form->data->hasField($field))
73  {
74  $form->override($field, $label, $this);
75  }
76  else
77  {
78  $form->add($this, $field);
79  $form->overrides[$field]['label'] = $label;
80  }
81  }
FieldRenderer($parent)
Constructor.

Member Function Documentation

◆ getMatchingPK()

RelatedItemCheckListFieldRenderer::getMatchingPK (   $relatedClass,
  $obj 
)

Find the field in the DataItem obj displayed in the AutoForm that exists in the back-reference class e.g., if ProductCategory AutoForm's obj, and products are the option items displayed, then product_category_id would exist in the Product table.

Parameters
unknown$relatedClass
unknown$obj
Exceptions
DataItemException
Returns
unknown

Definition at line 106 of file related_item_checklist_field_renderer.inc.

107  {
108  $pks = $obj->getPrimaryKeyList();
109  foreach($pks as $pk)
110  {
111  if ($relatedClass->hasField($pk)) return $pk;
112  }
113 
114  throw new DataItemException("No matching foreign key in related table {$relatedClass->table}");
115  }

◆ getRelatedItems()

RelatedItemCheckListFieldRenderer::getRelatedItems ( )

Definition at line 182 of file related_item_checklist_field_renderer.inc.

183  {
184  return Query::create($this->relatedClass, $this->constraint)->execute();
185  }
static create($class, $constraints="")
Static factory method to create a new Query.
Definition: query.inc:358

◆ getSelected()

RelatedItemCheckListFieldRenderer::getSelected (   $obj,
  $itempk,
  $pk 
)
Parameters
obj$objDataItem class in AutoForm
String$itempkback-reference key of the related class
String$pk- primary key of the back-reference related table
Returns
array of objects of related class

Definition at line 194 of file related_item_checklist_field_renderer.inc.

195  {
196  return IndexedQuery::create($this->relatedClass, "WHERE {$itempk}=:{$itempk}", $pk)
197  ->bind("{$itempk}", $obj->get($itempk))
198  ->execute();
199  }
static create($class, $constraints="", $indexBy="")

◆ groupBy()

RelatedItemCheckListFieldRenderer::groupBy (   $groupBy,
  $format = "" 
)

Definition at line 89 of file related_item_checklist_field_renderer.inc.

90  {
91  $this->groupBy = $groupBy;
92  $this->groupByFormat = $format;
93  }

◆ postProcess()

RelatedItemCheckListFieldRenderer::postProcess (   $field = "")

FieldRenderers can override this method to provide behavior that occurs after the parent form's target object has been saved to the database.

For example, the CrossReferenceSelectFieldRenderer overrides this method to update crossference tables based on the user's selection.

Parameters
string$field

Save each checked item to the back-reference related table

Remove the link from the back-reference related table to the form's data obj if no longer selected.

Reimplemented from FieldRenderer.

Definition at line 242 of file related_item_checklist_field_renderer.inc.

243  {
244  if (count($this->options) == 0) return;
245 
246  $option_pk = $this->options[0]->getPrimaryKey();
247  $obj_pk = $this->parent->data->getPrimaryKey();
248  $obj_id = $this->parent->data->$obj_pk;
249 
250  $items = reindexList($this->options, $option_pk);
251 
252  $selected = $_POST[$this->field];
253 
258  foreach($selected as $selected_pk => $value)
259  {
260  if(!array_key_exists($selected_pk, $items)) continue;
261 
262  $item = $items[$selected_pk];
263  $item->filter = new InclusionFilter($obj_pk);
264  $item->$obj_pk= $obj_id;
265  $item->save();
266  }
267 
272  foreach($items as $item_id => $item)
273  {
274  if($item->$obj_pk == $obj_id && !array_key_exists($item->$option_pk, $selected))
275  {
276  $item->filter = new InclusionFilter($obj_pk);
277  $item->$obj_pk = 0;
278  $item->save();
279  }
280  }
281  }
Used to place a filter on the contents of a DataItem-derived object.
reindexList($list, $field, $autoPromote=true)
Reindex an indexed or grouped query result by a different field.
Definition: data_item.inc:1898

◆ renderCheckboxes()

RelatedItemCheckListFieldRenderer::renderCheckboxes (   $field,
  $items,
  $pk,
  $selected 
)

Definition at line 201 of file related_item_checklist_field_renderer.inc.

202  {
203  foreach($items as $item)
204  {
205  $item_id = $item->$pk;
206 
207  echo "<input type='checkbox' class='checkbox' name='{$field}[$item_id]' id='checkbox_{$field}_$item_id' value='1'";
208  if (array_key_exists($item_id, $selected))
209  {
210  echo " checked";
211  }
212  echo ">&nbsp;";
213 
214  echo "<label for='checkbox_{$field}_$item_id'>".$this->formatName($item, $this->nameField)."</label>";
215  echo "<br/>";
216  }
217  }

◆ renderField()

RelatedItemCheckListFieldRenderer::renderField (   $field = "")

FieldRenderers must override this method to provide the HTML implementation of the control used to edit the field.

Parameters
string$fieldthe field name

Reimplemented from FieldRenderer.

Definition at line 117 of file related_item_checklist_field_renderer.inc.

118  {
119  if (count($this->options) == 0) return;
120 
122 
123  $this->_startField($field);
124 
125  $obj = $this->parent->data;
126 
127  if (is_array($this->groupBy) && count($this->groupBy) > 0)
128  {
129  $keys = array_keys($this->options);
130  }
131 
132  $itempk = $this->getMatchingPK($this->options[0], $obj);
133  $related_pk = $this->options[0]->getPrimaryKey();
134  $selected = $this->getSelected($obj, $itempk, $related_pk);
135 
136  if ($this->width && $this->height)
137  {
138  echo "<div id='{$this->parent->id}_{$field}_container' class='selectbox scrollbox{$resizable}{$dropdown}' style='width: {$this->width}px; height: {$this->height}px; overflow: auto'>\n";
139  }
140  else
141  {
142  echo "<div class='selectbox'>\n";
143  }
144  $countOptions = count($this->options);
145 
146  if (is_array($this->groupBy) && count($this->groupBy) > 0)
147  {
148  $gpk = $this->groupBy[0]->getPrimaryKey();
149 
150  foreach($this->groupBy as $group)
151  {
152  if (array_key_exists($group->get($gpk), $this->options))
153  {
154  echo $group->format($this->groupByFormat);
155  $this->renderCheckboxes($field, $this->options[$group->get($gpk)], $related_pk, $selected);
156  }
157  }
158  }
159  else if (is_string($this->groupBy))
160  {
161  $options = regroupList($this->options, $this->groupBy);
162 
163  foreach($options as $group => $choices)
164  {
165  echo "<br/><h4>$group</h4>";
166  $this->renderCheckboxes($field, $choices, $related_pk, $selected);
167  }
168  }
169  else
170  {
171  $this->renderCheckBoxes($field, $this->options, $related_pk, $selected);
172  }
173 
174  echo "</div>\n";
175 
176  // Save this count to loop through checkboxes on required validation
177  echo "<input type=\"hidden\" name=\"count_{$field}\" value=\"$countOptions\">";
178 
179  $this->_endField($field);
180  }
_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.
regroupList($list, $field)
Regroup an indexed or grouped query result by a different field.
Definition: data_item.inc:1948

◆ renderReadOnly()

RelatedItemCheckListFieldRenderer::renderReadOnly (   $field = "")

Definition at line 219 of file related_item_checklist_field_renderer.inc.

220  {
221  if (count($this->options) == 0) return;
222 
223  $obj = $this->parent->data;
224  $itempk = $this->getMatchingPK($this->options[0], $obj);
225  $related_pk = $this->options[0]->getPrimaryKey();
226  $selected = $this->getSelected($obj, $itempk, $related_pk);
227 
228 
229  $this->_startField($field);
230 
231  foreach($this->options as $item)
232  {
233  if (array_key_exists($item->$related_pk, $selected))
234  {
235  echo $this->formatName($item, $this->nameField)."<br/>";
236  }
237  }
238 
239  $this->_endField($field);
240  }
formatName($item, $name)
Formats the given DataItem based on the supplied format string.

◆ setSize()

RelatedItemCheckListFieldRenderer::setSize (   $width,
  $height 
)

Definition at line 83 of file related_item_checklist_field_renderer.inc.

84  {
85  $this->width = $width;
86  $this->height = $height;
87  }

Member Data Documentation

◆ $constraint

RelatedItemCheckListFieldRenderer::$constraint

Definition at line 54 of file related_item_checklist_field_renderer.inc.

◆ $field

RelatedItemCheckListFieldRenderer::$field

Definition at line 56 of file related_item_checklist_field_renderer.inc.

◆ $groupBy

RelatedItemCheckListFieldRenderer::$groupBy

Definition at line 59 of file related_item_checklist_field_renderer.inc.

◆ $label

RelatedItemCheckListFieldRenderer::$label

Definition at line 57 of file related_item_checklist_field_renderer.inc.

◆ $nameField

RelatedItemCheckListFieldRenderer::$nameField

Definition at line 55 of file related_item_checklist_field_renderer.inc.

◆ $options

RelatedItemCheckListFieldRenderer::$options

Definition at line 58 of file related_item_checklist_field_renderer.inc.

◆ $relatedClass

RelatedItemCheckListFieldRenderer::$relatedClass

Definition at line 53 of file related_item_checklist_field_renderer.inc.


The documentation for this class was generated from the following file: