From 620e20f0e4fd010116426a5e22188344df86b698 Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Thu, 23 Aug 2018 15:24:13 +0200 Subject: [PATCH] fix admin_cmd_user_preferences to use values given and not global preferences object --- .../class.admin_cmd_edit_preferences.inc.php | 70 ++++++++++++++++--- .../inc/class.preferences_settings.inc.php | 30 +++++--- 2 files changed, 80 insertions(+), 20 deletions(-) diff --git a/admin/inc/class.admin_cmd_edit_preferences.inc.php b/admin/inc/class.admin_cmd_edit_preferences.inc.php index 865aa0fc8b..8dff565d11 100644 --- a/admin/inc/class.admin_cmd_edit_preferences.inc.php +++ b/admin/inc/class.admin_cmd_edit_preferences.inc.php @@ -7,30 +7,43 @@ * @package admin * @copyright (c) 2018 by Hadi Nategh * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License - * @version $Id$ */ +use EGroupware\Api; + /** * admin command: edit preferences + * + * @property-read int $account numerical account_id + * @property-read string $pref "user", "default", "forced" or "group" + * @property-read string $app app-name + * @property-read array $set values to set + * @property-read array $old old values */ class admin_cmd_edit_preferences extends admin_cmd { /** * Constructor - * @param string|int|array $account account name or id (!$account to add a new account), or array with all parameters - * @param array $set changed values - * @param array $old old values + * @param string|int|array $account account name or id, or array with all parameters + * @param string $type ="user" "user", "default", "forced" or "group" + * @param string $app =null app-name, required if $account is no array + * @param array $set =null name => value pairs to change + * @param array $old =null name => value pairs of old values + * @param array $extra =array() values for keys requested(_email) or comment */ - function __construct($account,$set=null, $old=null) + function __construct($account, $type=null, $app=null, array $set=null, array $old=null, array $extra=array()) { if (!is_array($account)) { $account = array( 'account' => $account, + 'pref' => $type, // type is __CLASS__! + 'app' => $app, 'set' => $set, - 'old' => $old - ); + 'old' => $old, + )+$extra; } + admin_cmd::__construct($account); } @@ -42,8 +55,39 @@ class admin_cmd_edit_preferences extends admin_cmd */ protected function exec($check_only=false) { + switch($this->pref) + { + case 'forced': + case 'default': + unset($this->account); + break; + case 'user': + $this->account = self::parse_account($this->account, true); + break; + case 'group': + $this->account = self::parse_account($this->account, false); + break; + default: + throw new Api\Exception\WrongUserinput(lang('Invalid type "%1"!', $this->pref)); + } + if ($this->app !== 'common') self::parse_apps(array($this->app)); + if ($check_only) return; - $GLOBALS['egw']->preferences->save_repository(True, $this->type); + + $prefs = new Api\Preferences(in_array($this->pref, 'default', 'forced') ? $this->pref : $this->account); + $prefs->read_repository(); + foreach($this->set as $name => $value) + { + if (!isset($value) || $value === '') + { + $prefs->delete($this->app, $name, in_array($this->pref, 'default', 'forced') ? $this->pref : 'user'); + } + else + { + $prefs->add($this->app, $name, $value, in_array($this->pref, 'default', 'forced') ? $this->pref : 'user'); + } + } + $prefs->save_repository(true, $this->pref); return lang('Preferences saved.'); } @@ -54,6 +98,14 @@ class admin_cmd_edit_preferences extends admin_cmd */ function __tostring() { - return lang('Preferences of user %1 changed by %2', $this->data['account'], $this->requested_email); + switch($this->pref) + { + case 'forced': + return lang('Forced preferences'); + + case 'default': + return lang('Default preferences'); + } + return lang('Preferences').' '.self::display_account($this->account); } } \ No newline at end of file diff --git a/preferences/inc/class.preferences_settings.inc.php b/preferences/inc/class.preferences_settings.inc.php index 37b16772e6..9f3f7c6c66 100644 --- a/preferences/inc/class.preferences_settings.inc.php +++ b/preferences/inc/class.preferences_settings.inc.php @@ -195,18 +195,26 @@ class preferences_settings * @param array $values * @param string $account_id */ - static function admin_cmd_run($content, $values, $account_id) + static function admin_cmd_run($content, $values, $account_id, $type, $appname) { - $changes = array_udiff_assoc($values, $content['old_values'], function($a, $b){ - return (int) $a !== $b; + $changes = array_udiff_assoc($values, $content['old_values'], function($a, $b) + { + // some prefs are still comma-delimitered + if (is_array($a) != is_array($b)) + { + if (!is_array($a)) $a = explode(',', $a); + if (!is_array($b)) $b = explode(',', $b); + } + return (int)($a != $b); }); $old = array_intersect_key($content['old_values'], $changes); - $cmd = new admin_cmd_edit_preferences(array( - 'account' => $account_id, - 'set' => $changes, - 'old' =>$old - )+(array)$content['admin_cmd']); - $cmd->run(); + + if ($changes) + { + $cmd = new admin_cmd_edit_preferences($account_id, $type, $appname, $changes, $old, (array)$content['admin_cmd']); + return $cmd->run(); + } + return lang('Nothing to save.'); } /** @@ -326,11 +334,11 @@ class preferences_settings { if ($content['is_admin']) { - self::admin_cmd_run($content, $values, $GLOBALS['egw']->preferences->get_account_id()); + self::admin_cmd_run($content, $values, $GLOBALS['egw']->preferences->get_account_id(), $type, $appname); } else { - $GLOBALS['egw']->preferences->save_repository(True,$type); + $GLOBALS['egw']->preferences->save_repository(True, $type); } // certain common prefs (language, template, ...) require the session to be re-created