Passed
Push — development ( 924c99...c29c1d )
by Spuds
01:04 queued 20s
created

AdminLog::action_pruningSettings_display()   B

Complexity

Conditions 10
Paths 6

Size

Total Lines 73
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 73
rs 7.6666
c 0
b 0
f 0
cc 10
nc 6
nop 0
ccs 0
cts 31
cp 0
crap 110

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
 * This manages the admin logs, and forwards to display, pruning,
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\SettingsForm\SettingsForm;
19
use ElkArte\Languages\Txt;
20
21
/**
22
 * Admin logs controller.
23
 *
24
 * What it does:
25
 *
26
 * - This class manages logs, and forwards to display, pruning, and other actions on logs.
27
 *
28
 */
29
class AdminLog extends AbstractController
30
{
31
	/**
32
	 * This method decides which log to load.
33
	 * Accessed by ?action=admin;area=logs
34
	 *
35
	 * @event integrate_sa_manage_logs used to add additional log viewing functions, passed subActions array
36
	 * @uses _initPruningSettingsForm
37
	 */
38
	public function action_index()
39
	{
40
		global $context, $txt, $modSettings;
41
42
		// These are the logs they can load.
43
		$subActions = [
44
			'errorlog' => [
45
				'function' => 'action_index',
46
				'controller' => ManageErrors::class],
47
				'disabled' => empty($modSettings['enableErrorLogging']),
48
			'adminlog' => [
49
				'function' => 'action_log',
50
				'controller' => Modlog::class],
51
			'modlog' => [
52
				'function' => 'action_log',
53
				'controller' => Modlog::class,
54
				'disabled' => !featureEnabled('ml') || empty($modSettings['modlog_enabled'])],
55
			'banlog' => [
56
				'function' => 'action_log',
57
				'controller' => ManageBans::class],
58
			'spiderlog' => [
59
				'function' => 'action_logs',
60
				'controller' => ManageSearchEngines::class],
61
			'tasklog' => [
62
				'function' => 'action_log',
63
				'controller' => ManageScheduledTasks::class],
64
			'pruning' => [
65
				'controller' => $this,
66
				'function' => 'action_pruningSettings_display'],
67
		];
68
69
		// Setup the custom tabs.
70
		$context[$context['admin_menu_name']]['object']->prepareTabData([
71
				'title' => 'logs',
72
				'description' => 'maintain_info',
73
				'tabs' => [
74
					'errorlog' => [
75
						'url' => getUrl('admin', ['action' => 'admin', 'area' => 'logs', 'sa' => 'errorlog', 'desc']),
76
						'description' => sprintf($txt['errlog_desc'], $txt['remove']),
77
						'disabled' => empty($modSettings['enableErrorLogging']),
78
					],
79
					'adminlog' => [
80
						'description' => $txt['admin_log_desc'],
81
					],
82
					'modlog' => [
83
						'description' => $txt['moderation_log_desc'],
84
						'disabled' => !featureEnabled('ml') || empty($modSettings['modlog_enabled']),
85
					],
86
					'banlog' => [
87
						'description' => $txt['ban_log_description'],
88
					],
89
					'spiderlog' => [
90
						'description' => $txt['spider_log_desc'],
91
					],
92
					'tasklog' => [
93
						'description' => $txt['scheduled_log_desc'],
94
					],
95
					'pruning' => [
96
						'description' => $txt['pruning_log_desc'],
97
					],
98
				]]
99
		);
100
101
		// If there is no sa set it must have come here for first time,
102
		// redirect to the error log with reverse order by default instead of
103
		// mutating the request object.
104
		if (!$this->_req->hasQuery('sa') && !$this->_req->hasQuery('desc'))
105
		{
106
			redirectexit('action=admin;area=logs;sa=errorlog;desc');
107
		}
108
109
		// Set up the action control
110
		$action = new Action('manage_logs');
111
112
		// By default do the basic settings, call integrate_sa_manage_logs
113
		$subAction = $action->initialize($subActions, 'errorlog');
114
115
		// Call the right function for this sub-action.
116
		$action->dispatch($subAction);
117
	}
118
119
	/**
120
	 * Allow to edit the settings on the pruning screen.
121
	 *
122
	 * @event integrate_prune_settings add additional settings to the auto pruning display.  If you add any
123
	 * additional logs make sure to add them at the end.  Additionally, make sure you add them to the
124
	 * weekly scheduled task.
125
	 * @uses _pruningSettings form.
126
	 */
127
	public function action_pruningSettings_display(): void
128
	{
129
		global $txt, $context, $modSettings;
130
131
		// Make sure we understand what's going on.
132
		Txt::load('ManageSettings');
133
134
		$context['page_title'] = $txt['log_settings'];
135
136
		$settingsForm = new SettingsForm(SettingsForm::DB_ADAPTER);
137
138
		// Initialize settings
139
		$config_vars = $this->_settings();
140
		$settingsForm->setConfigVars($config_vars);
141
142
		call_integration_hook('integrate_prune_settings');
143
144
		// Saving?
145
		if ($this->_req->hasQuery('save'))
146
		{
147
			checkSession();
148
149
			$savevar = [
150
				['check', 'enableErrorLogging'],
151
				['check', 'enableErrorQueryLogging'],
152
				['check', 'modlog_enabled'],
153
				['check', 'userlog_enabled'],
154
				['text', 'pruningOptions']
155
			];
156
157
			// If pruning is enabled, compile all pruneXYZlog options into a CSV string, yes
158
			// this is really this badly thought out.
159
			if (!empty($this->_req->post->pruningOptions))
160
			{
161
				$vals = [];
162
				foreach ($config_vars as $index => $config_value)
163
				{
164
					if (!is_array($config_value) || $index === 'pruningOptions' || !str_starts_with($config_value[1], 'prune'))
165
					{
166
						continue;
167
					}
168
169
					$vals[] = empty($this->_req->post->{$config_value[1]}) || $this->_req->post->{$config_value[1]} < 0 ? 0 : $this->_req->getPost($config_value[1], 'intval');
170
				}
171
172
				$_POST['pruningOptions'] = implode(',', $vals);
173
			}
174
			else
175
			{
176
				$_POST['pruningOptions'] = '';
177
			}
178
179
			$settingsForm->setConfigVars($savevar);
180
			$settingsForm->setConfigValues($_POST);
181
			$settingsForm->save();
182
			redirectexit('action=admin;area=logs;sa=pruning');
183
		}
184
185
		$context['post_url'] = getUrl('admin', ['action' => 'admin', 'area' => 'logs', 'sa' => 'pruning', 'save']);
186
		$context['settings_title'] = $txt['log_settings'];
187
		$context['sub_template'] = 'show_settings';
188
189
		// Get the actual values
190
		if (!empty($modSettings['pruningOptions']))
191
		{
192
			[$modSettings['pruneErrorLog'], $modSettings['pruneModLog'], $modSettings['pruneBanLog'], $modSettings['pruneReportLog'], $modSettings['pruneScheduledTaskLog'], $modSettings['pruneSpiderHitLog']] = array_pad(explode(',', $modSettings['pruningOptions']), 7, 0);
193
		}
194
		else
195
		{
196
			$modSettings['pruneErrorLog'] = $modSettings['pruneModLog'] = $modSettings['pruneBanLog'] = $modSettings['pruneReportLog'] = $modSettings['pruneScheduledTaskLog'] = $modSettings['pruneSpiderHitLog'] = 0;
197
		}
198
199
		$settingsForm->prepare();
200
	}
201
202
	/**
203 2
	 * Returns the configuration settings for pruning logs.
204
	 */
205 2
	private function _settings(): array
206
	{
207
		global $txt;
208
209
		return [
210 2
			// See all the mistakes the developers make
211 2
			['check', 'enableErrorLogging'],
212
			['check', 'enableErrorQueryLogging'],
213 2
			// Moderation logging is a Core feature, it enables Admin, Moderation and Profile Edit logging.  This
214 2
			// allows some fine-tuning of that features, e.g. only allow admin logging
215 2
			featureEnabled('ml') ? ['check', 'modlog_enabled'] : '',
216 2
			featureEnabled('ml') ? ['check', 'userlog_enabled'] : '',
217 2
			// Even do the pruning?
218 2
			['title', 'pruning_title', 'force_div_id' => 'pruning_title'],
219 2
			// The array indexes are here so, we can remove/change them before saving.
220
			'pruningOptions' => ['check', 'pruningOptions'],
221
			'',
222
			// Various logs that could be pruned.
223
			['int', 'pruneErrorLog', 'postinput' => $txt['days_word'], 'subtext' => $txt['zero_to_disable']], // Error log.
224
			['int', 'pruneModLog', 'postinput' => $txt['days_word'], 'subtext' => $txt['zero_to_disable']], // Moderation log.
225
			['int', 'pruneBanLog', 'postinput' => $txt['days_word'], 'subtext' => $txt['zero_to_disable']], // Ban hit log.
226
			['int', 'pruneReportLog', 'postinput' => $txt['days_word'], 'subtext' => $txt['zero_to_disable']], // Report to moderator log.
227 2
			['int', 'pruneScheduledTaskLog', 'postinput' => $txt['days_word'], 'subtext' => $txt['zero_to_disable']], // Log of the scheduled tasks and how long they ran.
228
			['int', 'pruneSpiderHitLog', 'postinput' => $txt['days_word'], 'subtext' => $txt['zero_to_disable']], // Log of the scheduled tasks and how long they ran.
229 2
			// If you add any additional logs make sure to add them after this point.  Additionally, make sure you
230
			// add them to the weekly scheduled task.
231
		];
232
	}
233
234
	/**
235
	 * Return the search engine settings for use in admin search
236
	 */
237
	public function settings_search(): array
238
	{
239
		return $this->_settings();
240
	}
241
}
242