xref: /webtrees/app/Module/UserMessagesModule.php (revision 492c7072c1472147ff61cf99716e77eacff9560e)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDate;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
23cf700360SGreg Roachuse Fisharebest\Webtrees\Tree;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\User;
25cf700360SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse;
26cf700360SGreg Roachuse Symfony\Component\HttpFoundation\Request;
27cf700360SGreg Roachuse Symfony\Component\HttpFoundation\Response;
288c2e8227SGreg Roach
298c2e8227SGreg Roach/**
308c2e8227SGreg Roach * Class UserMessagesModule
318c2e8227SGreg Roach */
32c1010edaSGreg Roachclass UserMessagesModule extends AbstractModule implements ModuleBlockInterface
33c1010edaSGreg Roach{
348c2e8227SGreg Roach    /** {@inheritdoc} */
35c1010edaSGreg Roach    public function getTitle()
36c1010edaSGreg Roach    {
37c1010edaSGreg Roach        return /* I18N: Name of a module */
38c1010edaSGreg Roach            I18N::translate('Messages');
398c2e8227SGreg Roach    }
408c2e8227SGreg Roach
418c2e8227SGreg Roach    /** {@inheritdoc} */
42c1010edaSGreg Roach    public function getDescription()
43c1010edaSGreg Roach    {
44c1010edaSGreg Roach        return /* I18N: Description of the “Messages” module */
45c1010edaSGreg Roach            I18N::translate('Communicate directly with other users, using private messages.');
468c2e8227SGreg Roach    }
478c2e8227SGreg Roach
4876692c8bSGreg Roach    /**
49cf700360SGreg Roach     * Delete one or messages belonging to a user.
502da2404eSGreg Roach     *
51cf700360SGreg Roach     * @param Request $request
52b6db7c1fSGreg Roach     * @param Tree    $tree
53cf700360SGreg Roach     *
54cf700360SGreg Roach     * @return Response
552da2404eSGreg Roach     */
56b6db7c1fSGreg Roach    public function postDeleteMessageAction(Request $request, Tree $tree): Response
57c1010edaSGreg Roach    {
58cf700360SGreg Roach        $message_ids = (array)$request->get('message_id', []);
59cf700360SGreg Roach
602da2404eSGreg Roach        $stmt = Database::prepare("DELETE FROM `##message` WHERE user_id = :user_id AND message_id = :message_id");
612da2404eSGreg Roach
62cf700360SGreg Roach        foreach ($message_ids as $message_id) {
6313abd6f3SGreg Roach            $stmt->execute([
64cf700360SGreg Roach                'message_id' => $message_id,
652da2404eSGreg Roach                'user_id'    => Auth::id(),
6613abd6f3SGreg Roach            ]);
672da2404eSGreg Roach        }
68cf700360SGreg Roach
69cf700360SGreg Roach        if ($request->get('ctype') === 'user') {
70cf700360SGreg Roach            $url = route('user-page', ['ged' => $tree->getName()]);
71cf700360SGreg Roach        } else {
72cf700360SGreg Roach            $url = route('tree-page', ['ged' => $tree->getName()]);
732da2404eSGreg Roach        }
742da2404eSGreg Roach
75cf700360SGreg Roach        return new RedirectResponse($url);
762da2404eSGreg Roach    }
772da2404eSGreg Roach
782da2404eSGreg Roach    /**
7976692c8bSGreg Roach     * Generate the HTML content of this block.
8076692c8bSGreg Roach     *
81e490cd80SGreg Roach     * @param Tree     $tree
8276692c8bSGreg Roach     * @param int      $block_id
8376692c8bSGreg Roach     * @param bool     $template
84727f238cSGreg Roach     * @param string[] $cfg
8576692c8bSGreg Roach     *
8676692c8bSGreg Roach     * @return string
8776692c8bSGreg Roach     */
88c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
89c1010edaSGreg Roach    {
90e490cd80SGreg Roach        global $ctype;
912da2404eSGreg Roach
928c2e8227SGreg Roach        $messages = Database::prepare("SELECT message_id, sender, subject, body, UNIX_TIMESTAMP(created) AS created FROM `##message` WHERE user_id=? ORDER BY message_id DESC")
9313abd6f3SGreg Roach            ->execute([Auth::id()])
948c2e8227SGreg Roach            ->fetchAll();
958c2e8227SGreg Roach
968c2e8227SGreg Roach        $count = count($messages);
97*492c7072SGreg Roach        $users = array_filter(User::all(), function (User $user): bool {
988c2e8227SGreg Roach            return $user->getUserId() !== Auth::id() && $user->getPreference('verified_by_admin') && $user->getPreference('contactmethod') !== 'none';
998c2e8227SGreg Roach        });
1008c2e8227SGreg Roach
1017bab8982SGreg Roach        $content = '';
102e92490bcSGreg Roach        if (!empty($users)) {
103e490cd80SGreg Roach            $url     = route('user-page', ['ged' => $tree->getName()]);
10446bb661fSGreg Roach            $content .= '<form onsubmit="return $(&quot;#to&quot;).val() !== &quot;&quot;">';
10546bb661fSGreg Roach            $content .= '<input type="hidden" name="route" value="message">';
106e490cd80SGreg Roach            $content .= '<input type="hidden" name="ged" value="' . e($tree->getName()) . '">';
107d53324c9SGreg Roach            $content .= '<input type="hidden" name="url" value="' . e($url) . '">';
1087bab8982SGreg Roach            $content .= '<label for="to">' . I18N::translate('Send a message') . '</label>';
1097bab8982SGreg Roach            $content .= '<select id="to" name="to">';
1108c2e8227SGreg Roach            $content .= '<option value="">' . I18N::translate('&lt;select&gt;') . '</option>';
1118c2e8227SGreg Roach            foreach ($users as $user) {
112d53324c9SGreg Roach                $content .= sprintf('<option value="%1$s">%2$s - %1$s</option>', e($user->getUserName()), e($user->getRealName()));
1138c2e8227SGreg Roach            }
1148c2e8227SGreg Roach            $content .= '</select>';
1157bab8982SGreg Roach            $content .= '<button type="submit">' . I18N::translate('Send') . '</button><br><br>';
1167bab8982SGreg Roach            $content .= '</form>';
1178c2e8227SGreg Roach        }
118c1010edaSGreg Roach        $content .= '<form id="messageform" name="messageform" method="post" action="' . e(route('module', [
119c1010edaSGreg Roach                'action' => 'DeleteMessage',
120c1010edaSGreg Roach                'module' => $this->getName(),
121c1010edaSGreg Roach                'ctype'  => $ctype,
122c1010edaSGreg Roach                'ged'    => $tree->getName(),
123c1010edaSGreg 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);">';
124cf700360SGreg Roach        $content .= csrf_field();
125cf700360SGreg Roach
126e92490bcSGreg Roach        if (!empty($messages)) {
12746bb661fSGreg Roach            $content .= '<table class="list_table w-100"><tr>';
128bc6628fbSRico 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>';
129e284b8e1SGreg Roach            $content .= '<th class="list_label">' . I18N::translate('Subject') . '</th>';
130e284b8e1SGreg Roach            $content .= '<th class="list_label">' . I18N::translate('Date sent') . '</th>';
131e284b8e1SGreg Roach            $content .= '<th class="list_label">' . I18N::translate('Email address') . '</th>';
1328c2e8227SGreg Roach            $content .= '</tr>';
1338c2e8227SGreg Roach            foreach ($messages as $message) {
1348c2e8227SGreg Roach                $content .= '<tr>';
135a33af35aSmakitso                $content .= '<td class="list_value_wrap center"><input type="checkbox" name="message_id[]" value="' . $message->message_id . '" id="cb_message' . $message->message_id . '"></td>';
136d53324c9SGreg Roach                $content .= '<td class="list_value_wrap"><a href="#" onclick="return expand_layer(\'message' . $message->message_id . '\');"><i id="message' . $message->message_id . '_img" class="icon-plus"></i> <b dir="auto">' . e($message->subject) . '</b></a></td>';
1373d7a8a4cSGreg Roach                $content .= '<td class="list_value_wrap">' . FunctionsDate::formatTimestamp($message->created + WT_TIMESTAMP_OFFSET) . '</td>';
1388c2e8227SGreg Roach                $content .= '<td class="list_value_wrap">';
1398c2e8227SGreg Roach                $user    = User::findByIdentifier($message->sender);
1408c2e8227SGreg Roach                if ($user) {
1411a46bd06SGreg Roach                    $content .= '<span dir="auto">' . e($user->getRealName()) . '</span> - <span dir="auto">' . $user->getEmail() . '</span>';
1428c2e8227SGreg Roach                } else {
143d53324c9SGreg Roach                    $content .= '<a href="mailto:' . e($message->sender) . '">' . e($message->sender) . '</a>';
1448c2e8227SGreg Roach                }
1458c2e8227SGreg Roach                $content .= '</td>';
1468c2e8227SGreg Roach                $content .= '</tr>';
1478c2e8227SGreg Roach                $content .= '<tr><td class="list_value_wrap" colspan="4"><div id="message' . $message->message_id . '" style="display:none;">';
148e490cd80SGreg Roach                $content .= '<div dir="auto" style="white-space: pre-wrap;">' . Filter::expandUrls($message->body, $tree) . '</div><br>';
149c1010edaSGreg Roach                if (strpos($message->subject, /* I18N: When replying to an email, the subject becomes “RE: <subject>” */
150c1010edaSGreg Roach                        I18N::translate('RE: ')) !== 0) {
1518c2e8227SGreg Roach                    $message->subject = I18N::translate('RE: ') . $message->subject;
1528c2e8227SGreg Roach                }
15346bb661fSGreg Roach
15446bb661fSGreg Roach                // If this user still exists, show a reply link.
1558c2e8227SGreg Roach                if ($user) {
156c1010edaSGreg Roach                    $reply_url = route('message', [
157c1010edaSGreg Roach                        'to'      => $user->getUserName(),
158c1010edaSGreg Roach                        'subject' => $message->subject,
159c1010edaSGreg Roach                        'ged'     => $tree->getName(),
160c1010edaSGreg Roach                    ]);
16146bb661fSGreg Roach                    $content   .= '<a class="btn btn-primary" href="' . e($reply_url) . '" title="' . I18N::translate('Reply') . '">' . I18N::translate('Reply') . '</a> ';
1628c2e8227SGreg Roach                }
16346bb661fSGreg 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>';
1648c2e8227SGreg Roach            }
1658c2e8227SGreg Roach            $content .= '</table>';
1662da2404eSGreg Roach            $content .= '<p><button type="submit">' . I18N::translate('Delete selected messages') . '</button></p>';
1678c2e8227SGreg Roach        }
1688c2e8227SGreg Roach        $content .= '</form>';
1698c2e8227SGreg Roach
1708c2e8227SGreg Roach        if ($template) {
171147e99aaSGreg Roach            return view('modules/block-template', [
1729c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1739c6524dcSGreg Roach                'id'         => $block_id,
1749c6524dcSGreg Roach                'config_url' => '',
175f7f4b984SGreg Roach                'title'      => I18N::plural('%s message', '%s messages', $count, I18N::number($count)),
1769c6524dcSGreg Roach                'content'    => $content,
1779c6524dcSGreg Roach            ]);
1788c2e8227SGreg Roach        } else {
1798c2e8227SGreg Roach            return $content;
1808c2e8227SGreg Roach        }
1818c2e8227SGreg Roach    }
1828c2e8227SGreg Roach
1838c2e8227SGreg Roach    /** {@inheritdoc} */
184c1010edaSGreg Roach    public function loadAjax(): bool
185c1010edaSGreg Roach    {
1868c2e8227SGreg Roach        return false;
1878c2e8227SGreg Roach    }
1888c2e8227SGreg Roach
1898c2e8227SGreg Roach    /** {@inheritdoc} */
190c1010edaSGreg Roach    public function isUserBlock(): bool
191c1010edaSGreg Roach    {
1928c2e8227SGreg Roach        return true;
1938c2e8227SGreg Roach    }
1948c2e8227SGreg Roach
1958c2e8227SGreg Roach    /** {@inheritdoc} */
196c1010edaSGreg Roach    public function isGedcomBlock(): bool
197c1010edaSGreg Roach    {
1988c2e8227SGreg Roach        return false;
1998c2e8227SGreg Roach    }
2008c2e8227SGreg Roach
20176692c8bSGreg Roach    /**
20276692c8bSGreg Roach     * An HTML form to edit block settings
20376692c8bSGreg Roach     *
204e490cd80SGreg Roach     * @param Tree $tree
20576692c8bSGreg Roach     * @param int  $block_id
206a9430be8SGreg Roach     *
207a9430be8SGreg Roach     * @return void
20876692c8bSGreg Roach     */
209c1010edaSGreg Roach    public function configureBlock(Tree $tree, int $block_id)
210c1010edaSGreg Roach    {
2118c2e8227SGreg Roach    }
2128c2e8227SGreg Roach}
213