38 require_once realpath(dirname(__FILE__)).
"/../field_renderers.inc";
76 var $defaultBlank =
false;
77 var $defaultText =
"Choose One";
78 var $defaultValue =
"";
79 var $allowDeselect =
false;
82 var $stripHTML =
false;
84 function SelectFieldRenderer(&$form, $field, $label, $options =
null, $onChange=
"", $grouped =
false)
86 $this->options = $options;
87 $this->onChange = $onChange;
88 $this->grouped = $grouped;
90 if ($form->getData()->hasField($field))
94 $form->override($field, $label, $this);
95 if ($this->options ==
null)
97 $opt = $form->getData()->distinctValues($field,
true);
100 $this->options = array_combine($opt, $opt);
104 $this->options = array();
110 $form->add($this, $field);
112 $form->overrides[$field][
'label'] = $label;
121 function setMaxChars($max)
123 $this->max_chars = $max;
132 function allowAddEntry($text =
"or add new")
134 $this->addEntry =
true;
135 $this->addEntryText = $text;
136 $this->addOtherOption();
144 function allowBlank($text =
"", $value =
"")
146 if (!$this->grouped && !$text) $text =
"Choose One";
147 $this->defaultBlank =
true;
148 $this->defaultText = $text;
149 $this->defaultValue = $value;
159 function addDefaultOption($field)
161 $val = $this->parent->data->get($field);
162 if(!$this->defaultBlank || ($val && !$this->allowDeselect))
return;
164 if(count($this->options) && !array_search(
"", $this->options))
167 $options[$this->defaultValue] = $val ?
"" : $this->defaultText;
168 foreach($this->options as $key => $value)
170 $options[$key] = $value;
172 $this->options = $options;
181 function extendOptions($options)
185 throw new FakoliException(
"SelectFieldRenderer::extendOptions() not supported in grouped mode");
188 foreach($options as $value => $text)
190 if (!array_key_exists($value, $this->options))
192 $this->options[$value] = $text;
201 if (count($this->options) > 0 || !$this->addEntry)
203 $cssClass = $this->CSSclass ?
" class='{$this->CSSclass}'" :
"";
205 $style =
"style='width: {$this->width}'";
206 echo
"<select {$class}{$style} name='$field' id='{$this->parent->id}_{$field}'";
208 if ($this->onChange !=
"")
209 echo
" onchange='$this->onChange(this)'";
210 elseif($this->addEntry)
211 echo
" onchange='onChange_{$this->parent->id}_{$field}(this)'";
220 $current = $this->parent->data->get($field);
222 if ($this->parent->data->getType($field) ==
'String')
224 $comparator =
function($a, $b) {
return (strcmp($a, $b) == 0); };
228 $comparator =
function($a, $b) {
return $a == $b; };
233 if ($this->defaultBlank)
235 echo
"<option value=''>{$this->defaultText}</option>\n";
240 foreach($this->options as $groupName => $group)
242 if ($groupName ==
"")
248 echo
"<optgroup label=\"$groupName\">\n";
250 foreach($group as $value => $name)
252 $name = ($this->stripHTML) ?
stripHTML($name) : htmlSafe($name);
253 $trunced = $this->max_chars ?
ellipsis($name, $this->max_chars,
true) : $name;
255 if ($comparator($current, $value))
257 $selected =
" selected";
263 trace(
"$current == $value ".($current==$value).
" $selected", 3);
265 $optionTitle = ($name != $trunced) ?
" title='$name'" :
"";
267 echo
"<option$optionTitle value='".htmlSafe($value).
"'$selected>$trunced</option>\n";
270 echo
"</optgroup>\n";
280 if(!array_key_exists($current, $this->options))
282 $addEntryValue = $current;
286 $this->addDefaultOption($field);
288 foreach($this->options as $value => $name)
290 $name = ($this->stripHTML) ?
stripHTML($name) : htmlSafe($name);
291 $trunced = $this->max_chars ?
ellipsis($name, $this->max_chars,
true) : $name;
293 if($comparator($current, $value))
295 $selected =
" selected";
300 $optionTitle = ($name != $trunced) ?
" title='$name'" :
"";
302 echo
"<option$optionTitle value='".htmlSafe($value).
"'$selected>$trunced</option>\n";
314 $type = $this->addEntry ?
'text' :
'hidden';
318 echo
" {$this->addEntryText} ";
319 $onChange =
" onchange='onChange_{$this->parent->id}_{$field}_addEntry(this)'";
322 echo
"<input type='$type' name='{$field}_addEntry' class='add_entry' id='{$field}_addEntry' value='{$addEntryValue}' size='20' $onChange/>";
329 if ($mode !=
"equal" && $mode !=
"member") $mode =
"equal";
331 $searchValue = (!$value) ? $this->parent->params->get($field, $mode) : $value;
337 $style =
"style='width: {$this->width}'";
340 echo
"<select $style id='$field' name='$field:$mode'";
342 if ($this->onChange !=
"")
343 echo
" onchange='$this->onChange(this)'";
345 echo
"><option value=''></option>\n";
346 foreach($this->options as $value => $name)
348 $selected = ($searchValue === $value) ?
" selected" :
"";
349 $name = ($this->stripHTML) ?
stripHTML($name) : htmlSafe($name);
351 echo
"<option value='".htmlSafe($value).
"'$selected>$name</option>";
359 function renderReadOnly($field)
363 $value = $this->parent->data->get($field);
367 foreach($this->options as $group => $options)
369 if (array_key_exists($value, $options))
371 echo $options[$value];
378 echo
"Not specified";
383 if (array_key_exists($value, $this->options))
385 echo $this->options[$value];
393 echo
"Not specified";
404 $current = $this->parent->data->get($field);
408 foreach($this->options as $groupName => $group)
410 foreach($group as $value => $name)
412 if($current == $value && $value !=
'')
419 foreach($this->options as $value => $name)
421 if($current == $value && $value !=
'')
426 $addEntry = $_POST[
"{$field}_addEntry"];
427 trace(
"addEntry($field): $addEntry and found $found", 3);
429 if ($_POST[
"{$field}_addEntry"] && !$found)
431 trace(
"setting field $field with add entry value $addEntry", 3);
432 $this->parent->data->set($field, $addEntry);
448 $fn =
"{$this->parent->id}_$field";
449 $addEntry = $field .
"_addEntry";
451 if(!$this->onChange && $this->addEntry)
454 <script type=
"text/javascript">
455 function onChange_<?echo $fn?>(elt)
459 var addEntry = document.id(
'<?php echo $addEntry ?>');
471 <script type=
"text/javascript">
472 function onChange_<?echo
"{$fn}_addEntry" ?>(elt)
476 var selected = document.id(
'<?php echo $field ?>');
492 function addOtherOption($text =
"Other")
494 if(!array_key_exists(
"", $this->options))
496 $this->options[
""] = $text;
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)
preProcess($field="")
FieldRenderers can override this method to provide behavior that occurs prior to the saving of the pa...
renderField($field)
FieldRenderers must override this method to provide the HTML implementation of the control used to ed...
_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.
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
ellipsis($txt, $max, $wholeWord=false)
Truncate the supplied text at the given maximum length.