Issues (1686)

ElkArte/AdminController/ManageDraftsModule.php (1 issue)

Severity
1
<?php
2
3
/**
4
 * Allows for the modifying of the forum drafts 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 dev
11
 */
12
13
namespace ElkArte\AdminController;
14
15
use ElkArte\AbstractController;
16
use ElkArte\DraftsIntegrate;
17
use ElkArte\EventManager;
18
use ElkArte\Hooks;
19
use ElkArte\Languages\Txt;
20
use ElkArte\SettingsForm\SettingsForm;
21
use ElkArte\User;
22
use Exception;
23
24
/**
25
 * Drafts administration controller.
26
 * This class allows to modify admin drafts settings for the forum.
27
 *
28
 * @package Drafts
29
 */
30
class ManageDraftsModule extends AbstractController
31
{
32
	/**
33
	 * Used to add the Drafts entry to the Core Features list.
34
	 *
35
	 * @param array $core_features The core features array
36
	 */
37
	public static function addCoreFeature(&$core_features)
38
	{
39
		$core_features['dr'] = array(
40
			'url' => getUrl('admin', ['action' => 'admin', 'area' => 'managedrafts', '{session_data}']),
41
			'settings' => array(
42
				'drafts_enabled' => 1,
43
				'drafts_post_enabled' => 2,
44
				'drafts_pm_enabled' => 2,
45
				'drafts_autosave_enabled' => 2,
46
				'drafts_show_saved_enabled' => 2,
47
			),
48
			'setting_callback' => static function ($value) {
49
				require_once(SUBSDIR . '/ScheduledTasks.subs.php');
50
				toggleTaskStatusByName('remove_old_drafts', $value);
51
				$modules = ['admin', 'post', 'display', 'profile', 'personalmessage', 'messageindex'];
52
53
				// Enabling, let's register the modules and prepare the scheduled task
54
				if ($value)
55
				{
56
					enableModules('drafts', $modules);
57
					calculateNextTrigger('remove_old_drafts');
58
					Hooks::instance()->enableIntegration(DraftsIntegrate::class);
59
				}
60
				// Disabling, just forget about the modules
61
				else
62
				{
63
					disableModules('drafts', $modules);
64
					Hooks::instance()->disableIntegration(DraftsIntegrate::class);
65
				}
66
			},
67
		);
68
	}
69
70
	/**
71
	 * Integrate drafts in to the delete member chain
72
	 *
73
	 * @param int[] $users
74
	 * @throws Exception
75
	 */
76
	public static function integrate_delete_members($users)
77
	{
78
		$db = database();
79
80
		// Delete any drafts...
81
		$db->query('', '
82
			DELETE FROM {db_prefix}user_drafts
83
			WHERE id_member IN ({array_int:users})',
84
			array(
85
				'users' => $users,
86
			)
87
		);
88
	}
89
90
	/**
91
	 * Integrate draft permission in to the members and board permissions
92
	 *
93
	 * @param array $permissionGroups
94
	 * @param array $permissionList
95
	 */
96
	public static function integrate_load_permissions(&$permissionGroups, &$permissionList)
0 ignored issues
show
The parameter $permissionGroups is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

96
	public static function integrate_load_permissions(/** @scrutinizer ignore-unused */ &$permissionGroups, &$permissionList)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
97
	{
98
		$permissionList['board'] += array(
99
			'post_draft' => array(false, 'topic'),
100
			'post_autosave_draft' => array(false, 'topic'),
101
		);
102
103
		$permissionList['membergroup'] += array(
104
			'pm_draft' => array(false, 'pm'),
105
			'pm_autosave_draft' => array(false, 'pm'),
106
		);
107
	}
108
109
	/**
110
	 * Integrate draft permission in to illegal guest permissions
111
	 */
112
	public static function integrate_load_illegal_guest_permissions()
113
	{
114
		global $context;
115
116
		$context['non_guest_permissions'] += array(
117
			'post_draft',
118
			'post_autosave_draft',
119
		);
120
	}
121
122
	/**
123
	 * Integrate draft options in to the topics maintenance procedures
124
	 *
125
	 * @param array $topics_actions
126
	 */
127
	public static function integrate_topics_maintenance(&$topics_actions)
128
	{
129
		global $txt;
130
131
		$topics_actions['olddrafts'] = array(
132
			'url' => getUrl('admin', ['action' => 'admin', 'area' => 'maintain', 'sa' => 'topics', 'activity' => 'olddrafts']),
133
			'title' => $txt['maintain_old_drafts'],
134
			'submit' => $txt['maintain_old_remove'],
135
			'confirm' => $txt['maintain_old_drafts_confirm'],
136
			'hidden' => array(
137
				'session_var' => 'session_id',
138
				'admin-maint_token_var' => 'admin-maint_token',
139
			)
140
		);
141
	}
142
143
	/**
144
	 * Drafts maintenance integration hooks
145
	 *
146
	 * @param array $subActions
147
	 */
148
	public static function integrate_sa_manage_maintenance(&$subActions)
149
	{
150
		$subActions['topics']['activities']['olddrafts'] = static function () {
151
			$controller = new ManageDraftsModule(new EventManager());
152
			$controller->setUser(User::$info);
153
			$controller->pre_dispatch();
154
			$controller->action_olddrafts_display();
155
		};
156
	}
157
158
	/**
159
	 * This method removes old drafts.
160
	 */
161
	public function action_olddrafts_display()
162
	{
163
		global $context, $txt;
164
165
		validateToken('admin-maint');
166
167
		require_once(SUBSDIR . '/Drafts.subs.php');
168
		$drafts = getOldDrafts($this->_req->getPost('draftdays', 'intval', 0));
169
170
		// If we have old drafts, remove them
171
		if (count($drafts) > 0)
172
		{
173
			deleteDrafts($drafts, -1, false);
174
		}
175
176
		// Errors?  no errors, only success !
177
		$context['maintenance_finished'] = array(
178
			'errors' => array(sprintf($txt['maintain_done'], $txt['maintain_old_drafts'])),
179
		);
180
	}
181
182
	/**
183
	 * Default method.
184
	 * Requires admin_forum permissions
185
	 *
186
	 * @uses Drafts language file
187
	 */
188
	public function action_index()
189
	{
190
		isAllowedTo('admin_forum');
191
		Txt::load('Drafts');
192
193
		$this->action_draftSettings_display();
194
	}
195
196
	/**
197
	 * Modify any setting related to drafts.
198
	 *
199
	 * - Requires the admin_forum permission.
200
	 * - Accessed from ?action=admin;area=managedrafts
201
	 *
202
	 * @event integrate_save_drafts_settings
203
	 * @uses Admin template, edit_topic_settings sub-template.
204
	 */
205
	public function action_draftSettings_display()
206
	{
207
		global $context, $txt;
208
209
		isAllowedTo('admin_forum');
210
		Txt::load('Drafts');
211
212
		// Initialize the form
213
		$settingsForm = new SettingsForm(SettingsForm::DB_ADAPTER);
214
215
		// Initialize it with our settings
216
		$settingsForm->setConfigVars($this->_settings());
217
218
		// Setup the template.
219
		$context['page_title'] = $txt['managedrafts_settings'];
220
		$context['sub_template'] = 'show_settings';
221
222
		$context[$context['admin_menu_name']]['object']->prepareTabData([
223
			'title' => 'drafts',
224
			'description' => 'managedrafts_settings_description',
225
		]);
226
227
		// Saving them ?
228
		if (isset($this->_req->query->save))
229
		{
230
			checkSession();
231
232
			call_integration_hook('integrate_save_drafts_settings');
233
234
			// Protect them from themselves.
235
			$this->_req->post->drafts_autosave_frequency = min((int) $this->_req->post->drafts_autosave_frequency, 30);
236
237
			$settingsForm->setConfigValues((array) $this->_req->post);
238
			$settingsForm->save();
239
			redirectexit('action=admin;area=managedrafts');
240
		}
241
242
		// Some javascript to enable / disable the frequency input box
243
		theme()->addInlineJavascript('
244
			var autosave = document.getElementById(\'drafts_autosave_enabled\');
245
246
			createEventListener(autosave);
247
			autosave.addEventListener(\'change\', toggle);
248
			toggle();
249
250
			function toggle()
251
			{
252
				var select_elem = document.getElementById(\'drafts_autosave_frequency\');
253
254
				select_elem.disabled = !autosave.checked;
255
			}', true);
256
257
		// Final settings...
258
		$context['post_url'] = getUrl('admin', ['action' => 'admin', 'area' => 'managedrafts', 'save']);
259
		$context['settings_title'] = $txt['managedrafts_settings'];
260
261
		// Prepare the settings...
262
		$settingsForm->prepare();
263
	}
264
265
	/**
266
	 * Returns all admin drafts settings in config_vars format.
267
	 *
268
	 * @event integrate_modify_drafts_settings
269
	 */
270
	private function _settings()
271
	{
272
		global $txt;
273
274
		Txt::load('Drafts');
275
276
		// Here are all the draft settings, a bit lite for now, but we can add more :P
277
		$config_vars = array(
278
			// Draft settings ...
279
			array('check', 'drafts_post_enabled'),
280
			array('check', 'drafts_pm_enabled'),
281
			array('int', 'drafts_keep_days', 'postinput' => $txt['days_word'], 'subtext' => $txt['drafts_keep_days_subnote']),
282
			'',
283
			array('check', 'drafts_autosave_enabled', 'subtext' => $txt['drafts_autosave_enabled_subnote']),
284
			array('int', 'drafts_autosave_frequency', 'postinput' => $txt['manageposts_seconds'], 'subtext' => $txt['drafts_autosave_frequency_subnote']),
285
		);
286
287
		call_integration_hook('integrate_modify_drafts_settings', array(&$config_vars));
288
289
		return $config_vars;
290
	}
291
292
	/**
293
	 * Return the form settings for use in admin search
294
	 */
295
	public function settings_search()
296
	{
297
		if (isModuleEnabled('drafts'))
298
		{
299
			return $this->_settings();
300
		}
301
302
		return ['check', 'dummy_drafts'];
303
	}
304
}
305