xref: /webtrees/resources/views/modules/user-messages/user-messages.phtml (revision b11cdcd45131b1585d66693fab363cfeb18c51a4)
1<?php
2
3declare(strict_types=1);
4
5use Fisharebest\Webtrees\Http\RequestHandlers\MessagePage;
6use Fisharebest\Webtrees\Http\RequestHandlers\MessageSelect;
7use Fisharebest\Webtrees\Http\RequestHandlers\UserPage;
8use Fisharebest\Webtrees\I18N;
9use Fisharebest\Webtrees\Module\ModuleInterface;
10use Fisharebest\Webtrees\Registry;
11use Fisharebest\Webtrees\Services\UserService;
12use Fisharebest\Webtrees\Tree;
13use Fisharebest\Webtrees\User;
14use Illuminate\Support\Collection;
15
16/**
17 * @var int                    $block_id
18 * @var string                 $context
19 * @var ModuleInterface        $module
20 * @var Tree                   $tree
21 * @var Collection<int,object> $messages
22 * @var Collection<int,User>   $users
23 * @var UserService            $user_service
24 */
25
26?>
27
28<?php if ($users->isNotEmpty()) : ?>
29    <form method="post" action="<?= e(route(MessageSelect::class, ['tree' => $tree->name()])) ?>">
30        <input type="hidden" name="url" value="<?= e(route(UserPage::class, ['tree' => $tree->name()])) ?>">
31        <label for="to"><?= I18N::translate('Send a message') ?></label>
32
33        <div class="input-group">
34            <select class="form-select" id="to" name="to" required="required">
35                <option value=""><?= I18N::translate('&lt;select&gt;') ?></option>
36                <?php foreach ($users as $user) : ?>
37                    <option value="<?= e($user->userName()) ?>">
38                        <?= e($user->realName()) ?> - <?= e($user->userName()) ?>
39                    </option>
40                <?php endforeach ?>
41            </select>
42
43            <button type="submit" class="btn btn-primary"><?= I18N::translate('Send') ?></button>
44        </div>
45        <?= csrf_field() ?>
46    </form>
47    <br><br>
48<?php endif ?>
49
50<form
51    method="post"
52    action="<?= e(route('module', ['action' => 'DeleteMessage', 'module' => $module->name(), 'context' => $context, 'tree' => $tree->name()])) ?>"
53    data-wt-confirm="<?= I18N::translate('Are you sure you want to delete this message? It cannot be retrieved later.') ?>"
54    onsubmit="return confirm(this.dataset.confirm);"
55    id="messageform"
56    name="messageform"
57>
58    <?php if ($messages->isNotEmpty()) : ?>
59        <div class="table-responsive">
60            <table class="table-sm w-100">
61                <tr>
62                    <th class="wt-page-options-label">
63                        <?= I18N::translate('Delete') ?>
64                        <br>
65                        <a href="#"
66                           onclick="$('#block-<?= $block_id ?> :checkbox').prop('checked', true); return false;">
67                            <?= I18N::translate('All') ?>
68                        </a>
69                    </th>
70                    <th class="wt-page-options-label"><?= I18N::translate('Subject') ?></th>
71                    <th class="wt-page-options-label"><?= I18N::translate('Date sent') ?></th>
72                    <th class="wt-page-options-label"><?= I18N::translate('Email address') ?></th>
73                </tr>
74
75                <?php foreach ($messages as $message) : ?>
76                    <tr>
77                        <td class="wt-page-options-value center">
78                            <input type="checkbox" name="message_id[]" value="<?= $message->message_id ?>" id="cb_message<?= $message->message_id ?>">
79                        </td>
80
81                        <td class="wt-page-options-value">
82                            <a href="#message<?= $message->message_id ?>" data-bs-toggle="collapse" role="button"
83                               aria-expanded="false" aria-controls="message<?= $message->message_id ?>">
84                                <?= view('icons/expand') ?>
85                                <?= view('icons/collapse') ?>
86                                <b dir="auto"><?= e($message->subject) ?></b>
87                            </a>
88                        </td>
89
90                        <td class="wt-page-options-value">
91                            <?= view('components/datetime', ['timestamp' => $message->created]) ?>
92                        </td>
93
94                        <td class="wt-page-options-value">
95                            <?php $user = $user_service->findByIdentifier($message->sender) ?>
96
97                            <?php if ($user instanceof User) : ?>
98                                <bdi><?= e($user->realName()) ?></bdi> - <bdi><?= $user->email() ?></bdi>
99                            <?php else : ?>
100                                <a href="mailto:<?= e($message->sender) ?>"><?= e($message->sender) ?></a>
101                            <?php endif ?>
102                        </td>
103                    </tr>
104
105                    <tr>
106                        <td class="wt-page-options-value" colspan="4">
107                            <div id="message<?= $message->message_id ?>" class="collapse">
108                                <div dir="auto" style="white-space: pre-wrap;">
109                                    <?= Registry::markdownFactory()->autolink($message->body) ?>
110                                </div>
111                                <br>
112
113                                <?php
114                                /* I18N: When replying to an email, the subject becomes “RE: subject” */
115                                if (!str_starts_with($message->subject, I18N::translate('RE: '))) {
116                                    $message->subject = I18N::translate('RE: ') . $message->subject;
117                                }
118
119                                // If this user still exists, show a reply link.
120                                if ($user instanceof User) {
121                                    $reply_url = route(MessagePage::class, [
122                                        'subject' => $message->subject,
123                                        'to'      => $user->userName(),
124                                        'tree'    => $tree->name(),
125                                        'url'     => route(UserPage::class, ['tree' => $tree->name()]),
126                                    ]);
127                                    ?>
128
129                                    <a class="btn btn-primary" href="<?= e($reply_url) ?>"
130                                       title="<?= I18N::translate('Reply') ?>">
131                                        <?= I18N::translate('Reply') ?>
132                                    </a>
133                                <?php } ?>
134                                <button type="button" class="btn btn-danger"
135                                        data-wt-confirm="<?= I18N::translate('Are you sure you want to delete this message? It cannot be retrieved later.') ?>"
136                                        onclick="if (confirm(this.dataset.confirm)) {$('#messageform :checkbox').prop('checked', false); $('#cb_message<?= $message->message_id ?>').prop('checked', true); document.messageform.submit();}">
137                                    <?= I18N::translate('Delete') ?>
138                                </button>
139                            </div>
140                        </td>
141                    </tr>
142                <?php endforeach ?>
143            </table>
144        </div>
145        <p>
146            <button type="submit" class="btn btn-primary"><?= I18N::translate('Delete selected messages') ?></button>
147        </p>
148    <?php endif ?>
149
150    <?= csrf_field() ?>
151</form>
152