forked from elkarte/Elkarte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManageDraftsModule.php
304 lines (260 loc) · 7.79 KB
/
ManageDraftsModule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
/**
* Allows for the modifying of the forum drafts settings.
*
* @package ElkArte Forum
* @copyright ElkArte Forum contributors
* @license BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
*
* @version 2.0 dev
*/
namespace ElkArte\AdminController;
use ElkArte\AbstractController;
use ElkArte\DraftsIntegrate;
use ElkArte\EventManager;
use ElkArte\Hooks;
use ElkArte\Languages\Txt;
use ElkArte\SettingsForm\SettingsForm;
use ElkArte\User;
use Exception;
/**
* Drafts administration controller.
* This class allows to modify admin drafts settings for the forum.
*
* @package Drafts
*/
class ManageDraftsModule extends AbstractController
{
/**
* Used to add the Drafts entry to the Core Features list.
*
* @param array $core_features The core features array
*/
public static function addCoreFeature(&$core_features)
{
$core_features['dr'] = array(
'url' => getUrl('admin', ['action' => 'admin', 'area' => 'managedrafts', '{session_data}']),
'settings' => array(
'drafts_enabled' => 1,
'drafts_post_enabled' => 2,
'drafts_pm_enabled' => 2,
'drafts_autosave_enabled' => 2,
'drafts_show_saved_enabled' => 2,
),
'setting_callback' => static function ($value) {
require_once(SUBSDIR . '/ScheduledTasks.subs.php');
toggleTaskStatusByName('remove_old_drafts', $value);
$modules = ['admin', 'post', 'display', 'profile', 'personalmessage', 'messageindex'];
// Enabling, let's register the modules and prepare the scheduled task
if ($value)
{
enableModules('drafts', $modules);
calculateNextTrigger('remove_old_drafts');
Hooks::instance()->enableIntegration(DraftsIntegrate::class);
}
// Disabling, just forget about the modules
else
{
disableModules('drafts', $modules);
Hooks::instance()->disableIntegration(DraftsIntegrate::class);
}
},
);
}
/**
* Integrate drafts in to the delete member chain
*
* @param int[] $users
* @throws Exception
*/
public static function integrate_delete_members($users)
{
$db = database();
// Delete any drafts...
$db->query('', '
DELETE FROM {db_prefix}user_drafts
WHERE id_member IN ({array_int:users})',
array(
'users' => $users,
)
);
}
/**
* Integrate draft permission in to the members and board permissions
*
* @param array $permissionGroups
* @param array $permissionList
*/
public static function integrate_load_permissions(&$permissionGroups, &$permissionList)
{
$permissionList['board'] += array(
'post_draft' => array(false, 'topic'),
'post_autosave_draft' => array(false, 'topic'),
);
$permissionList['membergroup'] += array(
'pm_draft' => array(false, 'pm'),
'pm_autosave_draft' => array(false, 'pm'),
);
}
/**
* Integrate draft permission in to illegal guest permissions
*/
public static function integrate_load_illegal_guest_permissions()
{
global $context;
$context['non_guest_permissions'] += array(
'post_draft',
'post_autosave_draft',
);
}
/**
* Integrate draft options in to the topics maintenance procedures
*
* @param array $topics_actions
*/
public static function integrate_topics_maintenance(&$topics_actions)
{
global $txt;
$topics_actions['olddrafts'] = array(
'url' => getUrl('admin', ['action' => 'admin', 'area' => 'maintain', 'sa' => 'topics', 'activity' => 'olddrafts']),
'title' => $txt['maintain_old_drafts'],
'submit' => $txt['maintain_old_remove'],
'confirm' => $txt['maintain_old_drafts_confirm'],
'hidden' => array(
'session_var' => 'session_id',
'admin-maint_token_var' => 'admin-maint_token',
)
);
}
/**
* Drafts maintenance integration hooks
*
* @param array $subActions
*/
public static function integrate_sa_manage_maintenance(&$subActions)
{
$subActions['topics']['activities']['olddrafts'] = static function () {
$controller = new ManageDraftsModule(new EventManager());
$controller->setUser(User::$info);
$controller->pre_dispatch();
$controller->action_olddrafts_display();
};
}
/**
* This method removes old drafts.
*/
public function action_olddrafts_display()
{
global $context, $txt;
validateToken('admin-maint');
require_once(SUBSDIR . '/Drafts.subs.php');
$drafts = getOldDrafts($this->_req->getPost('draftdays', 'intval', 0));
// If we have old drafts, remove them
if (count($drafts) > 0)
{
deleteDrafts($drafts, -1, false);
}
// Errors? no errors, only success !
$context['maintenance_finished'] = array(
'errors' => array(sprintf($txt['maintain_done'], $txt['maintain_old_drafts'])),
);
}
/**
* Default method.
* Requires admin_forum permissions
*
* @uses Drafts language file
*/
public function action_index()
{
isAllowedTo('admin_forum');
Txt::load('Drafts');
$this->action_draftSettings_display();
}
/**
* Modify any setting related to drafts.
*
* - Requires the admin_forum permission.
* - Accessed from ?action=admin;area=managedrafts
*
* @event integrate_save_drafts_settings
* @uses Admin template, edit_topic_settings sub-template.
*/
public function action_draftSettings_display()
{
global $context, $txt;
isAllowedTo('admin_forum');
Txt::load('Drafts');
// Initialize the form
$settingsForm = new SettingsForm(SettingsForm::DB_ADAPTER);
// Initialize it with our settings
$settingsForm->setConfigVars($this->_settings());
// Setup the template.
$context['page_title'] = $txt['managedrafts_settings'];
$context['sub_template'] = 'show_settings';
$context[$context['admin_menu_name']]['object']->prepareTabData([
'title' => 'drafts',
'description' => 'managedrafts_settings_description',
]);
// Saving them ?
if (isset($this->_req->query->save))
{
checkSession();
call_integration_hook('integrate_save_drafts_settings');
// Protect them from themselves.
$this->_req->post->drafts_autosave_frequency = min((int) $this->_req->post->drafts_autosave_frequency, 30);
$settingsForm->setConfigValues((array) $this->_req->post);
$settingsForm->save();
redirectexit('action=admin;area=managedrafts');
}
// Some javascript to enable / disable the frequency input box
theme()->addInlineJavascript('
var autosave = document.getElementById(\'drafts_autosave_enabled\');
createEventListener(autosave);
autosave.addEventListener(\'change\', toggle);
toggle();
function toggle()
{
var select_elem = document.getElementById(\'drafts_autosave_frequency\');
select_elem.disabled = !autosave.checked;
}', true);
// Final settings...
$context['post_url'] = getUrl('admin', ['action' => 'admin', 'area' => 'managedrafts', 'save']);
$context['settings_title'] = $txt['managedrafts_settings'];
// Prepare the settings...
$settingsForm->prepare();
}
/**
* Returns all admin drafts settings in config_vars format.
*
* @event integrate_modify_drafts_settings
*/
private function _settings()
{
global $txt;
Txt::load('Drafts');
// Here are all the draft settings, a bit lite for now, but we can add more :P
$config_vars = array(
// Draft settings ...
array('check', 'drafts_post_enabled'),
array('check', 'drafts_pm_enabled'),
array('int', 'drafts_keep_days', 'postinput' => $txt['days_word'], 'subtext' => $txt['drafts_keep_days_subnote']),
'',
array('check', 'drafts_autosave_enabled', 'subtext' => $txt['drafts_autosave_enabled_subnote']),
array('int', 'drafts_autosave_frequency', 'postinput' => $txt['manageposts_seconds'], 'subtext' => $txt['drafts_autosave_frequency_subnote']),
);
call_integration_hook('integrate_modify_drafts_settings', array(&$config_vars));
return $config_vars;
}
/**
* Return the form settings for use in admin search
*/
public function settings_search()
{
if (isModuleEnabled('drafts'))
{
return $this->_settings();
}
return ['check', 'dummy_drafts'];
}
}