Completed
Pull Request — master (#3325)
by Emanuele
11:19
created

Modlog_Controller::action_log()   F

Complexity

Conditions 37
Paths > 20000

Size

Total Lines 247
Code Lines 158

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 1406

Importance

Changes 0
Metric Value
cc 37
eloc 158
dl 0
loc 247
rs 0
c 0
b 0
f 0
nc 1474560
nop 0
ccs 0
cts 209
cp 0
crap 1406

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
 * The moderation log is this file's only job. It views it, and that's about all it does.
5
 *
6
 * @name      ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
9
 *
10
 * This file contains code covered by:
11
 * copyright:	2011 Simple Machines (http://www.simplemachines.org)
12
 * license:		BSD, See included LICENSE.TXT for terms and conditions.
13
 *
14
 * @version 1.1
15
 *
16
 */
17
18
/**
19
 * Admin and moderation log controller.
20
 * Depending on permissions, this class will display and allow to act on the log
21
 * for administrators or for moderators.
22
 */
23
class Modlog_Controller extends Action_Controller
24
{
25
	/**
26
	 * Default method for this controller.
27
	 *
28
	 * @see Action_Controller::action_index()
29
	 */
30
	public function action_index()
31
	{
32
		// We haz nothing to do. :P
33
		$this->action_log();
34
	}
35
36
	/**
37
	 * Prepares the information from the moderation log for viewing.
38
	 * Show the moderation log, or admin log...
39
	 * Disallows the deletion of events within twenty-four hours of now.
40
	 * Requires the admin_forum permission for admin log.
41
	 * Accessed via ?action=moderate;area=modlog.
42
	 *
43
	 * @uses Modlog template, main sub-template.
44
	 */
45
	public function action_log()
46
	{
47
		global $txt, $context, $scripturl;
48
49
		require_once(SUBSDIR . '/Modlog.subs.php');
50
51
		// Are we looking at the moderation log or the administration log.
52
		$context['log_type'] = isset($this->_req->query->sa) && $this->_req->query->sa === 'adminlog' ? 3 : 1;
53
54
		// Trying to view the admin log, lets check you can.
55
		if ($context['log_type'] == 3)
56
			isAllowedTo('admin_forum');
57
58
		// These change dependant on whether we are viewing the moderation or admin log.
59
		if ($context['log_type'] == 3 || $this->_req->query->action === 'admin')
60
			$context['url_start'] = '?action=admin;area=logs;sa=' . ($context['log_type'] == 3 ? 'adminlog' : 'modlog') . ';type=' . $context['log_type'];
61
		else
62
			$context['url_start'] = '?action=moderate;area=modlog;type=' . $context['log_type'];
63
64
		$context['can_delete'] = allowedTo('admin_forum');
65
66
		loadLanguage('Modlog');
67
68
		$context['page_title'] = $context['log_type'] == 3 ? $txt['modlog_admin_log'] : $txt['modlog_view'];
69
70
		// The number of entries to show per page of log file.
71
		$context['displaypage'] = 30;
72
73
		// Amount of hours that must pass before allowed to delete file.
74
		$context['hoursdisable'] = 24;
75
76
		// Handle deletion...
77
		if (isset($this->_req->post->removeall) && $context['can_delete'])
78
		{
79
			checkSession();
80
			validateToken('mod-ml');
81
			deleteLogAction($context['log_type'], $context['hoursdisable']);
82
		}
83
		elseif (!empty($this->_req->post->remove) && isset($this->_req->post->delete) && $context['can_delete'])
84
		{
85
			checkSession();
86
			validateToken('mod-ml');
87
			deleteLogAction($context['log_type'], $context['hoursdisable'], $this->_req->post->delete);
88
		}
89
90
		// If we're coming from a search, get the variables.
91
		if (!empty($this->_req->post->params) && empty($this->_req->post->is_search))
92
		{
93
			$search_params = base64_decode(strtr($this->_req->post->params, array(' ' => '+')));
94
			$search_params = @json_decode($search_params, true);
95
		}
96
97
		// This array houses all the valid quick search types.
98
		$searchTypes = array(
99
			'action' => array('sql' => 'lm.action', 'label' => $txt['modlog_action']),
100
			'member' => array('sql' => 'mem.real_name', 'label' => $txt['modlog_member']),
101
			'position' => array('sql' => 'mg.group_name', 'label' => $txt['modlog_position']),
102
			'ip' => array('sql' => 'lm.ip', 'label' => $txt['modlog_ip'])
103
		);
104
105
		// Setup the allowed search
106
		$context['order'] = isset($this->_req->query->sort) && isset($searchTypes[$this->_req->query->sort]) ? $this->_req->query->sort : 'member';
107
108
		if (!isset($search_params['string']) || (!empty($this->_req->post->search) && $search_params['string'] != $this->_req->post->search))
109
			$search_params_string = $this->_req->getPost('search', 'trim', '');
110
		else
111
			$search_params_string = $search_params['string'];
112
113
		if (isset($this->_req->post->search_type) || empty($search_params['type']) || !isset($searchTypes[$search_params['type']]))
114
			$search_params_type = isset($this->_req->post->search_type) && isset($searchTypes[$this->_req->post->search_type]) ? $this->_req->query->search_type : $context['order'];
115
		else
116
			$search_params_type = $search_params['type'];
117
118
		$search_params_column = $searchTypes[$search_params_type]['sql'];
119
		$search_params = array(
120
			'string' => $search_params_string,
121
			'type' => $search_params_type,
122
		);
123
124
		// Setup the search context.
125
		$context['search_params'] = empty($search_params['string']) ? '' : base64_encode(json_encode($search_params));
126
		$context['search'] = array(
127
			'string' => $search_params['string'],
128
			'type' => $search_params['type'],
129
			'label' => $searchTypes[$search_params_type]['label'],
130
		);
131
132
		// If they are searching by action, then we must do some manual intervention to search in their language!
133
		if ($search_params['type'] === 'action' && !empty($search_params['string']))
134
		{
135
			// Build a regex which looks for the words
136
			$regex = '';
137
			$search = explode(' ', $search_params['string']);
138
			foreach ($search as $word)
139
				$regex .= '(?=[\w\s]*' . $word . ')';
140
141
			// For the moment they can only search for ONE action!
142
			foreach ($txt as $key => $text)
143
			{
144
				if (strpos($key, 'modlog_ac_') === 0 && preg_match('~' . $regex . '~i', $text))
145
				{
146
					$search_params['string'] = substr($key, 10);
147
					break;
148
				}
149
			}
150
		}
151
152
		// This is all the information required for a moderation/admin log listing.
153
		$listOptions = array(
154
			'id' => 'moderation_log_list',
155
			'width' => '100%',
156
			'items_per_page' => $context['displaypage'],
157
			'no_items_label' => $txt['modlog_' . ($context['log_type'] == 3 ? 'admin_log_' : '') . 'no_entries_found'],
158
			'base_href' => $scripturl . $context['url_start'],
159
			'default_sort_col' => 'time',
160
			'get_items' => array(
161
				'function' => array($this, 'getModLogEntries'),
162
				'params' => array(
163
					(!empty($search_params['string']) ? ' INSTR({raw:sql_type}, {string:search_string})' : ''),
164
					array('sql_type' => $search_params_column, 'search_string' => $search_params['string']),
165
					$context['log_type'],
166
				),
167
			),
168
			'get_count' => array(
169
				'function' => array($this, 'getModLogEntryCount'),
170
				'params' => array(
171
					(!empty($search_params['string']) ? ' INSTR({raw:sql_type}, {string:search_string})' : ''),
172
					array('sql_type' => $search_params_column, 'search_string' => $search_params['string']),
173
					$context['log_type'],
174
				),
175
			),
176
			'columns' => array(
177
				'action' => array(
178
					'header' => array(
179
						'value' => $txt['modlog_action'],
180
						'class' => 'lefttext',
181
					),
182
					'data' => array(
183
						'db' => 'action_text',
184
						'class' => 'smalltext',
185
					),
186
					'sort' => array(
187
						'default' => 'lm.action',
188
						'reverse' => 'lm.action DESC',
189
					),
190
				),
191
				'time' => array(
192
					'header' => array(
193
						'value' => $txt['modlog_date'],
194
						'class' => 'lefttext',
195
					),
196
					'data' => array(
197
						'db' => 'time',
198
						'class' => 'smalltext',
199
					),
200
					'sort' => array(
201
						'default' => 'lm.log_time DESC',
202
						'reverse' => 'lm.log_time',
203
					),
204
				),
205
				'moderator' => array(
206
					'header' => array(
207
						'value' => $txt['modlog_member'],
208
						'class' => 'lefttext',
209
					),
210
					'data' => array(
211
						'db' => 'moderator_link',
212
						'class' => 'smalltext',
213
					),
214
					'sort' => array(
215
						'default' => 'mem.real_name',
216
						'reverse' => 'mem.real_name DESC',
217
					),
218
				),
219
				'position' => array(
220
					'header' => array(
221
						'value' => $txt['modlog_position'],
222
						'class' => 'lefttext',
223
					),
224
					'data' => array(
225
						'db' => 'position',
226
						'class' => 'smalltext',
227
					),
228
					'sort' => array(
229
						'default' => 'mg.group_name',
230
						'reverse' => 'mg.group_name DESC',
231
					),
232
				),
233
				'ip' => array(
234
					'header' => array(
235
						'value' => $txt['modlog_ip'],
236
						'class' => 'lefttext',
237
					),
238
					'data' => array(
239
						'db' => 'ip',
240
						'class' => 'smalltext',
241
					),
242
					'sort' => array(
243
						'default' => 'lm.ip',
244
						'reverse' => 'lm.ip DESC',
245
					),
246
				),
247
				'delete' => array(
248
					'header' => array(
249
						'value' => '<input type="checkbox" name="all" class="input_check" onclick="invertAll(this, this.form);" />',
250
						'class' => 'centertext',
251
					),
252
					'data' => array(
253
						'function' => function ($entry) {
254
							return '<input type="checkbox" name="delete[]" value="' . $entry['id'] . '"' . ($entry['editable'] ? '' : ' disabled="disabled"') . ' />';
255
						},
256
						'class' => 'centertext',
257
					),
258
				),
259
			),
260
			'form' => array(
261
				'href' => $scripturl . $context['url_start'],
262
				'include_sort' => true,
263
				'include_start' => true,
264
				'hidden_fields' => array(
265
					$context['session_var'] => $context['session_id'],
266
					'params' => $context['search_params']
267
				),
268
				'token' => 'mod-ml',
269
			),
270
			'additional_rows' => array(
271
				array(
272
					'class' => 'submitbutton',
273
					'position' => 'below_table_data',
274
					'value' => '
275
						' . $txt['modlog_search'] . ' (' . $txt['modlog_by'] . ': ' . $context['search']['label'] . ')
276
						<input type="text" name="search" size="18" value="' . Util::htmlspecialchars($context['search']['string']) . '" class="input_text" />
277
						<input type="submit" name="is_search" value="' . $txt['modlog_go'] . '" />
278
						' . ($context['can_delete'] ? '|&nbsp;
279
						<input type="submit" name="remove" value="' . $txt['modlog_remove'] . '" onclick="return confirm(\'' . $txt['modlog_remove_selected_confirm'] . '\');" />
280
						<input type="submit" name="removeall" value="' . $txt['modlog_removeall'] . '" onclick="return confirm(\'' . $txt['modlog_remove_all_confirm'] . '\');"/>' : ''),
281
				),
282
			),
283
		);
284
285
		createToken('mod-ml');
286
287
		// Create the log listing
288
		createList($listOptions);
289
290
		$context['sub_template'] = 'show_list';
291
		$context['default_list'] = 'moderation_log_list';
292
	}
293
294
	/**
295
	 * Callback for createList()
296
	 * Returns a list of moderation log entries
297
	 * Uses list_getModLogEntries in modlog subs
298
	 *
299
	 * @param int $start The item to start with (for pagination purposes)
300
	 * @param int $items_per_page  The number of items to show per page
301
	 * @param string $sort A string indicating how to sort the results
302
	 * @param string $query_string
303
	 * @param mixed[] $query_params
304
	 * @param int $log_type
305
	 */
306
	public function getModLogEntries($start, $items_per_page, $sort, $query_string, $query_params, $log_type)
307
	{
308
		// Get all entries of $log_type
309
		return list_getModLogEntries($start, $items_per_page, $sort, $query_string, $query_params, $log_type);
310
	}
311
312
	/**
313
	 * Callback for createList()
314
	 * Returns a count of moderation/admin log entries
315
	 * Uses list_getModLogEntryCount in modlog subs
316
	 *
317
	 * @param string $query_string
318
	 * @param mixed[] $query_params
319
	 * @param int $log_type
320
	 */
321
	public function getModLogEntryCount($query_string, $query_params, $log_type)
322
	{
323
		// Get the count of our solved topic entries
324
		return list_getModLogEntryCount($query_string, $query_params, $log_type);
325
	}
326
}