ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
class.ilObjTermsOfServiceGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27use ILIAS\Data\Factory as DataFactory;
29
36{
40 private readonly Config $config;
41 private readonly UI $ui;
42 private readonly Settings $tos_settings;
43
44 public function __construct(int $a_id = 0, int $a_id_type = self::REPOSITORY_NODE_ID, int $a_parent_node_id = 0)
45 {
46 global $DIC;
47
48 $this->dic = $DIC;
49 $this->lng = $DIC->language();
50 $this->error = $DIC['ilErr'];
51
52 parent::__construct($a_id, $a_id_type, $a_parent_node_id);
53
54 $this->lng->loadLanguageModule('tos');
55 $this->lng->loadLanguageModule('meta');
56 $config = new Config($this->dic['legalDocuments']->provide(Consumer::ID));
57 if ($this->rbac_system->checkAccess('write', $this->object->getRefId())) {
59 }
60 $this->config = $config;
61 $this->legal_documents = new ilLegalDocumentsAdministrationGUI(self::class, $this->config, $this->afterDocumentDeletion(...));
62 $this->ui = new UI(Consumer::ID, $this->dic->ui(), $this->dic->language());
63 $this->tos_settings = $this->createSettings();
64 }
65
66 public function getType(): string
67 {
68 return 'tos';
69 }
70
71 public function executeCommand(): void
72 {
73 if (!$this->rbac_system->checkAccess('read', $this->object->getRefId())) {
74 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
75 }
76 $this->prepareOutput();
77 $next_class = $this->ctrl->getNextClass($this) ?? '';
78 $cmd = $this->ctrl->getCmd() ?? '';
79
80 switch (strtolower($next_class)) {
81 case strtolower(ilLegalDocumentsAdministrationGUI::class):
82 switch ($cmd) {
83 case 'documents': $this->documents();
84 // no break.
85 default: $this->ctrl->forwardCommand($this->legal_documents);
86 }
87 return;
88 case strtolower(ilPermissionGUI::class):
89 $this->tabs_gui->activateTab('permissions');
90 $this->ctrl->forwardCommand(new ilPermissionGUI($this));
91 return;
92 default:
93 switch ($cmd) {
94 case 'documents':
95 $this->ctrl->redirectByClass([self::class, ilLegalDocumentsAdministrationGUI::class], 'documents');
96 return;
97 case 'confirmReset': $this->confirmReset();
98 return;
99 case 'resetNow': $this->resetNow();
100 return;
101 case 'documents':
102 $this->ctrl->redirectByClass([self::class, get_class($this->legal_documents)], 'documents');
103 return;
104 default: $this->settings();
105 return;
106 }
107 }
108 }
109
110 public function getAdminTabs(): void
111 {
112 $can_edit_permissions = $this->rbac_system->checkAccess('edit_permission', $this->object->getRefId());
113
114 // Read right is already required by self::executeCommand.
115 $this->legal_documents->tabs([
116 'documents' => fn() => $this->tabs_gui->addTab('settings', $this->lng->txt('settings'), $this->ctrl->getLinkTarget($this, 'settings')),
117 ]);
118
119 if ($can_edit_permissions) {
120 $this->tabs_gui->addTab(
121 'permissions',
122 $this->lng->txt('perm_settings'),
123 $this->ctrl->getLinkTargetByClass([self::class, ilPermissionGUI::class], 'perm')
124 );
125 }
126 }
127
128 public function form(): Form
129 {
130 $read_only = !$this->rbac_system->checkAccess('write', $this->object->getRefId());
131
132 $no_documents = $this->config->legalDocuments()->document()->repository()->countAll() === 0;
133 if ($no_documents) {
134 $this->tpl->setOnScreenMessage('info', $this->lng->txt('tos_no_documents_exist'));
135 }
136
137 $enabled = $this->dic->ui()->factory()->input()->field()->optionalGroup(
138 [
139 'reeval_on_login' => $this->dic->ui()->factory()->input()->field()->checkbox(
140 $this->ui->txt('reevaluate_on_login'),
141 $this->ui->txt('reevaluate_on_login_desc')
142 )
143 ],
144 $this->lng->txt('tos_status_enable'),
145 $this->lng->txt('tos_status_desc')
146 );
147 $enabled = $enabled->withValue($this->tos_settings->enabled()->value() ? ['reeval_on_login' => $this->tos_settings->validateOnLogin()->value()] : null);
148 $enabled = $enabled->withDisabled($read_only);
149
150 $form = $this->dic->ui()->factory()->input()->container()->form()->standard(
151 $this->ctrl->getFormAction($this, 'saveSettings'),
152 ['enabled' => $enabled]
153 );
154
155 if ($read_only) {
156 $form = $form->withSubmitLabel($this->lng->txt('refresh'));
157 return $this->legal_documents->admin()->withFormData($form, function () {
158 $this->ctrl->redirect($this, 'settings');
159 });
160 }
161
162 return $this->legal_documents->admin()->withFormData($form, function (array $data) use ($no_documents): void {
163 if ($no_documents && $data['enabled']) {
164 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('tos_no_documents_exist_cant_save'), true);
165 $this->ctrl->redirect($this, 'settings');
166 }
167 $this->tos_settings->enabled()->update(isset($data['enabled']));
168 $this->tos_settings->validateOnLogin()->update($data['enabled']['reeval_on_login'] ?? false);
169 $this->tpl->setOnScreenMessage('success', $this->lng->txt('msg_obj_modified'), true);
170 $this->ctrl->redirect($this, 'settings');
171 });
172 }
173
174 public function afterDocumentDeletion(): void
175 {
176 if ($this->config->legalDocuments()->document()->repository()->countAll() === 0) {
177 $this->tos_settings->enabled()->update(false);
178 }
179 }
180
181 protected function settings(): void
182 {
183 if (!$this->rbac_system->checkAccess('read', $this->object->getRefId())) {
184 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
185 }
186
187 $this->tabs_gui->activateTab('settings');
188
189 $components = [];
190
191 if ($this->tos_settings->enabled()->value() && $this->config->legalDocuments()->document()->repository()->countAll() === 0) {
192 $components[] = $this->ui->create()->messageBox()->info(
193 $this->ui->txt('no_documents_exist')
194 );
195 }
196
197 $components[] = $this->legal_documents->admin()->externalSettingsMessage($this->tos_settings->deleteUserOnWithdrawal()->value());
198
199 $components[] = $this->form();
200 $this->tpl->setContent($this->dic->ui()->renderer()->render($components));
201 }
202
203 private function documents(): void
204 {
205 $buttons = $this->config->editable() ?
206 [$this->legal_documents->admin()->resetButton($this->dic->ctrl()->getLinkTarget($this, 'confirmReset'))] :
207 [];
208
209 $reset_date = new DateTimeImmutable('@' . $this->dic->settings()->get('tos_last_reset', '0'));
210
211 $this->tpl->setCurrentBlock('mess');
212 $this->legal_documents->admin()->setVariable('MESSAGE', $this->legal_documents->admin()->resetBox($reset_date, $buttons));
213 $this->tpl->parseCurrentBlock('mess');
214 }
215
216 private function confirmReset(): void
217 {
218 $this->legal_documents->admin()->requireEditable();
219 $this->tabs_gui->activateTab('documents');
220 $this->legal_documents->admin()->setContent((new Confirmation($this->dic->language()))->render(
221 $this->dic->ctrl()->getFormAction($this, 'resetNow'),
222 'resetNow',
223 'documents',
224 $this->dic->language()->txt('tos_sure_reset_tos')
225 ));
226 }
227
228 private function resetNow(): void
229 {
230 $this->legal_documents->admin()->requireEditable();
231 $in = $this->dic->database()->in('usr_id', [ANONYMOUS_USER_ID, SYSTEM_USER_ID], true, 'integer');
232 $this->dic->database()->manipulate("UPDATE usr_data SET agree_date = NULL WHERE $in");
233 $this->tos_settings->lastResetDate()->update((new DataFactory())->clock()->system()->now());
234 $this->tpl->setOnScreenMessage('success', $this->lng->txt('msg_obj_modified'), true);
235 $this->dic->ctrl()->redirectByClass([self::class, $this->legal_documents::class], 'documents');
236 }
237
238 private function createSettings(): Settings
239 {
240 $blocks = new Blocks($this->config->legalDocuments()->id(), $this->dic, $this->config->legalDocuments());
241 return new Settings($blocks->selectSettingsFrom($blocks->globalStore()));
242 }
243}
$components
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
Builds data types.
Definition: Factory.php:36
error(string $a_errmsg)
Error Handling & global info handling.
@ilCtrl_Calls ilObjTermsOfServiceGUI: ilPermissionGUI @ilCtrl_Calls ilObjTermsOfServiceGUI: ilLegalDo...
getType()
Functions that must be overwritten.
readonly ilLegalDocumentsAdministrationGUI $legal_documents
__construct(int $a_id=0, int $a_id_type=self::REPOSITORY_NODE_ID, int $a_parent_node_id=0)
getAdminTabs()
administration tabs show only permissions and trash folder
New implementation of ilObjectGUI.
prepareOutput(bool $show_sub_objects=true)
const SYSTEM_USER_ID
This file contains constants for PHPStan analyis, see: https://phpstan.org/config-reference#constants...
Definition: constants.php:26
const ANONYMOUS_USER_ID
Definition: constants.php:27
This describes commonalities between all forms.
Definition: Form.php:34
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26