main_listener   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 23
eloc 48
c 1
b 0
f 0
dl 0
loc 182
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 8 1
A user_add_modify_notifications_data() 0 18 6
A ucp_notifications_submit_notification_is_set() 0 14 3
A __construct() 0 5 1
A ucp_display_module_before() 0 7 3
A ucp_notifications_output_notification_types_modify_template_vars() 0 18 3
A notification_manager_add_notifications_for_users_modify_data() 0 24 6
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\event;
12
13
/**
14
 * @ignore
15
 */
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
use dark1\usernotificationcontrol\core\unc_table;
18
use phpbb\config\config;
19
use phpbb\language\language;
20
21
/**
22
 * User Notification Control [UNC] Event listener.
23
 */
24
class main_listener implements EventSubscriberInterface
25
{
26
	/** @var unc_table */
27
	protected $unc_table;
28
29
	/** @var config */
30
	protected $config;
31
32
	/** @var language */
33
	protected $language;
34
35
	/**
36
	 * Constructor
37
	 *
38
	 * @param unc_table		$unc_table	UNC Table object
39
	 * @param config		$config		phpBB config
40
	 * @param language		$language	Language object
41
	 */
42
	public function __construct(unc_table $unc_table, config $config, language $language)
43
	{
44
		$this->unc_table	= $unc_table;
45
		$this->config		= $config;
46
		$this->language		= $language;
47
	}
48
49
	/**
50
	* Assign functions defined in this class to event listeners in the core
51
	*
52
	* @return array
53
	* @access public
54
	*/
55
	public static function getSubscribedEvents()
56
	{
57
		return [
58
			'core.ucp_display_module_before'										=> 'ucp_display_module_before',
59
			'core.user_add_modify_notifications_data'								=> 'user_add_modify_notifications_data',
60
			'core.ucp_notifications_submit_notification_is_set'						=> 'ucp_notifications_submit_notification_is_set',
61
			'core.ucp_notifications_output_notification_types_modify_template_vars'	=> 'ucp_notifications_output_notification_types_modify_template_vars',
62
			'core.notification_manager_add_notifications_for_users_modify_data'		=> 'notification_manager_add_notifications_for_users_modify_data',
63
		];
64
	}
65
66
67
68
	/**
69
	 * Load language files in UCP
70
	 *
71
	 * @param object	$event	Event object
72
	 *
73
	 * @return void
74
	 * @access public
75
	 */
76
	public function ucp_display_module_before($event)
77
	{
78
		$mode = $event['mode'];
79
80
		if ($this->config['dark1_unc_enable'] == 1 && $mode == 'notification_options')
81
		{
82
			$this->language->add_lang('lang_unc', 'dark1/usernotificationcontrol');
83
		}
84
	}
85
86
87
88
	/**
89
	* User add modify notifications data
90
	*
91
	* @param object $event The event object
92
	*
93
	* @return void
94
	* @access public
95
	*/
96
	public function user_add_modify_notifications_data($event)
97
	{
98
		$notifications_data = $event['notifications_data'];
99
100
		if ($this->config['dark1_unc_enable'] == 1 && !empty($notifications_data))
101
		{
102
			$notify_matrix = $this->unc_table->get_notify_method_type_matrix();
103
104
			foreach ($notifications_data as $key => $subscription)
105
			{
106
				if (isset($notify_matrix[$subscription['method']][$subscription['item_type']]) && !$notify_matrix[$subscription['method']][$subscription['item_type']])
107
				{
108
					unset($notifications_data[$key]);
109
				}
110
			}
111
		}
112
113
		$event['notifications_data'] = $notifications_data;
114
	}
115
116
117
118
	/**
119
	* UCP on notifications submit check $is_set_notify
120
	*
121
	* @param object $event The event object
122
	*
123
	* @return void
124
	* @access public
125
	*/
126
	public function ucp_notifications_submit_notification_is_set($event)
127
	{
128
		$type_data = $event['type_data'];
129
		$method_data = $event['method_data'];
130
		$is_set_notify = $event['is_set_notify'];
131
132
		if ($this->config['dark1_unc_enable'] == 1)
133
		{
134
			$value = $this->unc_table->get_notify_method_type_value($method_data['id'], $type_data['id']);
135
136
			$is_set_notify = (isset($value)) ? $value : $is_set_notify ;
137
		}
138
139
		$event['is_set_notify'] = $is_set_notify;
140
	}
141
142
143
144
	/**
145
	* UCP notifications modify 'notification_types' template vars
146
	*
147
	* @param object $event The event object
148
	*
149
	* @return void
150
	* @access public
151
	*/
152
	public function ucp_notifications_output_notification_types_modify_template_vars($event)
153
	{
154
		$type_data = $event['type_data'];
155
		$method_data = $event['method_data'];
156
		$tpl_ary = $event['tpl_ary'];
157
158
		if ($this->config['dark1_unc_enable'] == 1)
159
		{
160
			$value = $this->unc_table->get_notify_method_type_value($method_data['id'], $type_data['id']);
161
162
			if (isset($value))
163
			{
164
				$tpl_ary['SUBSCRIBED'] = $value;
165
				$tpl_ary['AVAILABLE'] = false;
166
			}
167
		}
168
169
		$event['tpl_ary'] = $tpl_ary;
170
	}
171
172
173
174
	/**
175
	* Modify data in 'notification_manager' at 'add_notifications' for users
176
	*
177
	* @param object $event The event object
178
	*
179
	* @return void
180
	* @access public
181
	*/
182
	public function notification_manager_add_notifications_for_users_modify_data($event)
183
	{
184
		$notification_type_name = $event['notification_type_name'];
185
		$notify_users = $event['notify_users'];
186
187
		if ($this->config['dark1_unc_enable'] == 1)
188
		{
189
			$notify_matrix = $this->unc_table->get_notify_method_type_matrix();
190
191
			// Go through each user
192
			foreach ($notify_users as $user => $methods)
193
			{
194
				foreach ($methods as $key => $method)
195
				{
196
					// unset the notification method
197
					if (isset($notify_matrix[$method][$notification_type_name]) && !$notify_matrix[$method][$notification_type_name])
198
					{
199
						unset($notify_users[$user][$key]);
200
					}
201
				}
202
			}
203
		}
204
205
		$event['notify_users'] = $notify_users;
206
	}
207
}
208