Completed
Pull Request — release-2.1 (#4846)
by Jeremy
09:05
created

Topic.php ➔ Sticky()   C

Complexity

Conditions 11
Paths 12

Size

Total Lines 55

Duplication

Lines 4
Ratio 7.27 %

Importance

Changes 0
Metric Value
cc 11
nc 12
nop 0
dl 4
loc 55
rs 6.8351
c 0
b 0
f 0

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 takes care of actions on topics:
5
 * lock/unlock a topic, sticky/unsticky it,
6
 *
7
 * Simple Machines Forum (SMF)
8
 *
9
 * @package SMF
10
 * @author Simple Machines http://www.simplemachines.org
11
 * @copyright 2018 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
 * Locks a topic... either by way of a moderator or the topic starter.
22
 * What this does:
23
 *  - locks a topic, toggles between locked/unlocked/admin locked.
24
 *  - only admins can unlock topics locked by other admins.
25
 *  - requires the lock_own or lock_any permission.
26
 *  - logs the action to the moderator log.
27
 *  - returns to the topic after it is done.
28
 *  - it is accessed via ?action=lock.
29
*/
30
function LockTopic()
31
{
32
	global $topic, $user_info, $sourcedir, $board, $smcFunc;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
33
34
	// Just quit if there's no topic to lock.
35
	if (empty($topic))
36
		fatal_lang_error('not_a_topic', false);
37
38
	checkSession('get');
39
40
	// Get Subs-Post.php for sendNotifications.
41
	require_once($sourcedir . '/Subs-Post.php');
42
43
	// Find out who started the topic - in case User Topic Locking is enabled.
44
	$request = $smcFunc['db_query']('', '
45
		SELECT id_member_started, locked
46
		FROM {db_prefix}topics
47
		WHERE id_topic = {int:current_topic}
48
		LIMIT 1',
49
		array(
50
			'current_topic' => $topic,
51
		)
52
	);
53
	list ($starter, $locked) = $smcFunc['db_fetch_row']($request);
54
	$smcFunc['db_free_result']($request);
55
56
	// Can you lock topics here, mister?
57
	$user_lock = !allowedTo('lock_any');
58
	if ($user_lock && $starter == $user_info['id'])
59
		isAllowedTo('lock_own');
60
	else
61
		isAllowedTo('lock_any');
62
63
	// Another moderator got the job done first?
64 View Code Duplication
	if (isset($_GET['sa']) && $_GET['sa'] == 'unlock' && $locked == '0')
65
		fatal_lang_error('error_topic_locked_already', false);
66
	elseif (isset($_GET['sa']) && $_GET['sa'] == 'lock' && ($locked == '1' || $locked == '2'))
67
		fatal_lang_error('error_topic_locked_already', false);
68
69
	// Locking with high privileges.
70
	if ($locked == '0' && !$user_lock)
71
		$locked = '1';
72
	// Locking with low privileges.
73
	elseif ($locked == '0')
74
		$locked = '2';
75
	// Unlocking - make sure you don't unlock what you can't.
76
	elseif ($locked == '2' || ($locked == '1' && !$user_lock))
77
		$locked = '0';
78
	// You cannot unlock this!
79
	else
80
		fatal_lang_error('locked_by_admin', 'user');
81
82
	// Actually lock the topic in the database with the new value.
83
	$smcFunc['db_query']('', '
84
		UPDATE {db_prefix}topics
85
		SET locked = {int:locked}
86
		WHERE id_topic = {int:current_topic}',
87
		array(
88
			'current_topic' => $topic,
89
			'locked' => $locked,
90
		)
91
	);
92
93
	// If they are allowed a "moderator" permission, log it in the moderator log.
94
	if (!$user_lock)
95
		logAction($locked ? 'lock' : 'unlock', array('topic' => $topic, 'board' => $board));
96
	// Notify people that this topic has been locked?
97
	sendNotifications($topic, empty($locked) ? 'unlock' : 'lock');
98
99
	// Back to the topic!
100
	redirectexit('topic=' . $topic . '.' . $_REQUEST['start'] . ';moderate');
101
}
102
103
/**
104
 * Sticky a topic.
105
 * Can't be done by topic starters - that would be annoying!
106
 * What this does:
107
 *  - stickies a topic - toggles between sticky and normal.
108
 *  - requires the make_sticky permission.
109
 *  - adds an entry to the moderator log.
110
 *  - when done, sends the user back to the topic.
111
 *  - accessed via ?action=sticky.
112
 */
113
function Sticky()
114
{
115
	global $topic, $board, $sourcedir, $smcFunc;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
116
117
	// Make sure the user can sticky it, and they are stickying *something*.
118
	isAllowedTo('make_sticky');
119
120
	// You can't sticky a board or something!
121
	if (empty($topic))
122
		fatal_lang_error('not_a_topic', false);
123
124
	checkSession('get');
125
126
	// We need Subs-Post.php for the sendNotifications() function.
127
	require_once($sourcedir . '/Subs-Post.php');
128
129
	// Is this topic already stickied, or no?
130
	$request = $smcFunc['db_query']('', '
131
		SELECT is_sticky
132
		FROM {db_prefix}topics
133
		WHERE id_topic = {int:current_topic}
134
		LIMIT 1',
135
		array(
136
			'current_topic' => $topic,
137
		)
138
	);
139
	list ($is_sticky) = $smcFunc['db_fetch_row']($request);
140
	$smcFunc['db_free_result']($request);
141
142
	// Another moderator got the job done first?
143 View Code Duplication
	if (isset($_GET['sa']) && $_GET['sa'] == 'nonsticky' && $is_sticky == '0')
144
		fatal_lang_error('error_topic_nonsticky_already', false);
145
	elseif (isset($_GET['sa']) && $_GET['sa'] == 'sticky' && $is_sticky == '1')
146
		fatal_lang_error('error_topic_sticky_already', false);
147
148
	// Toggle the sticky value.... pretty simple ;).
149
	$smcFunc['db_query']('', '
150
		UPDATE {db_prefix}topics
151
		SET is_sticky = {int:is_sticky}
152
		WHERE id_topic = {int:current_topic}',
153
		array(
154
			'current_topic' => $topic,
155
			'is_sticky' => empty($is_sticky) ? 1 : 0,
156
		)
157
	);
158
159
	// Log this sticky action - always a moderator thing.
160
	logAction(empty($is_sticky) ? 'sticky' : 'unsticky', array('topic' => $topic, 'board' => $board));
161
	// Notify people that this topic has been stickied?
162
	if (empty($is_sticky))
163
		sendNotifications($topic, 'sticky');
164
165
	// Take them back to the now stickied topic.
166
	redirectexit('topic=' . $topic . '.' . $_REQUEST['start'] . ';moderate');
167
}
168
169
?>