PortalMain_Controller::action_userblockorder()   B
last analyzed

Complexity

Conditions 9
Paths 6

Size

Total Lines 87
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 42
nc 6
nop 0
dl 0
loc 87
rs 7.6924
c 1
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
 * @package SimplePortal ElkArte
5
 *
6
 * @author SimplePortal Team
7
 * @copyright 2015-2021 SimplePortal Team
8
 * @license BSD 3-clause
9
 * @version 1.0.0
10
 */
11
12
use ElkArte\sources\Frontpage_Interface;
13
14
/**
15
 * PortalMain_Controller controller.
16
 *
17
 * - This class handles requests that allow viewing the main portal or the portal credits
18
 * - Handles the front page block arrangement, including resetting
19
 */
20
class PortalMain_Controller extends Action_Controller implements Frontpage_Interface
21
{
22
	/**
23
	 * Common actions for all methods in the class
24
	 */
25
	public function pre_dispatch()
26
	{
27
		global $context;
28
29
		if (!sp_is_active())
30
		{
31
			redirectexit();
32
		}
33
34
		$context['page_title'] = $context['forum_name'];
35
36
		if (isset($context['page_title_html_safe']))
37
		{
38
			$context['page_title_html_safe'] = Util::htmlspecialchars(un_htmlspecialchars($context['page_title']));
39
		}
40
41
		if (!empty($context['standalone']))
42
		{
43
			setupMenuContext();
44
		}
45
	}
46
47
	/**
48
	 * Default method, just forwards
49
	 */
50
	public function action_index()
51
	{
52
		require_once(SUBSDIR . '/Action.class.php');
53
54
		// Where do you want to go today?
55
		$subActions = array(
56
			'index' => array($this, 'action_sportal_index'),
57
			'credits' => array($this, 'action_sportal_credits'),
58
			'resetlayout' => array($this, 'action_sportal_resetLayout'),
59
			'userorder' => array($this, 'action_userblockorder'),
60
			'spattach' => array('controller' => 'PortalArticles_Controller', 'dir' => CONTROLLERDIR, 'file' => 'PortalArticles.controller.php', 'function' => 'action_index'),
61
		);
62
63
		// We like action, so lets get ready for some
64
		$action = new Action('');
65
66
		// Get the subAction, or just go to action_sportal_index
67
		$subAction = $action->initialize($subActions, 'index');
68
69
		// Finally go to where we want to go
70
		$action->dispatch($subAction);
71
	}
72
73
	/**
74
	 * Don't track for xml requests
75
	 */
76
	public function trackStats($action = '')
77
	{
78
		if (isset($this->_req->xml))
79
		{
80
			return false;
81
		}
82
83
		return parent::trackStats($action);
84
	}
85
86
	/**
87
	 * Used to change the default action with a new one.
88
	 *
89
	 * Called statically from the Dispatcher to the controller listed in $modSettings['front_page']
90
	 * Can be used to call add_integration_function() for added functions (non permanent)
91
	 *
92
	 * @param string[] $default_action
93
	 */
94
	public static function frontPageHook(&$default_action)
95
	{
96
		global $modSettings;
97
98
		// Need to determine if the portal is active
99
		require_once(SUBSDIR . '/Portal.subs.php');
100
101
		// Any actions we need to handle with the portal, set up the action here.
102
		if (sp_is_active())
103
		{
104
			$file = null;
105
			$function = null;
106
107
			if (empty($_GET['page']) && empty($_GET['article']) && empty($_GET['category']) && $modSettings['sp_portal_mode'] == 1)
108
			{
109
				// View the portal front page
110
				$file = CONTROLLERDIR . '/PortalMain.controller.php';
111
				$controller = 'PortalMain_Controller';
112
				$function = 'action_sportal_index';
113
			}
114
			elseif (!empty($_GET['page']))
115
			{
116
				// View a specific page
117
				$file = CONTROLLERDIR . '/PortalPages.controller.php';
118
				$controller = 'PortalPages_Controller';
119
				$function = 'action_sportal_page';
120
			}
121
			elseif (!empty($_GET['article']))
122
			{
123
				// View a specific article
124
				$file = CONTROLLERDIR . '/PortalArticles.controller.php';
125
				$controller = 'PortalArticles_Controller';
126
				$function = 'action_sportal_article';
127
			}
128
			elseif (!empty($_GET['category']))
129
			{
130
				// View a specific category
131
				$file = CONTROLLERDIR . '/PortalCategories.controller.php';
132
				$controller = 'PortalCategories_Controller';
133
				$function = 'action_sportal_category';
134
			}
135
136
			// Something portal-ish, then set the new action
137
			if (isset($file, $function))
138
			{
139
				$default_action = array(
140
					'file' => $file,
141
					'controller' => $controller ?? null,
142
					'function' => $function
143
				);
144
			}
145
		}
146
	}
147
148
	/**
149
	 * If this controller is capable of being the front page.
150
	 *
151
	 * - damn right it can!
152
	 * - Used by system to "find" front page controllers
153
	 */
154
	public static function canFrontPage()
155
	{
156
		return true;
157
	}
158
159
	/**
160
	 * Add the portal action to allowable ones for the front page
161
	 *
162
	 * Used by Configuration -> Layout
163
	 */
164
	public static function frontPageOptions()
165
	{
166
		global $txt;
167
168
		parent::frontPageOptions();
169
170
		loadLanguage('SPortalAdmin');
171
172
		// Used to show/hide the portal front page options
173
		addInlineJavascript('
174
			$(\'#front_page\').on(\'change\', function() {
175
				var $base = $(\'#sp_portal_mode\').parent();
176
				
177
				if ($(this).val() === \'PortalMain_Controller\')
178
				{
179
					$base.fadeIn();
180
					$base.prev().fadeIn();
181
				}
182
				else
183
				{
184
					$base.fadeOut();
185
					$base.prev().fadeOut();
186
				}
187
			}).change();', true);
188
189
		// Adds Frontpage, Integrate and Standalone portal mode options.
190
		return array(array('select', 'sp_portal_mode', explode('|', $txt['sp_portal_mode_options'])));
191
	}
192
193
	/**
194
	 * Loads article previews for display with the portal index template
195
	 */
196
	public function action_sportal_index()
197
	{
198
		global $context, $modSettings;
199
200
		// Showing articles on the index page?
201
		if (!empty($modSettings['sp_articles_index']))
202
		{
203
			require_once(SUBSDIR . '/PortalArticle.subs.php');
204
205
			// Set up the pages
206
			$total_articles = sportal_get_articles_count();
207
			$total = min($total_articles, !empty($modSettings['sp_articles_index_total']) ? $modSettings['sp_articles_index_total'] : 20);
208
			$per_page = min($total, !empty($modSettings['sp_articles_index_per_page']) ? $modSettings['sp_articles_index_per_page'] : 5);
209
			$start = !empty($_REQUEST['articles']) ? (int) $_REQUEST['articles'] : 0;
210
211
			if ($total > $per_page)
212
			{
213
				$context['article_page_index'] = constructPageIndex($context['portal_url'] . '?articles=%1$d', $start, $total, $per_page, true);
214
			}
215
216
			// If we have some articles
217
			require_once(SUBSDIR . '/PortalArticle.subs.php');
218
			$context['articles'] = sportal_get_articles(0, true, true, 'spa.id_article DESC', 0, $per_page, $start);
219
220
			foreach ($context['articles'] as $article)
221
			{
222
				$context['articles'][$article['id']]['preview'] = censor($article['body']);
223
				$context['articles'][$article['id']]['date'] = htmlTime($article['date']);
224
				$context['articles'][$article['id']]['time'] = $article['date'];
225
226
				// Fetch attachments, if there are any
227
				if (!empty($modSettings['attachmentEnable']) && !empty($article['has_attachments']))
228
				{
229
					$context['articles'][$article['id']]['attachment'] = sportal_load_attachment_context($article['id']);
230
				}
231
232
				// Parse / shorten as required
233
				$context['article']['id'] = $article['id'];
234
				sportal_parse_cutoff_content($context['articles'][$article['id']]['preview'], $article['type'], $modSettings['sp_articles_length'], $context['articles'][$article['id']]['article_id']);
235
			}
236
		}
237
238
		$context['sub_template'] = 'portal_index';
239
		Templates::instance()->load('Portal');
240
	}
241
242
	/**
243
	 * Displays the credit page outside of the admin area,
244
	 *
245
	 * - Forwards to admin controller to display credits outside the admin area
246
	 */
247
	public function action_sportal_credits()
248
	{
249
		loadLanguage('SPortalAdmin');
250
251
		require_once(ADMINDIR . '/PortalAdminMain.controller.php');
252
		$admin_main = new ManagePortalConfig_Controller();
253
		$admin_main->action_information(false);
254
	}
255
256
	/**
257
	 * Reset a users custom portal block arrangement
258
	 */
259
	public function action_sportal_resetLayout()
260
	{
261
		global $scripturl;
262
263
		checkSession('request');
264
265
		// Remove the block layout settings
266
		require_once(SUBSDIR . '/Portal.subs.php');
267
		resetMemberLayout();
268
269
		// Redirect to the main page
270
		redirectexit($scripturl . '?action=portal');
271
	}
272
273
	/**
274
	 * Reorders the front page blocks in response to a D&D ajax request
275
	 */
276
	public function action_userblockorder()
277
	{
278
		global $context, $txt, $user_info, $settings, $modSettings;
279
280
		// Should not happen, but no guest processing
281
		if ($user_info['is_guest'] || $user_info['id'] == 0)
282
		{
283
			return;
284
		}
285
286
		// Start off with nothing
287
		$context['xml_data'] = array();
288
		$errors = array();
289
		$order = array();
290
291
		// Chances are
292
		loadLanguage('SPortal');
293
294
		// You have to be allowed to do this
295
		$validation_session = checkSession();
296
		if (empty($validation_session))
297
		{
298
			$block_tree = array();
299
300
			// No questions that we are rearranging the blocks
301
			if (isset($_POST['order'], $_POST['received'], $_POST['moved']))
302
			{
303
				$column_numbers = array(
304
					'sp_left_div' => 1,
305
					'sp_top_div' => 2,
306
					'sp_bottom_div' => 3,
307
					'sp_right_div' => 4,
308
					'sp_header' => 5,
309
					'sp_footer' => 6
310
				);
311
312
				// What block was drag and dropped? e.g. block_2,4
313
				list ($block_moved,) = explode(',', $_POST['moved']);
314
				$block_moved = (int) str_replace('block_', '', $block_moved);
315
316
				// Where is it going
317
				$target_column = $column_numbers[$_POST['received']];
318
319
				// The block ids arrive in 1-n view order ... block,column
320
				foreach ($_POST['block'] as $id)
321
				{
322
					list ($block, $column) = explode(',', $id);
323
324
					// Update the moved blocks column
325
					if ($block == $block_moved)
326
					{
327
						$column = $target_column;
328
					}
329
330
					$block_tree[$column][] = $block;
331
				}
332
			}
333
334
			// Update the option so its remembered
335
			require_once(SUBSDIR . '/Themes.subs.php');
336
			updateThemeOptions(array($settings['theme_id'], $user_info['id'], 'sp_block_layout', serialize($block_tree)));
337
338
			if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2)
339
			{
340
				cache_put_data('theme_settings-' . $settings['theme_id'] . ':' . $user_info['id'], null, 60);
0 ignored issues
show
Unused Code introduced by
The call to cache_put_data() has too many arguments starting with null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

340
				/** @scrutinizer ignore-call */ 
341
    cache_put_data('theme_settings-' . $settings['theme_id'] . ':' . $user_info['id'], null, 60);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
341
			}
342
343
			$order[] = array(
344
				'value' => $txt['sp-blocks_success_arrange'],
345
			);
346
		}
347
		// Failed validation, tough day for you
348
		else
349
		{
350
			$errors[] = array('value' => $txt['sp-blocks_fail_arrange']);
351
		}
352
353
		// Return the response
354
		$context['sub_template'] = 'generic_xml';
355
		$context['xml_data'] = array(
356
			'orders' => array(
357
				'identifier' => 'order',
358
				'children' => $order,
359
			),
360
			'errors' => array(
361
				'identifier' => 'error',
362
				'children' => $errors,
363
			),
364
		);
365
	}
366
}
367