Browse Source

! And now the meat of notifying users for a new registration - the old adminNotify function should be a wrapper for creating a background task and that should do everything else, this seems to work, including sending emails.

Signed-off-by: Peter Spicer <[email protected]>
Peter Spicer 10 years ago
parent
commit
8b2fa8620d
2 changed files with 153 additions and 66 deletions
  1. 11 66
      Sources/Subs-Post.php
  2. 142 0
      Sources/tasks/Register-Notify.php

+ 11 - 66
Sources/Subs-Post.php

@@ -2793,10 +2793,6 @@ function adminNotify($type, $memberID, $member_name = null)
 {
 	global $txt, $modSettings, $language, $scripturl, $user_info, $context, $smcFunc;
 
-	// If the setting isn't enabled then just exit.
-	if (empty($modSettings['notify_new_registration']))
-		return;
-
 	if ($member_name == null)
 	{
 		// Get the new user's name....
@@ -2813,69 +2809,18 @@ function adminNotify($type, $memberID, $member_name = null)
 		$smcFunc['db_free_result']($request);
 	}
 
-	$toNotify = array();
-	$groups = array();
-
-	// All membergroups who can approve members.
-	$request = $smcFunc['db_query']('', '
-		SELECT id_group
-		FROM {db_prefix}permissions
-		WHERE permission = {string:moderate_forum}
-			AND add_deny = {int:add_deny}
-			AND id_group != {int:id_group}',
-		array(
-			'add_deny' => 1,
-			'id_group' => 0,
-			'moderate_forum' => 'moderate_forum',
-		)
+	// This is really just a wrapper for making a new background task to deal with all the fun.
+	$smcFunc['db_insert']('insert',
+		'{db_prefix}background_tasks',
+		array('task_file' => 'string', 'task_class' => 'string', 'task_data' => 'string', 'claimed_time' => 'int'),
+		array('$sourcedir/tasks/Register-Notify.php', 'Register_Notify_Background', serialize(array(
+			'new_member_id' => $memberID,
+			'new_member_name' => $member_name,
+			'notify_type' => $type,
+			'time' => time(),
+		)), 0),
+		array('id_task')
 	);
-	while ($row = $smcFunc['db_fetch_assoc']($request))
-		$groups[] = $row['id_group'];
-	$smcFunc['db_free_result']($request);
-
-	// Add administrators too...
-	$groups[] = 1;
-	$groups = array_unique($groups);
-
-	// Get a list of all members who have ability to approve accounts - these are the people who we inform.
-	$request = $smcFunc['db_query']('', '
-		SELECT id_member, lngfile, email_address
-		FROM {db_prefix}members
-		WHERE (id_group IN ({array_int:group_list}) OR FIND_IN_SET({raw:group_array_implode}, additional_groups) != 0)
-			AND notify_types != {int:notify_types}
-		ORDER BY lngfile',
-		array(
-			'group_list' => $groups,
-			'notify_types' => 4,
-			'group_array_implode' => implode(', additional_groups) != 0 OR FIND_IN_SET(', $groups),
-		)
-	);
-
-	$current_language = $user_info['language'];
-	while ($row = $smcFunc['db_fetch_assoc']($request))
-	{
-		$replacements = array(
-			'USERNAME' => $member_name,
-			'PROFILELINK' => $scripturl . '?action=profile;u=' . $memberID
-		);
-		$emailtype = 'admin_notify';
-
-		// If they need to be approved add more info...
-		if ($type == 'approval')
-		{
-			$replacements['APPROVALLINK'] = $scripturl . '?action=admin;area=viewmembers;sa=browse;type=approve';
-			$emailtype .= '_approval';
-		}
-
-		$emaildata = loadEmailTemplate($emailtype, $replacements, empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile']);
-
-		// And do the actual sending...
-		sendmail($row['email_address'], $emaildata['subject'], $emaildata['body'], null, null, false, 0);
-	}
-	$smcFunc['db_free_result']($request);
-
-	if (isset($current_language) && $current_language != $user_info['language'])
-		loadLanguage('Login');
 }
 
 /**

+ 142 - 0
Sources/tasks/Register-Notify.php

@@ -0,0 +1,142 @@
+<?php
+
+/**
+ * This task handles notifying users when someone new signs up.
+ *
+ * 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
+ */
+
+class Register_Notify_Background extends SMF_BackgroundTask
+{
+	public function execute()
+	{
+		global $smcFunc, $sourcedir, $language, $scripturl;
+
+		// Get everyone who could be notified.
+		require_once($sourcedir . '/Subs-Members.php');
+		$members = membersAllowedTo('moderate_forum');
+
+		// Having successfully figured this out, now let's get the preferences of everyone.
+		require_once($sourcedir . '/Subs-Notify.php');
+		$prefs = getNotifyPrefs($members, 'member_register');
+
+		// Apply defaults if we got some.
+		if (isset($prefs[0]))
+		{
+			foreach ($members as $member)
+				if (!isset($prefs[$member]))
+					$prefs[$member] = $prefs[0];
+
+			unset ($prefs[0]); // Don't need this any more.
+		}
+
+		// So now we find out who wants what.
+		$alert_bits = array(
+			'alert' => 0x01,
+			'email' => 0x02,
+		);
+		$notifies = array();
+
+		foreach ($prefs as $member => $pref_option)
+		{
+			foreach ($alert_bits as $type => $bitvalue)
+				if ($pref_option & $bitvalue == $bitvalue)
+					$notifies[$type][] = $member;
+		}
+
+		// Firstly, anyone who wants alerts.
+		if (!empty($notifies['alert']))
+		{
+			// Alerts are relatively easy.
+			$insert_rows = array();
+			foreach ($notifies['alert'] as $member)
+			{
+				$insert_rows[] = array(
+					'alert_time' => $this->_details['time'],
+					'id_member' => $member,
+					'id_member_started' => $this->_details['new_member_id'],
+					'member_name' => $this->_details['new_member_name'],
+					'content_type' => 'member',
+					'content_id' => 0,
+					'content_action' => 'register_' . $this->_details['notify_type'],
+					'is_read' => 0,
+					'extra' => '',
+				);
+			}
+
+			$smcFunc['db_insert']('insert',
+				'{db_prefix}user_alerts',
+				array('alert_time' => 'int', 'id_member' => 'int', 'id_member_started' => 'int',
+					'member_name' => 'string', 'content_type' => 'string', 'content_id' => 'int',
+					'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string'),
+				$insert_rows,
+				array('id_alert')
+			);
+
+			// And update the count of alerts for those people.
+			updateMemberData($members, array('alerts' => '+'));
+		}
+
+		// Secondly, anyone who wants emails.
+		if (!empty($notifies['email']))
+		{
+			// Emails are a bit complicated. We have to do language stuff.
+			require_once($sourcedir . '/Subs-Post.php');
+			require_once($sourcedir . '/ScheduledTasks.php');
+			loadEssentialThemeData();
+
+			// First, get everyone's language and details.
+			$emails = array();
+			$request = $smcFunc['db_query']('', '
+				SELECT id_member, lngfile, email_address
+				FROM {db_prefix}members
+				WHERE id_member IN ({array_int:members})',
+				array(
+					'members' => $notifies['email'],
+				)
+			);
+			while ($row = $smcFunc['db_fetch_assoc']($request))
+			{
+				if (empty($row['lngfile']))
+					$row['lngfile'] = $language;
+				$emails[$row['lngfile']][$row['id_member']] = $row['email_address'];
+			}
+			$smcFunc['db_free_result']($request);
+
+			// Second, iterate through each language, load the relevant templates and set up sending.
+			foreach ($emails as $this_lang => $recipients)
+			{
+				$replacements = array(
+					'USERNAME' => $this->_details['new_member_name'],
+					'PROFILELINK' => $scripturl . '?action=profile;u=' . $this->_details['new_member_id']
+				);
+				$emailtype = 'admin_notify';
+
+				// If they need to be approved add more info...
+				if ($this->_details['notify_type'] == 'approval')
+				{
+					$replacements['APPROVALLINK'] = $scripturl . '?action=admin;area=viewmembers;sa=browse;type=approve';
+					$emailtype .= '_approval';
+				}
+
+				$emaildata = loadEmailTemplate($emailtype, $replacements, empty($modSettings['userLanguage']) ? $language : $this_lang);
+
+				// And do the actual sending...
+				foreach ($recipients as $id_member => $email_address)
+					sendmail($email_address, $emaildata['subject'], $emaildata['body'], null, null, false, 0);
+			}
+		}
+
+		// And now we're all done.
+		return true;
+	}
+}
+
+?>