Parcourir la source

! Now actually load the preferences for a user from the database, and some of them are permission dependent so reflect that. (This was so much fun.)

Signed-off-by: Peter Spicer <[email protected]>
Peter Spicer il y a 10 ans
Parent
commit
5520d5cba8
3 fichiers modifiés avec 134 ajouts et 15 suppressions
  1. 61 11
      Sources/Profile-Modify.php
  2. 60 0
      Sources/Subs-Notify.php
  3. 13 4
      Themes/default/Profile.template.php

+ 61 - 11
Sources/Profile-Modify.php

@@ -1855,6 +1855,17 @@ function alert_configuration($memID)
 
 	loadThemeOptions($memID);
 
+	// Now load all the values for this user.
+	require_once($sourcedir . '/Subs-Notify.php');
+	$prefs = getNotifyPrefs($memID);
+	// And we might as well now perform the splicing of default values.
+	if (!empty($prefs[0]))
+		foreach ($prefs[0] as $this_pref => $value)
+			if (!isset($prefs[$memID][$this_pref]))
+				$prefs[$memID][$this_pref] = $value;
+
+	$context['alert_prefs'] = !empty($prefs[$memID]) ? $prefs[$memID] : array();
+
 	// Now for the exciting stuff.
 	// We have groups of items, each item has both an alert and an email key as well as an optional help string.
 	// Valid values for these keys are 'always', 'yes', 'never'; if using always or never you should add a help string.
@@ -1865,17 +1876,17 @@ function alert_configuration($memID)
 			'msg_like' => array('alert' => 'yes', 'email' => 'yes'),
 		),
 		'pm' => array(
-			'pm_new' => array('alert' => 'always', 'email' => 'yes', 'help' => 'alert_pm_new'),
-			'pm_reply' => array('alert' => 'always', 'email' => 'yes', 'help' => 'alert_pm_new'),
+			'pm_new' => array('alert' => 'always', 'email' => 'yes', 'help' => 'alert_pm_new', 'permission' => array('name' => 'pm_read', 'is_board' => false)),
+			'pm_reply' => array('alert' => 'always', 'email' => 'yes', 'help' => 'alert_pm_new', 'permission' => array('name' => 'pm_send', 'is_board' => false)),
 		),
 		'moderation' => array(
-			'msg_report' => array('alert' => 'yes', 'email' => 'yes'),
-			'msg_report_reply' => array('alert' => 'yes', 'email' => 'yes'),
+			'msg_report' => array('alert' => 'yes', 'email' => 'yes', 'permission' => array('name' => 'moderate_board', 'is_board' => true)),
+			'msg_report_reply' => array('alert' => 'yes', 'email' => 'yes', 'permission' => array('name' => 'moderate_board', 'is_board' => true)),
 		),
 		'members' => array(
 			'register_new' => array('alert' => 'yes', 'email' => 'yes'),
 			'warn_own' => array('alert' => 'yes', 'email' => 'yes'),
-			'warn_any' => array('alert' => 'yes', 'email' => 'yes'),
+			'warn_any' => array('alert' => 'yes', 'email' => 'yes', 'permission' => array('name' => 'issue_warning', 'is_board' => false)),
 		),
 		'calendar' => array(
 			'event_new' => array('alert' => 'yes', 'email' => 'yes', 'help' => 'alert_event_new'),
@@ -1885,19 +1896,58 @@ function alert_configuration($memID)
 		'msg' => array(
 			array('check', 'msg_auto_notify', 'label' => 'after'),
 			array('select', 'msg_notify_pref', 'label' => 'before', 'opts' => array(
-				'nothing' => $txt['alert_opt_msg_notify_pref_nothing'],
-				'instant' => $txt['alert_opt_msg_notify_pref_instant'],
-				'first' => $txt['alert_opt_msg_notify_pref_first'],
-				'daily' => $txt['alert_opt_msg_notify_pref_daily'],
-				'weekly' => $txt['alert_opt_msg_notify_pref_weekly'],
+				0 => $txt['alert_opt_msg_notify_pref_nothing'],
+				1 => $txt['alert_opt_msg_notify_pref_instant'],
+				2 => $txt['alert_opt_msg_notify_pref_first'],
+				3 => $txt['alert_opt_msg_notify_pref_daily'],
+				4 => $txt['alert_opt_msg_notify_pref_weekly'],
 			)),
 		),
 	);
+	$disabled_options = array();
+	// There are certain things that are disabled at the group level.
+	if (empty($modSettings['cal_enabled']))
+	{
+		foreach ($alert_types['calendar'] as $k => $v)
+			$disabled_options[] = $k;
+		unset ($alert_types['calendar']);
+	}
+
 	// Now, now, we could pass this through global but we should really get into the habit of
 	// passing content to hooks, not expecting hooks to splatter everything everywhere.
-	call_integration_hook('integrate_alert_types', array(&$alert_types, &$group_options));
+	call_integration_hook('integrate_alert_types', array(&$alert_types, &$group_options, &$disabled_options));
+
+	// Now we have to do some permissions testing.
+	require_once($sourcedir . '/Subs-Members.php');
+	$perms_cache = array();
+	foreach ($alert_types as $group => $items)
+	{
+		foreach ($items as $alert_key => $alert_value)
+		{
+			if (!isset($alert_value['permission']))
+				continue;
+			if (!isset($perms_cache[$alert_value['permission']['name']]))
+			{
+				$in_board = !empty($alert_value['permission']['is_board']) ? 0 : null;
+				$members = membersAllowedTo($alert_value['permission']['name'], $in_board);
+				$perms_cache[$alert_value['permission']['name']] = in_array($memID, $members);
+			}
+
+			if (!$perms_cache[$alert_value['permission']['name']])
+			{
+				$disabled_options[] = $alert_key;
+				unset ($alert_types[$group][$alert_key]);
+			}
+		}
+
+		if (empty($alert_types[$group]))
+			unset ($alert_types[$group]);
+	}
+
+	// And finally, exporting it to be useful later.
 	$context['alert_types'] = $alert_types;
 	$context['alert_group_options'] = $group_options;
+	$context['disabled_alerts'] = $disabled_options;
 
 	if (isset($_POST['saving or something']))
 	{

+ 60 - 0
Sources/Subs-Notify.php

@@ -0,0 +1,60 @@
+<?php
+
+/**
+ * Simple Machines Forum (SMF)
+ *
+ * @package SMF
+ * @author Simple Machines http://www.simplemachines.org
+ * @copyright 2013 Simple Machines and individual contributors
+ * @license http://www.simplemachines.org/about/smf/license.php BSD
+ *
+ * @version 2.1 Alpha 1
+ */
+
+if (!defined('SMF'))
+	die('No direct access...');
+
+/**
+ * Fetches the list of preferences (or a single/subset of preferences) for
+ * notifications for one or more users.
+ *
+ * @param mixed $members A user id or an array of (integer) user ids to load preferences for
+ * @param mixed $prefs An empty string to load all preferences, or a string (or array) of preference name(s) to load
+ * @return array An array of user ids => array (pref name -> value), with user id 0 representing the defaults
+ */
+function getNotifyPrefs($members, $prefs = '')
+{
+	global $smcFunc;
+
+	// We want this as an array whether it is or not.
+	$members = is_array($members) ? $members : (array) $members;
+
+	if (!empty($prefs))
+		$prefs = is_array($prefs) ? $prefs : (array) $prefs;
+
+	$result = array();
+
+	// We want to now load the default, which is stored with a member id of 0.
+	$members[] = 0;
+
+	$request = $smcFunc['db_query']('', '
+		SELECT id_member, alert_pref, alert_value
+		FROM {db_prefix}user_alerts_prefs
+		WHERE id_member IN ({array_int:members})' . (!empty($prefs) ? '
+			AND alert_pref IN ({array_string:prefs})' : ''),
+		array(
+			'members' => $members,
+			'prefs' => $prefs,
+		)
+	);
+	while ($row = $smcFunc['db_fetch_assoc']($request))
+	{
+		$result[$row['id_member']][$row['alert_pref']] = $row['alert_value'];
+	}
+
+	// Let's not bother to reduplicate the default value for all the other values,
+	// no point splurging on potentially a lot of memory unnecessarily.
+	return $result;
+}
+
+?>

+ 13 - 4
Themes/default/Profile.template.php

@@ -1860,6 +1860,13 @@ function template_alert_configuration()
 						</tr>';
 	$use_bg2 = true;
 
+	// Before we get into the loop, just quickly set this up.
+	// We store these prefs, generally, as a bitwise thing to save some space.
+	$alert_bits = array(
+		'alert' => 0x01,
+		'email' => 0x02,
+	);
+
 	foreach ($context['alert_types'] as $alert_group => $alerts)
 	{
 		echo '
@@ -1878,18 +1885,19 @@ function template_alert_configuration()
 					echo '
 							<label for="opt_', $opts[1], '">', $label, '</label>';
 
+				$this_value = isset($context['alert_prefs'][$opts[1]]) ? $context['alert_prefs'][$opts[1]] : 0;
 				switch ($opts[0])
 				{
 					case 'check':
 						echo '
-								<input type="checkbox" name="opt_', $opts[1], '" id="opt_', $opts[1], '" />';
+								<input type="checkbox" name="opt_', $opts[1], '" id="opt_', $opts[1], '"', $this_value ? ' checked="checked"' : '', ' />';
 						break;
 					case 'select':
 						echo '
 								<select name="opt_', $opts[1], '" id="opt_', $opts[1], '">';
 						foreach ($opts['opts'] as $k => $v)
 							echo '
-									<option value="', $k, '">', $v, '</option>';
+									<option value="', $k, '"', $this_value == $k ? ' selected="selected"' : '', '>', $v, '</option>';
 						echo '
 								</select>';
 						break;
@@ -1916,7 +1924,8 @@ function template_alert_configuration()
 			foreach (array('alert', 'email') as $type)
 			{
 				echo '
-							<td>';
+							<td class="centercol">';
+				$this_value = isset($context['alert_prefs'][$alert_id]) ? $context['alert_prefs'][$alert_id] : 0;
 				switch ($alert_details[$type])
 				{
 					case 'always':
@@ -1925,7 +1934,7 @@ function template_alert_configuration()
 						break;
 					case 'yes':
 						echo '
-								<input type="checkbox" />';
+								<input type="checkbox" name="', $type, '_', $alert_id, '"', $this_value & $alert_bits[$type] != 0 ? ' checked="checked"' : '', ' />';
 						break;
 					case 'never':
 						echo '