From 8c44986e7bb16d128204c953d2fbba7370b4a8f2 Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Fri, 10 Jan 2014 09:08:40 +0000 Subject: [PATCH] fixed default value for selectbox with optgroups does not show label --- .../inc/class.preferences_settings.inc.php | 76 +++++++++++-------- 1 file changed, 46 insertions(+), 30 deletions(-) diff --git a/preferences/inc/class.preferences_settings.inc.php b/preferences/inc/class.preferences_settings.inc.php index 633b3c6fa1..54cd854637 100644 --- a/preferences/inc/class.preferences_settings.inc.php +++ b/preferences/inc/class.preferences_settings.inc.php @@ -387,37 +387,10 @@ class preferences_settings $GLOBALS['egw']->preferences->group[$appname][$setting['name']] : $GLOBALS['egw']->preferences->default[$appname][$setting['name']]; - if (isset($setting['values']) && !is_array($default) && - (string)$setting['values'][$default] !== '' && (string)$default !== '') + // replace default value(s) for selectboxes with selectbox labels + if (isset($setting['values']) && is_array($setting['values'])) { - if(is_array($setting['values'][$default])) - { - foreach($setting['values'] as $key => $value) - { - if($value['value'] == $default && $value['label']) - { - $default = $value['label']; - break; - } - } - } - else - { - $default = $setting['values'][$default]; - } - } - else if (strpos($default, ',') !== false) - { - $default = explode(',',$default); - } - if(is_array($default)) - { - $values = array(); - foreach($default as $value) - { - if (isset($setting['values'][$value])) $values[] = $setting['values'][$value]; - } - if ($values) $default = implode(', ', $values); + $default = self::get_default_label($default, $setting['values']); } if (is_array($types[$setting['name']])) // translate the substitution names { @@ -487,6 +460,49 @@ class preferences_settings return $content; } + /** + * Get label for given default value(s) + * + * @param string|array $default default value(s) to get label for + * @param array $values values optional including optgroups + * @param boolean $lang=true + * @return string comma-separated and translated labels + */ + protected static function get_default_label($default, array $values, $lang=true) + { + // explode comma-separated multiple default values + if (!is_array($default) && !isset($values[$default]) && strpos($default, ',') !== false) + { + $labels = explode(',', $default); + } + else + { + $labels = (array)$default; + } + foreach($labels as &$def) + { + if (isset($values[$def])) + { + $def = is_array($values[$def]) ? $values[$def]['label'] : $values[$def]; + } + else // value could be in an optgroup + { + foreach($values as $value) + { + if (is_array($value) && !isset($value['label']) && isset($value[$def])) + { + $def = is_array($value[$def]) ? $value[$def]['label'] : $value[$def]; + break; + } + } + } + if ($lang) $def = lang($def); + } + $label = implode(', ', $labels); + //error_log(__METHOD__."(".array2string($default).', '.array2string($values).") returning $label"); + return $label; + } + /** * Get preferences by calling various hooks to supply them *