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

MemberReportReply_Notify_Background::execute()   D

Complexity

Conditions 15
Paths 68

Size

Total Lines 160
Code Lines 90

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 90
c 0
b 0
f 0
nc 68
nop 0
dl 0
loc 160
rs 4.9515

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 task handles notifying users when they've commented to a moderation report and
5
 * someone else replies to them.
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 MemberReportReply_Notify_Background
19
 */
20
class MemberReportReply_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 $smcFunc, $sourcedir, $modSettings, $language, $scripturl;
29
30
		// Let's see. Let us, first of all, establish the list of possible people.
31
		$possible_members = array();
32
		$request = $smcFunc['db_query']('', '
33
			SELECT id_member
34
			FROM {db_prefix}log_comments
35
			WHERE id_notice = {int:report}
36
				AND comment_type = {literal:reportc}
37
				AND id_comment < {int:last_comment}',
38
			array(
39
				'report' => $this->_details['report_id'],
40
				'last_comment' => $this->_details['comment_id'],
41
			)
42
		);
43
		while ($row = $smcFunc['db_fetch_row']($request))
44
			$possible_members[] = $row[0];
45
		$smcFunc['db_free_result']($request);
46
47
		// Presumably, there are some people?
48
		if (!empty($possible_members))
49
		{
50
			$possible_members = array_flip(array_flip($possible_members));
51
			$possible_members = array_diff($possible_members, array($this->_details['sender_id']));
52
		}
53
		if (empty($possible_members))
54
			return true;
55
56
		// We need to know who can moderate this board - and therefore who can see this report.
57
		// First up, people who have moderate_board in the board this topic was in.
58
		require_once($sourcedir . '/Subs-Members.php');
59
		$members = membersAllowedTo('moderate_forum');
60
61
		// Having successfully figured this out, now let's get the preferences of everyone.
62
		require_once($sourcedir . '/Subs-Notify.php');
63
		$prefs = getNotifyPrefs($members, 'member_report_reply', true);
64
65
		// So now we find out who wants what.
66
		$alert_bits = array(
67
			'alert' => self::RECEIVE_NOTIFY_ALERT,
68
			'email' => self::RECEIVE_NOTIFY_EMAIL,
69
		);
70
		$notifies = array();
71
72
		foreach ($prefs as $member => $pref_option)
73
		{
74
			foreach ($alert_bits as $type => $bitvalue)
75
			{
76
				if ($pref_option['member_report_reply'] & $bitvalue)
77
					$notifies[$type][] = $member;
78
			}
79
		}
80
81
		// Firstly, anyone who wants alerts.
82
		if (!empty($notifies['alert']))
83
		{
84
			// Alerts are relatively easy.
85
			$insert_rows = array();
86
			foreach ($notifies['alert'] as $member)
87
			{
88
				$insert_rows[] = array(
89
					'alert_time' => $this->_details['time'],
90
					'id_member' => $member,
91
					'id_member_started' => $this->_details['sender_id'],
92
					'member_name' => $this->_details['sender_name'],
93
					'content_type' => 'member',
94
					'content_id' => $this->_details['report_id'],
95
					'content_action' => 'report_reply',
96
					'is_read' => 0,
97
					'extra' => $smcFunc['json_encode'](
98
						array(
99
							'content_link' => '{SCRIPTURL}?action=moderate;area=reportedmembers;sa=details;rid={CONTENT_ID}',
100
							'user_name' => $this->_details['user_name'],
101
						)
102
					),
103
				);
104
			}
105
106
			$smcFunc['db_insert'](
107
				'insert',
108
				'{db_prefix}user_alerts',
109
				array(
110
					'alert_time' => 'int',
111
					'id_member' => 'int',
112
					'id_member_started' => 'int',
113
					'member_name' => 'string',
114
					'content_type' => 'string',
115
					'content_id' => 'int',
116
					'content_action' => 'string',
117
					'is_read' => 'int',
118
					'extra' => 'string',
119
				),
120
				$insert_rows,
121
				array('id_alert')
122
			);
123
124
			// And update the count of alerts for those people.
125
			updateMemberData($notifies['alert'], array('alerts' => '+'));
126
		}
127
128
		// Secondly, anyone who wants emails.
129
		if (!empty($notifies['email']))
130
		{
131
			// Emails are a bit complicated. We have to do language stuff.
132
			require_once($sourcedir . '/Subs-Post.php');
133
			require_once($sourcedir . '/ScheduledTasks.php');
134
			loadEssentialThemeData();
135
136
			// First, get everyone's language and details.
137
			$emails = array();
138
			$request = $smcFunc['db_query']('', '
139
				SELECT id_member, lngfile, email_address
140
				FROM {db_prefix}members
141
				WHERE id_member IN ({array_int:members})',
142
				array(
143
					'members' => $notifies['email'],
144
				)
145
			);
146
			while ($row = $smcFunc['db_fetch_assoc']($request))
147
			{
148
				if (empty($row['lngfile']))
149
					$row['lngfile'] = $language;
150
				$emails[$row['lngfile']][$row['id_member']] = $row['email_address'];
151
			}
152
			$smcFunc['db_free_result']($request);
153
154
			// Iterate through each language, load the relevant templates and set up sending.
155
			foreach ($emails as $this_lang => $recipients)
156
			{
157
				$replacements = array(
158
					'MEMBERNAME' => $this->_details['member_name'],
159
					'COMMENTERNAME' => $this->_details['sender_name'],
160
					'PROFILELINK' => $scripturl . 'action=profile;u=' . $this->_details['user_id'],
161
					'REPORTLINK' => $scripturl . '?action=moderate;area=userreports;report=' . $this->_details['report_id'],
162
				);
163
164
				$emaildata = loadEmailTemplate(
165
					'reply_to_user_reports',
166
					$replacements,
167
					empty($modSettings['userLanguage']) ? $language : $this_lang
168
				);
169
170
				// And do the actual sending...
171
				foreach ($recipients as $id_member => $email_address)
172
					sendmail(
173
						$email_address,
174
						$emaildata['subject'],
175
						$emaildata['body'],
176
						null,
177
						'urptrpy' . $this->_details['comment_id'],
178
						$emaildata['is_html'],
179
						3
180
					);
181
			}
182
		}
183
184
		// And now we're all done.
185
		return true;
186
	}
187
}
188
189
?>