labels, options and widgets for admin_cmd_preferences

This commit is contained in:
Ralf Becker 2019-03-20 18:14:31 +01:00
parent 6afccb215b
commit 4be62b63ee
3 changed files with 130 additions and 33 deletions

View File

@ -109,4 +109,83 @@ class admin_cmd_edit_preferences extends admin_cmd
}
return lang('Preferences').' '.self::display_account($this->account);
}
/**
* Get settings from various hooks
*
* @return array
*/
protected function settings()
{
static $settings = null;
if (!isset($settings))
{
Api\Translation::add_app('preferences');
$settings = Api\Preferences::settings($this->app, $this->pref, $this->account);
}
return $settings;
}
/**
* Return (human readable) labels for keys of changes
*
* @return array
*/
function get_change_labels()
{
$labels = [];
foreach($this->settings() as $setting)
{
if (in_array($setting['type'], ['section', 'subsection']))
{
continue;
}
$labels[$setting['name']] = preg_replace('|<br[ /]*>|i', "\n", $setting['label']);
}
return $labels;
}
/**
* Return widget types (indexed by field key) for changes
*
* Used by historylog widget to show the changes the command recorded.
*/
function get_change_widgets()
{
$widgets = [];
foreach($this->settings() as $setting)
{
switch ($setting['type'])
{
// ignore the following
case 'section': case 'subsection':
case 'int': case 'integer': case 'textbox': case 'textarea':
case 'password': case 'input':
case 'vfs_file': case 'vfs_dir': case 'vfs_dirs':
break;
case 'color':
$widgets[$setting['name']] = 'colorpicker';
break;
case 'check':
$setting['type'] = 'select';
$setting['values'] = array('1' => lang('yes'), '0' => lang('no'));
// fall through
default:
if (!empty($setting['values']) && is_array($setting['values']))
{
$widgets[$setting['name']] = $setting['values'];
}
else
{
$widgets[$setting['name']] = $setting['type'];
}
break;
}
}
//error_log(__METHOD__."() returning ".json_encode($widgets));
return $widgets;
}
}

View File

@ -1002,4 +1002,53 @@ class Preferences
error_log(__METHOD__."($category,$charset) lang=$lang, country=$country, country_from_lang=$country_from_lang: Could not set local!");
return false; // should not happen, as the 'C' local should at least be available everywhere
}
/**
* Get preferences by calling various hooks to supply them
*
* @param string $_appname appname or 'common'
* @param string $type ='user' 'default', 'forced', 'user' or 'group'
* @param int|string $account_id =null account_id for user or group prefs, or "forced" or "default"
* @return array
*/
public static function settings($_appname, $type='user', $account_id=null)
{
$all_settings = [];
$appname = $_appname == 'common' ? 'preferences' : $_appname;
// make type available, to hooks from applications can use it, eg. activesync
$hook_data = array(
'location' => 'settings',
'type' => $type,
'account_id' => $account_id,
);
$GLOBALS['type'] = $type; // old global variable
// calling app specific settings hook
$settings = Hooks::single($hook_data, $appname);
// it either returns the settings or save it in $GLOBALS['settings'] (deprecated!)
if (isset($settings) && is_array($settings) && $settings)
{
$all_settings = array_merge($all_settings, $settings);
}
elseif(isset($GLOBALS['settings']) && is_array($GLOBALS['settings']) && $GLOBALS['settings'])
{
$all_settings = array_merge($all_settings, $GLOBALS['settings']);
}
else
{
return False; // no settings returned
}
// calling settings hook all apps can answer (for a specific app)
$hook_data['location'] = 'settings_'.$appname;
foreach(Hooks::process($hook_data, $appname,true) as $settings)
{
if (isset($settings) && is_array($settings) && $settings)
{
$all_settings = array_merge($all_settings,$settings);
}
}
return $all_settings;
}
}

View File

@ -361,7 +361,7 @@ class preferences_settings
* @param array &$sel_options
* @param array &$readonlys
* @param array &$types on return setting-name => setting-type
* @param etemplate $tpl
* @param Api\Etemplate|etemplate $tpl
* @throws Api\Exception\WrongParameter
* @return array content
*/
@ -641,39 +641,8 @@ class preferences_settings
Api\Translation::add_app('preferences'); // we need the prefs translations too
}
// make type available, to hooks from Egw\Applications can use it, eg. activesync
$hook_data = array(
'location' => 'settings',
'type' => $type,
'account_id' => $account_id,
);
$GLOBALS['type'] = $type; // old global variable
$this->settings = Api\Preferences::settings($appname, $type, $account_id);
// calling app specific settings hook
$settings = Api\Hooks::single($hook_data, $this->appname);
// it either returns the settings or save it in $GLOBALS['settings'] (deprecated!)
if (isset($settings) && is_array($settings) && $settings)
{
$this->settings = array_merge($this->settings, $settings);
}
elseif(isset($GLOBALS['settings']) && is_array($GLOBALS['settings']) && $GLOBALS['settings'])
{
$this->settings = array_merge($this->settings, $GLOBALS['settings']);
}
else
{
return False; // no settings returned
}
// calling settings hook all apps can answer (for a specific app)
$hook_data['location'] = 'settings_'.$this->appname;
foreach(Api\Hooks::process($hook_data, $this->appname,true) as $settings)
{
if (isset($settings) && is_array($settings) && $settings)
{
$this->settings = array_merge($this->settings,$settings);
}
}
/* Remove ui-only settings */
if($this->xmlrpc)
{