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