xref: /webtrees/app/Module/UserMessagesModule.php (revision a427f55aa3bfca02b905c8124daec898bfa98778)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98c2e8227SGreg Roach * (at your option) any later version.
108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138c2e8227SGreg Roach * GNU General Public License for more details.
148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
234459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
24e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
26e381f98dSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\MessagePage;
27e381f98dSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\MessageSelect;
288e0e1b25SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\TreePage;
298e0e1b25SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\UserPage;
300e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
31e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
32cf700360SGreg Roachuse Fisharebest\Webtrees\Tree;
330e62c4b8SGreg Roachuse Fisharebest\Webtrees\User;
34d39b150cSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
351e7a7a28SGreg Roachuse Illuminate\Support\Str;
366ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
376ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
38ca52a408SGreg Roachuse stdClass;
3971378461SGreg Roach
404ea62551SGreg Roachuse function assert;
4183615acfSGreg Roachuse function e;
4283615acfSGreg Roachuse function route;
43*a427f55aSGreg Roachuse function view;
448c2e8227SGreg Roach
458c2e8227SGreg Roach/**
468c2e8227SGreg Roach * Class UserMessagesModule
478c2e8227SGreg Roach */
4837eb8894SGreg Roachclass UserMessagesModule extends AbstractModule implements ModuleBlockInterface
49c1010edaSGreg Roach{
5049a243cbSGreg Roach    use ModuleBlockTrait;
5149a243cbSGreg Roach
52961ec755SGreg Roach    /**
53e5a6b4d4SGreg Roach     * @var UserService
54e5a6b4d4SGreg Roach     */
55e5a6b4d4SGreg Roach    private $user_service;
56e5a6b4d4SGreg Roach
57e5a6b4d4SGreg Roach    /**
58e5a6b4d4SGreg Roach     * UserMessagesModule constructor.
59e5a6b4d4SGreg Roach     *
60e5a6b4d4SGreg Roach     * @param UserService $user_service
61e5a6b4d4SGreg Roach     */
62e2cbf57aSGreg Roach    public function __construct(UserService $user_service)
63e2cbf57aSGreg Roach    {
64e5a6b4d4SGreg Roach        $this->user_service = $user_service;
65e5a6b4d4SGreg Roach    }
66e5a6b4d4SGreg Roach
67e5a6b4d4SGreg Roach    /**
680cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
69961ec755SGreg Roach     *
70961ec755SGreg Roach     * @return string
71961ec755SGreg Roach     */
7249a243cbSGreg Roach    public function title(): string
73c1010edaSGreg Roach    {
74bbb76c12SGreg Roach        /* I18N: Name of a module */
75bbb76c12SGreg Roach        return I18N::translate('Messages');
768c2e8227SGreg Roach    }
778c2e8227SGreg Roach
78961ec755SGreg Roach    /**
79961ec755SGreg Roach     * A sentence describing what this module does.
80961ec755SGreg Roach     *
81961ec755SGreg Roach     * @return string
82961ec755SGreg Roach     */
8349a243cbSGreg Roach    public function description(): string
84c1010edaSGreg Roach    {
85bbb76c12SGreg Roach        /* I18N: Description of the “Messages” module */
86bbb76c12SGreg Roach        return I18N::translate('Communicate directly with other users, using private messages.');
878c2e8227SGreg Roach    }
888c2e8227SGreg Roach
8976692c8bSGreg Roach    /**
90cf700360SGreg Roach     * Delete one or messages belonging to a user.
912da2404eSGreg Roach     *
926ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
93cf700360SGreg Roach     *
946ccdf4f0SGreg Roach     * @return ResponseInterface
952da2404eSGreg Roach     */
9657ab2231SGreg Roach    public function postDeleteMessageAction(ServerRequestInterface $request): ResponseInterface
97c1010edaSGreg Roach    {
9857ab2231SGreg Roach        $tree = $request->getAttribute('tree');
994ea62551SGreg Roach        assert($tree instanceof Tree);
1004ea62551SGreg Roach
101b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
102b46c87bdSGreg Roach
103b46c87bdSGreg Roach        $message_ids = $params['message_id'] ?? [];
104cf700360SGreg Roach
105d39b150cSGreg Roach        DB::table('message')
106d39b150cSGreg Roach            ->where('user_id', '=', Auth::id())
107d39b150cSGreg Roach            ->whereIn('message_id', $message_ids)
108d39b150cSGreg Roach            ->delete();
109cf700360SGreg Roach
1103caaa4d2SGreg Roach        if ($request->getQueryParams()['context'] === ModuleBlockInterface::CONTEXT_USER_PAGE) {
1118e0e1b25SGreg Roach            $url = route(UserPage::class, ['tree' => $tree->name()]);
112cf700360SGreg Roach        } else {
1138e0e1b25SGreg Roach            $url = route(TreePage::class, ['tree' => $tree->name()]);
1142da2404eSGreg Roach        }
1152da2404eSGreg Roach
1166ccdf4f0SGreg Roach        return redirect($url);
1172da2404eSGreg Roach    }
1182da2404eSGreg Roach
1192da2404eSGreg Roach    /**
12076692c8bSGreg Roach     * Generate the HTML content of this block.
12176692c8bSGreg Roach     *
122e490cd80SGreg Roach     * @param Tree     $tree
12376692c8bSGreg Roach     * @param int      $block_id
1243caaa4d2SGreg Roach     * @param string   $context
1253caaa4d2SGreg Roach     * @param string[] $config
12676692c8bSGreg Roach     *
12776692c8bSGreg Roach     * @return string
12876692c8bSGreg Roach     */
1293caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
130c1010edaSGreg Roach    {
131d39b150cSGreg Roach        $messages = DB::table('message')
132d39b150cSGreg Roach            ->where('user_id', '=', Auth::id())
133d39b150cSGreg Roach            ->orderByDesc('message_id')
134ca52a408SGreg Roach            ->get()
1350b5fd0a6SGreg Roach            ->map(static function (stdClass $row): stdClass {
1364459dc9aSGreg Roach                $row->created = Carbon::make($row->created);
137ca52a408SGreg Roach
138ca52a408SGreg Roach                return $row;
139ca52a408SGreg Roach            });
1408c2e8227SGreg Roach
1410b5fd0a6SGreg Roach        $users = $this->user_service->all()->filter(static function (UserInterface $user) use ($tree): bool {
14276a8e0f3SGreg Roach            $public_tree  = $tree->getPreference('REQUIRE_AUTHENTICATION') !== '1';
14376a8e0f3SGreg Roach            $can_see_tree = $public_tree || Auth::accessLevel($tree, $user) <= Auth::PRIV_USER;
14476a8e0f3SGreg Roach
14576a8e0f3SGreg Roach            return
146895230eeSGreg Roach                $user->id() !== Auth::id() &&
1477c4add84SGreg Roach                $user->getPreference(User::PREF_IS_ACCOUNT_APPROVED) &&
14876a8e0f3SGreg Roach                $can_see_tree &&
1497c4add84SGreg Roach                $user->getPreference(User::PREF_CONTACT_METHOD) !== 'none';
1508c2e8227SGreg Roach        });
1518c2e8227SGreg Roach
1527bab8982SGreg Roach        $content = '';
1538b67c11aSGreg Roach        if ($users->isNotEmpty()) {
1548e0e1b25SGreg Roach            $url = route(UserPage::class, ['tree' => $tree->name()]);
155d39b150cSGreg Roach
156e381f98dSGreg Roach            $content .= '<form method="post" action="' . e(route(MessageSelect::class, ['tree' => $tree->name()])) . '">';
157e381f98dSGreg Roach            $content .= csrf_field();
158d53324c9SGreg Roach            $content .= '<input type="hidden" name="url" value="' . e($url) . '">';
1597bab8982SGreg Roach            $content .= '<label for="to">' . I18N::translate('Send a message') . '</label>';
160e381f98dSGreg Roach            $content .= '<select id="to" name="to" required>';
1618c2e8227SGreg Roach            $content .= '<option value="">' . I18N::translate('&lt;select&gt;') . '</option>';
1628c2e8227SGreg Roach            foreach ($users as $user) {
163e5a6b4d4SGreg Roach                $content .= sprintf('<option value="%1$s">%2$s - %1$s</option>', e($user->userName()), e($user->realName()));
1648c2e8227SGreg Roach            }
1658c2e8227SGreg Roach            $content .= '</select>';
1667bab8982SGreg Roach            $content .= '<button type="submit">' . I18N::translate('Send') . '</button><br><br>';
1677bab8982SGreg Roach            $content .= '</form>';
1688c2e8227SGreg Roach        }
16983615acfSGreg Roach        $content .= '<form method="post" action="' . e(route('module', [
170c1010edaSGreg Roach                'action'  => 'DeleteMessage',
17126684e68SGreg Roach                'module'  => $this->name(),
1723caaa4d2SGreg Roach                'context' => $context,
173d72b284aSGreg Roach                'tree'     => $tree->name(),
17483615acfSGreg Roach            ])) . '" data-confirm="' . I18N::translate('Are you sure you want to delete this message? It cannot be retrieved later.') . '" onsubmit="return confirm(this.dataset.confirm);" id="messageform" name="messageform">';
175cf700360SGreg Roach        $content .= csrf_field();
176cf700360SGreg Roach
177d39b150cSGreg Roach        if ($messages->isNotEmpty()) {
178b38a7b85SGreg Roach            $content .= '<div class="table-responsive">';
179b38a7b85SGreg Roach            $content .= '<table class="table table-sm w-100"><tr>';
180bc6628fbSRico Sonntag            $content .= '<th class="list_label">' . I18N::translate('Delete') . '<br><a href="#" onclick="$(\'#block-' . $block_id . ' :checkbox\').prop(\'checked\', true); return false;">' . I18N::translate('All') . '</a></th>';
181e284b8e1SGreg Roach            $content .= '<th class="list_label">' . I18N::translate('Subject') . '</th>';
182e284b8e1SGreg Roach            $content .= '<th class="list_label">' . I18N::translate('Date sent') . '</th>';
183e284b8e1SGreg Roach            $content .= '<th class="list_label">' . I18N::translate('Email address') . '</th>';
1848c2e8227SGreg Roach            $content .= '</tr>';
1858c2e8227SGreg Roach            foreach ($messages as $message) {
186*a427f55aSGreg Roach                $content .= '<tr>' .
187*a427f55aSGreg Roach                    '<td class="list_value_wrap center"><input type="checkbox" name="message_id[]" value="' . $message->message_id . '" id="cb_message' . $message->message_id . '"></td>' .
188*a427f55aSGreg Roach                    '<td class="list_value_wrap">' .
189*a427f55aSGreg Roach                    '<a href="#message' . $message->message_id . '" data-toggle="collapse" role="button" aria-expanded="false" aria-controls="message' . $message->message_id . '">' .
190*a427f55aSGreg Roach                    view('icons/expand') .
191*a427f55aSGreg Roach                    view('icons/collapse') .
192*a427f55aSGreg Roach                    '<b dir="auto">' . e($message->subject) . '</b>' .
193*a427f55aSGreg Roach                    '</a></td>' .
194*a427f55aSGreg Roach                    '<td class="list_value_wrap">' . view('components/datetime', ['timestamp' => $message->created]) . '</td>' .
195*a427f55aSGreg Roach                    '<td class="list_value_wrap">';
196d39b150cSGreg Roach
197e5a6b4d4SGreg Roach                $user = $this->user_service->findByIdentifier($message->sender);
198d39b150cSGreg Roach
19926eae716SGreg Roach                if ($user instanceof User) {
200e5a6b4d4SGreg Roach                    $content .= '<span dir="auto">' . e($user->realName()) . '</span> - <span dir="auto">' . $user->email() . '</span>';
2018c2e8227SGreg Roach                } else {
202d53324c9SGreg Roach                    $content .= '<a href="mailto:' . e($message->sender) . '">' . e($message->sender) . '</a>';
2038c2e8227SGreg Roach                }
204d39b150cSGreg Roach
2058c2e8227SGreg Roach                $content .= '</td>';
2068c2e8227SGreg Roach                $content .= '</tr>';
207*a427f55aSGreg Roach                $content .= '<tr><td class="list_value_wrap" colspan="4"><div id="message' . $message->message_id . '" class="collapse">';
208e490cd80SGreg Roach                $content .= '<div dir="auto" style="white-space: pre-wrap;">' . Filter::expandUrls($message->body, $tree) . '</div><br>';
209d39b150cSGreg Roach
210bbb76c12SGreg Roach                /* I18N: When replying to an email, the subject becomes “RE: <subject>” */
211bbb76c12SGreg Roach                if (strpos($message->subject, I18N::translate('RE: ')) !== 0) {
2128c2e8227SGreg Roach                    $message->subject = I18N::translate('RE: ') . $message->subject;
2138c2e8227SGreg Roach                }
21446bb661fSGreg Roach
21546bb661fSGreg Roach                // If this user still exists, show a reply link.
216e381f98dSGreg Roach                if ($user instanceof User) {
217e381f98dSGreg Roach                    $reply_url = route(MessagePage::class, [
218c1010edaSGreg Roach                        'subject' => $message->subject,
219d72b284aSGreg Roach                        'to'      => $user->userName(),
220d72b284aSGreg Roach                        'tree'    => $tree->name(),
2218e0e1b25SGreg Roach                        'url'     => route(UserPage::class, ['tree' => $tree->name()]),
222c1010edaSGreg Roach                    ]);
223d39b150cSGreg Roach
22446bb661fSGreg Roach                    $content .= '<a class="btn btn-primary" href="' . e($reply_url) . '" title="' . I18N::translate('Reply') . '">' . I18N::translate('Reply') . '</a> ';
2258c2e8227SGreg Roach                }
22646bb661fSGreg Roach                $content .= '<button type="button" class="btn btn-danger" data-confirm="' . I18N::translate('Are you sure you want to delete this message? It cannot be retrieved later.') . '" onclick="if (confirm(this.dataset.confirm)) {$(\'#messageform :checkbox\').prop(\'checked\', false); $(\'#cb_message' . $message->message_id . '\').prop(\'checked\', true); document.messageform.submit();}">' . I18N::translate('Delete') . '</button></div></td></tr>';
2278c2e8227SGreg Roach            }
2288c2e8227SGreg Roach            $content .= '</table>';
229b38a7b85SGreg Roach            $content .= '</div>';
2302da2404eSGreg Roach            $content .= '<p><button type="submit">' . I18N::translate('Delete selected messages') . '</button></p>';
2318c2e8227SGreg Roach        }
2328c2e8227SGreg Roach        $content .= '</form>';
2338c2e8227SGreg Roach
2343caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
235d39b150cSGreg Roach            $count = $messages->count();
236d39b150cSGreg Roach
237147e99aaSGreg Roach            return view('modules/block-template', [
2381e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
2399c6524dcSGreg Roach                'id'         => $block_id,
2409c6524dcSGreg Roach                'config_url' => '',
241f7f4b984SGreg Roach                'title'      => I18N::plural('%s message', '%s messages', $count, I18N::number($count)),
2429c6524dcSGreg Roach                'content'    => $content,
2439c6524dcSGreg Roach            ]);
2448c2e8227SGreg Roach        }
245b2ce94c6SRico Sonntag
246b2ce94c6SRico Sonntag        return $content;
2478c2e8227SGreg Roach    }
2488c2e8227SGreg Roach
2493caaa4d2SGreg Roach    /**
2503caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
2513caaa4d2SGreg Roach     *
2523caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
2533caaa4d2SGreg Roach     *
2543caaa4d2SGreg Roach     * @return bool
2553caaa4d2SGreg Roach     */
256c1010edaSGreg Roach    public function loadAjax(): bool
257c1010edaSGreg Roach    {
2588c2e8227SGreg Roach        return false;
2598c2e8227SGreg Roach    }
2608c2e8227SGreg Roach
2613caaa4d2SGreg Roach    /**
2623caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
2633caaa4d2SGreg Roach     *
2643caaa4d2SGreg Roach     * @return bool
2653caaa4d2SGreg Roach     */
266c1010edaSGreg Roach    public function isUserBlock(): bool
267c1010edaSGreg Roach    {
2688c2e8227SGreg Roach        return true;
2698c2e8227SGreg Roach    }
2708c2e8227SGreg Roach
2713caaa4d2SGreg Roach    /**
2723caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
2733caaa4d2SGreg Roach     *
2743caaa4d2SGreg Roach     * @return bool
2753caaa4d2SGreg Roach     */
27663276d8fSGreg Roach    public function isTreeBlock(): bool
277c1010edaSGreg Roach    {
2788c2e8227SGreg Roach        return false;
2798c2e8227SGreg Roach    }
2808c2e8227SGreg Roach}
281