ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
MailAttachmentTableGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Mail\Attachments;
22 
23 use ilStr;
25 use ilUtil;
26 use ilDateTime;
27 
28 class MailAttachmentTableGUI implements \ILIAS\UI\Component\Table\DataRetrieval
29 {
30  private readonly \ILIAS\UI\URLBuilder $url_builder;
31  private readonly \ILIAS\UI\URLBuilderToken $action_parameter_token;
32  private readonly \ILIAS\UI\URLBuilderToken $row_id_token;
33  private readonly \ILIAS\Data\Factory $data_factory;
34 
38  public function __construct(
39  private readonly \ilMailAttachmentGUI $parent_gui,
40  private readonly \ilObjUser $actor,
41  private readonly array $records,
42  private readonly \ILIAS\UI\Factory $ui_factory,
43  private readonly \ILIAS\UI\Renderer $ui_renderer,
44  private readonly \ilLanguage $lng,
45  private readonly \ilCtrlInterface $ctrl,
46  private readonly \Psr\Http\Message\ServerRequestInterface $http_request,
47  private readonly \ILIAS\Data\Factory $df,
48  private readonly string $parent_cmd,
49  private readonly AttachmentManagement $mode
50  ) {
51  $form_action = $this->df->uri(
52  \ilUtil::_getHttpPath() . '/' .
53  $this->ctrl->getLinkTarget($this->parent_gui, $this->parent_cmd)
54  );
55 
56  [
60  ] = (new \ILIAS\UI\URLBuilder($form_action))->acquireParameters(
61  ['mail', 'attachments'],
62  'table_action',
63  'filename'
64  );
65 
66  $this->data_factory = new \ILIAS\Data\Factory();
67  }
68 
69  public function get(): \ILIAS\UI\Component\Table\Data
70  {
71  return $this->ui_factory
72  ->table()
73  ->data(
74  $this,
75  $this->lng->txt('attachment'),
76  $this->getColumnDefinition(),
77  )
78  ->withId(self::class . '_' . $this->mode->name)
79  ->withOrder(new \ILIAS\Data\Order('filename', \ILIAS\Data\Order::ASC))
80  ->withActions($this->getActions())
81  ->withRequest($this->http_request);
82  }
83 
87  private function getColumnDefinition(): array
88  {
89  if ((int) $this->actor->getTimeFormat() === \ilCalendarSettings::TIME_FORMAT_12) {
90  $date_format = $this->data_factory->dateFormat()->withTime12($this->actor->getDateFormat());
91  } else {
92  $date_format = $this->data_factory->dateFormat()->withTime24($this->actor->getDateFormat());
93  }
94 
95  return [
96  'filename' => $this->ui_factory
97  ->table()
98  ->column()
99  ->text($this->lng->txt('mail_file_name'))
100  ->withIsSortable(true),
101  'filesize' => $this->ui_factory
102  ->table()
103  ->column()
104  ->text($this->lng->txt('mail_file_size'))
105  ->withIsSortable(true),
106  'filecreatedate' => $this->ui_factory
107  ->table()
108  ->column()
109  ->date($this->lng->txt('create_date'), $date_format)
110  ->withIsSortable(true),
111  ];
112  }
113 
117  private function getActions(): array
118  {
119  $actions = [];
120 
121  if ($this->mode === AttachmentManagement::CONSUME) {
122  $actions['saveAttachments'] = $this->ui_factory->table()->action()->multi(
123  $this->lng->txt('adopt'),
124  $this->url_builder->withParameter($this->action_parameter_token, 'saveAttachments'),
126  );
127  } else {
128  $actions['deleteAttachments'] = $this->ui_factory->table()->action()->multi(
129  $this->lng->txt('delete'),
130  $this->url_builder->withParameter($this->action_parameter_token, 'deleteAttachments'),
132  );
133  }
134 
135  return $actions;
136  }
137 
141  private function getRecords(\ILIAS\Data\Range $range, \ILIAS\Data\Order $order): array
142  {
143  $records = $this->records;
144 
145  [$order_field, $order_direction] = $order->join([], static fn($ret, $key, $value) => [$key, $value]);
146 
147  usort($records, static function (array $left, array $right) use ($order_field): int {
148  if ($order_field === 'filename') {
149  return ilStr::strCmp($left[$order_field], $right[$order_field]);
150  }
151 
152  return $left[$order_field] <=> $right[$order_field];
153  });
154 
155  if ($order_direction === 'DESC') {
156  $records = array_reverse($records);
157  }
158 
159  $records = \array_slice($records, $range->getStart(), $range->getLength());
160 
161  return $records;
162  }
163 
164  public function getRows(
165  \ILIAS\UI\Component\Table\DataRowBuilder $row_builder,
166  array $visible_column_ids,
168  \ILIAS\Data\Order $order,
169  ?array $filter_data,
170  ?array $additional_parameters
171  ): \Generator {
172  foreach ($this->getRecords($range, $order) as $item) {
173  $record = [
174  'filename' => $item['filename'],
175  'filesize' => ilUtil::formatSize($item['filesize'], 'long'),
176  'filecreatedate' => (new \DateTimeImmutable('@' . $item['filecreatedate']))->setTimezone(
177  new \DateTimeZone($this->actor->getTimeZone())
178  )
179  ];
180 
181  yield $row_builder
182  ->buildDataRow(urlencode($record['filename']), $record);
183  }
184  }
185 
186  public function getTotalRowCount(?array $filter_data, ?array $additional_parameters): ?int
187  {
188  return \count($this->records);
189  }
190 }
__construct(private readonly \ilMailAttachmentGUI $parent_gui, private readonly \ilObjUser $actor, private readonly array $records, private readonly \ILIAS\UI\Factory $ui_factory, private readonly \ILIAS\UI\Renderer $ui_renderer, private readonly \ilLanguage $lng, private readonly \ilCtrlInterface $ctrl, private readonly \Psr\Http\Message\ServerRequestInterface $http_request, private readonly \ILIAS\Data\Factory $df, private readonly string $parent_cmd, private readonly AttachmentManagement $mode)
Interface Observer Contains several chained tasks and infos about them.
Both the subject and the direction need to be specified when expressing an order. ...
Definition: Order.php:28
readonly ILIAS UI URLBuilderToken $row_id_token
static formatSize(int $size, string $a_mode='short', ?ilLanguage $a_lng=null)
Returns the specified file size value in a human friendly form.
Builds data types.
Definition: Factory.php:35
getRows(\ILIAS\UI\Component\Table\DataRowBuilder $row_builder, array $visible_column_ids, \ILIAS\Data\Range $range, \ILIAS\Data\Order $order, ?array $filter_data, ?array $additional_parameters)
static strCmp(string $a, string $b)
Definition: class.ilStr.php:87
getTotalRowCount(?array $filter_data, ?array $additional_parameters)
Mainly for the purpose of pagination-support, it is important to know about the total number of recor...
static _getHttpPath()
readonly ILIAS UI URLBuilderToken $action_parameter_token
global $lng
Definition: privfeed.php:31
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:28
getRecords(\ILIAS\Data\Range $range, \ILIAS\Data\Order $order)