Completed
Pull Request — release-2.1 (#6024)
by John
15:33
created

GroupAct_Notify_Background::execute()   D

Complexity

Conditions 17
Paths 27

Size

Total Lines 154
Code Lines 87

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 17
eloc 87
c 1
b 0
f 0
nc 27
nop 0
dl 0
loc 154
rs 4.4569

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