Passed
Push — master ( 047563...0acf12 )
by Dark❶
08:37
created

acp_main::get_subscription_methods()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 14
rs 10
cc 2
nc 2
nop 0
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 phpbb\event\dispatcher_interface as dispatcher;
23
use dark1\usernotificationcontrol\core\unc_table;
24
use dark1\usernotificationcontrol\core\unc_helper;
25
26
/**
27
 * User Notification Control [UNC] ACP controller Main.
28
 */
29
class acp_main extends acp_base
30
{
31
	/** @var config */
32
	protected $config;
33
34
	/** @var dispatcher */
35
	protected $dispatcher;
36
37
	/** @var unc_table */
38
	protected $unc_table;
39
40
	/** @var unc_helper */
41
	protected $unc_helper;
42
43
	/**
44
	 * Constructor.
45
	 *
46
	 * @param language		$language				Language object
47
	 * @param log			$log					Log object
48
	 * @param request		$request				Request object
49
	 * @param template		$template				Template object
50
	 * @param user			$user					User object
51
	 * @param config		config					Config object
52
	 * @param dispatcher	$dispatcher				Dispatcher object
53
	 * @param unc_table		$unc_table				UNC Table object
54
	 * @param unc_helper	$unc_helper				UNC Helper object
55
	 */
56
	public function __construct(language $language, log $log, request $request, template $template, user $user, config $config, dispatcher $dispatcher, unc_table $unc_table, unc_helper $unc_helper)
57
	{
58
		parent::__construct($language, $log, $request, $template, $user);
59
60
		$this->config				= $config;
61
		$this->dispatcher			= $dispatcher;
62
		$this->unc_table			= $unc_table;
63
		$this->unc_helper			= $unc_helper;
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
		// Is the form being submitted to us?
75
		if ($this->request->is_set_post('submit'))
76
		{
77
			$this->check_form_on_submit();
78
79
			// Set the options the user configured
80
			$this->config->set('dark1_unc_enable', $this->request->variable('dark1_unc_enable', 0));
81
82
			// Get No Notify Matrix
83
			$notify_matrix = $this->request_notify_method_type_matrix();
84
85
			// Set No Notify Matrix
86
			$this->unc_table->set_notify_method_type_matrix($notify_matrix);
87
88
			// Reflect in other tables
89
			$this->unc_table->update_user_notifications_table($notify_matrix, (bool) $this->config['dark1_unc_enable']);
90
91
			$this->success_form_on_submit();
92
		}
93
94
		// Get No Notify Matrix & display the Options
95
		$notify_matrix = $this->unc_table->get_notify_method_type_matrix();
96
		$this->output_notification_methods_types($notify_matrix);
97
98
		// Set output variables for display in the template
99
		$this->template->assign_vars([
100
			'UNC_ENABLE'	=> $this->config['dark1_unc_enable'],
101
			'UNC_NOTICE'	=> $this->language->lang('ACP_UNC_NO_LANG_KEY_NOTICE', $this->get_lang_key('')),
102
		]);
103
	}
104
105
	/**
106
	 * Request the Notification Methods and Types Matrix.
107
	 *
108
	 * @return array $notify_matrix
109
	 * @access private
110
	 */
111
	private function request_notify_method_type_matrix()
112
	{
113
		// Get phpBB Notification
114
		$notification_methods = $this->unc_helper->get_subscription_methods();
115
		$notification_types_groups = $this->unc_helper->get_subscription_types();
116
117
		$notify_matrix = [];
118
		foreach ($notification_types_groups as $group => $notification_types)
119
		{
120
			foreach ($notification_types as $type => $type_data)
121
			{
122
				foreach ($notification_methods as $method => $method_data)
123
				{
124
					$notify_value = $this->request->variable(str_replace('.', '_', $type_data['id'] . '_' . $method_data['id']), 0);
125
					if ($notify_value == 1)
126
					{
127
						$notify_matrix[$method_data['id']][$type_data['id']] = true;
128
					}
129
					else if ($notify_value == -1)
130
					{
131
						$notify_matrix[$method_data['id']][$type_data['id']] = false;
132
					}
133
				}
134
			}
135
		}
136
137
		return $notify_matrix;
138
	}
139
140
	/**
141
	 * Display the Notification Methods and Types with their options.
142
	 *
143
	 * @param array $notify_matrix
144
	 *
145
	 * @return void
146
	 * @access private
147
	 */
148
	private function output_notification_methods_types($notify_matrix)
149
	{
150
		$add_ext_lang = $this->unc_helper->get_lang_unc_custom();
151
152
		/**
153
		 * Event to modify the similar topics template block
154
		 *
155
		 * @event dark1.usernotificationcontrol.add_ext_lang
156
		 *
157
		 * @var array add_ext_lang		Array with [(string) '<vendor>/<extension>' => (string|array) '<lang>' | ['<lang1>', '<lang2>']]
158
		 *
159
		 * @since 1.0.2
160
		 */
161
		$vars = ['add_ext_lang'];
162
		extract($this->dispatcher->trigger_event('dark1.usernotificationcontrol.add_ext_lang', compact($vars)));
163
164
		// Add Language File(s) that are Required
165
		$this->language->add_lang('ucp');
166
		if (!empty($add_ext_lang))
167
		{
168
			foreach ($add_ext_lang as $ext => $langs)
169
			{
170
				$langs = is_string($langs) ? [$langs] : $langs;
171
172
				if (is_array($langs))
173
				{
174
					foreach ($langs as $lang)
175
					{
176
						$this->language->add_lang((string) $lang, (string) $ext);
177
					}
178
				}
179
			}
180
		}
181
182
		// Get phpBB Notification
183
		$notification_methods = $this->unc_helper->get_subscription_methods();
184
		$notification_types_groups = $this->unc_helper->get_subscription_types();
185
186
		$block_method = 'notification_methods';
187
		$block_type = 'notification_types';
188
189
		foreach ($notification_methods as $method => $method_data)
190
		{
191
			$this->template->assign_block_vars($block_method, [
192
				'METHOD'	=> $method_data['id'],
193
				'NAME'		=> $this->get_lang_key($method_data['lang'], true, $method_data['id']),
194
			]);
195
		}
196
197
		foreach ($notification_types_groups as $group => $notification_types)
198
		{
199
			$this->template->assign_block_vars($block_type, [
200
				'GROUP_NAME'	=> $this->get_lang_key($group, true, $group),
201
			]);
202
203
			foreach ($notification_types as $type => $type_data)
204
			{
205
				$this->template->assign_block_vars($block_type, [
206
					'TYPE'		=> $type_data['id'],
207
					'NAME'		=> $this->get_lang_key($type_data['lang']),
208
					'EXPLAIN'	=> $this->get_lang_key($type_data['lang'] . '_EXPLAIN', false),
209
				]);
210
211
				foreach ($notification_methods as $method => $method_data)
212
				{
213
					$this->template->assign_block_vars($block_type . '.' . $block_method, [
214
						'METHOD'		=> $method_data['id'],
215
						'NAME'			=> $this->language->lang($method_data['lang']),
216
						'SUBSCRIBED'	=> isset($notify_matrix[$method_data['id']][$type_data['id']]) ? ($notify_matrix[$method_data['id']][$type_data['id']] ? 1 : -1) : 0 ,
217
					]);
218
				}
219
			}
220
		}
221
222
		$this->template->assign_vars([
223
			strtoupper($block_method) . '_COLS'	=> 3,
224
			strtoupper($block_type) . '_COLS'	=> (count($notification_methods) * 3) + 1,
225
		]);
226
	}
227
228
	/**
229
	 * Get all of the subscription methods
230
	 *
231
	 * @param string $lang_key
232
	 * @param bool $warn
233
	 * @param string $sub
234
	 *
235
	 * @return string Array of methods
236
	 * @access private
237
	 */
238
	private function get_lang_key($lang_key, $warn = true, $sub = '')
239
	{
240
		$warn = $warn ? ' ⚠️ ' . $this->language->lang('ACP_UNC_NO_LANG_KEY') : '';
241
242
		return $this->language->is_set($lang_key) ? $this->language->lang($lang_key) : $warn . (!empty($sub) ? ' : ' . $sub : '');
243
	}
244
}
245