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