ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
Administration.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
30 use ilDateTime;
33 use Closure;
41 use Exception;
47 
49 {
51  private readonly Closure $confirmation;
52 
56  public function __construct(
57  private readonly Config $config,
58  private readonly Container $container,
59  private readonly UI $ui,
60  ?Closure $confirmation = null
61  ) {
62  $this->confirmation = $confirmation ?? fn() => new Confirmation($this->container->language());
63  }
64 
68  public function deleteDocumentsConfirmation(string $form_link, string $submit_command, string $cancel_command, array $documents): string
69  {
70  $items = array_column(array_map(fn($x) => [$x->id(), $x->content()->title()], $documents), 1, 0);
71 
72  return (($this->confirmation)())->render(
73  $form_link,
74  $submit_command,
75  $cancel_command,
76  $this->ui->txt('sure_delete_documents_p'),
77  $items
78  );
79  }
80 
84  public function deleteDocuments(array $documents): void
85  {
86  array_map(
87  $this->config->legalDocuments()->document()->repository()->deleteDocument(...),
88  $documents
89  );
90  }
91 
95  public function withDocumentAndCriterion(Closure $proc): void
96  {
97  $document = $this->currentDocument()->value();
98  $criterion_id = ($this->container->http()->request()->getQueryParams()['criterion_id'] ?? null);
99  if (null === $criterion_id) {
100  throw new InvalidArgumentException('Missing query parameter criterion_id.');
101  }
102  $criterion_id = (int) $criterion_id;
103 
104  $criterion = $this->find(
105  fn($criterion) => $criterion->id() === $criterion_id,
106  $document->criteria()
107  );
108  if (!$criterion) {
109  throw new InvalidArgumentException('Invalid criterion_id given.');
110  }
111 
112  $proc($document, $criterion);
113  }
114 
115  public function retrieveDocuments(): array
116  {
117  $ids = $this->retrieveIds();
118  $documents = $this->config->legalDocuments()->document()->repository()->select($ids);
119  if (count($documents) !== count($ids)) {
120  throw new InvalidArgumentException('List contains invalid documents.');
121  }
122 
123  return $documents;
124  }
125 
126  public function retrieveIds(): array
127  {
128  $r = $this->container->refinery();
129  return $this->container->http()->wrapper()->post()->retrieve('ids', $this->container->refinery()->byTrying([
130  $r->in()->series([$r->null(), $r->always([])]),
131  $r->to()->listOf($r->kindlyTo()->int())
132  ]));
133  }
134 
138  public function idOrHash(object $gui, Closure $then): void
139  {
140  $with_doc_id = fn($document) => $then($this->willLinkWith($gui, ['doc_id' => $document->id()]), $document->content()->title(), new NumberId($document), false);
141  $with_hash = fn(string $hash) => $then($this->willLinkWith($gui, ['hash' => $hash]), '', new HashId($hash), true);
142  $try_hash = fn() => new Ok($with_hash($this->requireDocumentHash()));
143 
144  $this->currentDocument()
145  ->map($with_doc_id)
146  ->except($try_hash)
147  ->value();
148 
149  }
150 
151  public function targetWithDoc(object $gui, $document, string $cmd, string $method = 'getLinkTarget'): string
152  {
153  $link = $this->willLinkWith($gui, ['doc_id' => (string) $document->id()]);
154  return $link($cmd, $method);
155  }
156 
157  public function targetWithDocAndCriterion(object $gui, $document, $criterion, string $cmd, string $method = 'getLinkTarget')
158  {
159  $link = $this->willLinkWith($gui, [
160  'doc_id' => (string) $document->id(),
161  'criterion_id' => (string) $criterion->id(),
162  ]);
163 
164  return $link($cmd, $method);
165  }
166 
170  public function willLinkWith($gui, array $parameters = []): Closure
171  {
172  $class = is_string($gui) ? $gui : get_class($gui);
173  return function (string $cmd, ?string $method = null) use ($gui, $class, $parameters): string {
174  $method ??= $class === $gui ? 'getLinkTargetByClass' : 'getLinkTarget';
175  $array = $this->container->ctrl()->getParameterArrayByClass($class);
176  foreach ($parameters as $key => $value) {
177  $this->container->ctrl()->setParameterByClass($class, $key, $value);
178  }
179  $link = $this->container->ctrl()->$method($gui, $cmd);
180  foreach ($parameters as $key => $_) {
181  $this->container->ctrl()->setParameterByClass($class, $key, $array[$key] ?? '');
182  }
183 
184  return $link;
185  };
186  }
187 
191  public function withFormData($form, Closure $then)
192  {
193  $request = $this->container->http()->request();
194  if ($request->getMethod() !== 'POST') {
195  return $form;
196  }
197  $form = $form->withRequest($request);
198  $data = $form->getData();
199 
200  if ($data !== null) {
201  $then($data);
202  }
203 
204  return $form;
205  }
206 
213  public function find(Closure $predicate, array $array)
214  {
215  foreach ($array as $value) {
216  if ($predicate($value)) {
217  return $value;
218  }
219  }
220 
221  return null;
222  }
223 
227  public function currentDocument(): Result
228  {
229  $repo = $this->config->legalDocuments()->document()->repository();
230  $doc_id = $this->container->http()->request()->getQueryParams()['doc_id'] ?? null;
231  return $this->container->refinery()->kindlyTo()->int()->applyTo(new Ok($doc_id))->then($repo->find(...));
232  }
233 
234  public function criterionForm(string $url, Document $document, $criterion = null)
235  {
236  $groups = $this->config->legalDocuments()->document()->conditionGroups($criterion);
237  $group = $this->ui->create()->input()->field()->switchableGroup($groups->choices(), $this->ui->txt('form_criterion'));
238  $value = $criterion ? $criterion->type() : $groups->defaultSelection();
239  if ($value) {
240  $group = $group->withValue($value);
241  }
242 
243  $title = $this->ui->create()->input()->field()->text($this->ui->txt('form_document'))->withValue($document->content()->title())->withDisabled(true);
244 
245  $section = $this->ui->create()->input()->field()->section([
246  $title,
247  'content' => $group,
248  ], $this->ui->txt($criterion ? 'form_edit_criterion_head' : 'form_attach_criterion_head'));
249 
250  return $this->ui->create()->input()->container()->form()->standard($url, [$section]);
251  }
252 
253  public function requireDocumentHash(): string
254  {
255  return $this->container->http()->wrapper()->query()->retrieve('hash', $this->container->refinery()->to()->string());
256  }
257 
262  public function tabs(array $tabs, array $run_after = []): void
263  {
264  foreach ($tabs as $tab) {
265  $this->addTab(...$tab);
266  if (isset($run_after[$tab[0]])) {
267  $run_after[$tab[0]]();
268  }
269  }
270  }
271 
272  public function uploadContent(): string
273  {
274  $value = null;
275  $upload = $this->container->upload();
276  $upload->register(new PreProcessor(function (string $content) use (&$value): void {
277  $value = $content;
278  }));
279  $upload->process();
280  $result_array = $upload->getResults();
281  if (count($result_array) !== 1 || !current($result_array)->isOk()) {
282  throw new Exception('Unexpected upload result.');
283  }
284 
285  return $value;
286  }
287 
291  public function setContent($component): void
292  {
293  $this->ui->mainTemplate()->setContent($this->render($component));
294  }
295 
296  public function addDocumentButton(string $add_document_link): Component
297  {
298  return $this->ui->create()->button()->primary(
299  $this->ui->txt('add_document_btn_label'),
300  $add_document_link
301  );
302  }
303 
307  public function setVariable(string $variable, $component): void
308  {
309  $this->ui->mainTemplate()->setVariable($variable, $this->render($component));
310  }
311 
315  public function render($component): string
316  {
317  if (is_string($component)) {
318  return $component;
319  }
320  return $this->container->ui()->renderer()->render($component);
321  }
322 
326  public function resetBox(DateTimeImmutable $reset_date, array $buttons = []): Component
327  {
328  $reset_date = new ilDateTime($reset_date->getTimeStamp(), IL_CAL_UNIX);
329  return $this->ui->create()
330  ->messageBox()
331  ->info(sprintf($this->ui->txt('last_reset_date'), ilDatePresentation::formatDate($reset_date)))
332  ->withButtons($buttons);
333  }
334 
335  public function resetButton(string $confirm_reset_link): Component
336  {
337  return $this->ui->create()->button()->standard(
338  $this->ui->txt('reset_for_all_users'),
339  $confirm_reset_link
340  );
341  }
342 
347  public function documentForm(Closure $link, string $title, Closure $document_content, bool $may_be_new): Component
348  {
349  $edit_link = $link('editDocument');
350  $content_title = $may_be_new ? 'form_document' : 'form_document_new';
351 
352  $section = $this->ui->create()->input()->field()->section([
353  'title' => $this->ui->create()->input()->field()->text($this->ui->txt('title'))->withRequired(true)->withValue($title),
354  'content' => $this->ui->create()->input()->field()->file(new UploadHandler($link, $document_content, $this->ui->txt(...)), $this->ui->txt($content_title))->withAcceptedMimeTypes([
355  'text/html',
356  'text/plain',
357  ])->withRequired($may_be_new),
358  ], $this->ui->txt($may_be_new ? 'form_new_doc_head' : 'form_edit_doc_head'));
359 
360  return $this->ui->create()->input()->container()->form()->standard($edit_link, [$section]);
361  }
362 
367  public function saveDocumentOrder(array $documents, array $order_by_document): void
368  {
369  $update = $this->config->legalDocuments()->document()->repository()->updateDocumentOrder(...);
370 
371  usort($documents, fn($document, $other) => $order_by_document[$document->id()] - $order_by_document[$other->id()]);
372 
373  array_map(
374  fn($document, int $order) => $update(new NumberId($document), $order),
375  $documents,
376  range(10, 10 * count($documents), 10)
377  );
378  }
379 
383  public function withDocumentsAndOrder(Closure $proc)
384  {
385  // kindlyTo->int() does not accept numbers of the form "01".
386  $to_int = $this->container->refinery()->byTrying([
387  $this->container->refinery()->kindlyTo()->int(),
388  $this->container->refinery()->in()->series([
389  $this->container->refinery()->to()->string(),
390  $this->container->refinery()->custom()->transformation(fn($s) => ltrim($s, '0') ?: '0'),
391  $this->container->refinery()->kindlyTo()->int(),
392  ]),
393  ]);
394 
395  $order = $this->container->http()->request()->getParsedBody()['order'] ?? null;
396  if (!is_array($order)) {
397  throw new InvalidArgumentException('Invalid order given. List of numbers expected.');
398  }
399 
400  $order = array_map($to_int, $order);
401  $document_ids = array_map($to_int, array_keys($order));
402  $order = array_combine($document_ids, array_values($order));
403 
404  $documents = $this->config->legalDocuments()->document()->repository()->all();
405 
406  foreach ($documents as $document) {
407  if (!isset($order[$document->id()])) {
408  $order[$document->id()] = $document->meta()->sorting();
409  }
410  }
411 
412  return $proc($documents, $order);
413  }
414 
415  public function exitWithJsonResponse($value): void
416  {
417  // ... The content type cannot be set to application/json, because the src/UI/templates/js/Input/Field/file.js:392
418  // does not expect that the content type is correct and parses it again ...
419  $this->container->http()->saveResponse($this->container->http()->response()/* ->withHeader('Content-Type', 'application/json') */->withBody(
420  Streams::ofString(json_encode($value))
421  ));
422 
423  $this->container->http()->sendResponse();
424  $this->container->http()->close();
425  }
426 
427  public function requireEditable(): void
428  {
429  if (!$this->config->editable()) {
430  $this->container['ilErr']->raiseError($this->container->language()->txt('permission_denied'), $this->container['ilErr']->WARNING);
431  }
432  }
433 
435  {
436  return $this->ui->create()->messageBox()->info(
437  $this->ui->txt('withdrawal_usr_deletion') . ': ' . $this->ui->txt($enabled ? 'enabled' : 'disabled')
438  )->withLinks([
439  $this->ui->create()->link()->standard(
440  $this->ui->txt('adm_external_setting_edit'),
441  $this->willLinkWith(ilObjUserFolderGUI::class, ['ref_id' => USER_FOLDER_ID])('generalSettings')
442  )
443  ]);
444  }
445 
446  public function isInvalidHTML(string $string): bool
447  {
448  return !$this->isValidHTML($string);
449  }
450 
451  public function isValidHTML(string $string): bool
452  {
453  return (new ValidHTML())->isTrue($string);
454  }
455 
456  public function canReadUserAdministration(): bool
457  {
458  return $this->container->rbac()->system()->checkAccess('read', USER_FOLDER_ID);
459  }
460 
461  private function addTab(string $id, string $text, string $link, bool $can_access = true): void
462  {
463  if ($can_access) {
464  $this->container->tabs()->addTab($id, $text, $link);
465  }
466  }
467 }
then(callable $f)
Get a new result from the callable or do nothing if this is an error.
bool $enabled
Whether the system instance is enabled to accept connection requests.
Definition: System.php:123
const USER_FOLDER_ID
Definition: constants.php:33
setVariable(string $variable, $component)
deleteDocumentsConfirmation(string $form_link, string $submit_command, string $cancel_command, array $documents)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:14
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false)
tabs(array $tabs, array $run_after=[])
targetWithDocAndCriterion(object $gui, $document, $criterion, string $cmd, string $method='getLinkTarget')
const IL_CAL_UNIX
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
$container
Definition: wac.php:14
find(Closure $predicate, array $array)
A
idOrHash(object $gui, Closure $then)
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:16
string $key
Consumer key/client ID value.
Definition: System.php:193
saveDocumentOrder(array $documents, array $order_by_document)
$url
Definition: ltiregstart.php:35
__construct(private readonly Config $config, private readonly Container $container, private readonly UI $ui, ?Closure $confirmation=null)
criterionForm(string $url, Document $document, $criterion=null)
resetBox(DateTimeImmutable $reset_date, array $buttons=[])
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:58
targetWithDoc(object $gui, $document, string $cmd, string $method='getLinkTarget')
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:41
addTab(string $id, string $text, string $link, bool $can_access=true)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
documentForm(Closure $link, string $title, Closure $document_content, bool $may_be_new)
willLinkWith($gui, array $parameters=[])
resetButton(string $confirm_reset_link)
addDocumentButton(string $add_document_link)
$r