ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilObjDashboardSettingsGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 
33 {
34  public const VIEW_MODE_SETTINGS = 'Settings';
35  public const VIEW_MODE_PRESENTATION = 'Presentation';
36  public const VIEW_MODE_SORTING = 'Sorting';
37  public const DASH_SORT_PREFIX = 'dash_sort_by_';
38  public const DASH_ENABLE_PREFIX = 'dash_enable_';
39 
40  protected Factory $ui_factory;
43  protected UIServices $ui;
45 
46  public function __construct(
47  $a_data,
48  int $a_id,
49  bool $a_call_by_reference = true,
50  bool $a_prepare_output = true
51  ) {
52  global $DIC;
53 
54  $this->lng = $DIC->language();
55  $this->rbac_system = $DIC->rbac()->system();
56  $this->access = $DIC->access();
57  $this->ctrl = $DIC->ctrl();
58  $this->settings = $DIC->settings();
59  $lng = $DIC->language();
60  $this->ui_factory = $DIC->ui()->factory();
61  $this->ui_renderer = $DIC->ui()->renderer();
62  $this->request = $DIC->http()->request();
63  $this->ui = $DIC->ui();
64 
65  $this->type = 'dshs';
66  parent::__construct($a_data, $a_id, $a_call_by_reference, $a_prepare_output);
67 
68  $lng->loadLanguageModule('dash');
69 
70  $this->viewSettings = new ilPDSelectedItemsBlockViewSettings($DIC->user());
71 
72  $this->side_panel_settings = new ilDashboardSidePanelSettingsRepository();
73  }
74 
75  public function executeCommand(): void
76  {
77  $cmd = $this->ctrl->getCmd();
78 
79  $this->prepareOutput();
80 
81  if (!$this->rbac_system->checkAccess('visible,read', $this->object->getRefId())) {
82  throw new ilPermissionException($this->lng->txt('no_permission'));
83  }
84 
85  switch ($this->ctrl->getNextClass($this)) {
86  case strtolower(ilPermissionGUI::class):
87  $this->tabs_gui->setTabActive('perm_settings');
88  $perm_gui = new ilPermissionGUI($this);
89  $this->ctrl->forwardCommand($perm_gui);
90  break;
91 
92  default:
93  if (!$cmd || $cmd === 'view') {
94  $cmd = 'editSettings';
95  }
96 
97  $this->$cmd();
98  break;
99  }
100  }
101 
102  public function getAdminTabs(): void
103  {
104  if ($this->rbac_system->checkAccess('visible,read', $this->object->getRefId())) {
105  $this->tabs_gui->addTarget(
106  'settings',
107  $this->ctrl->getLinkTarget($this, 'editSettings'),
108  ['editSettings', 'view']
109  );
110  }
111 
112  if ($this->rbac_system->checkAccess('edit_permission', $this->object->getRefId())) {
113  $this->tabs_gui->addTarget(
114  'perm_settings',
115  $this->ctrl->getLinkTargetByClass(ilPermissionGUI::class, 'perm'),
116  [],
117  ilPermissionGUI::class
118  );
119  }
120  }
121 
122  public function editSettings(): void
123  {
124  if ($this->settings->get('rep_favourites', '0') !== '1') {
125  $content[] = $this->ui_factory->messageBox()->info($this->lng->txt('favourites_disabled_info'));
126  }
127 
128  if ($this->settings->get('mmbr_my_crs_grp', '0') !== '1') {
129  $content[] = $this->ui_factory->messageBox()->info($this->lng->txt('memberships_disabled_info'));
130  }
131  $this->setSettingsSubTabs('general');
132  $table = new ilDashboardSortationTableGUI($this, 'editSettings', !$this->canWrite());
133  $this->tpl->setContent($table->getHTML());
134  }
135 
136  public function editSorting(): void
137  {
138  $this->tabs_gui->activateTab('settings');
139  $this->setSettingsSubTabs('sorting');
140  $form = $this->getViewForm(self::VIEW_MODE_SORTING);
141  $this->tpl->setContent($this->ui->renderer()->renderAsync($form));
142  }
143 
144  public function getViewForm(string $mode): ?StandardForm
145  {
146  switch ($mode) {
147  case self::VIEW_MODE_PRESENTATION:
148  case self::VIEW_MODE_SORTING:
149  return $this->ui_factory->input()->container()->form()->standard(
150  $this->ctrl->getFormAction($this, 'save' . $mode),
151  array_map(
152  fn(int $view): Section => $this->getViewByMode($mode, $view),
153  $this->viewSettings->getPresentationViews()
154  )
155  );
156  }
157  return null;
158  }
159 
160  public function getViewSectionSorting(int $view, string $title): Section
161  {
162  if ($this->canWrite()) {
163  $this->tpl->addJavaScript('Services/Dashboard/Administration/js/SortationUserInputHandler.js');
164  }
165  $lng = $this->lng;
166  $availabe_sort_options = $this->viewSettings->getAvailableSortOptionsByView($view);
167  $options = array_reduce(
168  $availabe_sort_options,
169  static function (array $options, string $option) use ($lng): array {
170  $options[$option] = $lng->txt(self::DASH_SORT_PREFIX . $option);
171  return $options;
172  },
173  []
174  );
175 
176  $available_sorting = $this->ui_factory
177  ->input()
178  ->field()
179  ->multiSelect($this->lng->txt('dash_avail_sortation'), $options)
180  ->withValue(
181  $this->viewSettings->getActiveSortingsByView($view)
182  )
184  static fn(string $id) => "document.getElementById('$id').setAttribute('data-checkbox', 'activeSorting$view');
185  document.addEventListener('DOMContentLoaded', function () {
186  il.Dashboard.handleUserInputForSortationsByView($view);
187  });"
188  );
189  $default_sorting = $this->ui_factory
190  ->input()
191  ->field()
192  ->select($this->lng->txt('dash_default_sortation'), $options)
193  ->withValue($this->viewSettings->getDefaultSortingByView($view))
194  ->withRequired(true)
195  ->withAdditionalOnLoadCode(
196  static fn(string $id) => "document.getElementById('$id').setAttribute('data-select', 'sorting$view');"
197  );
198  return $this->ui_factory->input()->field()->section(
199  $this->maybeDisable(['avail_sorting' => $available_sorting, 'default_sorting' => $default_sorting]),
200  $title
201  );
202  }
203 
204  public function getViewByMode(string $mode, int $view): Section
205  {
206  switch ($mode) {
207  case self::VIEW_MODE_SORTING:
208  return $this->getViewSectionSorting(
209  $view,
210  $this->lng->txt('dash_' . $this->viewSettings->getViewName($view))
211  );
212  case self::VIEW_MODE_PRESENTATION:
213  default:
214  return $this->getViewSectionPresentation(
215  $view,
216  $this->lng->txt('dash_' . $this->viewSettings->getViewName($view))
217  );
218  }
219  }
220 
221  public function saveSettings(): void
222  {
223  if ($this->canWrite()) {
224  $form_data = $this->request->getParsedBody();
225  foreach ($this->viewSettings->getPresentationViews() as $presentation_view) {
226  if (isset($form_data['main_panel']['enable'][$presentation_view])) {
227  $this->viewSettings->enableView(
228  $presentation_view,
229  (bool) $form_data['main_panel']['enable'][$presentation_view]
230  );
231  } elseif ($presentation_view !== ilPDSelectedItemsBlockConstants::VIEW_RECOMMENDED_CONTENT) {
232  $this->viewSettings->enableView($presentation_view, false);
233  }
234  }
235 
236  $positions = $form_data['main_panel']['position'];
237  asort($positions);
238  $this->viewSettings->setViewPositions(array_keys($positions));
239 
240  foreach ($this->side_panel_settings->getValidModules() as $mod) {
241  $this->side_panel_settings->enable($mod, (bool) ($form_data['side_panel']['enable'][$mod] ?? false));
242  }
243 
244  $positions = $form_data['side_panel']['position'];
245  asort($positions);
246  $this->side_panel_settings->setPositions(array_keys($positions));
247 
248  $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_SUCCESS, $this->lng->txt('settings_saved'), true);
249  } else {
250  $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_FAILURE, $this->lng->txt('no_permission'), true);
251  }
252  $this->editSettings();
253  }
254 
255  public function setSettingsSubTabs(string $a_active): void
256  {
257  $tabs = $this->tabs_gui;
258  $ctrl = $this->ctrl;
259  $lng = $this->lng;
260 
261  if ($this->rbac_system->checkAccess('visible,read', $this->object->getRefId())) {
262  $tabs->addSubTab(
263  'general',
264  $lng->txt('general_settings'),
265  $ctrl->getLinkTarget($this, 'editSettings')
266  );
267 
268  $tabs->addSubTab(
269  'presentation',
270  $lng->txt('dash_presentation'),
271  $ctrl->getLinkTarget($this, 'editPresentation')
272  );
273 
274  $tabs->addSubTab(
275  'sorting',
276  $lng->txt('dash_sortation'),
277  $ctrl->getLinkTarget($this, 'editSorting')
278  );
279  }
280 
281  $tabs->activateSubTab($a_active);
282  }
283 
284  public function editPresentation(): void
285  {
286  $this->tabs_gui->activateTab('settings');
287  $this->setSettingsSubTabs('presentation');
288 
289  $form = $this->getViewForm(self::VIEW_MODE_PRESENTATION);
290 
291  $this->tpl->setContent($this->ui->renderer()->renderAsync($form));
292  }
293 
294  public function getViewSectionPresentation(int $view, string $title): Section
295  {
296  $lng = $this->lng;
297  $ops = $this->viewSettings->getAvailablePresentationsByView($view);
298  $pres_options = array_column(
299  array_map(
300  static fn(int $k, string $v): array => [$v, $lng->txt('dash_' . $v)],
301  array_keys($ops),
302  $ops
303  ),
304  1,
305  0
306  );
307  $avail_pres = $this->ui_factory->input()->field()->multiSelect(
308  $lng->txt('dash_avail_presentation'),
309  $pres_options
310  )
311  ->withValue($this->viewSettings->getActivePresentationsByView($view));
312  $default_pres = $this->ui_factory->input()->field()->radio($lng->txt('dash_default_presentation'))
313  ->withOption('list', $lng->txt('dash_list'))
314  ->withOption('tile', $lng->txt('dash_tile'));
315  $default_pres = $default_pres->withValue($this->viewSettings->getDefaultPresentationByView($view));
316  return $this->ui_factory->input()->field()->section(
317  $this->maybeDisable(['avail_pres' => $avail_pres, 'default_pres' => $default_pres]),
318  $title
319  );
320  }
321 
322  protected function savePresentation(): void
323  {
324  if ($this->canWrite()) {
325  $data = $this->getViewForm(self::VIEW_MODE_PRESENTATION)->withRequest($this->request)->getData();
326 
327  foreach ($data as $view => $view_data) {
328  $this->viewSettings->storeViewPresentation(
329  $view,
330  $view_data['default_pres'],
331  $view_data['avail_pres'] ?? []
332  );
333  }
334  $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_SUCCESS, $this->lng->txt('msg_obj_modified'), true);
335  } else {
336  $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_FAILURE, $this->lng->txt('no_permission'), true);
337  }
338  $this->editPresentation();
339  }
340 
341  public function saveSorting(): void
342  {
343  if ($this->canWrite()) {
344  $data = $this->getViewForm(self::VIEW_MODE_SORTING)->withRequest($this->request)->getData();
345 
346  foreach ($data as $view => $view_data) {
347  if (isset($view_data['default_sorting'])) {
348  if (!is_array($view_data['avail_sorting'] ?? null)) {
349  $view_data['avail_sorting'] = [$view_data['default_sorting']];
350  }
351  $this->viewSettings->storeViewSorting(
352  $view,
353  $view_data['default_sorting'],
354  $view_data['avail_sorting']
355  );
356  }
357  }
358  $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_SUCCESS, $this->lng->txt('msg_obj_modified'), true);
359  } else {
360  $this->tpl->setOnScreenMessage($this->tpl::MESSAGE_TYPE_FAILURE, $this->lng->txt('no_permission'), true);
361  }
362  $this->editSorting();
363  }
364 
369  private function maybeDisable(array $fields): array
370  {
371  if ($this->canWrite()) {
372  return $fields;
373  }
374 
375  return array_map(static fn(FormInput $field): FormInput => $field->withDisabled(true), $fields);
376  }
377 
378  private function canWrite(): bool
379  {
380  return $this->rbac_system->checkAccess('write', $this->object->getRefId());
381  }
382 }
This describes section inputs.
Definition: Section.php:28
An entity that renders components to a string output.
Definition: Renderer.php:30
ilTabsGUI $tabs_gui
ilDashboardSidePanelSettingsRepository $side_panel_settings
ilPDSelectedItemsBlockViewSettings $viewSettings
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...
prepareOutput(bool $show_sub_objects=true)
loadLanguageModule(string $a_module)
Load language module.
__construct( $a_data, int $a_id, bool $a_call_by_reference=true, bool $a_prepare_output=true)
getViewSectionPresentation(int $view, string $title)
global $DIC
Definition: feed.php:28
ilLanguage $lng
Provides fluid interface to RBAC services.
Definition: UIServices.php:23
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(VocabulariesInterface $vocabularies)
Class ilObjectGUI Basic methods of all Output classes.
ilObjDashboardSettingsGUI: ilPermissionGUI ilObjDashboardSettingsGUI: ilAdministrationGUI ...
withDisabled(bool $is_disabled)
Get an input like this, but set it to a disabled state.static
Definition: FormInput.php:139
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:58
getLinkTarget(object $a_gui_obj, string $a_cmd=null, string $a_anchor=null, bool $is_async=false, bool $has_xml_style=false)
getViewSectionSorting(int $view, string $title)
New PermissionGUI (extends from old ilPermission2GUI) RBAC related output.