Passed
Push — development ( 91c8ac...8f3e46 )
by Spuds
01:07 queued 27s
created

MessageIndex::prepare_context()   B

Complexity

Conditions 10
Paths 84

Size

Total Lines 53
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 28
nc 84
nop 3
dl 0
loc 53
rs 7.6666
c 1
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
 * Integration system for drafts into Post controller
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\Modules\Drafts;
18
19
use ElkArte\EventManager;
20
use ElkArte\Languages\Txt;
21
use ElkArte\Modules\AbstractModule;
22
23
/**
24
 * Class \ElkArte\Modules\Drafts\MessageIndex
25
 *
26
 * Enables draft functions for the MessageIndex controller page (quick topic)
27
 */
28
class MessageIndex extends AbstractModule
29
{
30
	/** @var bool Autosave switch  */
31
	protected static $_autosave_enabled = false;
32
33
	/** @var int Autosave frequency, default to 30 seconds */
34
	protected static $_autosave_frequency = 30000;
35
36
	/**
37
	 * {@inheritDoc}
38
	 */
39
	public static function hooks(EventManager $eventsManager)
40
	{
41
		global $modSettings;
42
43
		if (!empty($modSettings['drafts_enabled']) && !empty($modSettings['drafts_post_enabled']))
44
		{
45
			self::$_autosave_enabled = !empty($modSettings['drafts_autosave_enabled']);
46
47
			if (!empty($modSettings['drafts_autosave_frequency']))
48
			{
49
				self::$_autosave_frequency = (int) $modSettings['drafts_autosave_frequency'] * 1000;
50
			}
51
52
			return [
53
				['prepare_context', [MessageIndex::class, 'prepare_context'], ['use_quick_reply', 'editorOptions', 'board']],
54
			];
55
		}
56
57
		return [];
58
	}
59
60
	/**
61
	 * Prepares context for draft buttons
62
	 *
63
	 * What it does:
64
	 *
65
	 * - Sets/checks the ability to save and autosave drafts for JS and button display
66
	 * - Builds the list of drafts available to load
67
	 * - Loads necessary Draft javascript functions
68
	 *
69
	 * @param bool $use_quick_reply
70
	 * @param array $editorOptions
71
	 * @param int $board
72
	 */
73
	public function prepare_context($use_quick_reply, &$editorOptions, $board)
74
	{
75
		global $context, $options, $txt;
76
77
		// Check if the draft functions are enabled and that they have permission to use them (for quick topic.)
78
		$context['drafts_save'] = $use_quick_reply && allowedTo('post_draft') && $context['can_post_new'];
79
		$context['drafts_autosave'] = $context['drafts_save'] && self::$_autosave_enabled && allowedTo('post_autosave_draft') && !empty($options['drafts_autosave_enabled']);
80
81
		// Enable the drafts functions for the QR area
82
		if (!empty($context['drafts_save']))
83
		{
84
			Txt::load('Drafts');
85
86
			if ($context['drafts_autosave'])
87
			{
88
				Txt::load('Post');
89
90
				$editorOptions['plugin_addons'] = $editorOptions['plugin_addons'] ?? [];
91
				$editorOptions['plugin_options'] = $editorOptions['plugin_options'] ?? [];
92
93
				// @todo remove
94
				$context['drafts_autosave_frequency'] = self::$_autosave_frequency;
95
96
				$editorOptions['plugin_addons'][] = 'draft';
97
				$editorOptions['plugin_options'][] = '
98
					draftOptions: {
99
						sLastNote: \'draft_lastautosave\',
100
						sSceditorID: \'' . $editorOptions['id'] . '\',
101
						sType: \'post\',
102
						iBoard: ' . $board . ',
103
						iFreq: ' . self::$_autosave_frequency . ',
104
						sLastID: \'id_draft\',
105
						sTextareaID: \'' . $editorOptions['id'] . '\',
106
						id_draft: ' . (empty($context['id_draft']) ? 0 : $context['id_draft']) . '
107
					}';
108
109
				loadJavascriptFile('editor/drafts.plugin.js', ['defer' => true]);
110
			}
111
112
			// Hide this for quick topic
113
			$context['shortcuts_text'] = '';
114
115
			$editorOptions['buttons'] = $editorOptions['buttons'] ?? [];
116
			array_unshift($editorOptions['buttons'], [
117
				'name' => 'save_draft',
118
				'value' => $txt['draft_save'],
119
				'options' => 'onclick="return confirm(' . JavaScriptEscape($txt['draft_save_note']) . ') && submitThisOnce(this);" accesskey="d"',
120
			]);
121
122
			$editorOptions['hidden_fields'] = $editorOptions['hidden_fields'] ?? [];
123
			$editorOptions['hidden_fields'][] = [
124
				'name' => 'id_draft',
125
				'value' => empty($context['id_draft']) ? 0 : $context['id_draft'],
126
			];
127
		}
128
	}
129
}
130