function _getListOptionLabelByValue($fieldSchema, $record) { global $TABLE_PREFIX, $tableName; $fieldname = $fieldSchema['name']; $fieldValue = @$record[@$fieldname]; $output = ''; // build value to label map $listOptions = getListOptionsFromSchema($fieldSchema, $record); $valuesToLabels = array(); foreach ($listOptions as $valueAndLabel) { list($value, $label) = $valueAndLabel; $valuesToLabels[$value] = $label; } // if this is a multi-value list field, look up each value and comma separate them if (@$fieldSchema['listType'] == 'checkboxes' || @$fieldSchema['listType'] == 'pulldownMulti') { $labels = array(); foreach (preg_split('/\\t/', trim($fieldValue)) as $value) { $labels[] = @$valuesToLabels[$value] ? $valuesToLabels[$value] : $value; // if lookup fails, use value } return join(', ', $labels); } else { return array_key_exists($fieldValue, $valuesToLabels) ? $valuesToLabels[$fieldValue] : $fieldValue; // if lookup fails, use value } }
function editFormHtml($record) { // set field attributes $listOptions = getListOptionsFromSchema($this, $record); $valignTop = $this->listType != 'pulldown' ? 'style="vertical-align: top;"' : ''; $prefixText = @$this->fieldPrefix; $description = getEvalOutput(@$this->description); // get field value if ($record) { $fieldValue = @$record[$this->name]; } else { if (array_key_exists($this->name, $_REQUEST)) { $fieldValue = join("\t", (array) @$_REQUEST[$this->name]); } else { $fieldValue = ''; } } $fieldValues = preg_split("/\t/", $fieldValue, -1, PREG_SPLIT_NO_EMPTY); // for multi value fields $encodedValue = htmlencode($fieldValue); // get list of values in database that aren't in list options (happens when list values are removed or field // ... was a textfield than switched to a pulldown that doesn't offer all the previously entered values as options $fieldValuesNotInList = array(); $listOptionValues = array(); foreach ($listOptions as $optionArray) { list($value, $label) = $optionArray; $listOptionValues[] = $value; } $fieldValuesNotInList = array_diff($fieldValues, $listOptionValues); $noLongerInListText = count($fieldValuesNotInList) > 1 ? t('Previous selections (no longer in list)') : t('Previous selection (no longer in list)'); // print " <tr>\n"; print " <td {$valignTop}>{$this->label}</td>\n"; print " <td>\n"; // pulldown if ($this->listType == 'pulldown') { print "{$prefixText}\n"; print " <select name='{$this->name}'>\n"; print " <option value=''><select></option>\n"; foreach ($listOptions as $optionArray) { list($value, $label) = $optionArray; $encodedValue = htmlencode($value); $selectedAttr = selectedIf($value, $fieldValue, true); $encodedLabel = htmlencode($label); print "<option value=\"{$encodedValue}\" {$selectedAttr}>{$encodedLabel}</option>\n"; } // show database values not in current list options if ($fieldValuesNotInList) { print " <optgroup label='{$noLongerInListText}'>\n"; foreach ($fieldValuesNotInList as $value) { print " <option value=\"" . htmlencode($value) . "\" selected='selected'>" . htmlencode($value) . "</option>\n"; } print " </optgroup>\n"; } print " </select>\n"; print "{$description}\n"; } else { if ($this->listType == 'pulldownMulti') { if ($prefixText) { print "{$prefixText}<br/>\n"; } print " <select name='{$this->name}[]' multiple='multiple' size='5'>\n"; foreach ($listOptions as $optionArray) { list($value, $label) = $optionArray; $encodedValue = htmlencode($value); $selectedAttr = in_array($value, $fieldValues) ? 'selected="selected"' : ''; $encodedLabel = htmlencode($label); print "<option value=\"{$encodedValue}\" {$selectedAttr}>{$encodedLabel}</option>\n"; } // show database values not in current list options if ($fieldValuesNotInList) { print " <optgroup label='{$noLongerInListText}'>\n"; foreach ($fieldValuesNotInList as $value) { print " <option value=\"" . htmlencode($value) . "\" selected='selected'>" . htmlencode($value) . "</option>\n"; } print " </optgroup>\n"; } print " </select>\n"; if ($description) { print "<br/>{$description}\n"; } } else { if ($this->listType == 'radios') { if ($prefixText) { print "{$prefixText}<br/>\n"; } foreach ($listOptions as $optionArray) { list($value, $label) = $optionArray; $encodedValue = htmlencode($value); $encodedLabel = htmlencode($label); $checkedAttr = $value == $fieldValue ? 'checked="checked"' : ''; $idAttr = "{$this->name}.{$encodedValue}"; print "<input type='radio' name='{$this->name}' value='{$encodedValue}' id='{$idAttr}' {$checkedAttr}/>\n"; print "<label for='{$idAttr}'>{$encodedLabel}</label><br />\n\n"; } // show database values not in current list options if ($fieldValuesNotInList) { print "{$noLongerInListText}<br />\n"; foreach ($fieldValuesNotInList as $value) { $encodedValue = htmlencode($value); $encodedLabel = htmlencode($value); $idAttr = "{$this->name}.{$encodedValue}"; print "<input type='radio' name='{$this->name}' value='{$encodedValue}' id='{$idAttr}' checked='checked'/>\n"; print "<label for='{$idAttr}'>{$encodedLabel}</label><br />\n\n"; } } if ($description) { print "{$description}\n"; } } else { if ($this->listType == 'checkboxes') { if ($prefixText) { print "{$prefixText}<br/>\n"; } foreach ($listOptions as $optionArray) { list($value, $label) = $optionArray; $encodedValue = htmlencode($value); $encodedLabel = htmlencode($label); $checkedAttr = in_array($value, $fieldValues) ? 'checked="checked"' : ''; $idAttr = "{$this->name}.{$encodedValue}"; print "<input type='checkbox' name='{$this->name}[]' value='{$encodedValue}' id='{$idAttr}' {$checkedAttr}/>\n"; print "<label for='{$idAttr}'>{$encodedLabel}</label><br />\n"; } // show database values not in current list options if ($fieldValuesNotInList) { print "{$noLongerInListText}<br />\n"; foreach ($fieldValuesNotInList as $value) { $encodedValue = htmlencode($value); $encodedLabel = htmlencode($value); $idAttr = "{$this->name}.{$encodedValue}"; print "<input type='checkbox' name='{$this->name}[]' value='{$encodedValue}' id='{$idAttr}' checked='checked' />\n"; print "<label for='{$idAttr}'>{$encodedLabel}</label><br />\n\n"; } } if ($description) { print "{$description}\n"; } } else { die("Unknown listType '{$this->listType}'!"); } } } } // list fields w/ advanced filters - add onchange event handler to local filter field if (@$this->filterField) { ?> <script type="text/javascript"><!-- $("[name='<?php echo $this->filterField; ?> ']").change(function () { var targetListField = '<?php echo $this->name; ?> '; var newFilterValue = this.value; updateListFieldOptions(targetListField, newFilterValue); }); // --></script> <?php } // print " </td>\n"; print " </tr>\n"; }
function _getRecords_addPseudoFields(&$records, $options, $schema) { if (!$records) { return; } // get source fields $sourceFields = array(); $sourceFieldTypes = array('checkbox', 'list', 'date'); foreach ($schema as $fieldname => $fieldSchema) { if (!is_array($fieldSchema) || !@$fieldSchema['type']) { continue; } if (!in_array($fieldSchema['type'], $sourceFieldTypes)) { continue; } $sourceFields[$fieldname] = $fieldSchema; } if (!$sourceFields) { return; } // add pseudo-fields foreach ($sourceFields as $fieldname => $fieldSchema) { $isDate = $fieldSchema['type'] == 'date'; $isCheckbox = $fieldSchema['type'] == 'checkbox'; $_isList = $fieldSchema['type'] == 'list'; $isSingleList = $_isList && !in_array($fieldSchema['listType'], array('pulldownMulti', 'checkboxes')); $isMultiList = $_isList && !$isSingleList; if (!$isDate && !$isCheckbox && !$isSingleList && !$isMultiList) { die(__FUNCTION__ . ": field '{$fieldname}' of type '{$fieldSchema['type']}' isn't a known source field!"); } // List Fields that "Get Options from Database" - only lookup labels for values in record-set. $selectedValues = array(); if ($_isList && @$fieldSchema['optionsType'] == 'table') { foreach ($records as $record) { foreach (getListValues(null, null, $record[$fieldname]) as $value) { $selectedValues[$value] = 1; } } $selectedValues = array_keys($selectedValues); $selectedValues = array_filter($selectedValues); // remove blank entries } // get values to labels for list fields if ($_isList) { /* Special handling for list/query with a filer field because the available values/labels can change based on other fields. We need to check the possible options for each record instead of pulling the options for the table as a whole. Since this is a different process than any other field, we get the list options and assign the labels here then continue the main loop. */ if (@$fieldSchema['optionsType'] == 'query' && @$fieldSchema['filterField']) { $recordListOptions = array(); foreach (array_keys($records) as $index) { $record =& $records[$index]; // PHP4 safe references $recordListOptions = getListOptionsFromSchema($fieldSchema, $record); $values = array_pluck($recordListOptions, '0'); $labels = array_pluck($recordListOptions, '1'); $valuesToLabels = $recordListOptions ? @array_combine($values, $labels) : array(); $label = @$valuesToLabels[$record[$fieldname]]; $record["{$fieldname}:label"] = $label; } unset($record); continue; } else { $listOptions = getListOptionsFromSchema($fieldSchema, null, false, $selectedValues); $values = array_pluck($listOptions, '0'); $labels = array_pluck($listOptions, '1'); $valuesToLabels = $listOptions ? array_combine($values, $labels) : array(); } } // add pseudo-fields foreach (array_keys($records) as $index) { $record =& $records[$index]; // PHP4 safe references if ($isDate) { $time = @strtotime($record[$fieldname]); $record["{$fieldname}:unixtime"] = $time; } elseif ($isCheckbox) { $text = $record[$fieldname] ? @$fieldSchema['checkedValue'] : @$fieldSchema['uncheckedValue']; $record["{$fieldname}:text"] = $text; } elseif ($isSingleList) { $label = @$valuesToLabels[$record[$fieldname]]; $record["{$fieldname}:label"] = $label; } elseif ($isMultiList) { $values = getListValues($options['tableName'], $fieldname, $record[$fieldname]); $labels = array(); foreach ($values as $value) { $labels[] = @$valuesToLabels[$value]; } $record["{$fieldname}:values"] = $values; $record["{$fieldname}:labels"] = $labels; } // sort keys so related fields are grouped together ksort($record); unset($record); } } }
function getSelectOptionsFromSchema($selectedValue, $fieldSchema, $showEmptyOptionFirst = false) { // error checking if (!$fieldSchema) { die(__FUNCTION__ . ": No fieldSchema specified!"); } if (!@$fieldSchema['name']) { die(__FUNCTION__ . ": fieldSchema must have fieldname defined in 'name'!"); } // get field options $optionValues = array(); $optionLabels = array(); $listOptions = getListOptionsFromSchema($fieldSchema); foreach ($listOptions as $valueAndLabel) { list($optionValue, $optionLabel) = $valueAndLabel; $optionValues[] = $optionValue; $optionLabels[] = $optionLabel; } $optionsHTML = getSelectOptions($selectedValue, $optionValues, $optionLabels, $showEmptyOptionFirst); // return $optionsHTML; }
function ajaxUpdateListFieldOptions() { global $schema; $fieldname = @$_REQUEST['fieldname']; $fieldSchema = @$schema[$fieldname]; // error checking if (!$fieldname) { die("No fieldname specified!\n"); } if (!$fieldSchema) { die("Couldn't find field in schema!\n"); } if (@$fieldSchema['type'] != 'list') { die("Field type isn't 'list'!\n"); } if (@$fieldSchema['listType'] != 'pulldownMulti' && @$fieldSchema['listType'] != 'pulldown') { die("List type must be 'pulldown' or 'pulldownMulti'!\n"); } if (@$fieldSchema['optionsType'] != 'query') { die("Options type isn't 'query'!\n"); } if (!@$fieldSchema['filterField']) { die("No 'filterField' value specified for field!\n"); } // get list options $record = array($fieldSchema['filterField'] => $_REQUEST['newFilterValue']); $listOptions = getListOptionsFromSchema($fieldSchema, $record); // get options html $optionsHTML = "<option value=''><select></option>\n"; foreach ($listOptions as $optionArray) { list($value, $label) = $optionArray; $encodedValue = htmlencode($value); $selectedAttr = $value == @$_REQUEST['selectedValue'] ? 'selected="selected"' : ''; $encodedLabel = htmlencode($label); $optionsHTML .= "<option value=\"{$encodedValue}\" {$selectedAttr}>{$encodedLabel}</option>\n"; } // print options HTML print $optionsHTML; exit; }
function getListOptions($tablename, $fieldname, $useCache = false) { $valuesToLabels = array(); $schema = loadSchema($tablename); $fieldSchema = $schema[$fieldname]; $fieldOptions = getListOptionsFromSchema($fieldSchema, null, $useCache); foreach ($fieldOptions as $valueAndLabel) { list($value, $label) = $valueAndLabel; $valuesToLabels[$value] = $label; } return $valuesToLabels; }