Passed
Push — development ( 7fab02...a69570 )
by Spuds
01:06 queued 21s
created

ManageWarnings::action_index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 30
rs 9.7998
1
<?php
2
3
/**
4
 * Handles the warning moderation settings in the admin panel.
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 Beta 1
11
 *
12
 */
13
14
namespace ElkArte\AdminController;
15
16
use ElkArte\AbstractController;
17
use ElkArte\Action;
18
use ElkArte\Languages\Txt;
19
use ElkArte\SettingsForm\SettingsForm;
20
21
/**
22
 * ManageWarnings controller handles the moderation warning settings
23
 * pages in admin panel.
24
 *
25
 * @package Warnings
26
 */
27
class ManageWarnings extends AbstractController
28
{
29
	/**
30
	 * This function passes control through to the relevant moderation warning tab.
31
	 *
32
	 * @event integrate_sa_manage_warnings
33
	 * @see AbstractController::action_index()
34
	 */
35
	public function action_index()
36
	{
37
		global $context, $txt;
38
39
		Txt::load('Help+ManageSettings');
40
41
		$subActions = [
42
			'moderation' => [$this, 'action_moderationSettings_display', 'enabled' => featureEnabled('w'), 'permission' => 'admin_forum'],
43
		];
44
45
		// Action control
46
		$action = new Action('manage_warnings');
47
48
		// By default, do the basic settings, call integrate_sa_manage_warnings
49
		$subAction = $action->initialize($subActions, 'moderation');
50
51
		// Last pieces of the puzzle
52
		$context['sub_action'] = $subAction;
53
		$context['page_title'] = $txt['admin_security_moderation'];
54
		$context['sub_template'] = 'show_settings';
55
56
		// Load up all the tabs...
57
		$context[$context['admin_menu_name']]['object']->prepareTabData([
58
			'title' => 'moderation_warning_short',
59
			'help' => 'securitysettings',
60
			'description' => 'warning_enable',
61
		]);
62
63
		// Call the right function for this sub-action.
64
		$action->dispatch($subAction);
65
	}
66
67
	/**
68
	 * Allows displaying and eventually change the moderation settings of the forum.
69
	 *
70
	 * - Uses the moderation settings form.
71
	 *
72
	 * @event integrate_save_moderation_settings
73
	 */
74
	public function action_moderationSettings_display(): void
75
	{
76
		global $txt, $context, $modSettings;
77
78
		// Initialize the form
79
		$settingsForm = new SettingsForm(SettingsForm::DB_ADAPTER);
80
81
		// Initialize it with our settings
82
		$config_vars = $this->_warningSettings();
83
		$settingsForm->setConfigVars($config_vars);
84
85
		// Saving?
86
		if ($this->_req->hasQuery('save'))
87
		{
88
			checkSession();
89
90
			// Make sure these don't have an effect.
91
			if ($modSettings['warning_settings'][0] != 1)
92
			{
93
				$this->_req->post->warning_watch = 0;
94
				$this->_req->post->warning_moderate = 0;
95
				$this->_req->post->warning_mute = 0;
96
			}
97
			else
98
			{
99
				$this->_req->post->warning_watch = min($this->_req->post->warning_watch, 100);
100
				$this->_req->post->warning_moderate = $modSettings['postmod_active'] ? min($this->_req->post->warning_moderate, 100) : 0;
101
				$this->_req->post->warning_mute = min($this->_req->post->warning_mute, 100);
102
			}
103
104
			// Fix the warning setting array!
105
			$this->_req->post->warning_settings = '1,' . min(100, (int) $this->_req->post->user_limit) . ',' . min(100, (int) $this->_req->post->warning_decrement);
106
			$config_vars[] = ['text', 'warning_settings'];
107
			unset($config_vars['rem1'], $config_vars['rem2']);
108
109
			call_integration_hook('integrate_save_moderation_settings');
110
111
			$settingsForm->setConfigVars($config_vars);
112
			$settingsForm->setConfigValues((array) $this->_req->post);
113
			$settingsForm->save();
114
			redirectexit('action=admin;area=warnings;sa=moderation');
115
		}
116
117
		// We actually store lots of these together - for efficiency.
118
		[$modSettings['warning_enable'], $modSettings['user_limit'], $modSettings['warning_decrement']] = explode(',', $modSettings['warning_settings']);
119
120
		$context['post_url'] = getUrl('admin', ['action' => 'admin', 'area' => 'warnings', 'save']);
121
122
		$settingsForm->prepare();
123
	}
124
125
	/**
126
	 * Moderation settings.
127
	 *
128
	 * @event integrate_modify_moderation_settings add new moderation settings
129
	 */
130
	private function _warningSettings()
131
	{
132
		global $txt;
133
134
		$config_vars = [
135
			// Warning system
136
			['int', 'warning_watch', 'subtext' => $txt['setting_warning_watch_note'], 'help' => 'watch_enable'],
137
			'moderate' => ['int', 'warning_moderate', 'subtext' => $txt['setting_warning_moderate_note'], 'help' => 'moderate_enable'],
138
			['int', 'warning_mute', 'subtext' => $txt['setting_warning_mute_note'], 'help' => 'mute_enable'],
139
			'rem1' => ['int', 'user_limit', 'subtext' => $txt['setting_user_limit_note'], 'help' => 'perday_limit'],
140
			'rem2' => ['int', 'warning_decrement', 'subtext' => $txt['setting_warning_decrement_note']],
141
			['select', 'warning_show', 'subtext' => $txt['setting_warning_show_note'], [$txt['setting_warning_show_mods'], $txt['setting_warning_show_user'], $txt['setting_warning_show_all']]],
142
		];
143
144
		call_integration_hook('integrate_modify_moderation_settings', [&$config_vars]);
145
146
		return $config_vars;
147
	}
148
149
	/**
150
	 * Public method to return moderation settings, used for admin search
151
	 */
152
	public function warningSettings_search()
153
	{
154
		global $modSettings;
155
156
		if (!featureEnabled('w'))
157
		{
158
			return ['check', 'dummy_enable'];
159
		}
160
161
		return $this->_warningSettings();
162
	}
163
}
164