ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
MailFolderTableUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Mail\Folder;
22
23use ilLanguage;
24use ilMail;
28use ILIAS\Data\Factory as DataFactory;
32use ILIAS\Refinery\Factory as Refinery;
34use ILIAS\UI\Component\Table\Data as DataTable;
43use Psr\Http\Message\ServerRequestInterface;
44use DateTimeImmutable;
45use DateTimeZone;
46
48{
49 // table actions
50 public const string ACTION_SHOW = 'show';
51 public const string ACTION_EDIT = 'edit';
52 public const string ACTION_REPLY = 'reply';
53 public const string ACTION_FORWARD = 'forward';
54 public const string ACTION_DOWNLOAD_ATTACHMENT = 'download';
55 public const string ACTION_PRINT = 'print';
56 public const string ACTION_PROFILE = 'profile';
57 public const string ACTION_MOVE_TO = 'moveTo';
58 public const string ACTION_DELETE = 'delete';
59 public const string ACTION_MARK_READ = 'markRead';
60 public const string ACTION_MARK_UNREAD = 'marUnread';
61
63 private array $avatars = [];
64
68 public function __construct(
69 private readonly URLBuilder $url_builder,
70 private readonly URLBuilderToken $action_token,
71 private readonly URLBuilderToken $row_id_token,
72 private readonly URLBuilderToken $folder_token,
73 private readonly array $user_folders,
74 private readonly MailFolderData $current_folder,
75 private readonly MailFolderSearch $search,
76 private readonly ilMail $mail,
77 private readonly Factory $ui_factory,
78 private readonly Renderer $ui_renderer,
79 private readonly ilLanguage $lng,
80 private readonly ServerRequestInterface $http_request,
81 private readonly DataFactory $data_factory,
82 private readonly Refinery $refinery,
83 private readonly DateFormat $date_format,
84 private readonly string $time_format,
85 private readonly DateTimeZone $user_time_zone
86 ) {
87 }
88
89 public function getComponent(): DataTable
90 {
91 return $this->ui_factory
92 ->table()
93 ->data(
94 $this,
95 $this->getTableTitle(),
96 $this->getColumnDefinition(),
97 )
98 ->withId(str_replace('\\', '', self::class))
99 ->withOrder(new Order('date', Order::DESC))
100 ->withRange(new Range(0, 50))
101 ->withActions($this->getActions())
102 ->withRequest($this->http_request);
103 }
104
108 private function getColumnDefinition(): array
109 {
110 if ((int) $this->time_format === \ilCalendarSettings::TIME_FORMAT_12) {
111 $date_format = $this->data_factory->dateFormat()->withTime12($this->date_format);
112 } else {
113 $date_format = $this->data_factory->dateFormat()->withTime24($this->date_format);
114 }
115
116 $columns = [
117 'status' => $this->ui_factory
118 ->table()
119 ->column()
120 ->statusIcon($this->lng->txt('status'))
121 ->withIsSortable(true),
122
123 'avatar' => $this->ui_factory
124 ->table()
125 ->column()
126 ->status($this->lng->txt('personal_picture'))
127 ->withIsSortable(true),
128
129 'sender' => $this->ui_factory
130 ->table()
131 ->column()
132 ->text($this->lng->txt('sender'))
133 ->withIsSortable(true),
134
135 'recipients' => $this->ui_factory
136 ->table()
137 ->column()
138 ->text($this->lng->txt('recipient'))
139 ->withIsSortable(true),
140
141 'subject' => $this->ui_factory
142 ->table()
143 ->column()
144 ->link($this->lng->txt('subject'))
145 ->withIsSortable(true),
146
147 'attachments' => $this->ui_factory
148 ->table()
149 ->column()
150 ->status($this->lng->txt('attachments'))
151 ->withIsSortable(true),
152
153 'date' => $this->ui_factory
154 ->table()
155 ->column()
156 ->date(
157 $this->lng->txt('date'),
158 $date_format
159 )
160 ->withIsSortable(true),
161 ];
162
163 if ($this->current_folder->hasOutgoingMails()) {
164 unset($columns['status'], $columns['avatar'], $columns['sender']);
165 } else {
166 unset($columns['recipients']);
167 }
168
169 return $columns;
170 }
171
175 private function getActions(): array
176 {
177 $actions = [
178 self::ACTION_SHOW => $this->ui_factory->table()->action()->single(
179 $this->lng->txt('view'),
180 $this->url_builder->withParameter($this->action_token, self::ACTION_SHOW),
181 $this->row_id_token
182 ),
183 self::ACTION_EDIT => $this->ui_factory->table()->action()->single(
184 $this->lng->txt('edit'),
185 $this->url_builder->withParameter($this->action_token, self::ACTION_EDIT),
186 $this->row_id_token
187 ),
188 self::ACTION_REPLY => $this->ui_factory->table()->action()->single(
189 $this->lng->txt('reply'),
190 $this->url_builder->withParameter($this->action_token, self::ACTION_REPLY),
191 $this->row_id_token
192 ),
193 self::ACTION_FORWARD => $this->ui_factory->table()->action()->single(
194 $this->lng->txt('forward'),
195 $this->url_builder->withParameter($this->action_token, self::ACTION_FORWARD),
196 $this->row_id_token
197 ),
198 self::ACTION_DOWNLOAD_ATTACHMENT => $this->ui_factory->table()->action()->single(
199 $this->lng->txt('mail_download_attachment'),
200 $this->url_builder->withParameter($this->action_token, self::ACTION_DOWNLOAD_ATTACHMENT),
201 $this->row_id_token
202 ),
203 self::ACTION_PRINT => $this->ui_factory->table()->action()->single(
204 $this->lng->txt('print'),
205 $this->url_builder->withParameter($this->action_token, self::ACTION_PRINT),
206 $this->row_id_token
207 ),
208 self::ACTION_MARK_READ => $this->ui_factory->table()->action()->multi(
209 $this->lng->txt('mail_mark_read'),
210 $this->url_builder->withParameter($this->action_token, self::ACTION_MARK_READ),
211 $this->row_id_token
212 ),
213 self::ACTION_MARK_UNREAD => $this->ui_factory->table()->action()->multi(
214 $this->lng->txt('mail_mark_unread'),
215 $this->url_builder->withParameter($this->action_token, self::ACTION_MARK_UNREAD),
216 $this->row_id_token
217 ),
218 self::ACTION_DELETE => $this->ui_factory->table()->action()->standard(
219 $this->lng->txt('delete'),
220 $this->url_builder->withParameter($this->action_token, self::ACTION_DELETE),
221 $this->row_id_token
222 )->withAsync(), // for confirmation modal
223 ];
224
225 foreach ($this->user_folders as $target_folder) {
226 if ($target_folder->getFolderId() !== $this->current_folder->getFolderId()) {
227 $action_title = $this->lng->txt('mail_move_to') . ' ' . $target_folder->getTitle();
228 if ($target_folder->isTrash()) {
229 $action_title .= ' (' . $this->lng->txt('delete') . ')';
230 }
231
232 $actions[self::ACTION_MOVE_TO . $target_folder->getFolderId()] = $this->ui_factory
233 ->table()
234 ->action()
235 ->multi(
236 $action_title,
237 $this->url_builder
238 ->withParameter($this->action_token, self::ACTION_MOVE_TO)
239 ->withParameter($this->folder_token, (string) $target_folder->getFolderId()),
240 $this->row_id_token
241 );
242 }
243 }
244
245 if ($this->current_folder->isDrafts() || $this->current_folder->isOutbox()) {
246 unset($actions[self::ACTION_SHOW], $actions[self::ACTION_REPLY], $actions[self::ACTION_FORWARD]);
247 } else {
248 unset($actions[self::ACTION_EDIT]);
249 }
250
251 if ($this->current_folder->hasOutgoingMails()) {
252 unset($actions[self::ACTION_MARK_READ], $actions[self::ACTION_MARK_UNREAD]);
253 }
254
255 if (!$this->current_folder->isTrash()) {
256 unset($actions[self::ACTION_DELETE]);
257 }
258
259 return $actions;
260 }
261
262 public function getRows(
263 DataRowBuilder $row_builder,
264 array $visible_column_ids,
266 Order $order,
267 mixed $additional_viewcontrol_data,
268 mixed $filter_data, // not used, because data is filtered by MailDataSearch
269 mixed $additional_parameters // not used
270 ): \Generator {
271 // mapping of table columns to allowed order columns of the mailbox query
272 $order_columns = [
273 'status' => MailBoxOrderColumn::STATUS,
274 'subject' => MailBoxOrderColumn::SUBJECT,
275 'sender' => MailBoxOrderColumn::FROM,
276 'recipients' => MailBoxOrderColumn::RCP_TO,
277 'date' => MailBoxOrderColumn::SEND_TIME,
278 'attachments' => MailBoxOrderColumn::ATTACHMENTS
279 ];
280
281 [$order_column, $order_direction] = $order->join([], fn($ret, $key, $value) => [$key, $value]);
282
283 $records = $this->search->getPagedRecords(
284 $range->getLength(),
285 $range->getStart(),
286 $order_columns[$order_column] ?? null,
287 $order_direction
288 );
289
290 // preload user objects for display of avatar and sender
291 if ($this->current_folder->hasIncomingMails()) {
292 $user_ids = [];
293 foreach ($records as $record) {
294 if ($record->hasPersonalSender()) {
295 $user_ids[$record->getSenderId()] = $record->getSenderId();
296 }
297 }
299 }
300
301 foreach ($records as $record) {
302 if ($this->current_folder->hasIncomingMails()) {
303 $data = [
304 'status' => $this->getStatus($record),
305 'avatar' => $this->getAvatar($record),
306 'sender' => $this->getSender($record),
307 'subject' => $this->getSubject($record),
308 'attachments' => $this->getAttachments($record),
309 'date' => $this->getDate($record)
310 ];
311 } else {
312 $data = [
313 'recipients' => $this->getRecipients($record),
314 'subject' => $this->getSubject($record),
315 'attachments' => $this->getAttachments($record),
316 'date' => $this->getDate($record)
317 ];
318 }
319
320 yield $row_builder->buildDataRow(
321 (string) $record->getMailId(),
322 $data
323 )->withDisabledAction(self::ACTION_REPLY, !$record->hasPersonalSender())->withDisabledAction(
324 self::ACTION_DOWNLOAD_ATTACHMENT,
325 !$record->hasAttachments()
326 );
327 }
328 }
329
330 public function getTotalRowCount(
331 mixed $additional_viewcontrol_data,
332 mixed $filter_data,
333 mixed $additional_parameters
334 ): ?int {
335 return $this->search->getCount();
336 }
337
338 private function getTableTitle(): string
339 {
340 if ($this->current_folder->hasIncomingMails() && $this->search->getUnread() > 0) {
341 return \sprintf(
342 '%s: %s (%s %s)',
343 $this->current_folder->getTitle(),
344 $this->search->getCount() === 1
345 ? $this->lng->txt('mail_1')
346 : \sprintf($this->lng->txt('mail_s'), $this->search->getCount()),
347 $this->search->getUnread(),
348 $this->lng->txt('unread')
349 );
350 }
351
352 return \sprintf(
353 '%s: %s',
354 $this->current_folder->getTitle(),
355 $this->search->getCount() === 1
356 ? $this->lng->txt('mail_1')
357 : \sprintf($this->lng->txt('mail_s'), $this->search->getCount()),
358 );
359 }
360
361 private function getAvatar(MailRecordData $record): string
362 {
363 if (!\array_key_exists($record->getSenderId(), $this->avatars)) {
364 if ($record->getSenderId() === ANONYMOUS_USER_ID) {
365 $avatar = $this->ui_factory->symbol()->avatar()->picture(
366 \ilUtil::getImagePath('logo/ilias_logo_centered.png'),
367 $this->getSender($record)
368 );
369 } else {
371 $avatar = $user?->getAvatar();
372 }
373
374 $this->avatars[$record->getSenderId()] = $avatar
375 ? $this->ui_renderer->render($avatar)
376 : '';
377 }
378
379 return $this->avatars[$record->getSenderId()];
380 }
381
382 private function getStatus(MailRecordData $record): Icon
383 {
384 return $record->isRead()
385 ? $this->ui_factory->symbol()->icon()->standard('mailr', $this->lng->txt('mailr'))
386 : $this->ui_factory->symbol()->icon()->standard('mailu', $this->lng->txt('mailu'));
387 }
388
389 private function getSender(MailRecordData $record): string
390 {
391 if ($record->getSenderId() === ANONYMOUS_USER_ID) {
393 }
394
396 if ($user !== null) {
397 if ($user->hasPublicProfile()) {
398 return $this->ui_renderer->render(
399 $this->ui_factory->link()->standard(
400 $user->getPublicName(),
401 (string) $this->url_builder
402 ->withParameter($this->action_token, self::ACTION_PROFILE)
403 ->withParameter($this->row_id_token, (string) $record->getMailId())
404 ->buildURI()
405 )
406 );
407 }
408
409 return $user->getPublicName();
410 }
411
412 return trim(($record->getImportName() ?? '') . ' (' . $this->lng->txt('user_deleted') . ')');
413 }
414
415 private function getRecipients(MailRecordData $record): string
416 {
417 return $this->refinery->encode()->htmlSpecialCharsAsEntities()->transform(
418 $this->mail->formatNamesForOutput((string) $record->getRcpTo())
419 );
420 }
421
422 private function getSubject(MailRecordData $record): Link
423 {
424 return $this->ui_factory->link()->standard(
425 $this->refinery->encode()->htmlSpecialCharsAsEntities()->transform($record->getSubject()),
426 (string) $this->url_builder
427 ->withParameter(
428 $this->action_token,
429 $this->current_folder->isDrafts() || $this->current_folder->isOutbox() ? self::ACTION_EDIT : self::ACTION_SHOW
430 )
431 ->withParameter($this->row_id_token, (string) $record->getMailId())
432 ->buildURI()
433 );
434 }
435
436 private function getDate(MailRecordData $record): ?DateTimeImmutable
437 {
438 return empty($record->getSendTime()) ? null : $record->getSendTime()->setTimezone($this->user_time_zone);
439 }
440
441 private function getAttachments(MailRecordData $record): string
442 {
443 return $record->hasAttachments()
444 ? $this->ui_renderer->render($this->ui_factory->symbol()->glyph()->attachment())
445 : '';
446 }
447}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
A Date Format provides a format definition akin to PHP's date formatting options, but stores the sing...
Definition: DateFormat.php:27
Builds data types.
Definition: Factory.php:36
Both the subject and the direction need to be specified when expressing an order.
Definition: Order.php:29
join($init, callable $fn)
Definition: Order.php:75
const DESC
Definition: Order.php:31
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
getTotalRowCount(mixed $additional_viewcontrol_data, mixed $filter_data, mixed $additional_parameters)
Mainly for the purpose of pagination-support, it is important to know about the total number of recor...
getAttachments(MailRecordData $record)
getRows(DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, mixed $additional_viewcontrol_data, mixed $filter_data, mixed $additional_parameters)
This is called by the table to retrieve rows; map data-records to rows using the $row_builder e....
__construct(private readonly URLBuilder $url_builder, private readonly URLBuilderToken $action_token, private readonly URLBuilderToken $row_id_token, private readonly URLBuilderToken $folder_token, private readonly array $user_folders, private readonly MailFolderData $current_folder, private readonly MailFolderSearch $search, private readonly ilMail $mail, private readonly Factory $ui_factory, private readonly Renderer $ui_renderer, private readonly ilLanguage $lng, private readonly ServerRequestInterface $http_request, private readonly DataFactory $data_factory, private readonly Refinery $refinery, private readonly DateFormat $date_format, private readonly string $time_format, private readonly DateTimeZone $user_time_zone)
language handling
static getUserObjectById(int $usr_id)
static preloadUserObjects(array $usr_ids)
static _getIliasMailerName()
static getImagePath(string $image_name, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
const ANONYMOUS_USER_ID
Definition: constants.php:27
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This describes how an icon could be modified during construction of UI.
Definition: Icon.php:29
A Column describes the form of presentation for a certain aspect of data, i.e.
Definition: Column.php:28
buildDataRow(string $id, array $record)
This describes a Data Table.
Definition: Data.php:33
This is how the factory for UI elements looks.
Definition: Factory.php:38
An entity that renders components to a string output.
Definition: Renderer.php:31
global $lng
Definition: privfeed.php:31