BoardIndex::action_boardindex()   F
last analyzed

Complexity

Conditions 13
Paths 384

Size

Total Lines 112
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 46
CRAP Score 13.3895

Importance

Changes 0
Metric Value
cc 13
eloc 59
nc 384
nop 0
dl 0
loc 112
ccs 46
cts 53
cp 0.8679
crap 13.3895
rs 3.4833
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
 * The single function this file contains is used to display the main board index.
5
 *
6
 * @package   ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
9
 *
10
 * This file contains code covered by:
11
 * copyright: 2011 Simple Machines (http://www.simplemachines.org)
12
 *
13
 * @version 2.0 dev
14
 *
15
 */
16
17
namespace ElkArte\Controller;
18
19
use ElkArte\AbstractController;
20
use ElkArte\BoardsList;
21
use ElkArte\Cache\Cache;
22
use ElkArte\Exceptions\Exception;
23
use ElkArte\FrontpageInterface;
24
25
/**
26
 * Displays the main board index
27
 */
28
class BoardIndex extends AbstractController implements FrontpageInterface
29
{
30
	/**
31
	 * {@inheritDoc}
32
	 */
33
	public static function frontPageHook(&$default_action)
34
	{
35
		$default_action = [
36
			'controller' => BoardIndex::class,
37
			'function' => 'action_boardindex'
38
		];
39
	}
40
41
	/**
42
	 * Forwards to the action to execute here by default.
43
	 *
44
	 * @see AbstractController::action_index
45
	 */
46 2
	public function action_index()
47
	{
48
		// What to do... boardindex, 'course!
49 2
		$this->action_boardindex();
50 2
	}
51
52
	/**
53
	 * This function shows the board index.
54
	 *
55
	 * What it does:
56
	 *
57
	 * - It updates the most online statistics.
58
	 * - It is accessed by ?action=boardindex.
59
	 *
60
	 * @uses the BoardIndex template, and main sub template
61
	 */
62 2
	public function action_boardindex()
63
	{
64 2
		global $txt, $modSettings, $context, $settings;
65
66 2
		theme()->getTemplates()->load('BoardIndex');
67
68
		// Set a canonical URL for this page.
69 2
		$context['canonical_url'] = getUrl('boardindex', []);
70 2
		theme()->getLayers()->add('boardindex_outer');
71
72
		// Do not let search engines index anything if there is a random thing in $_GET.
73 2
		if (!empty($this->_req->query))
74
		{
75 2
			$context['robot_no_index'] = true;
76
		}
77
78
		// Retrieve the categories and boards.
79
		$boardIndexOptions = array(
80 2
			'include_categories' => true,
81 2
			'base_level' => 0,
82 2
			'parent_id' => 0,
83
			'set_latest_post' => true,
84 2
			'countChildPosts' => !empty($modSettings['countChildPosts']),
85
		);
86
87 2
		$this->_events->trigger('pre_load', array('boardIndexOptions' => &$boardIndexOptions));
88
89 2
		$boardlist = new BoardsList($boardIndexOptions);
90 2
		$context['categories'] = $boardlist->getBoards();
91 2
		$context['latest_post'] = $boardlist->getLatestPost();
92
93
		// Get the user online list.
94 2
		require_once(SUBSDIR . '/MembersOnline.subs.php');
95
		$membersOnlineOptions = array(
96 2
			'show_hidden' => allowedTo('moderate_forum'),
97 2
			'sort' => 'log_time',
98
			'reverse_sort' => true,
99
		);
100 2
		$context += getMembersOnlineStats($membersOnlineOptions);
101
102 2
		$context['show_buddies'] = !empty($this->user->buddies);
0 ignored issues
show
Bug Best Practice introduced by
The property buddies does not exist on ElkArte\Helper\ValuesContainer. Since you implemented __get, consider adding a @property annotation.
Loading history...
103
104
		// Are we showing all membergroups on the board index?
105 2
		if (!empty($settings['show_group_key']))
106
		{
107
			$context['membergroups'] = Cache::instance()->quick_get('membergroup_list', 'subs/Membergroups.subs.php', 'cache_getMembergroupList', array());
108
		}
109
110
		// Track most online statistics? (subs/Members.subs.phpOnline.php)
111 2
		if (!empty($modSettings['trackStats']))
112
		{
113 2
			trackStatsUsersOnline($context['num_guests'] + $context['num_users_online']);
114
		}
115
116
		// Retrieve the latest posts if the theme settings require it.
117 2
		if (isset($settings['number_recent_posts']) && $settings['number_recent_posts'] > 1)
118
		{
119
			$latestPostOptions = array(
120
				'number_posts' => $settings['number_recent_posts'],
121
				'id_member' => $this->user->id,
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on ElkArte\Helper\ValuesContainer. Since you implemented __get, consider adding a @property annotation.
Loading history...
122
			);
123
			if (empty($settings['recent_post_topics']))
124
			{
125
				$context['latest_posts'] = Cache::instance()->quick_get('boardindex-latest_posts:' . md5($this->user->query_wanna_see_board . $this->user->language), 'subs/Recent.subs.php', 'cache_getLastPosts', array($latestPostOptions));
0 ignored issues
show
Bug Best Practice introduced by
The property language does not exist on ElkArte\Helper\ValuesContainer. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property query_wanna_see_board does not exist on ElkArte\Helper\ValuesContainer. Since you implemented __get, consider adding a @property annotation.
Loading history...
126
			}
127
			else
128
			{
129
				$context['latest_posts'] = Cache::instance()->quick_get('boardindex-latest_topics:' . md5($this->user->query_wanna_see_board . $this->user->language), 'subs/Recent.subs.php', 'cache_getLastTopics', array($latestPostOptions));
130
			}
131
		}
132
133
		// Let the template know what the members can do if the theme enables these options
134 2
		$context['show_stats'] = allowedTo('view_stats') && !empty($modSettings['trackStats']);
135 2
		$context['show_member_list'] = allowedTo('view_mlist');
136 2
		$context['show_who'] = allowedTo('who_view') && !empty($modSettings['who_enabled']);
137
138 2
		$context['page_title'] = sprintf($txt['forum_index'], $context['forum_name']);
139 2
		$context['sub_template'] = 'boards_list';
140
		$context['page_description'] = implode(' | ', array_column($context['categories'], 'name'));
141 2
142 2
		$context['info_center_callbacks'] = [];
143
		if (!empty($settings['number_recent_posts']) && (!empty($context['latest_posts']) || !empty($context['latest_post'])))
144
		{
145
			$context['info_center_callbacks'][] = 'recent_posts';
146
		}
147 2
148
		if (!empty($settings['show_stats_index']))
149 2
		{
150
			$context['info_center_callbacks'][] = 'show_stats';
151
		}
152 2
153
		$context['info_center_callbacks'][] = 'show_users';
154 2
155
		$this->_events->trigger('post_load', array('callbacks' => &$context['info_center_callbacks']));
156 2
157 2
		theme()->addJavascriptVar(array(
158 2
			'txt_mark_as_read_confirm' => $txt['mark_as_read_confirm']
159
		), true);
160
161 2
		// Mark read button
162 1
		$context['mark_read_button'] = array(
163 2
			'markread' => array(
164 2
				'text' => 'mark_as_read',
165
				'lang' => true,
166 2
				'custom' => 'onclick="return markallreadButton(this);"',
167 2
				'url' => getUrl('action', array('action' => 'markasread', 'sa' => 'all', 'bi', '{session_data}'))
168
			),
169
		);
170
171
		// Allow mods to add additional buttons here
172 2
		call_integration_hook('integrate_mark_read_button');
173 2
		theme()->getLayers()->add('info_center');
174
	}
175 2
176
	/**
177 2
	 * Collapse or expand a category
178
	 *
179
	 * - accessed by ?action=collapse
180
	 */
181
	public function action_collapse()
182
	{
183
		global $context;
184
185
		// Just in case, no need, no need.
186
		$context['robot_no_index'] = true;
187
188
		checkSession('request');
189
190
		if (!isset($this->_req->query->sa))
191
		{
192
			throw new Exception('no_access', false);
193
		}
194
195
		// Check if the input values are correct.
196
		if (isset($this->_req->query->c) && in_array($this->_req->query->sa, array('expand', 'collapse', 'toggle')))
197
		{
198
			// And collapse/expand/toggle the category.
199
			require_once(SUBSDIR . '/Categories.subs.php');
200
			collapseCategories(array((int) $this->_req->query->c), $this->_req->query->sa, array($this->user->id));
201
		}
202
203
		// And go back to the board index.
204
		$this->action_boardindex();
205
	}
206
}
207