Passed
Push — release-2.1 ( 37fb8c...587121 )
by Jon
05:40 queued 12s
created

GroupAct_Notify_Background::execute()   D

Complexity

Conditions 18
Paths 27

Size

Total Lines 136
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 18
eloc 71
c 1
b 1
f 0
nc 27
nop 0
dl 0
loc 136
rs 4.8666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file contains code used to notify a member when a group moderator has
5
 * taken action on that member's request to join a group.
6
 *
7
 * Simple Machines Forum (SMF)
8
 *
9
 * @package SMF
10
 * @author Simple Machines https://www.simplemachines.org
11
 * @copyright 2021 Simple Machines and individual contributors
12
 * @license https://www.simplemachines.org/about/smf/license.php BSD
13
 *
14
 * @version 2.1 RC4
15
 */
16
17
/**
18
 * Class GroupAct_Notify_Background
19
 */
20
class GroupAct_Notify_Background extends SMF_BackgroundTask
21
{
22
	/**
23
	 * This executes the task: loads up the info, puts the email in the queue
24
	 * and inserts any alerts as needed.
25
	 *
26
	 * @return bool Always returns true
27
	 */
28
	public function execute()
29
	{
30
		global $sourcedir, $smcFunc, $language, $modSettings;
31
32
		// Get the details of all the members concerned...
33
		$request = $smcFunc['db_query']('', '
34
			SELECT lgr.id_request, lgr.id_member, lgr.id_group, mem.email_address,
35
				mem.lngfile, mem.member_name,  mg.group_name, mg.hidden
36
			FROM {db_prefix}log_group_requests AS lgr
37
				INNER JOIN {db_prefix}members AS mem ON (mem.id_member = lgr.id_member)
38
				INNER JOIN {db_prefix}membergroups AS mg ON (mg.id_group = lgr.id_group)
39
			WHERE lgr.id_request IN ({array_int:request_list})
40
			ORDER BY mem.lngfile',
41
			array(
42
				'request_list' => $this->_details['request_list'],
43
			)
44
		);
45
		$affected_users = array();
46
		$members = array();
47
		$alert_rows = array();
48
		while ($row = $smcFunc['db_fetch_assoc']($request))
49
		{
50
			$members[] = $row['id_member'];
51
			$row['lngfile'] = empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile'];
52
53
			// If we are approving, add them!
54
			if ($this->_details['status'] == 'approve')
55
			{
56
				// Hack in blank permissions so that allowedTo() will fail.
57
				require_once($sourcedir . '/Security.php');
58
				$user_info['permissions'] = array();
59
60
				// For the modlog
61
				$user_info['id'] = $this->_details['member_id'];
62
				$user_info['ip'] = $this->_details['member_ip'];
63
64
				require_once($sourcedir . '/Subs-Membergroups.php');
65
				addMembersToGroup($row['id_member'], $row['id_group'], $row['hidden'] == 2 ? 'only_additional' : 'auto', true);
66
			}
67
68
			// Build the required information array
69
			$affected_users[$row['id_member']] = array(
70
				'rid' => $row['id_request'],
71
				'member_id' => $row['id_member'],
72
				'member_name' => $row['member_name'],
73
				'group_id' => $row['id_group'],
74
				'group_name' => $row['group_name'],
75
				'email' => $row['email_address'],
76
				'language' => $row['lngfile'],
77
			);
78
		}
79
		$smcFunc['db_free_result']($request);
80
81
		// Ensure everyone who is online gets their changes right away.
82
		updateSettings(array('settings_updated' => time()));
83
84
		if (!empty($affected_users))
85
		{
86
			require_once($sourcedir . '/Subs-Notify.php');
87
88
			$prefs = getNotifyPrefs($members, array('groupr_approved', 'groupr_rejected'), true);
89
90
			// Same as for approving, kind of.
91
			foreach ($affected_users as $user)
92
			{
93
				$custom_reason = isset($this->_details['reason']) && isset($this->_details['reason'][$user['rid']]) ? $this->_details['reason'][$user['rid']] : '';
94
95
				// They are being approved?
96
				if ($this->_details['status'] == 'approve')
97
				{
98
					$pref_name = 'approved';
99
					$email_template_name = 'mc_group_approve';
100
					$email_message_id_prefix = 'grpapp';
101
				}
102
103
				// Otherwise, they are getting rejected (With or without a reason).
104
				else
105
				{
106
					$pref_name = 'rejected';
107
					$email_template_name = empty($custom_reason) ? 'mc_group_reject' : 'mc_group_reject_reason';
108
					$email_message_id_prefix = 'grprej';
109
				}
110
111
				$pref = !empty($prefs[$user['member_id']]['groupr_' . $pref_name]) ? $prefs[$user['member_id']]['groupr_' . $pref_name] : 0;
112
				if ($pref & self::RECEIVE_NOTIFY_ALERT)
113
				{
114
					$alert_rows[] = array(
115
						'alert_time' => time(),
116
						'id_member' => $user['member_id'],
117
						'content_type' => 'groupr',
118
						'content_id' => 0,
119
						'content_action' => $pref_name,
120
						'is_read' => 0,
121
						'extra' => $smcFunc['json_encode'](array('group_name' => $user['group_name'], 'reason' => !empty($custom_reason) ? '<br><br>' . $custom_reason : '')),
122
					);
123
				}
124
125
				if ($pref & self::RECEIVE_NOTIFY_EMAIL)
126
				{
127
					// Emails are a bit complicated. We have to do language stuff.
128
					require_once($sourcedir . '/Subs-Post.php');
129
					require_once($sourcedir . '/ScheduledTasks.php');
130
					loadEssentialThemeData();
131
132
					$replacements = array(
133
						'USERNAME' => $user['member_name'],
134
						'GROUPNAME' => $user['group_name'],
135
					);
136
137
					if (!empty($custom_reason))
138
						$replacements['REASON'] = $custom_reason;
139
140
					$emaildata = loadEmailTemplate($email_template_name, $replacements, $user['language']);
141
142
					sendmail($user['email'], $emaildata['subject'], $emaildata['body'], null, $email_message_id_prefix . $user['rid'], $emaildata['is_html'], 2);
143
				}
144
			}
145
146
			// Insert the alerts if any
147
			if (!empty($alert_rows))
148
			{
149
				$smcFunc['db_insert']('',
150
					'{db_prefix}user_alerts',
151
					array(
152
						'alert_time' => 'int', 'id_member' => 'int', 'content_type' => 'string',
153
						'content_id' => 'int', 'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string',
154
					),
155
					$alert_rows,
156
					array()
157
				);
158
159
				updateMemberData(array_keys($affected_users), array('alerts' => '+'));
160
			}
161
		}
162
163
		return true;
164
	}
165
}
166
167
?>