issue #1640 user preferences

This commit is contained in:
plegall
2022-03-31 18:16:23 +02:00
parent c56aee6c31
commit c2f8238e16
4 changed files with 161 additions and 0 deletions
+98
View File
@@ -395,6 +395,8 @@ SELECT
}
unset($value);
$userdata['preferences'] = empty($userdata['preferences']) ? array() : unserialize($userdata['preferences']);
if ($use_cache)
{
if (!isset($userdata['need_update'])
@@ -1708,4 +1710,100 @@ UPDATE '.USER_INFOS_TABLE.'
return $last_visit;
}
/**
* Save user preferences in database
* @since 13
*/
function userprefs_save()
{
global $user;
$dbValue = pwg_db_real_escape_string(serialize($user['preferences']));
$query = '
UPDATE '.USER_INFOS_TABLE.'
SET preferences = \''.$dbValue.'\'
WHERE user_id = '.$user['id'].'
;';
pwg_query($query);
}
/**
* Add or update a user preferences parameter
* @since 13
*
* @param string $param
* @param string $value
* @param boolean $updateGlobal update global *$conf* variable
*/
function userprefs_update_param($param, $value)
{
global $user;
// If the field is true or false, the variable is transformed into a boolean value.
if ('true' == $value)
{
$value = true;
}
elseif ('false' == $value)
{
$value = false;
}
$user['preferences'][$param] = $value;
userprefs_save();
}
/**
* Delete one or more user preferences parameters
* @since 13
*
* @param string|string[] $params
*/
function userprefs_delete_param($params)
{
global $user;
if (!is_array($params))
{
$params = array($params);
}
if (empty($params))
{
return;
}
foreach ($params as $param)
{
if (isset($user['preferences'][$param]))
{
unset($user['preferences'][$param]);
}
}
userprefs_save();
}
/**
* Return a default value for a user preferences parameter.
* @since 13
*
* @param string $param the configuration value to be extracted (if it exists)
* @param mixed $default_value the default value if it does not exist yet.
*
* @return mixed The configuration value if the variable exists, otherwise the default.
*/
function userprefs_get_param($param, $default_value=null)
{
global $user;
if (isset($user['preferences'][$param]))
{
return $user['preferences'][$param];
}
return $default_value;
}
?>
+28
View File
@@ -763,6 +763,34 @@ SELECT
));
}
/**
* API method
* Set a preferences parameter to current user
* @since 13
* @param mixed[] $params
* @option string param
* @option string|mixed value
*/
function ws_users_preferences_set($params, &$service)
{
global $user;
if (!preg_match('/^[a-zA-Z0-9_-]+$/', $params['param']))
{
return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid param name #'.$params['param'].'#');
}
$value = stripslashes($params['value']);
if ($params['is_json'])
{
$value = json_decode($value, true);
}
userprefs_update_param($params['param'], $value, true);
return $user['preferences'];
}
/**
* API method
* Adds a favorite image for the current user