Changeset View
Changeset View
Standalone View
Standalone View
branches/5.2.x/core/kernel/utility/formatters/options_formatter.php
Show All 34 Lines | |||||
if (!array_key_exists('options', $field_options) || !is_array($field_options['options'])) { | if (!array_key_exists('options', $field_options) || !is_array($field_options['options'])) { | ||||
trigger_error('Options not defined for <strong>'.$object->Prefix.'</strong> field <strong>'.$field_name.'</strong>', E_USER_WARNING); | trigger_error('Options not defined for <strong>'.$object->Prefix.'</strong> field <strong>'.$field_name.'</strong>', E_USER_WARNING); | ||||
return $value; | return $value; | ||||
} | } | ||||
$options = $field_options['options']; | $options = $field_options['options']; | ||||
$use_phrases = array_key_exists('use_phrases', $field_options) ? $field_options['use_phrases'] : false; | $use_phrases = array_key_exists('use_phrases', $field_options) ? $field_options['use_phrases'] : false; | ||||
if (strpos($value, '|') !== false) { | if ( strpos($value, '|') !== false ) { | ||||
// multiple checkboxes OR multiselect | // Multiple checkboxes OR multiselect. | ||||
$values = Array (); | $sorted_values = array(); | ||||
$values_unsorted = explode('|', substr($value, 1, -1) ); | $selected_values = explode('|', substr($value, 1, -1)); | ||||
// 1. sort values using options order from unit config | // 1. sort values using options order from unit config | ||||
$key_indexes = array_keys($options); | $existing_values = array_keys($options); | ||||
foreach ($values_unsorted as $value) { | $non_existing_selected_values = array_diff($selected_values, $existing_values); | ||||
$values[ array_search($value, $key_indexes) ] = $value; | |||||
if ( $non_existing_selected_values ) { | |||||
$selected_values = array_diff($selected_values, $non_existing_selected_values); | |||||
foreach ( $selected_values as $unsorted_value ) { | |||||
$sorted_values[array_search($unsorted_value, $existing_values)] = $unsorted_value; | |||||
} | |||||
ksort($sorted_values); | |||||
$sorted_values = array_merge($sorted_values, $non_existing_selected_values); | |||||
} | |||||
else { | |||||
foreach ( $selected_values as $unsorted_value ) { | |||||
$sorted_values[array_search($unsorted_value, $existing_values)] = $unsorted_value; | |||||
} | |||||
ksort($sorted_values); | |||||
} | } | ||||
ksort($values); | |||||
// 2. convert values to titles | // 2. convert values to titles | ||||
$labels = Array (); | $labels = array(); | ||||
foreach ($values as $value) { | |||||
$label = $this->formatOption($value, $options, $use_phrases); | foreach ( $sorted_values as $sorted_value ) { | ||||
$label = $this->formatOption($sorted_value, $options, $use_phrases); | |||||
if ($label) { | if ( $label ) { | ||||
$labels[] = $label; | $labels[] = $label; | ||||
} | } | ||||
} | } | ||||
return implode($format, $labels); | return implode($format, $labels); | ||||
} | } | ||||
else { | |||||
return $this->formatOption($value, $options, $use_phrases); | return $this->formatOption($value, $options, $use_phrases); | ||||
} | } | ||||
} | |||||
function formatOption($value, $options, $use_phrases = true) | function formatOption($value, $options, $use_phrases = true) | ||||
{ | { | ||||
$label = getArrayValue($options, $value); | $label = getArrayValue($options, $value); | ||||
if ($label !== false) { | if ($label !== false) { | ||||
// option_id found in options array | // option_id found in options array | ||||
return $use_phrases ? $this->Application->Phrase($label) : $label; | return $use_phrases ? $this->Application->Phrase($label) : $label; | ||||
} | } | ||||
Show All 30 Lines | |||||
if ( "$option_value" === "$value" ) { | if ( "$option_value" === "$value" ) { | ||||
$found = true; | $found = true; | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
return $found ? $option_key : $value; | return $found ? $option_key : $value; | ||||
} | } | ||||
} | } | ||||
No newline at end of file |