ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilObjDashboardSettingsGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
29
36{
37 public const VIEW_MODE_SETTINGS = 'Settings';
38 public const VIEW_MODE_PRESENTATION = 'Presentation';
39 public const VIEW_MODE_SORTING = 'Sorting';
40 public const DASH_SORT_PREFIX = 'dash_sort_by_';
41 public const DASH_ENABLE_PREFIX = 'dash_enable_';
42
46 protected UIServices $ui;
49
50 public function __construct(
51 $a_data,
52 int $a_id,
53 bool $a_call_by_reference = true,
54 bool $a_prepare_output = true
55 ) {
56 global $DIC;
57
58 $this->lng = $DIC->language();
59 $this->rbac_system = $DIC->rbac()->system();
60 $this->access = $DIC->access();
61 $this->ctrl = $DIC->ctrl();
62 $this->settings = $DIC->settings();
63 $lng = $DIC->language();
64 $this->ui_factory = $DIC->ui()->factory();
65 $this->ui_renderer = $DIC->ui()->renderer();
66 $this->request = $DIC->http()->request();
67 $this->ui = $DIC->ui();
68 $this->style_gui = $DIC->contentStyle()->gui();
69
70 $this->type = 'dshs';
71 parent::__construct($a_data, $a_id, $a_call_by_reference, $a_prepare_output);
72
73 $lng->loadLanguageModule('dash');
74
75 $this->viewSettings = new ilPDSelectedItemsBlockViewSettings($DIC->user());
76
77 $this->side_panel_settings = new ilDashboardSidePanelSettingsRepository();
78 }
79
80 public function executeCommand(): void
81 {
82 $cmd = $this->ctrl->getCmd();
83
84 $this->prepareOutput();
85
86 if (!$this->rbac_system->checkAccess('visible,read', $this->object->getRefId())) {
87 throw new ilPermissionException($this->lng->txt('no_permission'));
88 }
89
90 switch ($this->ctrl->getNextClass($this)) {
91 case strtolower(ilPermissionGUI::class):
92 $this->tabs_gui->activateTab('perm_settings');
93 $perm_gui = new ilPermissionGUI($this);
94 $this->ctrl->forwardCommand($perm_gui);
95 break;
96 case strtolower(ilDashboardPageLanguageSelectGUI::class):
97 $this->tabs_gui->activateTab('dash_customization');
98 $this->ctrl->forwardCommand(new ilDashboardPageLanguageSelectGUI());
99 break;
100 case strtolower(ilObjectContentStyleSettingsGUI::class):
101 $this->tabs_gui->clearTargets();
102 $this->ctrl->setParameterByClass(ilDashboardPageGUI::class, 'dshs_lang', $this->request->getQueryParams()['dshs_lang']);
103 $this->tabs_gui->setBackTarget($this->lng->txt('back'), $this->ctrl->getLinkTargetByClass(
104 [ilDashboardPageLanguageSelectGUI::class, ilDashboardPageGUI::class],
105 'edit'
106 ));
107 $gui = $this->style_gui->objectSettingsGUIForRefId(null, $this->getRefId());
108 $this->ctrl->setParameter($gui, 'dshs_lang', $this->request->getQueryParams()['dshs_lang']);
109 $this->ctrl->forwardCommand($gui);
110 break;
111 default:
112 $this->tabs_gui->activateTab('settings');
113 if (!$cmd || $cmd === 'view') {
114 $cmd = 'editSettings';
115 }
116
117 $this->$cmd();
118 break;
119 }
120 }
121
122 public function getAdminTabs(): void
123 {
124 if ($this->rbac_system->checkAccess('visible,read', $this->object->getRefId())) {
125 $this->tabs_gui->addTarget(
126 'settings',
127 $this->ctrl->getLinkTarget($this, 'editSettings'),
128 ['editSettings', 'view']
129 );
130 $this->tabs_gui->addTarget(
131 'dash_customization',
132 $this->ctrl->getLinkTargetByClass(ilDashboardPageLanguageSelectGUI::class)
133 );
134 }
135
136 if ($this->rbac_system->checkAccess('edit_permission', $this->object->getRefId())) {
137 $this->tabs_gui->addTarget(
138 'perm_settings',
139 $this->ctrl->getLinkTargetByClass(ilPermissionGUI::class, 'perm'),
140 [],
141 ilPermissionGUI::class
142 );
143 }
144 }
145
146 public function editSettings(): void
147 {
148 if ($this->settings->get('rep_favourites', '0') !== '1') {
149 $content[] = $this->ui_factory->messageBox()->info($this->lng->txt('favourites_disabled_info'));
150 }
151
152 if ($this->settings->get('mmbr_my_crs_grp', '0') !== '1') {
153 $content[] = $this->ui_factory->messageBox()->info($this->lng->txt('memberships_disabled_info'));
154 }
155 $this->setSettingsSubTabs('general');
156 $table = new ilDashboardSortationTableGUI($this, 'editSettings', !$this->canWrite());
157 $this->tpl->setContent($table->getHTML());
158 }
159
160 public function editSorting(?StandardForm $form = null): void
161 {
162 $this->tabs_gui->activateTab('settings');
163 $this->setSettingsSubTabs('sorting');
164 $form = $form ?? $this->getViewForm(self::VIEW_MODE_SORTING);
165 $this->tpl->setContent($this->ui->renderer()->renderAsync($form));
166 }
167
168 public function getViewForm(string $mode): ?StandardForm
169 {
170 switch ($mode) {
173 return $this->ui_factory->input()->container()->form()->standard(
174 $this->ctrl->getFormAction($this, 'save' . $mode),
175 array_map(
176 fn(int $view): Section => $this->getViewByMode($mode, $view),
177 $this->viewSettings->getPresentationViews()
178 )
179 );
180 }
181 return null;
182 }
183
184 public function getViewSectionSorting(int $view, string $title): Section
185 {
186 if ($this->canWrite()) {
187 $this->tpl->addJavaScript("assets/js/SortationUserInputHandler.js");
188 }
189 $available = $this->viewSettings->getAvailableSortOptionsByView($view);
190 $options = array_combine($available, $available);
191 $select_options = array_map(fn($s) => $this->lng->txt(self::DASH_SORT_PREFIX . $s), $options);
192 $available_sorting = array_map($this->sortingInputs($view), $options);
193 $default_sorting = $this->ui_factory
194 ->input()
195 ->field()
196 ->select($this->lng->txt('dash_default_sortation'), $select_options)
197 ->withValue($this->viewSettings->getDefaultSortingByView($view))
198 ->withAdditionalOnLoadCode(
199 static fn(string $id) => "
200 document.getElementById('$id').setAttribute('data-select', 'sorting$view');
201 window.addEventListener('DOMContentLoaded', () => il.Dashboard.handleUserInputForSortationsByView($view));
202 "
203 );
204
205 return $this->ui_factory->input()->field()->section(
206 $this->maybeDisable([...$available_sorting, 'default_sorting' => $default_sorting]),
207 $title
208 );
209 }
210
211 public function getViewByMode(string $mode, int $view): Section
212 {
213 switch ($mode) {
215 return $this->getViewSectionSorting(
216 $view,
217 $this->lng->txt('dash_' . $this->viewSettings->getViewName($view))
218 );
220 default:
221 return $this->getViewSectionPresentation(
222 $view,
223 $this->lng->txt('dash_' . $this->viewSettings->getViewName($view))
224 );
225 }
226 }
227
228 public function saveSettings(): void
229 {
230 if ($this->canWrite()) {
231 $form_data = $this->request->getParsedBody();
232 foreach ($this->viewSettings->getPresentationViews() as $presentation_view) {
233 if (isset($form_data['main_panel']['enable'][$presentation_view])) {
234 $this->viewSettings->enableView(
235 $presentation_view,
236 (bool) $form_data['main_panel']['enable'][$presentation_view]
237 );
238 } elseif ($presentation_view !== ilPDSelectedItemsBlockConstants::VIEW_RECOMMENDED_CONTENT) {
239 $this->viewSettings->enableView($presentation_view, false);
240 }
241 }
242
243 $positions = $form_data['main_panel']['position'];
244 asort($positions);
245 $this->viewSettings->setViewPositions(array_keys($positions));
246
247 foreach ($this->side_panel_settings->getValidModules() as $mod) {
248 $this->side_panel_settings->enable($mod, (bool) ($form_data['side_panel']['enable'][$mod] ?? false));
249 }
250
251 $positions = $form_data['side_panel']['position'];
252 asort($positions);
253 $this->side_panel_settings->setPositions(array_keys($positions));
254
255 $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_SUCCESS, $this->lng->txt('settings_saved'), true);
256 } else {
257 $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_FAILURE, $this->lng->txt('no_permission'), true);
258 }
259 $this->editSettings();
260 }
261
262 public function setSettingsSubTabs(string $a_active): void
263 {
264 $tabs = $this->tabs_gui;
267
268 if ($this->rbac_system->checkAccess('visible,read', $this->object->getRefId())) {
269 $tabs->addSubTab(
270 'general',
271 $lng->txt('general_settings'),
272 $ctrl->getLinkTarget($this, 'editSettings')
273 );
274
275 $tabs->addSubTab(
276 'presentation',
277 $lng->txt('dash_presentation'),
278 $ctrl->getLinkTarget($this, 'editPresentation')
279 );
280
281 $tabs->addSubTab(
282 'sorting',
283 $lng->txt('dash_sortation'),
284 $ctrl->getLinkTarget($this, 'editSorting')
285 );
286 }
287
288 $tabs->activateSubTab($a_active);
289 }
290
291 public function editPresentation(): void
292 {
293 $this->tabs_gui->activateTab('settings');
294 $this->setSettingsSubTabs('presentation');
295
296 $form = $this->getViewForm(self::VIEW_MODE_PRESENTATION);
297
298 $this->tpl->setContent($this->ui->renderer()->renderAsync($form));
299 }
300
301 public function editCustomization(): void
302 {
303 $this->ui->mainTemplate()->setOnScreenMessage(
304 $this->ui->mainTemplate()::MESSAGE_TYPE_INFO,
305 $this->lng->txt('dash_page_edit_info'),
306 true
307 );
308
309 $this->tabs_gui->activateTab('dash_customization');
310
311 $content = $this->ui->renderer()->render(
312 $this->ui->factory()->button()->standard(
313 $this->lng->txt('customize_page'),
314 $this->ctrl->getLinkTargetByClass([self::class, ilDashboardPageLanguageSelectGUI::class], 'select')
315 )
316 );
317
318 if (ilDashboardPageGUI::isLanguageAvailable($this->user->getLanguage())) {
319 $content .= (new ilDashboardPageGUI($this->user->getLanguage()))->showPage();
320 } elseif (ilDashboardPageGUI::isLanguageAvailable($this->lng->getContentLanguage())) {
321 $content .= (new ilDashboardPageGUI($this->lng->getDefaultLanguage()))->showPage();
322 }
323
324 $this->tpl->setContent($content);
325 }
326
327 public function getViewSectionPresentation(int $view, string $title): Section
328 {
330 $ops = $this->viewSettings->getAvailablePresentationsByView($view);
331 $pres_options = array_column(
332 array_map(
333 static fn(int $k, string $v): array => [$v, $lng->txt('dash_' . $v)],
334 array_keys($ops),
335 $ops
336 ),
337 1,
338 0
339 );
340 $avail_pres = $this->ui_factory->input()->field()->multiSelect(
341 $lng->txt('dash_avail_presentation'),
342 $pres_options
343 )
344 ->withValue($this->viewSettings->getActivePresentationsByView($view));
345 $default_pres = $this->ui_factory->input()->field()->radio($lng->txt('dash_default_presentation'))
346 ->withOption('list', $lng->txt('dash_list'))
347 ->withOption('tile', $lng->txt('dash_tile'));
348 $default_pres = $default_pres->withValue($this->viewSettings->getDefaultPresentationByView($view));
349 return $this->ui_factory->input()->field()->section(
350 $this->maybeDisable(['avail_pres' => $avail_pres, 'default_pres' => $default_pres]),
351 $title
352 );
353 }
354
355 protected function savePresentation(): void
356 {
357 if ($this->canWrite()) {
358 $data = $this->getViewForm(self::VIEW_MODE_PRESENTATION)->withRequest($this->request)->getData();
359
360 foreach ($data as $view => $view_data) {
361 $this->viewSettings->storeViewPresentation(
362 $view,
363 $view_data['default_pres'],
364 $view_data['avail_pres'] ?? []
365 );
366 }
367 $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_SUCCESS, $this->lng->txt('msg_obj_modified'), true);
368 } else {
369 $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_FAILURE, $this->lng->txt('no_permission'), true);
370 }
371 $this->editPresentation();
372 }
373
374 public function saveSorting(): void
375 {
376 $form = null;
377 if ($this->canWrite()) {
378 $form = $this->getViewForm(self::VIEW_MODE_SORTING)->withRequest($this->request);
379 $data = $form->getData();
380 if ($data) {
381 foreach ($data as $view => $view_data) {
382 $avail_sorting = array_keys(array_filter($view_data));
383 $saved = array_filter($view_data, fn($x) => !is_bool($x));
384 if (isset($view_data['default_sorting'])) {
385 $this->viewSettings->storeViewSorting(
386 $view,
387 $view_data['default_sorting'],
388 $avail_sorting
389 );
390 $this->viewSettings->storeViewSortingOptions($view, $saved);
391 }
392 }
393 $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_SUCCESS, $this->lng->txt('msg_obj_modified'), true);
394 }
395 } else {
396 $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_FAILURE, $this->lng->txt('no_permission'), true);
397 }
398 $this->editSorting($form);
399 }
400
405 private function maybeDisable(array $fields): array
406 {
407 if ($this->canWrite()) {
408 return $fields;
409 }
410
411 return array_map(static fn(FormInput $field): FormInput => $field->withDisabled(true), $fields);
412 }
413
414 private function canWrite(): bool
415 {
416 return $this->rbac_system->checkAccess('write', $this->object->getRefId());
417 }
418
422 private function manuallySortingSettings(int $view, string $name, array $saved): Input
423 {
424 $options = $this->viewSettings->getSortingOptionsByView($view);
425 $sort_options = $this->viewSettings->getAvailableOptionsBySortation($name);
426 $value = isset($saved[$name]) ?
427 ['new_items' => ($options[$name]['new_items'] ?? null) ?: 'top'] :
428 null;
429
430 return $this->ui_factory->input()->field()->optionalGroup([
431 'new_items' => array_reduce(
432 $sort_options,
433 fn($r, $o) => $r->withOption($o, $this->lng->txt('dash_manual_new_item_pos_' . $o)),
434 $this->ui_factory->input()->field()->radio($this->lng->txt('dash_manual_new_item_pos'), '')
435 )
436 ], $this->lng->txt(self::DASH_SORT_PREFIX . $name))
437 ->withValue($value);
438 }
439
443 private function sortingCheckbox(int $view, string $name, array $saved): Input
444 {
445 return $this->ui_factory->input()->field()
446 ->checkbox($this->lng->txt(self::DASH_SORT_PREFIX . $name))
447 ->withAdditionalOnLoadCode(fn(string $id) => "
448 document.getElementById('$id').closest('form').addEventListener('submit', e => (
449 e.target.querySelectorAll('input[disabled]').forEach(x => {x.disabled = false;})
450 ));
451 ")
452 ->withValue(isset($saved[$name]));
453 }
454
455 private function sortingInputs(int $view): Closure
456 {
457 $saved = array_flip($this->viewSettings->getActiveSortingsByView($view));
458 $with_js = fn($name, $check) => $check->withAdditionalOnLoadCode(fn($id) => "
459 document.getElementById('$id').setAttribute('data-checkbox', 'activeSorting$view');
460 document.querySelector('#$id input').setAttribute('data-value', '$name')
461 ");
462
463 $special_settings = ['manually'];
464 $method_for = fn($name) => in_array($name, $special_settings, true) ?
465 $name . 'SortingSettings' :
466 'sortingCheckbox';
467
468 return fn(string $name) => $with_js($name, $this->{$method_for($name)}($view, $name, $saved));
469 }
470}
$check
Definition: buildRTE.php:81
Provides fluid interface to RBAC services.
Definition: UIServices.php:25
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Facade for consumer gui interface.
withDisabled(bool $is_disabled)
Get an input like this, but set it to a disabled state.static
Definition: FormInput.php:139
getLinkTarget(object $a_gui_obj, ?string $a_cmd=null, ?string $a_anchor=null, bool $is_async=false, bool $has_xml_style=false)
@inheritDoc
@ilCtrl_Calls ilDashboardPageGUI: ilPageEditorGUI, ilEditClipboardGUI, ilMDEditorGUI @ilCtrl_isCalled...
static isLanguageAvailable(string $lang)
@ilCtrl_isCalledBy ilDashboardPageLanguageSelectGUI: ilObjDashboardSettingsGUI
loadLanguageModule(string $a_module)
Load language module.
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
@ilCtrl_Calls ilObjDashboardSettingsGUI: ilPermissionGUI @ilCtrl_Calls ilObjDashboardSettingsGUI: ilD...
getViewSectionPresentation(int $view, string $title)
ilDashboardSidePanelSettingsRepository $side_panel_settings
manuallySortingSettings(int $view, string $name, array $saved)
ilPDSelectedItemsBlockViewSettings $viewSettings
getAdminTabs()
administration tabs show only permissions and trash folder
sortingCheckbox(int $view, string $name, array $saved)
__construct( $a_data, int $a_id, bool $a_call_by_reference=true, bool $a_prepare_output=true)
getViewSectionSorting(int $view, string $title)
Class ilObjectGUI Basic methods of all Output classes.
ilTabsGUI $tabs_gui
prepareOutput(bool $show_sub_objects=true)
ilLanguage $lng
This describes inputs that can be used in forms.
Definition: FormInput.php:33
This describes a standard form.
Definition: Standard.php:29
This describes section inputs.
Definition: Section.php:29
This describes commonalities between all inputs.
Definition: Input.php:47
withValue($value)
Get an input like this with another value displayed on the client side.
This is how the factory for UI elements looks.
Definition: Factory.php:38
An entity that renders components to a string output.
Definition: Renderer.php:31
__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
global $DIC
Definition: shib_login.php:26