Passed
Push — development ( 4b352c...7fab02 )
by Spuds
01:07 queued 20s
created

action_notificationsSettings_display()   D

Complexity

Conditions 18
Paths 17

Size

Total Lines 127
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 18
eloc 56
nc 17
nop 0
dl 0
loc 127
rs 4.8666
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Manage mentions (notifications) settings.
5
 *
6
 * @package   ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
9
 *
10
 * @version 2.0 Beta 1
11
 */
12
13
namespace ElkArte\AdminController;
14
15
use ElkArte\AbstractController;
16
use ElkArte\Action;
17
use ElkArte\Helper\Util;
18
use ElkArte\Languages\Txt;
19
use ElkArte\Mentions\MentionType\AbstractNotificationMessage;
20
use ElkArte\Notifications\Notifications;
21
use ElkArte\SettingsForm\SettingsForm;
22
23
/**
24
 * Mentions administration controller.
25
 * This class allows modifying mentions and notification settings for the forum.
26
 */
27
class ManageMentions extends AbstractController
28
{
29
	/**
30
	 * Pre-dispatch, called before other methods.
31
	 */
32
	public function pre_dispatch()
33
	{
34
		// We need this in a few places, so it's easier to have it loaded here
35
		require_once(SUBSDIR . '/ManageFeatures.subs.php');
36
37
		Txt::load('Help+ManageSettings+Mentions');
38
	}
39
40
	/**
41
	 * Default action for this controller.
42
	 */
43
	public function action_index()
44
	{
45
		$subActions = [
46
			'mentions' => [$this, 'action_notificationsSettings_display', 'permission' => 'admin_forum'],
47
		];
48
49
		// Set up the action control
50
		$action = new Action('modify_mentions');
51
52
		// There is only one option
53
		$subAction = $action->initialize($subActions, 'mentions');
54
		$action->dispatch($subAction);
55
	}
56
57
	/**
58
	 * Initializes the mentions settings admin page.
59
	 *
60
	 * - Accessed from ?action=admin;area=featuresettings;sa=mentions;
61
	 *
62
	 * @event integrate_save_modify_mention_settings
63
	 */
64
	public function action_notificationsSettings_display(): void
65
	{
66
		global $txt, $context, $modSettings;
67
68
		Txt::load('Mentions');
69
70
		// Instantiate the form
71
		$settingsForm = new SettingsForm(SettingsForm::DB_ADAPTER);
72
73
		// Initialize it with our settings
74
		$settingsForm->setConfigVars($this->_notificationsSettings());
75
76
		// Some context stuff
77
		$context['page_title'] = $txt['mentions_settings'];
78
		$context['sub_template'] = 'show_settings';
79
80
		// Saving the settings?
81
		if ($this->_req->hasQuery('save'))
82
		{
83
			checkSession();
84
85
			call_integration_hook('integrate_save_modify_mention_settings');
86
87
			if (!empty($this->_req->post->mentions_enabled))
88
			{
89
				enableModules('mentions', ['post', 'display']);
90
			}
91
			else
92
			{
93
				disableModules('mentions', ['post', 'display']);
94
			}
95
96
			if (!empty($modSettings['hidden_notification_methods']))
97
			{
98
				foreach ($modSettings['hidden_notification_methods'] as $class)
99
				{
100
					$this->_req->post->notifications[$class::getType()] = $class::getSettings();
101
				}
102
			}
103
104
			if (empty($this->_req->post->notifications))
105
			{
106
				$notification_methods = serialize([]);
107
			}
108
			else
109
			{
110
				$notification_methods = [];
111
				foreach ($this->_req->post->notifications as $type => $notification)
112
				{
113
					if (!empty($notification['enable']))
114
					{
115
						$defaults = $notification['default'] ?? [];
116
						unset($notification['enable'], $notification['default']);
117
						foreach ($notification as $k => $v)
118
						{
119
							$notification[$k] = in_array($k, $defaults) ? Notifications::DEFAULT_LEVEL : $v;
120
						}
121
122
						$notification_methods[$type] = $notification;
123
					}
124
				}
125
126
				$notification_methods = serialize($notification_methods);
127
			}
128
129
			require_once(SUBSDIR . '/Mentions.subs.php');
130
			$enabled_mentions = [];
131
			$current_settings = Util::unserialize($modSettings['notification_methods']);
132
133
			// Fist hide what was visible
134
			$modules_toggle = ['enable' => [], 'disable' => []];
135
			foreach ($current_settings as $type => $val)
136
			{
137
				if (!isset($this->_req->post->notifications[$type]))
138
				{
139
					toggleMentionsVisibility($type, false);
140
					$modules_toggle['disable'][] = $type;
141
				}
142
			}
143
144
			// Then make visible what was hidden, but only if there is anything
145
			if (!empty($this->_req->post->notifications))
146
			{
147
				foreach ($this->_req->post->notifications as $type => $val)
148
				{
149
					if (!isset($current_settings[$type]))
150
					{
151
						toggleMentionsVisibility($type, true);
152
						$modules_toggle['enable'][] = $type;
153
					}
154
				}
155
156
				$enabled_mentions = array_keys($this->_req->post->notifications);
157
			}
158
159
			// Let's just keep it active, there are too many reasons it should be.
160
			require_once(SUBSDIR . '/ScheduledTasks.subs.php');
161
			toggleTaskStatusByName('user_access_mentions');
162
163
			// Disable or enable modules as needed
164
			foreach ($modules_toggle as $action => $toggles)
165
			{
166
				if (!empty($toggles))
167
				{
168
					// The modules associated with the notification (mentionmem, likes, etc.) area
169
					$modules = getMentionsModules($toggles);
170
171
					// The action will either be enabled to disable
172
					$function = $action . 'Modules';
173
174
					// Something like enableModule('mentions', array('post', 'display');
175
					foreach ($modules as $key => $val)
176
					{
177
						$function($key, $val);
178
					}
179
				}
180
			}
181
182
			updateSettings(['enabled_mentions' => implode(',', array_unique($enabled_mentions)), 'notification_methods' => $notification_methods]);
183
			$settingsForm->setConfigValues((array) $this->_req->post);
184
			$settingsForm->save();
185
			redirectexit('action=admin;area=featuresettings;sa=mentions');
186
		}
187
188
		// Prepare the settings for display
189
		$context['post_url'] = getUrl('admin', ['action' => 'admin', 'area' => 'featuresettings', 'sa' => 'mentions', 'save']);
190
		$settingsForm->prepare();
191
	}
192
193
	/**
194
	 * Return mentions settings.
195
	 *
196
	 * @event integrate_modify_mention_settings Adds to Configuration->Mentions
197
	 */
198
	private function _notificationsSettings()
199
	{
200
		global $txt, $modSettings;
201
202
		Txt::load('Profile+UserNotifications');
203
		loadJavascriptFile('ext/jquery.multiselect.min.js');
204
		theme()->addInlineJavascript('
205
			$(\'.select_multiple\').multiselect({\'language_strings\': {\'Select all\': ' . JavaScriptEscape($txt['notify_select_all']) . '}});
206
			document.addEventListener("DOMContentLoaded", function() {
207
                 prepareNotificationOptions();
208
			});', true);
209
		loadCSSFile('multiselect.css');
210
211
		// Mentions settings
212
		$config_vars = [
213
			['title', 'mentions_settings'],
214
			['check', 'mentions_enabled'],
215
		];
216
217
		$notification_methods = Notifications::instance()->getNotifiers();
218
		$notification_classes = getAvailableNotifications();
219
		$current_settings = unserialize($modSettings['notification_methods'], ['allowed_classes' => false]);
220
221
		foreach ($notification_classes as $class)
222
		{
223
			// The canUse can be set by each notifier based on conditions, default is true;
224
			/* @var $class AbstractNotificationMessage */
225
			if ($class::canUse() === false)
226
			{
227
				continue;
228
			}
229
230
			if ($class::hasHiddenInterface() === true)
231
			{
232
				$modSettings['hidden_notification_methods'][] = $class;
233
				continue;
234
			}
235
236
			// Set up config enable/disable setting for all notifications.
237
			$title = strtolower($class::getType());
238
			$config_vars[] = ['title', 'setting_' . $title];
239
			$config_vars[] = ['check', 'notifications[' . $title . '][enable]', 'text_label' => $txt['setting_notify_enable_this']];
240
			$modSettings['notifications[' . $title . '][enable]'] = !empty($current_settings[$title]);
241
			$default_values = [];
242
			$is_default = [];
243
244
			// If it is enabled, show all the available ways, like email, notify, weekly ...
245
			foreach (array_keys($notification_methods) as $method_name)
246
			{
247
				$method_name = strtolower($method_name);
248
249
				// Are they excluding any, like don't let mailfail be allowed to send email!
250
				if ($class::isNotAllowed($method_name))
251
				{
252
					continue;
253
				}
254
255
				$config_vars[] = ['check', 'notifications[' . $title . '][' . $method_name . ']', 'text_label' => $txt['notify_' . $method_name]];
256
				$modSettings['notifications[' . $title . '][' . $method_name . ']'] = !empty($current_settings[$title][$method_name]);
257
				$default_values[] = [$method_name, $txt['notify_' . $method_name]];
258
				if (empty($current_settings[$title][$method_name]))
259
				{
260
					continue;
261
				}
262
263
				if ((int) $current_settings[$title][$method_name] !== Notifications::DEFAULT_LEVEL)
264
				{
265
					continue;
266
				}
267
268
				$is_default[] = $method_name;
269
			}
270
271
			$config_vars[] = ['select', 'notifications[' . $title . '][default]', $default_values, 'text_label' => $txt['default_active'], 'multiple' => true, 'value' => $is_default];
272
			$modSettings['notifications[' . $title . '][default]'] = $is_default;
273
		}
274
275
		call_integration_hook('integrate_modify_mention_settings', [&$config_vars]);
276
277
		return $config_vars;
278
	}
279
280
	/**
281
	 * Public method to return the mention settings, used for admin search
282
	 */
283
	public function mentionSettings_search()
284
	{
285
		return $this->_notificationsSettings();
286
	}
287
}
288