Passed
Push — master ( 0acf12...c33757 )
by Dark❶
02:27
created

acp_main::get_lang_key()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 4
nc 8
nop 3
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
	/**
40
	 * Constructor.
41
	 *
42
	 * @param language		$language				Language object
43
	 * @param log			$log					Log object
44
	 * @param request		$request				Request object
45
	 * @param template		$template				Template object
46
	 * @param user			$user					User object
47
	 * @param config		config					Config object
48
	 * @param unc_table		$unc_table				UNC Table object
49
	 * @param unc_helper	$unc_helper				UNC Helper object
50
	 */
51
	public function __construct(language $language, log $log, request $request, template $template, user $user, config $config, unc_table $unc_table, unc_helper $unc_helper)
52
	{
53
		parent::__construct($language, $log, $request, $template, $user);
54
55
		$this->config				= $config;
56
		$this->unc_table			= $unc_table;
57
		$this->unc_helper			= $unc_helper;
58
	}
59
60
	/**
61
	 * Display the options a user can configure for Main Mode.
62
	 *
63
	 * @return void
64
	 * @access public
65
	 */
66
	public function handle()
67
	{
68
		// Is the form being submitted to us?
69
		if ($this->request->is_set_post('submit'))
70
		{
71
			$this->check_form_on_submit();
72
73
			// Set the options the user configured
74
			$this->config->set('dark1_unc_enable', $this->request->variable('dark1_unc_enable', 0));
75
76
			// Get No Notify Matrix
77
			$notify_matrix = $this->request_notify_method_type_matrix();
78
79
			// Set No Notify Matrix
80
			$this->unc_table->set_notify_method_type_matrix($notify_matrix);
81
82
			// Reflect in other tables
83
			$this->unc_table->update_user_notifications_table($notify_matrix, (bool) $this->config['dark1_unc_enable']);
84
85
			$this->success_form_on_submit();
86
		}
87
88
		// Add Required Lang File(s)
89
		$this->unc_helper->add_lang();
90
91
		// Get No Notify Matrix & display the Options
92
		$notify_matrix = $this->unc_table->get_notify_method_type_matrix();
93
		$this->display_notification_methods_types($notify_matrix);
94
95
		// Set output variables for display in the template
96
		$this->template->assign_vars([
97
			'UNC_ENABLE'	=> $this->config['dark1_unc_enable'],
98
			'UNC_NOTICE'	=> $this->language->lang('ACP_UNC_NO_LANG_KEY_NOTICE', $this->unc_helper->get_lang_key('')),
99
		]);
100
	}
101
102
	/**
103
	 * Request the Notification Methods and Types Matrix.
104
	 *
105
	 * @return array $notify_matrix
106
	 * @access private
107
	 */
108
	private function request_notify_method_type_matrix()
109
	{
110
		// Get phpBB Notification
111
		$notification_methods = $this->unc_helper->get_subscription_methods();
112
		$notification_types_groups = $this->unc_helper->get_subscription_types();
113
114
		$notify_matrix = [];
115
		foreach ($notification_types_groups as $group => $notification_types)
116
		{
117
			foreach ($notification_types as $type => $type_data)
118
			{
119
				foreach ($notification_methods as $method => $method_data)
120
				{
121
					$notify_value = $this->request->variable(str_replace('.', '_', $type_data['id'] . '_' . $method_data['id']), 0);
122
					if ($notify_value == 1)
123
					{
124
						$notify_matrix[$method_data['id']][$type_data['id']] = true;
125
					}
126
					else if ($notify_value == -1)
127
					{
128
						$notify_matrix[$method_data['id']][$type_data['id']] = false;
129
					}
130
				}
131
			}
132
		}
133
134
		return $notify_matrix;
135
	}
136
137
	/**
138
	 * Display the Notification Methods and Types with their options.
139
	 *
140
	 * @param array $notify_matrix
141
	 *
142
	 * @return void
143
	 * @access private
144
	 */
145
	private function display_notification_methods_types($notify_matrix)
146
	{
147
		// Get phpBB Notification
148
		$notification_methods = $this->unc_helper->get_subscription_methods();
149
		$notification_types_groups = $this->unc_helper->get_subscription_types();
150
151
		$block_method = 'notification_methods';
152
		$block_type = 'notification_types';
153
154
		foreach ($notification_methods as $method => $method_data)
155
		{
156
			$this->template->assign_block_vars($block_method, [
157
				'METHOD'	=> $method_data['id'],
158
				'NAME'		=> $this->unc_helper->get_lang_key($method_data['lang'], true, $method_data['id']),
159
			]);
160
		}
161
162
		foreach ($notification_types_groups as $group => $notification_types)
163
		{
164
			$this->template->assign_block_vars($block_type, [
165
				'GROUP_NAME'	=> $this->unc_helper->get_lang_key($group, true, $group),
166
			]);
167
168
			foreach ($notification_types as $type => $type_data)
169
			{
170
				$this->template->assign_block_vars($block_type, [
171
					'TYPE'		=> $type_data['id'],
172
					'NAME'		=> $this->unc_helper->get_lang_key($type_data['lang']),
173
					'EXPLAIN'	=> $this->unc_helper->get_lang_key($type_data['lang'] . '_EXPLAIN', false),
174
				]);
175
176
				foreach ($notification_methods as $method => $method_data)
177
				{
178
					$this->template->assign_block_vars($block_type . '.' . $block_method, [
179
						'METHOD'		=> $method_data['id'],
180
						'SUBSCRIBED'	=> isset($notify_matrix[$method_data['id']][$type_data['id']]) ? ($notify_matrix[$method_data['id']][$type_data['id']] ? 1 : -1) : 0 ,
181
					]);
182
				}
183
			}
184
		}
185
186
		$this->template->assign_vars([
187
			strtoupper($block_method) . '_COLS'	=> 3,
188
			strtoupper($block_type) . '_COLS'	=> (count($notification_methods) * 3) + 1,
189
		]);
190
	}
191
}
192