xref: /webtrees/app/Module/UserMessagesModule.php (revision 26684e686fb5ab50ecb57e7e6c6a0a55852d2203)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 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 */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
223d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDate;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
24cf700360SGreg Roachuse Fisharebest\Webtrees\Tree;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\User;
26d39b150cSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
27cf700360SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse;
28cf700360SGreg Roachuse Symfony\Component\HttpFoundation\Request;
29cf700360SGreg Roachuse Symfony\Component\HttpFoundation\Response;
308c2e8227SGreg Roach
318c2e8227SGreg Roach/**
328c2e8227SGreg Roach * Class UserMessagesModule
338c2e8227SGreg Roach */
3449a243cbSGreg Roachclass UserMessagesModule extends AbstractModule implements ModuleInterface, ModuleBlockInterface
35c1010edaSGreg Roach{
3649a243cbSGreg Roach    use ModuleBlockTrait;
3749a243cbSGreg Roach
38961ec755SGreg Roach    /**
39961ec755SGreg Roach     * How should this module be labelled on tabs, menus, etc.?
40961ec755SGreg Roach     *
41961ec755SGreg Roach     * @return string
42961ec755SGreg Roach     */
4349a243cbSGreg Roach    public function title(): string
44c1010edaSGreg Roach    {
45bbb76c12SGreg Roach        /* I18N: Name of a module */
46bbb76c12SGreg Roach        return I18N::translate('Messages');
478c2e8227SGreg Roach    }
488c2e8227SGreg Roach
49961ec755SGreg Roach    /**
50961ec755SGreg Roach     * A sentence describing what this module does.
51961ec755SGreg Roach     *
52961ec755SGreg Roach     * @return string
53961ec755SGreg Roach     */
5449a243cbSGreg Roach    public function description(): string
55c1010edaSGreg Roach    {
56bbb76c12SGreg Roach        /* I18N: Description of the “Messages” module */
57bbb76c12SGreg Roach        return I18N::translate('Communicate directly with other users, using private messages.');
588c2e8227SGreg Roach    }
598c2e8227SGreg Roach
6076692c8bSGreg Roach    /**
61cf700360SGreg Roach     * Delete one or messages belonging to a user.
622da2404eSGreg Roach     *
63cf700360SGreg Roach     * @param Request $request
64b6db7c1fSGreg Roach     * @param Tree    $tree
65cf700360SGreg Roach     *
66cf700360SGreg Roach     * @return Response
672da2404eSGreg Roach     */
68b6db7c1fSGreg Roach    public function postDeleteMessageAction(Request $request, Tree $tree): Response
69c1010edaSGreg Roach    {
70cf700360SGreg Roach        $message_ids = (array) $request->get('message_id', []);
71cf700360SGreg Roach
72d39b150cSGreg Roach        DB::table('message')
73d39b150cSGreg Roach            ->where('user_id', '=', Auth::id())
74d39b150cSGreg Roach            ->whereIn('message_id', $message_ids)
75d39b150cSGreg Roach            ->delete();
76cf700360SGreg Roach
77cf700360SGreg Roach        if ($request->get('ctype') === 'user') {
78aa6f03bbSGreg Roach            $url = route('user-page', ['ged' => $tree->name()]);
79cf700360SGreg Roach        } else {
80aa6f03bbSGreg Roach            $url = route('tree-page', ['ged' => $tree->name()]);
812da2404eSGreg Roach        }
822da2404eSGreg Roach
83cf700360SGreg Roach        return new RedirectResponse($url);
842da2404eSGreg Roach    }
852da2404eSGreg Roach
862da2404eSGreg Roach    /**
8776692c8bSGreg Roach     * Generate the HTML content of this block.
8876692c8bSGreg Roach     *
89e490cd80SGreg Roach     * @param Tree     $tree
9076692c8bSGreg Roach     * @param int      $block_id
915f2ae573SGreg Roach     * @param string   $ctype
92727f238cSGreg Roach     * @param string[] $cfg
9376692c8bSGreg Roach     *
9476692c8bSGreg Roach     * @return string
9576692c8bSGreg Roach     */
965f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
97c1010edaSGreg Roach    {
98d39b150cSGreg Roach        $messages = DB::table('message')
99d39b150cSGreg Roach            ->where('user_id', '=', Auth::id())
100d39b150cSGreg Roach            ->orderByDesc('message_id')
101d39b150cSGreg Roach            ->select(['message_id', 'sender', 'subject', 'body', DB::raw('UNIX_TIMESTAMP(created) AS created')])
102d39b150cSGreg Roach            ->get();
1038c2e8227SGreg Roach
1048b67c11aSGreg Roach        $users = User::all()->filter(function (User $user) use ($tree): bool {
10576a8e0f3SGreg Roach            $public_tree  = $tree->getPreference('REQUIRE_AUTHENTICATION') !== '1';
10676a8e0f3SGreg Roach            $can_see_tree = $public_tree || Auth::accessLevel($tree, $user) <= Auth::PRIV_USER;
10776a8e0f3SGreg Roach
10876a8e0f3SGreg Roach            return
109895230eeSGreg Roach                $user->id() !== Auth::id() &&
11076a8e0f3SGreg Roach                $user->getPreference('verified_by_admin') &&
11176a8e0f3SGreg Roach                $can_see_tree &&
11276a8e0f3SGreg Roach                $user->getPreference('contactmethod') !== 'none';
1138c2e8227SGreg Roach        });
1148c2e8227SGreg Roach
1157bab8982SGreg Roach        $content = '';
1168b67c11aSGreg Roach        if ($users->isNotEmpty()) {
117aa6f03bbSGreg Roach            $url = route('user-page', ['ged' => $tree->name()]);
118d39b150cSGreg Roach
11946bb661fSGreg Roach            $content .= '<form onsubmit="return $(&quot;#to&quot;).val() !== &quot;&quot;">';
12046bb661fSGreg Roach            $content .= '<input type="hidden" name="route" value="message">';
121aa6f03bbSGreg Roach            $content .= '<input type="hidden" name="ged" value="' . e($tree->name()) . '">';
122d53324c9SGreg Roach            $content .= '<input type="hidden" name="url" value="' . e($url) . '">';
1237bab8982SGreg Roach            $content .= '<label for="to">' . I18N::translate('Send a message') . '</label>';
1247bab8982SGreg Roach            $content .= '<select id="to" name="to">';
1258c2e8227SGreg Roach            $content .= '<option value="">' . I18N::translate('&lt;select&gt;') . '</option>';
1268c2e8227SGreg Roach            foreach ($users as $user) {
127d53324c9SGreg Roach                $content .= sprintf('<option value="%1$s">%2$s - %1$s</option>', e($user->getUserName()), e($user->getRealName()));
1288c2e8227SGreg Roach            }
1298c2e8227SGreg Roach            $content .= '</select>';
1307bab8982SGreg Roach            $content .= '<button type="submit">' . I18N::translate('Send') . '</button><br><br>';
1317bab8982SGreg Roach            $content .= '</form>';
1328c2e8227SGreg Roach        }
133c1010edaSGreg Roach        $content .= '<form id="messageform" name="messageform" method="post" action="' . e(route('module', [
134c1010edaSGreg Roach                'action' => 'DeleteMessage',
135*26684e68SGreg Roach                'module' => $this->name(),
136c1010edaSGreg Roach                'ctype'  => $ctype,
137aa6f03bbSGreg Roach                'ged'    => $tree->name(),
138c1010edaSGreg 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);">';
139cf700360SGreg Roach        $content .= csrf_field();
140cf700360SGreg Roach
141d39b150cSGreg Roach        if ($messages->isNotEmpty()) {
14246bb661fSGreg Roach            $content .= '<table class="list_table w-100"><tr>';
143bc6628fbSRico 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>';
144e284b8e1SGreg Roach            $content .= '<th class="list_label">' . I18N::translate('Subject') . '</th>';
145e284b8e1SGreg Roach            $content .= '<th class="list_label">' . I18N::translate('Date sent') . '</th>';
146e284b8e1SGreg Roach            $content .= '<th class="list_label">' . I18N::translate('Email address') . '</th>';
1478c2e8227SGreg Roach            $content .= '</tr>';
1488c2e8227SGreg Roach            foreach ($messages as $message) {
1498c2e8227SGreg Roach                $content .= '<tr>';
150a33af35aSmakitso                $content .= '<td class="list_value_wrap center"><input type="checkbox" name="message_id[]" value="' . $message->message_id . '" id="cb_message' . $message->message_id . '"></td>';
151d53324c9SGreg 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>';
1521b367094SGreg Roach                $content .= '<td class="list_value_wrap">' . FunctionsDate::formatTimestamp((int) $message->created) . '</td>';
1538c2e8227SGreg Roach                $content .= '<td class="list_value_wrap">';
154d39b150cSGreg Roach
1558c2e8227SGreg Roach                $user = User::findByIdentifier($message->sender);
156d39b150cSGreg Roach
15726eae716SGreg Roach                if ($user instanceof User) {
1581a46bd06SGreg Roach                    $content .= '<span dir="auto">' . e($user->getRealName()) . '</span> - <span dir="auto">' . $user->getEmail() . '</span>';
1598c2e8227SGreg Roach                } else {
160d53324c9SGreg Roach                    $content .= '<a href="mailto:' . e($message->sender) . '">' . e($message->sender) . '</a>';
1618c2e8227SGreg Roach                }
162d39b150cSGreg Roach
1638c2e8227SGreg Roach                $content .= '</td>';
1648c2e8227SGreg Roach                $content .= '</tr>';
1658c2e8227SGreg Roach                $content .= '<tr><td class="list_value_wrap" colspan="4"><div id="message' . $message->message_id . '" style="display:none;">';
166e490cd80SGreg Roach                $content .= '<div dir="auto" style="white-space: pre-wrap;">' . Filter::expandUrls($message->body, $tree) . '</div><br>';
167d39b150cSGreg Roach
168bbb76c12SGreg Roach                /* I18N: When replying to an email, the subject becomes “RE: <subject>” */
169bbb76c12SGreg Roach                if (strpos($message->subject, I18N::translate('RE: ')) !== 0) {
1708c2e8227SGreg Roach                    $message->subject = I18N::translate('RE: ') . $message->subject;
1718c2e8227SGreg Roach                }
17246bb661fSGreg Roach
17346bb661fSGreg Roach                // If this user still exists, show a reply link.
1748c2e8227SGreg Roach                if ($user) {
175c1010edaSGreg Roach                    $reply_url = route('message', [
176c1010edaSGreg Roach                        'to'      => $user->getUserName(),
177c1010edaSGreg Roach                        'subject' => $message->subject,
178aa6f03bbSGreg Roach                        'ged'     => $tree->name(),
179c1010edaSGreg Roach                    ]);
180d39b150cSGreg Roach
18146bb661fSGreg Roach                    $content .= '<a class="btn btn-primary" href="' . e($reply_url) . '" title="' . I18N::translate('Reply') . '">' . I18N::translate('Reply') . '</a> ';
1828c2e8227SGreg Roach                }
18346bb661fSGreg 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>';
1848c2e8227SGreg Roach            }
1858c2e8227SGreg Roach            $content .= '</table>';
1862da2404eSGreg Roach            $content .= '<p><button type="submit">' . I18N::translate('Delete selected messages') . '</button></p>';
1878c2e8227SGreg Roach        }
1888c2e8227SGreg Roach        $content .= '</form>';
1898c2e8227SGreg Roach
1906a8879feSGreg Roach        if ($ctype !== '') {
191d39b150cSGreg Roach            $count = $messages->count();
192d39b150cSGreg Roach
193147e99aaSGreg Roach            return view('modules/block-template', [
194*26684e68SGreg Roach                'block'      => str_replace('_', '-', $this->name()),
1959c6524dcSGreg Roach                'id'         => $block_id,
1969c6524dcSGreg Roach                'config_url' => '',
197f7f4b984SGreg Roach                'title'      => I18N::plural('%s message', '%s messages', $count, I18N::number($count)),
1989c6524dcSGreg Roach                'content'    => $content,
1999c6524dcSGreg Roach            ]);
2008c2e8227SGreg Roach        }
201b2ce94c6SRico Sonntag
202b2ce94c6SRico Sonntag        return $content;
2038c2e8227SGreg Roach    }
2048c2e8227SGreg Roach
2058c2e8227SGreg Roach    /** {@inheritdoc} */
206c1010edaSGreg Roach    public function loadAjax(): bool
207c1010edaSGreg Roach    {
2088c2e8227SGreg Roach        return false;
2098c2e8227SGreg Roach    }
2108c2e8227SGreg Roach
2118c2e8227SGreg Roach    /** {@inheritdoc} */
212c1010edaSGreg Roach    public function isUserBlock(): bool
213c1010edaSGreg Roach    {
2148c2e8227SGreg Roach        return true;
2158c2e8227SGreg Roach    }
2168c2e8227SGreg Roach
2178c2e8227SGreg Roach    /** {@inheritdoc} */
218c1010edaSGreg Roach    public function isGedcomBlock(): bool
219c1010edaSGreg Roach    {
2208c2e8227SGreg Roach        return false;
2218c2e8227SGreg Roach    }
2228c2e8227SGreg Roach
22376692c8bSGreg Roach    /**
224a45f9889SGreg Roach     * Update the configuration for a block.
225a45f9889SGreg Roach     *
226a45f9889SGreg Roach     * @param Request $request
227a45f9889SGreg Roach     * @param int     $block_id
228a45f9889SGreg Roach     *
229a45f9889SGreg Roach     * @return void
230a45f9889SGreg Roach     */
231a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
232a45f9889SGreg Roach    {
233a45f9889SGreg Roach    }
234a45f9889SGreg Roach
235a45f9889SGreg Roach    /**
23676692c8bSGreg Roach     * An HTML form to edit block settings
23776692c8bSGreg Roach     *
238e490cd80SGreg Roach     * @param Tree $tree
23976692c8bSGreg Roach     * @param int  $block_id
240a9430be8SGreg Roach     *
241a9430be8SGreg Roach     * @return void
24276692c8bSGreg Roach     */
243a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
244c1010edaSGreg Roach    {
2458c2e8227SGreg Roach    }
2468c2e8227SGreg Roach}
247