ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilObjForumAdministrationGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25
31{
32 private const string PROP_SECTION_DEFAULTS = 'defaults';
33 private const string PROP_SECTION_FEATURES = 'features';
34 private const string PROP_SECTION_NOTIFICATIONS = 'notifications';
35 private const string PROP_SECTION_DRAFTS = 'drafts';
36
37 private readonly JobManager $cronManager;
38 private readonly UIServices $ui;
39
40 public function __construct($a_data, int $a_id, bool $a_call_by_reference = true, bool $a_prepare_output = true)
41 {
45 global $DIC;
46
47 $this->cronManager = $DIC->cron()->manager();
48 $this->ui = $DIC->ui();
49
50 $this->type = 'frma';
51 parent::__construct($a_data, $a_id, $a_call_by_reference, $a_prepare_output);
52 $this->lng->loadLanguageModule('forum');
53 $this->lng->loadLanguageModule('form');
54 }
55
56 public function executeCommand(): void
57 {
58 if (!$this->rbac_system->checkAccess('visible,read', $this->object->getRefId())) {
59 $this->error->raiseError($this->lng->txt('no_permission'), $this->error->WARNING);
60 }
61
62 $next_class = $this->ctrl->getNextClass($this);
63 $cmd = $this->ctrl->getCmd();
64
65 $this->prepareOutput();
66
67 switch ($next_class) {
68 case 'ilpermissiongui':
69 $this->tabs_gui->setTabActive('perm_settings');
70 $this->ctrl->forwardCommand(new ilPermissionGUI($this));
71 break;
72
73 default:
74 if (!$cmd || $cmd === 'view') {
75 $cmd = 'editSettings';
76 }
77
78 $this->$cmd();
79 break;
80 }
81 }
82
83 public function getAdminTabs(): void
84 {
85 if ($this->rbac_system->checkAccess('visible,read', $this->object->getRefId())) {
86 $this->tabs_gui->addTarget(
87 'settings',
88 $this->ctrl->getLinkTarget($this, 'editSettings'),
89 ['editSettings', 'view']
90 );
91 }
92
93 if ($this->rbac_system->checkAccess('edit_permission', $this->object->getRefId())) {
94 $this->tabs_gui->addTarget(
95 'perm_settings',
96 $this->ctrl->getLinkTargetByClass(ilPermissionGUI::class, 'perm'),
97 [],
98 'ilpermissiongui'
99 );
100 }
101 }
102
103 public function editSettings(?Form $form = null): void
104 {
105 $this->tabs_gui->activateTab('settings');
106
107 $this->tpl->setContent($this->render([
108 $this->cronMessage(),
109 $form ?? $this->settingsForm()
110 ]));
111 }
112
113 private function render($x): string
114 {
115 return $this->ui->renderer()->render($x);
116 }
117
118 public function saveSettings(): void
119 {
120 if (!$this->rbac_system->checkAccess('write', $this->object->getRefId())) {
121 $this->error->raiseError($this->lng->txt('no_permission'), $this->error->WARNING);
122 }
123
124 $form = $this->settingsForm()->withRequest($this->request);
125 $data = $form->getData();
126 if ($data === null || $this->request->getMethod() !== 'POST') {
127 $this->editSettings($form);
128 return;
129 }
130
131 $set_int = fn(string $field, string $section) => $this->settings->set(
132 $field,
133 (string) ((int) $data[$section][$field])
134 );
135
136 $data[self::PROP_SECTION_NOTIFICATIONS]['forum_notification'] = (
137 $data[self::PROP_SECTION_NOTIFICATIONS]['forum_notification'] || $this->forumJobActive()
138 );
139
140 array_map($set_int, [
141 'forum_default_view',
142 'forum_enable_print',
143 'enable_fora_statistics',
144 'enable_anonymous_fora',
145 'file_upload_allowed_fora',
146 'forum_notification',
147 'send_attachments_by_mail',
148 'save_post_drafts',
149 ], [
150 self::PROP_SECTION_DEFAULTS,
151 self::PROP_SECTION_FEATURES,
152 self::PROP_SECTION_FEATURES,
153 self::PROP_SECTION_FEATURES,
154 self::PROP_SECTION_FEATURES,
155 self::PROP_SECTION_NOTIFICATIONS,
156 self::PROP_SECTION_NOTIFICATIONS,
157 self::PROP_SECTION_DRAFTS
158 ]);
159
160 $drafts = $data[self::PROP_SECTION_DRAFTS]['autosave_drafts'] !== null;
161 $this->settings->set('autosave_drafts', (string) ((int) $drafts));
162 if ($drafts) {
163 $this->settings->set(
164 'autosave_drafts_ival',
165 (string) ((int) $data[self::PROP_SECTION_DRAFTS]['autosave_drafts']['ival'])
166 );
167 }
168
169 $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'), true);
170 $this->ctrl->redirect($this, 'editSettings');
171 }
172
173 protected function settingsForm(): Form
174 {
175 $may_write = $this->rbac_system->checkAccess('write', $this->object->getRefId());
176
177 $field = $this->ui->factory()->input()->field();
178
179 $section = fn(string $label, array $inputs): \ILIAS\UI\Component\Input\Field\Section => $field->section(
180 $inputs,
181 $this->lng->txt($label)
182 );
183 $checkbox = fn(string $label): \ILIAS\UI\Component\Input\Field\Checkbox => $field->checkbox(
184 $this->lng->txt($label),
185 $this->lng->txt($label . '_desc')
186 );
187 $by_date_with_additional_info = fn(string $label): string => sprintf(
188 '%s (%s)',
189 $this->lng->txt('sort_by_date'),
190 $this->lng->txt($label)
191 );
192 $to_string = static fn($value): string => (string) $value;
193 $radio_with_options = static fn(
194 \ILIAS\UI\Component\Input\Field\Radio $x,
195 array $options
196 ): \ILIAS\UI\Component\Input\Field\Radio => array_reduce(
197 $options,
198 static fn($field, array $option) => $field->withOption(...array_map($to_string, $option)),
199 $x
200 );
201 $checkbox_with_func = function (string $name, ?string $label = null, $f = null) use (
202 $checkbox
204 $f ??= static fn($x) => $x;
205 return $f($checkbox($label ?? $name)->withValue((bool) $this->settings->get($name)));
206 };
207 $disable_if_no_permission = $may_write ? static fn(
208 array $fields
209 ): array => $fields : static fn(
210 array $fields
211 ): array => array_map(
212 static fn($x) => $x->withDisabled(true),
213 $fields
214 );
215
216 $action = $this->ctrl->getFormAction($this, 'saveSettings');
217 if (!$may_write) {
218 $action = $this->ctrl->getFormAction($this, 'editSettings');
219 }
220
221 $form = $this->ui->factory()->input()->container()->form()->standard(
222 $action,
223 [
224 self::PROP_SECTION_DEFAULTS => $section('frm_adm_sec_default_settings', $disable_if_no_permission([
225 'forum_default_view' => $radio_with_options($field->radio($this->lng->txt('frm_default_view')), [
226 [
228 $this->lng->txt('sort_by_posts'),
229 $this->lng->txt('sort_by_posts_desc')
230 ],
231 [
233 $by_date_with_additional_info('ascending_order'),
234 $this->lng->txt('sort_by_date_desc')
235 ],
236 [
238 $by_date_with_additional_info('descending_order'),
239 $this->lng->txt('sort_by_date_desc')
240 ],
241 ])->withValue($this->settings->get('forum_default_view', (string) ilForumProperties::VIEW_DATE_ASC))
242 ])),
243 self::PROP_SECTION_FEATURES => $section('frm_adm_sec_features', $disable_if_no_permission([
244 'forum_enable_print' => $checkbox_with_func('forum_enable_print', 'frm_enable_print_option'),
245 'enable_fora_statistics' => $checkbox_with_func('enable_fora_statistics'),
246 'enable_anonymous_fora' => $checkbox_with_func('enable_anonymous_fora'),
247 'file_upload_allowed_fora' => $radio_with_options(
248 $field->radio($this->lng->txt('file_upload_allowed_fora')),
249 [
250 [
252 $this->lng->txt('file_upload_option_allow'),
253 $this->lng->txt('file_upload_option_allow_info')
254 ],
255 [
257 $this->lng->txt('file_upload_option_disallow'),
258 $this->lng->txt('file_upload_allowed_fora_desc')
259 ],
260 ]
261 )->withValue(
262 $this->settings->get(
263 'file_upload_allowed_fora',
265 )
266 )
267 ])),
268 self::PROP_SECTION_NOTIFICATIONS => $section('frm_adm_sec_notifications', $disable_if_no_permission([
269 'forum_notification' => $checkbox_with_func(
270 'forum_notification',
271 'forums_forum_notification',
272 fn($field) => (
273 $field
274 ->withDisabled($this->forumJobActive())
275 ->withValue($field->getValue() || $this->forumJobActive())
276 ->withByLine($this->forumByLine($field))
277 )
278 ),
279 'send_attachments_by_mail' => $checkbox_with_func(
280 'send_attachments_by_mail',
281 'enable_send_attachments'
282 )
283 ])),
284 self::PROP_SECTION_DRAFTS => $section('frm_adm_sec_drafts', $disable_if_no_permission([
285 'save_post_drafts' => $checkbox_with_func('save_post_drafts', 'adm_save_drafts'),
286 'autosave_drafts' => $field->optionalGroup([
287 'ival' => $field
288 ->numeric($this->lng->txt('adm_autosave_ival'))
289 ->withRequired(true)
290 ->withAdditionalTransformation(
291 $this->refinery->in()->series([
292 $this->refinery->int()->isGreaterThanOrEqual(30),
293 $this->refinery->int()->isLessThanOrEqual(60 * 60)
294 ])
295 )
296 ], $this->lng->txt('adm_autosave_drafts'), $this->lng->txt('adm_autosave_drafts_desc'))->withValue(
297 $this->settings->get('autosave_drafts') ? [
298 'ival' => $this->settings->get(
299 'autosave_drafts_ival',
300 '30'
301 )
302 ] : null
303 )
304 ]))
305 ]
306 );
307
308 if (!$may_write) {
309 $form = $form->withSubmitLabel($this->lng->txt('refresh'));
310 }
311
312 return $form;
313 }
314
315 public function addToExternalSettingsForm(int $a_form_id): array
316 {
318 $fields = [
319 'enable_fora_statistics' => [
320 (bool) $this->settings->get('enable_fora_statistics', '0'),
322 ],
323 'enable_anonymous_fora' => [
324 (bool) $this->settings->get('enable_anonymous_fora', '0'),
326 ]
327 ];
328 return [['editSettings', $fields]];
329 }
330
331 return [];
332 }
333
334 private function cronMessage(): Component
335 {
336 $gui = new ilCronManagerGUI();
337 $data = $gui->addToExternalSettingsForm(ilAdministrationSettingsFormHandler::FORM_FORUM);
338 $data = $data['cron_jobs'][1];
339
340 $url = $this->ctrl->getLinkTargetByClass(
341 [ilAdministrationGUI::class, ilObjSystemFolderGUI::class],
342 'jumpToCronJobs'
343 );
344
345 return $this->ui->factory()->messageBox()->info($this->lng->txt(key($data)) . ': ' . current($data))->withLinks(
346 [
347 $this->ui->factory()->link()->standard($this->lng->txt('adm_external_setting_edit'), $url)
348 ]
349 );
350 }
351
352 private function forumJobActive(): bool
353 {
354 return $this->cronManager->isJobActive('frm_notification');
355 }
356
357 private function forumByLine(Component $component): string
358 {
359 return $this->forumJobActive() ?
360 sprintf('%s<br/>%s', $component->getByLine(), $this->lng->txt('cron_forum_notification_disabled')) :
361 $component->getByLine();
362 }
363}
Provides fluid interface to RBAC services.
Definition: UIServices.php:25
error(string $a_errmsg)
@ilCtrl_Calls ilCronManagerGUI: ilPropertyFormGUI @ilCtrl_isCalledBy ilCronManagerGUI: ilAdministrati...
@ilCtrl_Calls ilObjForumAdministrationGUI: ilPermissionGUI @ilCtrl_IsCalledBy ilObjForumAdministratio...
getAdminTabs()
administration tabs show only permissions and trash folder
Class ilObjectGUI Basic methods of all Output classes.
prepareOutput(bool $show_sub_objects=true)
A component is the most general form of an entity in the UI.
Definition: Component.php:28
This describes commonalities between all forms.
Definition: Form.php:33
This describes checkbox inputs.
Definition: Checkbox.php:29
This is what a radio-input looks like.
Definition: Radio.php:29
This describes section inputs.
Definition: Section.php:29
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:61
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $DIC
Definition: shib_login.php:26
$url
Definition: shib_logout.php:68