Completed
Branch development (ad897a)
by Elk
06:53 queued 29s
created

ManageEditor::action_editorSettings_display()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 53
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 23
dl 0
loc 53
rs 9.2408
c 0
b 0
f 0
nc 6
nop 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
 * Handles administration options for BBC tags.
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
 * @version 2.0 dev
11
 *
12
 */
13
14
namespace ElkArte\AdminController;
15
16
use BBC\ParserWrapper;
17
use ElkArte\AbstractController;
18
use ElkArte\Action;
19
use ElkArte\SettingsForm\SettingsForm;
20
21
/**
22
 * ManageEditor controller handles administration options for BBC tags.
23
 *
24
 * @package Editor
25
 */
26
class ManageEditor extends AbstractController
27
{
28
	/**
29
	 * The Editor admin area
30
	 *
31
	 * What it does:
32
	 *
33
	 * - This method is the entry point for index.php?action=admin;area=postsettings;sa=editor
34
	 * and it calls a function based on the sub-action, here only display.
35
	 * - requires admin_forum permissions
36
	 *
37
	 * @event integrate_sa_manage_editor Used to add more sub actions
38
	 * @see \ElkArte\AbstractController::action_index()
39
	 */
40
	public function action_index()
41
	{
42
		global $context, $txt;
43
44
		$subActions = array(
45
			'display' => array(
46
				'controller' => $this,
47
				'function' => 'action_editorSettings_display',
48
				'permission' => 'admin_forum')
49
		);
50
51
		// Set up
52
		$action = new Action('manage_editor');
53
54
		// Only one option I'm afraid, but integrate_sa_manage_editor can add more
55
		$subAction = $action->initialize($subActions, 'display');
56
		$context['sub_action'] = $subAction;
57
		$context['page_title'] = $txt['manageposts_editor_settings_title'];
58
59
		// Make the call
60
		$action->dispatch($subAction);
61
	}
62
63
	/**
64
	 * Administration page in Posts and Topics > Editor.
65
	 *
66
	 * - This method handles displaying and changing which BBC tags are enabled on the forum.
67
	 *
68
	 * @event integrate_save_bbc_settings called during the save action
69
	 * @uses Admin template, edit_editor_settings sub-template.
70
	 */
71
	public function action_editorSettings_display()
72
	{
73
		global $context, $txt, $modSettings;
74
75
		// Initialize the form
76
		$settingsForm = new SettingsForm(SettingsForm::DB_ADAPTER);
77
78
		// Initialize it with our settings
79
		$settingsForm->setConfigVars($this->_settings());
80
81
		// Make sure a nifty javascript will enable/disable checkboxes, according to BBC globally set or not.
82
		theme()->addInlineJavascript('
83
			toggleBBCDisabled(\'disabledBBC\', ' . (empty($modSettings['enableBBC']) ? 'true' : 'false') . ');', true);
84
85
		// Make sure we check the right tags!
86
		$modSettings['bbc_disabled_disabledBBC'] = empty($modSettings['disabledBBC']) ? array() : explode(',', $modSettings['disabledBBC']);
87
88
		// Save page
89
		if (isset($this->_req->query->save))
90
		{
91
			checkSession();
92
93
			// Security: make a pass through all tags and fix them as necessary
94
			$codes = ParserWrapper::instance()->getCodes();
95
			$bbcTags = $codes->getTags();
96
97
			$disabledBBC_enabledTags = $this->_req->getPost('disabledBBC_enabledTags', null, []);
98
			if (!is_array($disabledBBC_enabledTags))
99
			{
100
				$disabledBBC_enabledTags = array($disabledBBC_enabledTags);
101
			}
102
103
			// Work out what is actually disabled!
104
			$this->_req->post->disabledBBC = implode(',', array_diff($bbcTags, $disabledBBC_enabledTags));
105
106
			// Notify addons and integrations
107
			call_integration_hook('integrate_save_bbc_settings', array($bbcTags));
108
109
			// Save the result
110
			$settingsForm->setConfigValues((array) $this->_req->post);
111
			$settingsForm->save();
112
113
			// And we're out of here!
114
			redirectexit('action=admin;area=editor');
115
		}
116
117
		// Make sure the template stuff is ready now...
118
		$context['sub_template'] = 'show_settings';
119
		$context['page_title'] = $txt['manageposts_editor_settings_title'];
120
		$context['post_url'] = getUrl('admin', ['action' => 'admin', 'area' => 'editor;save']);
121
		$context['settings_title'] = $txt['manageposts_editor_settings_title'];
122
123
		$settingsForm->prepare();
124
	}
125
126
	/**
127
	 * Return the editor settings of the forum.
128
	 *
129
	 * @event integrate_modify_editor_settings used to add more options to config vars,
130
	 * formerly known as integrate_modify_bbc_settings
131
	 */
132
	private function _settings()
133
	{
134
		$config_vars = array(
135
			array('check', 'enableBBC'),
136
			array('check', 'enableBBC', 0, 'onchange' => 'toggleBBCDisabled(\'disabledBBC\', !this.checked);'),
137
			array('bbc', 'disabledBBC'),
138
139
			array('title', 'editorSettings'),
140
			array('check', 'enableUndoRedo'),
141
			array('check', 'enableSplitTag'),
142
143
			array('title', 'mods_cat_modifications_misc'),
144
			array('check', 'autoLinkUrls'), // @todo not editor or bbc
145
			array('check', 'enablePostHTML'),
146
		);
147
148
		// Add new settings with a nice hook, makes them available for admin settings search as well
149
		call_integration_hook('integrate_modify_editor_settings', array(&$config_vars));
150
		return $config_vars;
151
	}
152
153
	/**
154
	 * Return the form settings for use in admin search
155
	 */
156
	public function settings_search()
157
	{
158
		return $this->_settings();
159
	}
160
}
161