Passed
Push — master ( bc9a55...f3faba )
by Dark❶
07:56
created

acp_main   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 245
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 25
eloc 81
c 1
b 0
f 0
dl 0
loc 245
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A handle() 0 31 2
B output_notification_methods_types() 0 47 7
A request_notify_method_type_matrix() 0 27 6
A get_subscription_types() 0 30 4
A get_lang_key() 0 5 3
A get_subscription_methods() 0 14 2
1
<?php
2
/**
3
 *
4
 * User Notification Control [UNC]. An extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2020, 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 phpbb\notification\type\type_interface;
24
use phpbb\notification\method\method_interface;
25
26
/**
27
 * User Notification Control [UNC] ACP controller Main.
28
 */
29
class acp_main extends acp_base
30
{
31
	/** @var \phpbb\config\config */
32
	protected $config;
33
34
	/** @var \dark1\usernotificationcontrol\core\unc_table */
35
	protected $unc_table;
36
37
	/** @var array */
38
	protected $notification_types;
39
40
	/** @var method_interface[] */
41
	protected $notification_methods;
42
43
	/**
44
	 * Constructor.
45
	 *
46
	 * @param \phpbb\language\language		$language		Language object
47
	 * @param \phpbb\log\log				$log			Log object
48
	 * @param \phpbb\request\request		$request		Request object
49
	 * @param \phpbb\template\template		$template		Template object
50
	 * @param \phpbb\user					$user			User object
51
	 * @param \phpbb\config\config			$config			Config object
52
	 * @param unc_table						$unc_table
53
	 * @param array							$notification_types
54
	 * @param array							$notification_methods
55
	 */
56
	public function __construct(language $language, log $log, request $request, template $template, user $user, config $config, unc_table $unc_table, $notification_types, $notification_methods)
57
	{
58
		parent::__construct($language, $log, $request, $template, $user);
59
60
		$this->config				= $config;
61
		$this->unc_table			= $unc_table;
62
		$this->notification_types	= $notification_types;
63
		$this->notification_methods	= $notification_methods;
64
	}
65
66
	/**
67
	 * Display the options a user can configure for Main Mode.
68
	 *
69
	 * @return void
70
	 * @access public
71
	 */
72
	public function handle()
73
	{
74
		$notify_matrix = [];
75
76
		// Is the form being submitted to us?
77
		if ($this->request->is_set_post('submit'))
78
		{
79
			$this->check_form_on_submit();
80
81
			// Set the options the user configured
82
			$this->config->set('dark1_unc_enable', $this->request->variable('dark1_unc_enable', 0));
83
84
			// Get No Notify Matrix
85
			$notify_matrix = $this->request_notify_method_type_matrix();
86
87
			// Set No Notify Matrix
88
			$this->unc_table->set_notify_method_type_matrix($notify_matrix);
89
90
			// Reflect in other tables
91
			$this->unc_table->update_user_notifications_table($notify_matrix, (bool) $this->config['dark1_unc_enable']);
92
93
			$this->success_form_on_submit();
94
		}
95
96
		// Get No Notify Matrix & display the Options
97
		$notify_matrix = $this->unc_table->get_notify_method_type_matrix();
98
		$this->output_notification_methods_types($notify_matrix);
99
100
		// Set output variables for display in the template
101
		$this->template->assign_vars([
102
			'UNC_ENABLE'		=> $this->config['dark1_unc_enable'],
103
		]);
104
	}
105
106
	/**
107
	 * Request the Notification Methods and Types Matrix.
108
	 *
109
	 * @return array $notify_matrix
110
	 * @access private
111
	 */
112
	private function request_notify_method_type_matrix()
113
	{
114
		// Get phpBB Notification
115
		$notification_methods = $this->get_subscription_methods();
116
		$notification_types_groups = $this->get_subscription_types();
117
118
		$notify_matrix = [];
119
		foreach ($notification_types_groups as $group => $notification_types)
120
		{
121
			foreach ($notification_types as $type => $type_data)
122
			{
123
				foreach ($notification_methods as $method => $method_data)
124
				{
125
					$notify_value = $this->request->variable(str_replace('.', '_', $type_data['id'] . '_' . $method_data['id']), 0);
126
					if ($notify_value == 1)
127
					{
128
						$notify_matrix[$method_data['id']][$type_data['id']] = true;
129
					}
130
					else if ($notify_value == -1)
131
					{
132
						$notify_matrix[$method_data['id']][$type_data['id']] = false;
133
					}
134
				}
135
			}
136
		}
137
138
		return $notify_matrix;
139
	}
140
141
	/**
142
	 * Display the Notification Methods and Types with their options.
143
	 *
144
	 * @param array $notify_matrix
145
	 *
146
	 * @return void
147
	 * @access private
148
	 */
149
	private function output_notification_methods_types($notify_matrix)
150
	{
151
		$this->language->add_lang('ucp');
152
153
		// Get phpBB Notification
154
		$notification_methods = $this->get_subscription_methods();
155
		$notification_types_groups = $this->get_subscription_types();
156
157
		$block_method = 'notification_methods';
158
		$block_type = 'notification_types';
159
160
		foreach ($notification_methods as $method => $method_data)
161
		{
162
			$this->template->assign_block_vars($block_method, [
163
				'METHOD'			=> $method_data['id'],
164
				'NAME'				=> $this->get_lang_key($method_data['lang'], $method_data['id']),
165
			]);
166
		}
167
168
		foreach ($notification_types_groups as $group => $notification_types)
169
		{
170
			$this->template->assign_block_vars($block_type, [
171
				'GROUP_NAME'	=> $this->get_lang_key($group, $group),
172
			]);
173
174
			foreach ($notification_types as $type => $type_data)
175
			{
176
				$this->template->assign_block_vars($block_type, [
177
					'TYPE'				=> $type_data['id'],
178
					'NAME'				=> $this->get_lang_key($type_data['lang'], $type_data['id']),
179
					'EXPLAIN'			=> $this->get_lang_key($type_data['lang'] . '_EXPLAIN', '', false),
180
				]);
181
182
				foreach ($notification_methods as $method => $method_data)
183
				{
184
					$this->template->assign_block_vars($block_type . '.' . $block_method, [
185
						'METHOD'			=> $method_data['id'],
186
						'NAME'				=> $this->language->lang($method_data['lang']),
187
						'SUBSCRIBED'		=> isset($notify_matrix[$method_data['id']][$type_data['id']]) ? ($notify_matrix[$method_data['id']][$type_data['id']] ? 1 : -1) : 0 ,
188
					]);
189
				}
190
			}
191
		}
192
193
		$this->template->assign_vars([
194
			strtoupper($block_method) . '_COLS' => 3,
195
			strtoupper($block_type) . '_COLS' => (count($notification_methods) * 3) + 1,
196
		]);
197
	}
198
199
	/**
200
	 * Get all of the subscription methods
201
	 *
202
	 * @param string $lang_key
203
	 * @param string $sub
204
	 * @param bool $warn
205
	 *
206
	 * @return string Array of methods
207
	 * @access private
208
	 */
209
	private function get_lang_key($lang_key, $sub = '', $warn = true)
210
	{
211
		$warn = $warn ? ' ⚠️ ' . $this->language->lang('ACP_UNC_NO_LANG_KEY') . ' : ' : '';
212
213
		return $this->language->is_set($lang_key) ? $this->language->lang($lang_key) : $warn . $sub;
214
	}
215
216
	/**
217
	 * Get all of the subscription methods
218
	 *
219
	 * @return array Array of methods
220
	 * @access private
221
	 */
222
	private function get_subscription_methods()
223
	{
224
		$subscription_methods = [];
225
226
		/** @var method_interface $method */
227
		foreach ($this->notification_methods as $method_name => $method)
228
		{
229
			$subscription_methods[$method_name] = [
230
				'id'		=> $method->get_type(),
231
				'lang'		=> str_replace('.', '_', strtoupper($method->get_type())),
232
			];
233
		}
234
235
		return $subscription_methods;
236
	}
237
238
	/**
239
	 * Get all of the subscription types
240
	 *
241
	 * @return array Array of item types
242
	 * @access private
243
	 */
244
	private function get_subscription_types()
245
	{
246
		$subscription_types = [];
247
248
		/** @var type_interface $type */
249
		foreach ($this->notification_types as $type_name => $type)
250
		{
251
			$type_ary = [
252
				'id'	=> $type->get_type(),
253
				'lang'	=> 'NOTIFICATION_TYPE_' . strtoupper($type->get_type()),
254
				'group'	=> 'NOTIFICATION_GROUP_MISCELLANEOUS',
255
			];
256
257
			if ($type::$notification_option !== false)
258
			{
259
				$type_ary = array_merge($type_ary, $type::$notification_option);
260
			}
261
262
			$subscription_types[$type_ary['group']][$type_ary['id']] = $type_ary;
263
		}
264
265
		// Move miscellaneous group to last section
266
		if (isset($subscription_types['NOTIFICATION_GROUP_MISCELLANEOUS']))
267
		{
268
			$miscellaneous = $subscription_types['NOTIFICATION_GROUP_MISCELLANEOUS'];
269
			unset($subscription_types['NOTIFICATION_GROUP_MISCELLANEOUS']);
270
			$subscription_types['NOTIFICATION_GROUP_MISCELLANEOUS'] = $miscellaneous;
271
		}
272
273
		return $subscription_types;
274
	}
275
}
276