ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
DocumentTable.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
24 use Closure;
34 use ilDateTime;
37 
38 class DocumentTable implements Table
39 {
41  private readonly Closure $create;
43  private readonly Closure $format_date;
44 
50  public function __construct(
51  private readonly Closure $criterion_as_component,
52  private readonly DocumentRepository $repository,
53  private readonly UI $ui,
54  private readonly DocumentModal $modal,
55  ?Closure $create = null,
56  ?Closure $format_date = null
57  ) {
58  $this->create = $create ?? fn($class, ...$args) => new $class(...$args);
59  $this->format_date = $format_date ?? fn(DateTimeImmutable $date) => ilDatePresentation::formatDate(new ilDateTime($date->getTimestamp(), IL_CAL_UNIX));
60  }
61 
62  public function columns(): array
63  {
64  return [
65  'order' => [$this->ui->txt('tbl_docs_head_sorting'), '', '5%'],
66  'title' => [$this->ui->txt('tbl_docs_head_title'), '', '25%'],
67  'created' => [$this->ui->txt('tbl_docs_head_created')],
68  'change' => [$this->ui->txt('tbl_docs_head_last_change')],
69  'criteria' => [$this->ui->txt('tbl_docs_head_criteria')],
70  ];
71  }
72 
73  public function config(TableConfig $config): void
74  {
75  $config->setTitle($this->ui->txt('tbl_docs_title'));
76  $config->setSelectableColumns('created', 'change');
77  }
78 
79  public function rows(TableSelection $select): array
80  {
81  return $this->mapSelection($this->row(...), $select);
82  }
83 
84  public function mapSelection(Closure $proc, TableSelection $select): array
85  {
86  $step = $this->step();
87  return array_map(fn($x) => $proc($x, $step()), $this->select($select));
88  }
89 
90  public function select(TableSelection $select): array
91  {
92  return $this->repository->all();
93  }
94 
95  public function row(Document $document, int $sorting): array
96  {
97  $render_order = $this->orderInputGui($document, $sorting);
98 
99  return [
100  'order' => fn() => $render_order->render(),
101  'title' => $this->modal->create($document->content()),
102  'created' => ($this->format_date)($document->meta()->creation()->time()),
103  'change' => ($this->format_date)($document->meta()->lastModification()->time()),
104  'criteria' => $this->showCriteria($document, $this->showCriterion(...)),
105  ];
106  }
107 
112  public function showCriteria(Document $document, Closure $proc)
113  {
114  if ([] === $document->criteria()) {
115  return $this->ui->txt('tbl_docs_cell_not_criterion');
116  }
117  return array_merge(...array_map(
118  $proc,
119  $document->criteria()
120  ));
121  }
122 
126  public function showCriterion(Criterion $criterion): array
127  {
128  return [
129  $this->criterionName($criterion),
130  $this->ui->create()->legacy('<br/>'),
131  ];
132  }
133 
134  public function criterionName(Criterion $criterion): Component
135  {
136  return ($this->criterion_as_component)($criterion->content());
137  }
138 
139  public function orderInputGui(Document $document, int $sorting): ilNumberInputGUI
140  {
141  $input = ($this->create)(ilNumberInputGUI::class, '', 'order[' . $document->id() . ']');
142  $input->setValue((string) $sorting);
143  $input->setMaxLength(4);
144  $input->setSize(2);
145  $input->setDisabled(true);
146 
147  return $input;
148  }
149 
150  public function step(): Closure
151  {
152  $step = 0;
153  return static function () use (&$step): int {
154  $step += 10;
155  return $step;
156  };
157  }
158 
159  public function ui(): UI
160  {
161  return $this->ui;
162  }
163 
164  public function name(): string
165  {
166  return self::class;
167  }
168 }
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ilObjUser $user=null,)
repository()
description: > Example for rendering a repository card
Definition: repository.php:17
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const IL_CAL_UNIX
row(Document $document, int $sorting)
showCriteria(Document $document, Closure $proc)
orderInputGui(Document $document, int $sorting)
__construct(private readonly Closure $criterion_as_component, private readonly DocumentRepository $repository, private readonly UI $ui, private readonly DocumentModal $modal, ?Closure $create=null, ?Closure $format_date=null)
modal(string $title="", string $cancel_label="")
mapSelection(Closure $proc, TableSelection $select)
setTitle(string $a_title, string $a_icon="", string $a_icon_alt="")
setSelectableColumns(string ... $names)