Passed
Push — master ( eb00b7...bfc209 )
by Stanislav
03:42 queued 13s
created

gallery_logs_module::main()   F

Complexity

Conditions 14
Paths 360

Size

Total Lines 137
Code Lines 86

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 210

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 86
c 1
b 0
f 0
dl 0
loc 137
ccs 0
cts 115
cp 0
rs 3.052
cc 14
nc 360
nop 2
crap 210

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
* @package phpBB Gallery
5
* @version $Id$
6
* @copyright (c) 2025 Leinad4Mind https://leinad4mind.top/forum
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
8
*
9
*/
10
11
namespace phpbbgallery\core\acp;
12
13
/**
14
* @package acp
15
*/
16
class gallery_logs_module
17
{
18
	var $u_action;
19
20
	function main($id, $mode)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
21
	{
22
		global $auth, $template, $user, $request;
23
		global $phpbb_container;
24
25
		$this->language = $phpbb_container->get('language');
0 ignored issues
show
Bug Best Practice introduced by
The property language does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
26
		$this->language->add_lang(['info_acp_gallery_logs'], 'phpbbgallery/core');
27
28
		$this->tpl_name = 'gallery_logs';
0 ignored issues
show
Bug Best Practice introduced by
The property tpl_name does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
29
		add_form_key('acp_logs');
0 ignored issues
show
Bug introduced by
The function add_form_key was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
		/** @scrutinizer ignore-call */ 
30
  add_form_key('acp_logs');
Loading history...
30
31
		$page				= $request->variable('page', 0);
32
		$filter_log		= $request->variable('lf', 'all');
33
		$sort_days		= $request->variable('st', 0);
34
		$sort_key		= $request->variable('sk', 't');
35
		$sort_dir		= $request->variable('sd', 'd');
36
		$deletemark		= $request->is_set_post('delmarked');
37
		$marked			= $request->variable('mark', []);
38
39
		$log = $phpbb_container->get('phpbbgallery.core.log');
40
41
		$valid_filters   = ['all', 'admin', 'moderator', 'system'];
42
		$valid_sort_keys = ['u', 't', 'i', 'o'];
43
		$valid_sort_dirs = ['a', 'd'];
44
45
		// Sanitize inputs
46
		if (!in_array($filter_log, $valid_filters))
47
		{
48
			$filter_log = 'all';
49
		}
50
		if (!in_array($sort_key, $valid_sort_keys))
51
		{
52
			$sort_key = 't';
53
		}
54
		if (!in_array($sort_dir, $valid_sort_dirs))
55
		{
56
			$sort_dir = 'd';
57
		}
58
59
		// Delete entries if requested and able
60
		if ($deletemark && $auth->acl_get('a_clearlogs') && !empty($marked))
61
		{
62
			if (!check_form_key('acp_logs'))
0 ignored issues
show
Bug introduced by
The function check_form_key was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
			if (!/** @scrutinizer ignore-call */ check_form_key('acp_logs'))
Loading history...
63
			{
64
				trigger_error($this->language->lang('FORM_INVALID'));
65
			}
66
67
			if (confirm_box(true))
0 ignored issues
show
Bug introduced by
The function confirm_box was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
			if (/** @scrutinizer ignore-call */ confirm_box(true))
Loading history...
68
			{
69
				$log->delete_logs($marked);
70
			}
71
			else
72
			{
73
				confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields([
0 ignored issues
show
Bug introduced by
The function build_hidden_fields was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
				confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), /** @scrutinizer ignore-call */ build_hidden_fields([
Loading history...
74
					'page'		=> $page,
75
					'delmarked'	=> $deletemark,
76
					'mark'		=> $marked,
77
					'st'		=> $sort_days,
78
					'sk'		=> $sort_key,
79
					'sd'		=> $sort_dir,
80
					'i'			=> $id,
81
					'mode'		=> $mode,
82
					'action'		=> $this->u_action,
83
				]));
84
			}
85
		}
86
87
		switch ($mode)
88
		{
89
			case 'main':
90
				// Template vars based on filter
91
				$log_titles = [
92
					'all'      => ['ACP_GALLERY_LOGS', ''],
93
					'admin'    => ['ACP_LOG_GALLERY_ADM', 'ACP_LOG_GALLERY_ADM_EXP'],
94
					'moderator'=> ['ACP_LOG_GALLERY_MOD', 'ACP_LOG_GALLERY_MOD_EXP'],
95
					'system'   => ['ACP_LOG_GALLERY_SYSTEM', 'ACP_LOG_GALLERY_SYSTEM_EXP'],
96
				];
97
				$title = $log_titles[$filter_log][0];
98
99
				$template->assign_vars([
100
					'L_TITLE'         => $this->language->lang($log_titles[$filter_log][0]),
101
					'L_EXPLAIN'       => $log_titles[$filter_log][1] ? $this->language->lang($log_titles[$filter_log][1]) : '',
102
					'S_SELECT_OPTION' => $filter_log,
103
				]);
104
105
				// Sorting
106
				$limit_days = [
107
					0   => $this->language->lang('ALL_ENTRIES'),
108
					1   => $this->language->lang('1_DAY'),
109
					7   => $this->language->lang('7_DAYS'),
110
					14  => $this->language->lang('2_WEEKS'),
111
					30  => $this->language->lang('1_MONTH'),
112
					90  => $this->language->lang('3_MONTHS'),
113
					180 => $this->language->lang('6_MONTHS'),
114
					365 => $this->language->lang('1_YEAR'),
115
				];
116
				$sort_by_text = [
117
					'u' => $this->language->lang('SORT_USER_ID'),
118
					't' => $this->language->lang('SORT_DATE'),
119
					'i' => $this->language->lang('SORT_IP'),
120
					'o' => $this->language->lang('SORT_ACTION'),
121
				];
122
123
				$s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
124
				gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param);
0 ignored issues
show
Bug introduced by
The function gen_sort_selects was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

124
				/** @scrutinizer ignore-call */ 
125
    gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param);
Loading history...
125
126
				$template->assign_vars(array(
127
					'S_LIMIT_DAYS'	=> $s_limit_days,
128
					'S_SORT_KEY'	=> $s_sort_key,
129
					'S_SORT_DIR'	=> $s_sort_dir,
130
					'S_CLEARLOGS'	=> $auth->acl_get('a_clearlogs'),
131
					'U_ACTION'		=> $this->u_action . "&amp;$u_sort_param&amp;page=$page",
132
				));
133
				$this->page_title = $this->language->lang($title);
0 ignored issues
show
Bug Best Practice introduced by
The property page_title does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
134
135
				// Build additional filters
136
				$additional = [];
137
				if ($sort_days > 0)
138
				{
139
					$additional['sort_days'] = $sort_days;
140
				}
141
				if ($sort_key !== 't')
142
				{
143
					$additional['sort_key'] = $sort_key;
144
				}
145
				if ($sort_dir !== 'd')
146
				{
147
					$additional['sort_dir'] = $sort_dir;
148
				}
149
150
				// Build list
151
				$log->build_list($filter_log, 0, $page, -1, 0, $additional);
152
			break;
153
154
			default:
155
				trigger_error('NO_MODE', E_USER_ERROR);
156
			break;
157
		}
158
	}
159
}
160