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

unc_helper::get_lang_key()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
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\core;
12
13
/**
14
 * @ignore
15
 */
16
use phpbb\user;
17
use phpbb\language\language;
18
use phpbb\extension\manager as ext_manager;
19
use phpbb\event\dispatcher_interface as dispatcher;
20
use phpbb\notification\type\type_interface;
21
use phpbb\notification\method\method_interface;
22
use phpbb\finder;
23
24
/**
25
 * User Notification Control Core Helper Class.
26
 */
27
class unc_helper
28
{
29
	/** @var user */
30
	protected $user;
31
32
	/** @var language */
33
	protected $language;
34
35
	/** @var ext_manager */
36
	protected $ext_manager;
37
38
	/** @var dispatcher */
39
	protected $dispatcher;
40
41
	/** @var array Notification Types */
42
	protected $notification_types;
43
44
	/** @var array Notification Methods */
45
	protected $notification_methods;
46
47
	/** @var string phpBB php ext */
48
	protected $php_ext;
49
50
	/**
51
	 * Constructor for User Notification Control Core Table Class.
52
	 *
53
	 * @param language		$language				Language object
54
	 * @param user			$user					User object
55
	 * @param ext_manager	$ext_manager			phpBB Extension Manager
56
	 * @param dispatcher	$dispatcher				Dispatcher object
57
	 * @param array			$notification_types		phpBB Notification Types
58
	 * @param array			$notification_methods	phpBB Notification Methods
59
	 * @param string		$php_ext				phpBB php ext
60
	 */
61
	public function __construct(user $user, language $language, ext_manager $ext_manager, dispatcher $dispatcher, $notification_types, $notification_methods, $php_ext)
62
	{
63
		$this->user					= $user;
64
		$this->language				= $language;
65
		$this->ext_manager			= $ext_manager;
66
		$this->dispatcher			= $dispatcher;
67
		$this->notification_types	= $notification_types;
68
		$this->notification_methods	= $notification_methods;
69
		$this->php_ext				= $php_ext;
70
	}
71
72
73
74
	/**
75
	 * Get all of the subscription methods
76
	 *
77
	 * @return array Array of methods
78
	 * @access public
79
	 */
80
	public function get_subscription_methods()
81
	{
82
		$subscription_methods = [];
83
84
		/** @var method_interface $method */
85
		foreach ($this->notification_methods as $method_name => $method)
86
		{
87
			$subscription_methods[$method_name] = [
88
				'id'		=> $method->get_type(),
89
				'lang'		=> str_replace('.', '_', strtoupper($method->get_type())),
90
			];
91
		}
92
93
		return $subscription_methods;
94
	}
95
96
97
98
	/**
99
	 * Get all of the subscription types
100
	 *
101
	 * @return array Array of item types
102
	 * @access public
103
	 */
104
	public function get_subscription_types()
105
	{
106
		$subscription_types = [];
107
		$prefix = 'NOTIFICATION_TYPE_';
108
109
		/** @var type_interface $type */
110
		foreach ($this->notification_types as $type_name => $type)
111
		{
112
			$lang = str_replace('.', '_', strtoupper($type->get_type()));
113
			if (substr($lang, 0, strlen($prefix)) == $prefix)
114
			{
115
				$lang = substr($lang, strlen($prefix));
116
			}
117
118
			$type_ary = [
119
				'id'	=> $type->get_type(),
120
				'lang'	=> $prefix . $lang,
121
				'group'	=> 'NOTIFICATION_GROUP_MISCELLANEOUS',
122
			];
123
124
			if ($type::$notification_option !== false)
125
			{
126
				$type_ary = array_merge($type_ary, $type::$notification_option);
127
			}
128
129
			$subscription_types[$type_ary['group']][$type_ary['id']] = $type_ary;
130
		}
131
132
		// Move miscellaneous group to last section
133
		if (isset($subscription_types['NOTIFICATION_GROUP_MISCELLANEOUS']))
134
		{
135
			$miscellaneous = $subscription_types['NOTIFICATION_GROUP_MISCELLANEOUS'];
136
			unset($subscription_types['NOTIFICATION_GROUP_MISCELLANEOUS']);
137
			$subscription_types['NOTIFICATION_GROUP_MISCELLANEOUS'] = $miscellaneous;
138
		}
139
140
		return $subscription_types;
141
	}
142
143
144
145
	/**
146
	 * Get all of the subscription methods
147
	 *
148
	 * @return array Array of methods
149
	 * @access public
150
	 */
151
	public function get_lang_unc_custom()
152
	{
153
		$ext_name = 'dark1/usernotificationcontrol';
154
		$ext_lang = 'lang_unc_custom';
155
156
		/** @var finder $finder */
157
		$finder = $this->ext_manager->get_finder();
158
		$lang_file_path = $finder
159
			->set_extensions([$ext_name])
160
			->prefix($ext_lang)
161
			->suffix('.'.$this->php_ext)
162
			->directory("language/".$this->user->lang_name)
163
			->get_files();
164
165
		// Check if exists
166
		$lang_ary = (current($lang_file_path)) ? [$ext_name => $ext_lang] : [];
167
168
		return $lang_ary;
169
	}
170
171
172
173
	/**
174
	 * Add Required Lang File(s).
175
	 *
176
	 * @return void
177
	 * @access public
178
	 */
179
	public function add_lang()
180
	{
181
		$add_ext_lang = $this->get_lang_unc_custom();
182
183
		/**
184
		 * Event to modify the similar topics template block
185
		 *
186
		 * @event dark1.usernotificationcontrol.add_ext_lang
187
		 *
188
		 * @var array add_ext_lang		Array with [(string) '<vendor>/<extension>' => (string|array) '<lang>' | ['<lang1>', '<lang2>']]
189
		 *
190
		 * @since 1.0.2
191
		 * @changed 1.0.4	The add_ext_lang now accepts array of lang(s)
192
		 */
193
		$vars = ['add_ext_lang'];
194
		extract($this->dispatcher->trigger_event('dark1.usernotificationcontrol.add_ext_lang', compact($vars)));
195
196
		// Add Language File(s) that are Required
197
		$this->language->add_lang('ucp');
198
		if (!empty($add_ext_lang))
199
		{
200
			foreach ($add_ext_lang as $ext => $langs)
201
			{
202
				$langs = is_string($langs) ? [$langs] : $langs;
203
				foreach ($langs as $lang)
204
				{
205
					$this->language->add_lang((string) $lang, (string) $ext);
206
				}
207
			}
208
		}
209
	}
210
211
212
213
	/**
214
	 * Get all of the subscription methods
215
	 *
216
	 * @param string $lang_key
217
	 * @param bool $warn
218
	 * @param string $sub
219
	 *
220
	 * @return string Array of methods
221
	 * @access public
222
	 */
223
	public function get_lang_key($lang_key, $warn = true, $sub = '')
224
	{
225
		$warn = $warn ? ' ⚠️ ' . $this->language->lang('ACP_UNC_NO_LANG_KEY') : '';
226
		return $this->language->is_set($lang_key) ? $this->language->lang($lang_key) : $warn . (!empty($sub) ? ' : ' . $sub : '');
227
	}
228
229
}
230