acp_main   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
eloc 62
c 1
b 0
f 0
dl 0
loc 200
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A handle() 0 37 3
A request_notify_method_type_matrix() 0 17 6
A display_notification_methods_types() 0 11 1
A display_notification_methods() 0 9 5
A display_notification_types() 0 19 3
1
<?php
2
/**
3
 *
4
 * User Notification Control [UNC]. An extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2020-forever, 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 Collection Arrays
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
76
	/**
77
	 * Display the options a user can configure for Main Mode.
78
	 *
79
	 * @return void
80
	 * @access public
81
	 */
82
	public function handle()
83
	{
84
		// Is the form being submitted to us?
85
		if ($this->request->is_set_post('submit'))
86
		{
87
			$this->check_form_on_submit();
88
89
			// Set the options the user configured
90
			$dark1_unc_enable = $this->request->variable('dark1_unc_enable', 0);
91
			$this->config->set('dark1_unc_enable', $dark1_unc_enable);
92
93
			// Get No Notify Matrix
94
			$this->request_notify_method_type_matrix();
95
96
			// Set No Notify Matrix
97
			$this->unc_table->set_notify_method_type_matrix($this->notify_matrix);
98
99
			// Reflect in other tables if enabled
100
			if ($dark1_unc_enable)
101
			{
102
				$this->unc_table->update_user_notifications_table($this->notify_matrix);
103
			}
104
105
			$this->success_form_on_submit();
106
		}
107
108
		// Add Required Lang File(s)
109
		$this->unc_helper->add_lang();
110
111
		// Get Not Notify Matrix & display the Options
112
		$this->notify_matrix = $this->unc_table->get_notify_method_type_matrix();
113
		$this->display_notification_methods_types();
114
115
		// Set output variables for display in the template
116
		$this->template->assign_vars([
117
			'UNC_ENABLE'	=> $this->config['dark1_unc_enable'],
118
			'UNC_NOTICE'	=> $this->unc_helper->get_lang_key(''),
119
		]);
120
	}
121
122
123
124
	/**
125
	 * Request the Notification Methods and Types Matrix.
126
	 *
127
	 * @access private
128
	 */
129
	private function request_notify_method_type_matrix()
130
	{
131
		$this->notify_matrix = [];
132
		foreach ($this->notification_types_groups as $group => $notification_types)
133
		{
134
			foreach ($notification_types as $type => $type_data)
135
			{
136
				foreach ($this->notification_methods as $method => $method_data)
137
				{
138
					$notify_value = $this->request->variable(str_replace('.', '_', $type_data['id'] . '_' . $method_data['id']), 0);
139
					if ($notify_value == 1)
140
					{
141
						$this->notify_matrix[$method_data['id']][$type_data['id']] = true;
142
					}
143
					else if ($notify_value == -1)
144
					{
145
						$this->notify_matrix[$method_data['id']][$type_data['id']] = false;
146
					}
147
				}
148
			}
149
		}
150
	}
151
152
153
154
	/**
155
	 * Display the Notification Methods and Types with their options.
156
	 *
157
	 * @return void
158
	 * @access private
159
	 */
160
	private function display_notification_methods_types()
161
	{
162
		$block_method = 'notification_methods';
163
		$block_type = 'notification_types';
164
165
		$this->display_notification_methods($block_method);
166
		$this->display_notification_types($block_type, $block_method);
167
168
		$this->template->assign_vars([
169
			strtoupper($block_method) . '_COLS'	=> 3,
170
			strtoupper($block_type) . '_COLS'	=> (count($this->notification_methods) * 3) + 1,
171
		]);
172
	}
173
174
175
176
	/**
177
	 * Display the Notification Types.
178
	 *
179
	 * @param string $block_type
180
	 * @param string $block_method
181
	 *
182
	 * @return void
183
	 * @access private
184
	 */
185
	private function display_notification_types($block_type, $block_method)
186
	{
187
		foreach ($this->notification_types_groups as $group => $notification_types)
188
		{
189
			$this->template->assign_block_vars($block_type, [
190
				'GROUP_NAME'	=> $this->unc_helper->get_lang_key($group, true, $group),
191
			]);
192
193
			foreach ($notification_types as $type => $type_data)
194
			{
195
				$this->template->assign_block_vars($block_type, [
196
					'TYPE'		=> $type_data['id'],
197
					'TEXT'		=> implode(' ',array_unique(explode(' ', str_replace(['notification.type', '.', '_'], ' ', $type_data['id'])))),
198
					'NAME'		=> $this->unc_helper->get_lang_key($type_data['lang']),
199
					'LANG_KEY'	=> $type_data['lang'],
200
					'EXPLAIN'	=> $this->unc_helper->get_lang_key($type_data['lang'] . '_EXPLAIN', false),
201
				]);
202
203
				$this->display_notification_methods($block_type . '.' . $block_method, $type_data['id']);
204
			}
205
		}
206
	}
207
208
209
210
	/**
211
	 * Display the Notification Methods.
212
	 *
213
	 * @param string		$block_var
214
	 * @param string|bool	$type_id
215
	 *
216
	 * @return void
217
	 * @access private
218
	 */
219
	private function display_notification_methods($block_var, $type_id = false)
220
	{
221
		foreach ($this->notification_methods as $method => $method_data)
222
		{
223
			$this->template->assign_block_vars($block_var, array_merge(
224
				['METHOD'	=> $method_data['id']],
225
				($type_id === false)
226
				? ['NAME' => $this->unc_helper->get_lang_key($method_data['lang'], true, $method_data['id'])]
227
				: ['SUBSCRIBED'	=> isset($this->notify_matrix[$method_data['id']][$type_id]) ? ($this->notify_matrix[$method_data['id']][$type_id] ? 1 : -1) : 0]
228
			));
229
		}
230
	}
231
}
232