ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilObjContentPageAdministrationGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
28 
33 {
34  private const string CMD_VIEW = 'view';
35  private const string CMD_EDIT = 'edit';
36  private const string CMD_SAVE = 'save';
37  private const string F_READING_TIME = 'reading_time';
38 
39  private readonly Storage $settings_storage;
40 
41  public function __construct($a_data, int $a_id, bool $a_call_by_reference = true, bool $a_prepare_output = true)
42  {
43  global $DIC;
44 
45  $this->type = 'cpad';
46  parent::__construct($a_data, $a_id, $a_call_by_reference, $a_prepare_output);
47  $this->lng->loadLanguageModule($this->type);
48 
49  $this->settings_storage = new StorageImpl($DIC->settings());
50  }
51 
52  public function getAdminTabs(): void
53  {
54  if ($this->rbac_system->checkAccess('visible,read', $this->object->getRefId())) {
55  $this->tabs_gui->addTarget('settings', $this->ctrl->getLinkTargetByClass(self::class, self::CMD_EDIT));
56  }
57 
58  if ($this->rbac_system->checkAccess('edit_permission', $this->object->getRefId())) {
59  $this->tabs_gui->addTarget(
60  'perm_settings',
61  $this->ctrl->getLinkTargetByClass(ilPermissionGUI::class, 'perm'),
62  [],
63  ilPermissionGUI::class
64  );
65  }
66  }
67 
68  public function executeCommand(): void
69  {
70  if (!$this->rbac_system->checkAccess('visible,read', $this->object->getRefId())) {
71  $this->error->raiseError($this->lng->txt('no_permission'), $this->error->WARNING);
72  }
73 
74  $nextClass = $this->ctrl->getNextClass($this) ?? '';
75  $cmd = $this->ctrl->getCmd() ?? '';
76  $this->prepareOutput();
77 
78  switch (strtolower($nextClass)) {
79  case strtolower(ilPermissionGUI::class):
80  $this->tabs_gui->setTabActive('perm_settings');
81  $perm_gui = new ilPermissionGUI($this);
82  $this->ctrl->forwardCommand($perm_gui);
83  break;
84 
85  default:
86  match ($cmd) {
87  self::CMD_VIEW, self::CMD_EDIT => $this->edit(),
88  self::CMD_SAVE => $this->save(),
89  default => throw new RuntimeException(__METHOD__ . ' :: Unknown command ' . $cmd),
90  };
91  }
92  }
93 
94  private function getForm(array $values = []): Form
95  {
96  $may_write = $this->rbac_system->checkAccess('write', $this->object->getRefId());
97 
98  $action = $this->ctrl->getLinkTargetByClass(self::class, self::CMD_SAVE);
99  if (!$may_write) {
100  $action = $this->ctrl->getLinkTargetByClass(self::class, self::CMD_VIEW);
101  }
102 
103  $readingTimeStatus = $this->ui_factory
104  ->input()
105  ->field()
106  ->checkbox(
107  $this->lng->txt('cpad_reading_time_status'),
108  $this->lng->txt('cpad_reading_time_status_desc')
109  )
110  ->withDisabled(!$may_write);
111 
112  if (isset($values[self::F_READING_TIME])) {
113  $readingTimeStatus = $readingTimeStatus->withValue($values[self::F_READING_TIME]);
114  }
115 
116  $section = $this->ui_factory->input()->field()->section(
117  [self::F_READING_TIME => $readingTimeStatus],
118  $this->lng->txt('settings')
119  )->withDisabled(!$may_write);
120 
121  $form = $this->ui_factory
122  ->input()
123  ->container()
124  ->form()
125  ->standard($action, [$section])
126  ->withAdditionalTransformation($this->refinery->custom()->transformation(static function ($values): array {
127  return array_merge(...$values);
128  }));
129 
130  if (!$may_write) {
131  $form = $form->withSubmitLabel($this->lng->txt('refresh'));
132  }
133 
134  return $form;
135  }
136 
140  private function show(array $components): void
141  {
142  $this->tpl->setContent(
143  $this->ui_renderer->render($components)
144  );
145  }
146 
147  private function edit(): void
148  {
149  $values = [
150  self::F_READING_TIME => $this->settings_storage->getSettings()->isReadingTimeEnabled(),
151  ];
152 
153  $form = $this->getForm($values);
154 
155  $this->show([$form]);
156  }
157 
158  private function save(): void
159  {
160  if (!$this->rbac_system->checkAccess('write', $this->object->getRefId())) {
161  $this->error->raiseError($this->lng->txt('no_permission'), $this->error->WARNING);
162  }
163 
164  $form = $this->getForm()->withRequest($this->http->request());
165  $data = $form->getData();
166 
167  if ($data === null || $this->request->getMethod() !== 'POST') {
168  $this->show([$form]);
169  return;
170  }
171 
172  $readingTime = $data[self::F_READING_TIME];
173  $settings = $this->settings_storage
174  ->getSettings()
175  ->withDisabledReadingTime();
176  if ($readingTime) {
177  $settings = $settings->withEnabledReadingTime();
178  }
179  $this->settings_storage->store($settings);
180 
181  $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'), true);
182  $this->ctrl->redirect($this, self::CMD_EDIT);
183  }
184 }
__construct($a_data, int $a_id, bool $a_call_by_reference=true, bool $a_prepare_output=true)
prepareOutput(bool $show_sub_objects=true)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$components
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static http()
Fetches the global http state from ILIAS.
Class ilObjectGUI Basic methods of all Output classes.
global $DIC
Definition: shib_login.php:26
ilObjContentPageAdministrationGUI: ilPermissionGUI
__construct(Container $dic, ilPlugin $plugin)
ilSetting $settings