aboutsummaryrefslogtreecommitdiff
path: root/actions/plugins/usersettings/save.php
blob: 794ce4046a3fb5fddab95c2ebeffaaa39ea7aad6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
/**
 * Saves user-specific plugin settings.
 *
 * This action can be overriden for a specific plugin by creating the
 * settings/<plugin_id>/save action in that plugin.
 *
 * @uses array $_REQUEST['params']    A set of key/value pairs to save to the ElggPlugin entity
 * @uses int   $_REQUEST['plugin_id'] The id of the plugin
 * @uses int   $_REQUEST['user_guid'] The GUID of the user to save settings for.
 *
 * @package Elgg.Core
 * @subpackage Plugins.Settings
 */

$params = get_input('params');
$plugin_id = get_input('plugin_id');
$user_guid = get_input('user_guid', elgg_get_logged_in_user_guid());
$plugin = elgg_get_plugin_from_id($plugin_id);
$user = get_entity($user_guid);

if (!($plugin instanceof ElggPlugin)) {
	register_error(elgg_echo('plugins:usersettings:save:fail', array($plugin_id)));
	forward(REFERER);
}

if (!($user instanceof ElggUser)) {
	register_error(elgg_echo('plugins:usersettings:save:fail', array($plugin_id)));
	forward(REFERER);
}

$plugin_name = $plugin->manifest->getName();

// make sure we're admin or the user
if (!$user->canEdit()) {
	register_error(elgg_echo('plugins:usersettings:save:fail', array($plugin_name)));
	forward(REFERER);
}

$result = false;

if (elgg_action_exist("usersettings/$plugin_id/save")) {
	action("usersettings/$plugin_id/save");
} else {
	foreach ($params as $k => $v) {
		// Save
		$result = $plugin->setUserSetting($k, $v, $user->guid);

		// Error?
		if (!$result) {
			register_error(elgg_echo('plugins:usersettings:save:fail', array($plugin_name)));
			forward(REFERER);
		}
	}
}

system_message(elgg_echo('plugins:usersettings:save:ok', array($plugin_name)));
forward(REFERER);