38 require_once realpath(dirname(__FILE__)).
"/../field_renderers.inc";
58 var $resizable =
false;
60 var $dropdownMaxWidth =
"340px";
61 var $dropdownMessage =
"Click to Select Terms";
62 var $showSelectAll =
false;
66 function CrossReferenceSelectFieldRenderer(&$form, $field, $label, $options, $nameField, $xrefClass, $xrefField =
null)
68 $this->field = $field;
69 $this->label = ($label) ? $label : $form->overrides[$field][
'label'];
71 $this->options = $options;
72 $this->nameField = $nameField;
73 $this->xrefClass = $xrefClass;
74 $this->xrefField = $xrefField;
75 $this->showSelectAll =
false;
80 if ($form->data->hasField($field))
82 $form->override($field, $label, $this);
86 $form->add($this, $field);
87 $form->overrides[$field][
'label'] = $label;
94 $foreignKey = $this->xrefField ? $this->xrefField : $options[0]->getPrimaryKey();
96 $class = get_class($form->data);
98 trace(
"## Adding CrossReferenceSearchParameterHandler for $class via $xrefClass", 3);
101 $form->setMatchingMode(
"member", $field);
106 function setSize($width, $height)
108 $this->width = $width;
109 $this->height = $height;
112 function groupBy($groupBy, $format =
"")
114 $this->groupBy = $groupBy;
115 $this->groupByFormat = $format;
120 if ($this->showSelectAll)
123 <script type=
"text/javascript">
125 function <?echo $this->field ?>_toggleSelectAll(link)
127 var value = (link.innerHTML ==
"Select All");
129 var inputs = document.getElementsByTagName(
"INPUT");
131 for(var i = 0; i < inputs.length; ++i)
133 var input = inputs[i];
134 if (input.attributes[
'id'] && input.attributes[
'id'].value.indexOf(
'<?php echo $this->parent->id?>_checkbox_<?echo $this->field?>') == 0)
136 input.checked = value;
144 document.id(
'<?echo $this->parent->id?>_<?echo $field?>_container').multiSelect.selectionChanged();
148 link.innerHTML = (value) ?
"Deselect All" :
"Select All";
160 <script type=
'text/javascript'>
161 window.addEvent(
'domready',
function()
163 new MultiSelect(
'<?echo $this->parent->id?>_<?echo $field?>_container', {maxWidth:
'<?echo $this->dropdownMaxWidth?>', message:
'<?echo jsSafe($this->dropdownMessage)?>'});
178 <script type=
'text/javascript'>
179 window.addEvent(
'domready',
function()
181 new MultiSelect(
'<?echo $this->parent->id?>_<?echo $this->field?>_container', {maxWidth:
'<?echo $this->dropdownMaxWidth?>', message:
'<?echo jsSafe($this->dropdownMessage)?>'});
188 function getMatchingPK($xref, $obj)
190 $pks = $obj->getPrimaryKeyList();
193 if ($xref->hasField($pk))
return $pk;
201 if (count($this->options) == 0)
return;
203 $field = $this->field;
207 $obj = $this->parent->data;
209 if (is_array($this->groupBy) && count($this->groupBy) > 0)
211 $keys = array_keys($this->options);
212 $rc = $this->options[$keys[0]][0];
216 $rc = $this->options[0];
219 $xref =
new $this->xrefClass;
221 $itempk = $this->getMatchingPK($xref, $rc);
222 $pk = $this->getMatchingPK($xref, $obj);
224 $selected = $this->getSelected($obj, $rc, $pk);
226 if ($this->showSelectAll)
228 echo
"<a class='select_toggle' href='#' onclick='return {$field}_toggleSelectAll(this);'>Select All</a><br/>";
230 if ($this->width && $this->height)
232 $resizable = ($this->resizable && !$this->dropdown) ?
" resizable":
"";
233 $dropdown = $this->dropdown ?
" multi_select_dropdown_list" :
"";
234 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";
238 echo
"<div id='{$this->parent->id}_{$field}' class='selectbox'>\n";
241 $countOptions = count($this->options);
243 if (is_array($this->groupBy) && count($this->groupBy) > 0)
245 $gpk = $this->groupBy[0]->getPrimaryKey();
247 foreach($this->groupBy as $group)
249 if (array_key_exists($group->get($gpk), $this->options))
251 echo $group->format($this->groupByFormat);
252 $this->renderCheckboxes($field, $this->options[$group->get($gpk)], $itempk, $selected);
256 else if (is_string($this->groupBy))
258 $options =
regroupList($this->options, $this->groupBy);
260 foreach($options as $group => $choices)
262 if (is_callable($this->groupByFormat))
264 $group = call_user_func($this->groupByFormat, $group);
266 echo
"<br/><h4>$group</h4>";
267 $this->renderCheckboxes($field, $choices, $itempk, $selected);
272 $this->renderCheckBoxes($field, $this->options, $itempk, $selected);
278 echo
"<input type=\"hidden\" name=\"count_{$field}\" value=\"$countOptions\">";
283 function getSelected($obj, $rc, $pk)
285 $itempk = $this->xrefField ? $this->xrefField: $rc->getPrimaryKey();
286 return ($obj->get($pk) !=
"") ?
indexedQuery($this->xrefClass,
"WHERE {$pk}={$obj->get($pk)}", $itempk) : array();
290 function renderCheckboxes($field, $items, $itempk, $selected, $mode =
null)
292 $name = $mode ?
"$field:$mode" : $field;
294 if ($this->columns > 1)
296 echo
"<table class='layout'><tbody><tr><td>";
297 $colheight = ceil(count($items) / $this->columns);
301 foreach($items as $item)
306 $v = $mode ? $p :
"1";
308 $disable = ($this->disable) ?
"disabled='disabled'" :
"";
309 echo
"<label for='{$this->parent->id}_checkbox_{$field}_$p'>";
310 echo
"<input type='checkbox' class='checkbox' name='{$name}[$p]' id='{$this->parent->id}_checkbox_{$field}_$p' $disable value='{$v}'";
311 if (array_key_exists($item->$itempk, $selected))
317 echo $this->
formatName($item, $this->nameField).
"</label>";
320 if ($this->columns > 1 && ($i % $colheight == 0))
323 if ($i < count($items))
330 if ($this->columns > 1)
332 echo
"</td></tr></tbody></table>\n";
333 $colheight = ceil(count($items) / $this->columns);
339 if (count($this->options) == 0)
return;
341 $field = $this->field;
345 $obj = $this->parent->data;
347 if (is_array($this->groupBy) && count($this->groupBy) > 0)
349 $keys = array_keys($this->options);
350 $rc = $this->options[$keys[0]][0];
354 $rc = $this->options[0];
357 $xref =
new $this->xrefClass;
359 $itempk = $this->getMatchingPK($xref, $rc);
360 $pk = $this->getMatchingPK($xref, $obj);
362 $selected = $this->getSelected($obj, $rc, $pk);
364 if ($this->showSelectAll)
366 echo
"<a href='#' onclick='return {$field}_toggleSelectAll(this);'>Select All</a><br/>";
368 if ($this->width && $this->height)
370 $resizable = ($this->resizable && !$this->dropdown) ?
" resizable":
"";
371 $dropdown = $this->dropdown ?
" multi_select_dropdown_list" :
"";
372 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";
376 echo
"<div id='{$this->parent->id}_{$field}' class='selectbox'>\n";
379 $countOptions = count($this->options);
381 if (is_array($this->groupBy) && count($this->groupBy) > 0)
383 $gpk = $this->groupBy[0]->getPrimaryKey();
385 foreach($this->groupBy as $group)
387 if (array_key_exists($group->get($gpk), $this->options))
389 echo $group->format($this->groupByFormat);
390 $this->renderCheckboxes($field, $this->options[$group->get($gpk)], $itempk, $selected, $mode);
394 else if (is_string($this->groupBy))
396 $options =
regroupList($this->options, $this->groupBy);
398 foreach($options as $group => $choices)
400 echo
"<br/><h4>$group</h4>";
401 $this->renderCheckboxes($field, $choices, $itempk, $selected, $mode);
406 $this->renderCheckBoxes($field, $this->options, $itempk, $selected, $mode);
412 echo
"<input type=\"hidden\" name=\"count_{$field}\" value=\"$countOptions\">";
417 function renderReadOnly($field =
"")
419 if (count($this->options) == 0)
return;
421 $xref =
new $this->xrefClass;
423 $obj = $this->parent->data;
424 $pk = $this->getMatchingPK($xref, $obj);
426 if ($obj->get($pk) ==
"")
return;
431 if (is_array($this->groupBy) && count($this->groupBy) > 0)
433 $gpk = $this->groupBy[0]->getPrimaryKey();
435 foreach($this->groupBy as $group)
437 if (array_key_exists($group->get($gpk), $this->options))
439 echo
"<span class='cross_reference_group'>".$group->format($this->groupByFormat).
"</span>";
440 foreach($this->options[$group->get($gpk)] as $item)
442 if (array_key_exists($item->$optpk, $items))
444 echo $this->
formatName($item, $this->nameField).
"<br/>";
452 $optpk = $this->getMatchingPK($xref, $this->options[0]);
454 $items =
indexedQuery($this->xrefClass,
"WHERE {$pk}=".$obj->get($pk), $optpk);
456 foreach($this->options as $item)
458 if (array_key_exists($item->$optpk, $items))
460 echo $this->
formatName($item, $this->nameField).
"<br/>";
470 if (count($this->options) == 0)
return;
472 $xref =
new $this->xrefClass;
474 $obj = $this->parent->data;
475 $pk = $this->getMatchingPK($xref, $obj);
477 if (is_array($this->groupBy) && count($this->groupBy) > 0)
479 $keys = array_keys($this->options);
480 $rc = $this->options[$keys[0]][0];
484 $rc = $this->options[0];
487 $optpk = $this->xrefField ? $this->xrefField : $this->getMatchingPK($xref, $rc);
489 $this->clearOutRelations($obj, $pk);
491 if (isset($_POST[$this->field]))
493 foreach($_POST[$this->field] as $optionpk => $dummy)
495 $xref =
new $this->xrefClass;
496 $xref->set($pk, $obj->get($pk));
497 $xref->set($optpk, $optionpk);
509 function clearOutRelations($obj, $pk)
511 $xref =
new $this->xrefClass;
512 $xref->delete(
"WHERE {$pk}=".$obj->get($pk));
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)
renderField($field)
FieldRenderers must override this method to provide the HTML implementation of the control used to ed...
renderSearchScript($field, $mode)
FieldRenderers can override this method to provide any Javascript that the control requires when bein...
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.
renderScript($field)
FieldRenderers can override this method to provide any Javascript that their control requires for an ...
renderSearchField($field, $mode)
FieldRenderers must override this method to provide the HTML implementation of the control displayed ...
FieldRenderer($parent)
Constructor.
postProcess($field="")
FieldRenderers can override this method to provide behavior that occurs after the parent form's targe...
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
regroupList($list, $field)
Regroup an indexed or grouped query result by a different field.
indexedQuery($class)
Performs a query against the database, returning an array of DataItem objects of the specified class,...