Completed
Push — release-2.1 ( 6f6d35...abeae7 )
by Mathias
08:46
created

Notify.php ➔ BoardNotify()   B

Complexity

Conditions 7
Paths 36

Size

Total Lines 59
Code Lines 31

Duplication

Lines 32
Ratio 54.24 %

Importance

Changes 0
Metric Value
cc 7
eloc 31
nc 36
nop 0
dl 32
loc 59
rs 7.5346
c 0
b 0
f 0

How to fix   Long Method   

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 just the functions that turn on and off notifications
5
 * to topics or boards.
6
 *
7
 * Simple Machines Forum (SMF)
8
 *
9
 * @package SMF
10
 * @author Simple Machines http://www.simplemachines.org
11
 * @copyright 2017 Simple Machines and individual contributors
12
 * @license http://www.simplemachines.org/about/smf/license.php BSD
13
 *
14
 * @version 2.1 Beta 4
15
 */
16
17
if (!defined('SMF'))
18
	die('No direct access...');
19
20
/**
21
 * Turn off/on notification for a particular board.
22
 * Must be called with a board specified in the URL.
23
 * Only uses the template if no sub action is used. (on/off)
24
 * Redirects the user back to the board after it is done.
25
 * Accessed via ?action=notifyboard.
26
 *
27
 * @uses Notify template, notify_board sub-template.
28
 */
29
function BoardNotify()
30
{
31
	global $board, $user_info, $context, $smcFunc, $sourcedir;
32
33
	// Permissions are an important part of anything ;).
34
	is_not_guest();
35
36
	// You have to specify a board to turn notifications on!
37
	if (empty($board))
38
		fatal_lang_error('no_board', false);
39
40
	// No subaction: find out what to do.
41
	if (isset($_GET['mode']))
42
	{
43
		checkSession('get');
44
45
		$mode = (int) $_GET['mode'];
46
		$alertPref = $mode <= 1 ? 0 : ($mode == 2 ? 1 : 3);
47
48
		require_once($sourcedir . '/Subs-Notify.php');
49
		setNotifyPrefs($user_info['id'], array('board_notify_' . $board => $alertPref));
50
51 View Code Duplication
		if ($mode > 1)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
			// Turn notification on.  (note this just blows smoke if it's already on.)
53
			$smcFunc['db_insert']('ignore',
54
				'{db_prefix}log_notify',
55
				array('id_member' => 'int', 'id_board' => 'int'),
56
				array($user_info['id'], $board),
57
				array('id_member', 'id_board')
58
			);
59
		else
60
			$smcFunc['db_query']('', '
61
				DELETE FROM {db_prefix}log_notify
62
				WHERE id_member = {int:current_member}
63
				AND id_board = {int:current_board}',
64
				array(
65
					'current_board' => $board,
66
					'current_member' => $user_info['id'],
67
				)
68
			);
69
70
	}
71
72
	// Back to the board!
73 View Code Duplication
	if (isset($_GET['xml']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
	{
75
		$context['xml_data']['errors'] = array(
76
			'identifier' => 'error',
77
			'children' => array(
78
				array(
79
					'value' => 0,
80
				),
81
			),
82
		);
83
		$context['sub_template'] = 'generic_xml';
84
	}
85
	else
86
		redirectexit('board=' . $board . '.' . $_REQUEST['start']);
87
}
88
89
/**
90
 * Turn off/on unread replies subscription for a topic as well as sets individual topic's alert preferences
91
 * Must be called with a topic specified in the URL.
92
 * The mode can be from 0 to 3
93
 * 0 => unwatched, 1 => no alerts/emails, 2 => alerts, 3 => emails/alerts
94
 * Upon successful completion of action will direct user back to topic.
95
 * Accessed via ?action=unwatchtopic.
96
 */
97
function TopicNotify()
98
{
99
	global $smcFunc, $user_info, $topic, $sourcedir, $context;
100
101
	// Let's do something only if the function is enabled
102
	if (!$user_info['is_guest'])
103
	{
104
		checkSession('get');
105
106
		if (isset($_GET['mode']))
107
		{
108
			$mode = (int) $_GET['mode'];
109
			$alertPref = $mode <= 1 ? 0 : ($mode == 2 ? 1 : 3);
110
111
			if (empty($mode))
112
				$mode = 1;
113
114
			$request = $smcFunc['db_query']('', '
115
				SELECT id_member, id_topic, id_msg, unwatched
116
				FROM {db_prefix}log_topics
117
				WHERE id_member = {int:current_user}
118
					AND id_topic = {int:current_topic}',
119
				array(
120
					'current_user' => $user_info['id'],
121
					'current_topic' => $topic,
122
				)
123
			);
124
			$log = $smcFunc['db_fetch_assoc']($request);
125
			$smcFunc['db_free_result']($request);
126
			if (empty($log))
127
			{
128
				$insert = true;
129
				$log = array(
130
					'id_member' => $user_info['id'],
131
					'id_topic' => $topic,
132
					'id_msg' => 0,
133
					'unwatched' => empty($mode) ? 1 : 0,
134
				);
135
			}
136
			else
137
			{
138
				$insert = false;
139
				$log['unwatched'] = empty($mode) ? 1 : 0;
140
			}
141
142
			$smcFunc['db_insert']($insert ? 'insert' : 'replace',
143
				'{db_prefix}log_topics',
144
				array(
145
					'id_member' => 'int', 'id_topic' => 'int', 'id_msg' => 'int', 'unwatched' => 'int',
146
				),
147
				$log,
148
				array('id_member', 'id_topic')
149
			);
150
151
			require_once($sourcedir . '/Subs-Notify.php');
152
			setNotifyPrefs($user_info['id'], array('topic_notify_' . $log['id_topic'] => $alertPref));
153
154 View Code Duplication
			if ($mode > 1)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
			{
156
				// Turn notification on.  (note this just blows smoke if it's already on.)
157
				$smcFunc['db_insert']('ignore',
158
					'{db_prefix}log_notify',
159
					array('id_member' => 'int', 'id_topic' => 'int'),
160
					array($user_info['id'], $log['id_topic']),
161
					array('id_member', 'id_board')
162
				);
163
			}
164
			else
165
				$smcFunc['db_query']('', '
166
					DELETE FROM {db_prefix}log_notify
167
					WHERE id_topic = {int:topic}
168
						AND id_member = {int:member}',
169
					array(
170
						'topic' => $log['id_topic'],
171
						'member' => $user_info['id'],
172
					));
173
174
		}
175
	}
176
177
	// Back to the topic.
178 View Code Duplication
	if (isset($_GET['xml']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
179
	{
180
		$context['xml_data']['errors'] = array(
181
			'identifier' => 'error',
182
			'children' => array(
183
				array(
184
					'value' => 0,
185
				),
186
			),
187
		);
188
		$context['sub_template'] = 'generic_xml';
189
	}
190
	else
191
		redirectexit('topic=' . $topic . '.' . $_REQUEST['start']);
192
}
193
194
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...