mirror of
https://github.com/Piwigo/Piwigo.git
synced 2026-06-01 20:04:51 +02:00
issue #1640 user preferences
This commit is contained in:
@@ -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;
|
||||
}
|
||||
?>
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
// +-----------------------------------------------------------------------+
|
||||
// | This file is part of Piwigo. |
|
||||
// | |
|
||||
// | For copyright and license information, please view the COPYING.txt |
|
||||
// | file that was distributed with this source code. |
|
||||
// +-----------------------------------------------------------------------+
|
||||
|
||||
if (!defined('PHPWG_ROOT_PATH'))
|
||||
{
|
||||
die('Hacking attempt!');
|
||||
}
|
||||
|
||||
$upgrade_description = 'add user_infos.preferences';
|
||||
|
||||
pwg_query('
|
||||
ALTER TABLE `'.PREFIX_TABLE.'user_infos`
|
||||
ADD COLUMN `preferences` TEXT default NULL
|
||||
;');
|
||||
|
||||
echo "\n".$upgrade_description."\n";
|
||||
|
||||
?>
|
||||
@@ -1209,6 +1209,18 @@ enabled_high, registration_date, registration_date_string, registration_date_sin
|
||||
array('admin_only'=>true, 'post_only'=>true)
|
||||
);
|
||||
|
||||
$service->addMethod(
|
||||
'pwg.users.preferences.set',
|
||||
'ws_users_preferences_set',
|
||||
array(
|
||||
'param' => array(),
|
||||
'value' => array('flags'=>WS_PARAM_OPTIONAL),
|
||||
'is_json' => array('default'=>false, 'type'=>WS_TYPE_BOOL),
|
||||
),
|
||||
'Set a user preferences parameter. JSON encode the value (and set is_json to true) if you need a complex data structure.',
|
||||
$ws_functions_root . 'pwg.users.php'
|
||||
);
|
||||
|
||||
$service->addMethod(
|
||||
'pwg.users.favorites.add',
|
||||
'ws_users_favorites_add',
|
||||
|
||||
Reference in New Issue
Block a user