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