Passed
Push — master ( c33757...f74ec1 )
by Dark❶
05:05
created

acp_main::display_notification_methods()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 9
rs 9.6111
cc 5
nc 2
nop 2
1
<?php
2
/**
3
 *
4
 * User Notification Control [UNC]. An extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2020-2021, Dark❶, https://dark1.tech
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace dark1\usernotificationcontrol\controller;
12
13
/**
14
 * @ignore
15
 */
16
use phpbb\language\language;
17
use phpbb\log\log;
18
use phpbb\request\request;
19
use phpbb\template\template;
20
use phpbb\user;
21
use phpbb\config\config;
22
use dark1\usernotificationcontrol\core\unc_table;
23
use dark1\usernotificationcontrol\core\unc_helper;
24
25
/**
26
 * User Notification Control [UNC] ACP controller Main.
27
 */
28
class acp_main extends acp_base
29
{
30
	/** @var config */
31
	protected $config;
32
33
	/** @var unc_table */
34
	protected $unc_table;
35
36
	/** @var unc_helper */
37
	protected $unc_helper;
38
39
	/** @var array */
40
	protected $notification_methods;
41
42
	/** @var array */
43
	protected $notification_types_groups;
44
45
	/** @var array */
46
	protected $notify_matrix;
47
48
	/**
49
	 * Constructor.
50
	 *
51
	 * @param language		$language				Language object
52
	 * @param log			$log					Log object
53
	 * @param request		$request				Request object
54
	 * @param template		$template				Template object
55
	 * @param user			$user					User object
56
	 * @param config		config					Config object
57
	 * @param unc_table		$unc_table				UNC Table object
58
	 * @param unc_helper	$unc_helper				UNC Helper object
59
	 */
60
	public function __construct(language $language, log $log, request $request, template $template, user $user, config $config, unc_table $unc_table, unc_helper $unc_helper)
61
	{
62
		parent::__construct($language, $log, $request, $template, $user);
63
64
		$this->config			= $config;
65
		$this->unc_table		= $unc_table;
66
		$this->unc_helper		= $unc_helper;
67
		$this->notify_matrix	= [];
68
69
		// Get phpBB Notification
70
		$this->notification_methods = $this->unc_helper->get_subscription_methods();
71
		$this->notification_types_groups = $this->unc_helper->get_subscription_types();
72
	}
73
74
	/**
75
	 * Display the options a user can configure for Main Mode.
76
	 *
77
	 * @return void
78
	 * @access public
79
	 */
80
	public function handle()
81
	{
82
		// Is the form being submitted to us?
83
		if ($this->request->is_set_post('submit'))
84
		{
85
			$this->check_form_on_submit();
86
87
			// Set the options the user configured
88
			$dark1_unc_enable = $this->request->variable('dark1_unc_enable', 0);
89
			$this->config->set('dark1_unc_enable', $dark1_unc_enable);
90
91
			// Get No Notify Matrix
92
			$this->request_notify_method_type_matrix();
93
94
			// Set No Notify Matrix
95
			$this->unc_table->set_notify_method_type_matrix($this->notify_matrix);
96
97
			// Reflect in other tables if enabled
98
			if ($dark1_unc_enable)
99
			{
100
				$this->unc_table->update_user_notifications_table($this->notify_matrix);
101
			}
102
103
			$this->success_form_on_submit();
104
		}
105
106
		// Add Required Lang File(s)
107
		$this->unc_helper->add_lang();
108
109
		// Get No Notify Matrix & display the Options
110
		$this->notify_matrix = $this->unc_table->get_notify_method_type_matrix();
111
		$this->display_notification_methods_types($this->notify_matrix);
0 ignored issues
show
Unused Code introduced by
The call to dark1\usernotificationco...ication_methods_types() has too many arguments starting with $this->notify_matrix. ( Ignorable by Annotation )

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

111
		$this->/** @scrutinizer ignore-call */ 
112
         display_notification_methods_types($this->notify_matrix);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
112
113
		// Set output variables for display in the template
114
		$this->template->assign_vars([
115
			'UNC_ENABLE'	=> $this->config['dark1_unc_enable'],
116
			'UNC_NOTICE'	=> $this->language->lang('ACP_UNC_NO_LANG_KEY_NOTICE', $this->unc_helper->get_lang_key('')),
117
		]);
118
	}
119
120
	/**
121
	 * Request the Notification Methods and Types Matrix.
122
	 *
123
	 * @access private
124
	 */
125
	private function request_notify_method_type_matrix()
126
	{
127
		$this->notify_matrix = [];
128
		foreach ($this->notification_types_groups as $group => $notification_types)
129
		{
130
			foreach ($notification_types as $type => $type_data)
131
			{
132
				foreach ($this->notification_methods as $method => $method_data)
133
				{
134
					$notify_value = $this->request->variable(str_replace('.', '_', $type_data['id'] . '_' . $method_data['id']), 0);
135
					if ($notify_value == 1)
136
					{
137
						$this->notify_matrix[$method_data['id']][$type_data['id']] = true;
138
					}
139
					else if ($notify_value == -1)
140
					{
141
						$this->notify_matrix[$method_data['id']][$type_data['id']] = false;
142
					}
143
				}
144
			}
145
		}
146
	}
147
148
	/**
149
	 * Display the Notification Methods and Types with their options.
150
	 *
151
	 * @return void
152
	 * @access private
153
	 */
154
	private function display_notification_methods_types()
155
	{
156
		$block_method = 'notification_methods';
157
		$block_type = 'notification_types';
158
159
		$this->display_notification_methods($block_method);
160
		$this->display_notification_types($block_type, $block_method);
161
162
		$this->template->assign_vars([
163
			strtoupper($block_method) . '_COLS'	=> 3,
164
			strtoupper($block_type) . '_COLS'	=> (count($this->notification_methods) * 3) + 1,
165
		]);
166
	}
167
168
	/**
169
	 * Display the Notification Types.
170
	 *
171
	 * @param string $block_type
172
	 * @param string $block_method
173
	 *
174
	 * @return void
175
	 * @access private
176
	 */
177
	private function display_notification_types($block_type, $block_method)
178
	{
179
		foreach ($this->notification_types_groups as $group => $notification_types)
180
		{
181
			$this->template->assign_block_vars($block_type, [
182
				'GROUP_NAME'	=> $this->unc_helper->get_lang_key($group, true, $group),
183
			]);
184
185
			foreach ($notification_types as $type => $type_data)
186
			{
187
				$this->template->assign_block_vars($block_type, [
188
					'TYPE'		=> $type_data['id'],
189
					'TEXT'		=> implode(' ',array_unique(explode(' ', str_replace(['notification.type', '.', '_'], ' ', $type_data['id'])))),
190
					'NAME'		=> $this->unc_helper->get_lang_key($type_data['lang']),
191
					'EXPLAIN'	=> $this->unc_helper->get_lang_key($type_data['lang'] . '_EXPLAIN', false),
192
				]);
193
194
				$this->display_notification_methods($block_type . '.' . $block_method, $type_data['id']);
195
			}
196
		}
197
	}
198
199
	/**
200
	 * Display the Notification Methods.
201
	 *
202
	 * @param string		$block_var
203
	 * @param string|bool	$type_id
204
	 *
205
	 * @return void
206
	 * @access private
207
	 */
208
	private function display_notification_methods($block_var, $type_id = false)
209
	{
210
		foreach ($this->notification_methods as $method => $method_data)
211
		{
212
			$this->template->assign_block_vars($block_var, array_merge(
213
				['METHOD'	=> $method_data['id']],
214
				($type_id === false)
215
				? ['NAME' => $this->unc_helper->get_lang_key($method_data['lang'], true, $method_data['id'])]
216
				: ['SUBSCRIBED'	=> isset($this->notify_matrix[$method_data['id']][$type_id]) ? ($this->notify_matrix[$method_data['id']][$type_id] ? 1 : -1) : 0]
217
			));
218
		}
219
	}
220
}
221