xref: /webtrees/app/Module/UserMessagesModule.php (revision ec589cf2488a026936cb76fab93a33b983013ee6)
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 */
34c1010edaSGreg Roachclass UserMessagesModule extends AbstractModule implements ModuleBlockInterface
35c1010edaSGreg Roach{
368c2e8227SGreg Roach    /** {@inheritdoc} */
378f53f488SRico Sonntag    public function getTitle(): string
38c1010edaSGreg Roach    {
39bbb76c12SGreg Roach        /* I18N: Name of a module */
40bbb76c12SGreg Roach        return I18N::translate('Messages');
418c2e8227SGreg Roach    }
428c2e8227SGreg Roach
438c2e8227SGreg Roach    /** {@inheritdoc} */
448f53f488SRico Sonntag    public function getDescription(): string
45c1010edaSGreg Roach    {
46bbb76c12SGreg Roach        /* I18N: Description of the “Messages” module */
47bbb76c12SGreg Roach        return I18N::translate('Communicate directly with other users, using private messages.');
488c2e8227SGreg Roach    }
498c2e8227SGreg Roach
5076692c8bSGreg Roach    /**
51cf700360SGreg Roach     * Delete one or messages belonging to a user.
522da2404eSGreg Roach     *
53cf700360SGreg Roach     * @param Request $request
54b6db7c1fSGreg Roach     * @param Tree    $tree
55cf700360SGreg Roach     *
56cf700360SGreg Roach     * @return Response
572da2404eSGreg Roach     */
58b6db7c1fSGreg Roach    public function postDeleteMessageAction(Request $request, Tree $tree): Response
59c1010edaSGreg Roach    {
60cf700360SGreg Roach        $message_ids = (array) $request->get('message_id', []);
61cf700360SGreg Roach
62d39b150cSGreg Roach        DB::table('message')
63d39b150cSGreg Roach            ->where('user_id', '=', Auth::id())
64d39b150cSGreg Roach            ->whereIn('message_id', $message_ids)
65d39b150cSGreg Roach            ->delete();
66cf700360SGreg Roach
67cf700360SGreg Roach        if ($request->get('ctype') === 'user') {
68aa6f03bbSGreg Roach            $url = route('user-page', ['ged' => $tree->name()]);
69cf700360SGreg Roach        } else {
70aa6f03bbSGreg Roach            $url = route('tree-page', ['ged' => $tree->name()]);
712da2404eSGreg Roach        }
722da2404eSGreg Roach
73cf700360SGreg Roach        return new RedirectResponse($url);
742da2404eSGreg Roach    }
752da2404eSGreg Roach
762da2404eSGreg Roach    /**
7776692c8bSGreg Roach     * Generate the HTML content of this block.
7876692c8bSGreg Roach     *
79e490cd80SGreg Roach     * @param Tree     $tree
8076692c8bSGreg Roach     * @param int      $block_id
815f2ae573SGreg Roach     * @param string   $ctype
82727f238cSGreg Roach     * @param string[] $cfg
8376692c8bSGreg Roach     *
8476692c8bSGreg Roach     * @return string
8576692c8bSGreg Roach     */
865f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
87c1010edaSGreg Roach    {
88d39b150cSGreg Roach        $messages = DB::table('message')
89d39b150cSGreg Roach            ->where('user_id', '=', Auth::id())
90d39b150cSGreg Roach            ->orderByDesc('message_id')
91d39b150cSGreg Roach            ->select(['message_id', 'sender', 'subject', 'body', DB::raw('UNIX_TIMESTAMP(created) AS created')])
92d39b150cSGreg Roach            ->get();
938c2e8227SGreg Roach
9476a8e0f3SGreg Roach        $users = array_filter(User::all(), function (User $user) use ($tree): bool {
9576a8e0f3SGreg Roach            $public_tree  = $tree->getPreference('REQUIRE_AUTHENTICATION') !== '1';
9676a8e0f3SGreg Roach            $can_see_tree = $public_tree || Auth::accessLevel($tree, $user) <= Auth::PRIV_USER;
9776a8e0f3SGreg Roach
9876a8e0f3SGreg Roach            return
9976a8e0f3SGreg Roach                $user->getUserId() !== Auth::id() &&
10076a8e0f3SGreg Roach                $user->getPreference('verified_by_admin') &&
10176a8e0f3SGreg Roach                $can_see_tree &&
10276a8e0f3SGreg Roach                $user->getPreference('contactmethod') !== 'none';
1038c2e8227SGreg Roach        });
1048c2e8227SGreg Roach
1057bab8982SGreg Roach        $content = '';
106e92490bcSGreg Roach        if (!empty($users)) {
107aa6f03bbSGreg Roach            $url = route('user-page', ['ged' => $tree->name()]);
108d39b150cSGreg Roach
10946bb661fSGreg Roach            $content .= '<form onsubmit="return $(&quot;#to&quot;).val() !== &quot;&quot;">';
11046bb661fSGreg Roach            $content .= '<input type="hidden" name="route" value="message">';
111aa6f03bbSGreg Roach            $content .= '<input type="hidden" name="ged" value="' . e($tree->name()) . '">';
112d53324c9SGreg Roach            $content .= '<input type="hidden" name="url" value="' . e($url) . '">';
1137bab8982SGreg Roach            $content .= '<label for="to">' . I18N::translate('Send a message') . '</label>';
1147bab8982SGreg Roach            $content .= '<select id="to" name="to">';
1158c2e8227SGreg Roach            $content .= '<option value="">' . I18N::translate('&lt;select&gt;') . '</option>';
1168c2e8227SGreg Roach            foreach ($users as $user) {
117d53324c9SGreg Roach                $content .= sprintf('<option value="%1$s">%2$s - %1$s</option>', e($user->getUserName()), e($user->getRealName()));
1188c2e8227SGreg Roach            }
1198c2e8227SGreg Roach            $content .= '</select>';
1207bab8982SGreg Roach            $content .= '<button type="submit">' . I18N::translate('Send') . '</button><br><br>';
1217bab8982SGreg Roach            $content .= '</form>';
1228c2e8227SGreg Roach        }
123c1010edaSGreg Roach        $content .= '<form id="messageform" name="messageform" method="post" action="' . e(route('module', [
124c1010edaSGreg Roach                'action' => 'DeleteMessage',
125c1010edaSGreg Roach                'module' => $this->getName(),
126c1010edaSGreg Roach                'ctype'  => $ctype,
127aa6f03bbSGreg Roach                'ged'    => $tree->name(),
128c1010edaSGreg 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);">';
129cf700360SGreg Roach        $content .= csrf_field();
130cf700360SGreg Roach
131d39b150cSGreg Roach        if ($messages->isNotEmpty()) {
13246bb661fSGreg Roach            $content .= '<table class="list_table w-100"><tr>';
133bc6628fbSRico 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>';
134e284b8e1SGreg Roach            $content .= '<th class="list_label">' . I18N::translate('Subject') . '</th>';
135e284b8e1SGreg Roach            $content .= '<th class="list_label">' . I18N::translate('Date sent') . '</th>';
136e284b8e1SGreg Roach            $content .= '<th class="list_label">' . I18N::translate('Email address') . '</th>';
1378c2e8227SGreg Roach            $content .= '</tr>';
1388c2e8227SGreg Roach            foreach ($messages as $message) {
1398c2e8227SGreg Roach                $content .= '<tr>';
140a33af35aSmakitso                $content .= '<td class="list_value_wrap center"><input type="checkbox" name="message_id[]" value="' . $message->message_id . '" id="cb_message' . $message->message_id . '"></td>';
141d53324c9SGreg 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>';
142*ec589cf2SGreg Roach                $content .= '<td class="list_value_wrap">' . FunctionsDate::formatTimestamp($message->created) . '</td>';
1438c2e8227SGreg Roach                $content .= '<td class="list_value_wrap">';
144d39b150cSGreg Roach
1458c2e8227SGreg Roach                $user = User::findByIdentifier($message->sender);
146d39b150cSGreg Roach
14726eae716SGreg Roach                if ($user instanceof User) {
1481a46bd06SGreg Roach                    $content .= '<span dir="auto">' . e($user->getRealName()) . '</span> - <span dir="auto">' . $user->getEmail() . '</span>';
1498c2e8227SGreg Roach                } else {
150d53324c9SGreg Roach                    $content .= '<a href="mailto:' . e($message->sender) . '">' . e($message->sender) . '</a>';
1518c2e8227SGreg Roach                }
152d39b150cSGreg Roach
1538c2e8227SGreg Roach                $content .= '</td>';
1548c2e8227SGreg Roach                $content .= '</tr>';
1558c2e8227SGreg Roach                $content .= '<tr><td class="list_value_wrap" colspan="4"><div id="message' . $message->message_id . '" style="display:none;">';
156e490cd80SGreg Roach                $content .= '<div dir="auto" style="white-space: pre-wrap;">' . Filter::expandUrls($message->body, $tree) . '</div><br>';
157d39b150cSGreg Roach
158bbb76c12SGreg Roach                /* I18N: When replying to an email, the subject becomes “RE: <subject>” */
159bbb76c12SGreg Roach                if (strpos($message->subject, I18N::translate('RE: ')) !== 0) {
1608c2e8227SGreg Roach                    $message->subject = I18N::translate('RE: ') . $message->subject;
1618c2e8227SGreg Roach                }
16246bb661fSGreg Roach
16346bb661fSGreg Roach                // If this user still exists, show a reply link.
1648c2e8227SGreg Roach                if ($user) {
165c1010edaSGreg Roach                    $reply_url = route('message', [
166c1010edaSGreg Roach                        'to'      => $user->getUserName(),
167c1010edaSGreg Roach                        'subject' => $message->subject,
168aa6f03bbSGreg Roach                        'ged'     => $tree->name(),
169c1010edaSGreg Roach                    ]);
170d39b150cSGreg Roach
17146bb661fSGreg Roach                    $content .= '<a class="btn btn-primary" href="' . e($reply_url) . '" title="' . I18N::translate('Reply') . '">' . I18N::translate('Reply') . '</a> ';
1728c2e8227SGreg Roach                }
17346bb661fSGreg 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>';
1748c2e8227SGreg Roach            }
1758c2e8227SGreg Roach            $content .= '</table>';
1762da2404eSGreg Roach            $content .= '<p><button type="submit">' . I18N::translate('Delete selected messages') . '</button></p>';
1778c2e8227SGreg Roach        }
1788c2e8227SGreg Roach        $content .= '</form>';
1798c2e8227SGreg Roach
1806a8879feSGreg Roach        if ($ctype !== '') {
181d39b150cSGreg Roach            $count = $messages->count();
182d39b150cSGreg Roach
183147e99aaSGreg Roach            return view('modules/block-template', [
1849c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1859c6524dcSGreg Roach                'id'         => $block_id,
1869c6524dcSGreg Roach                'config_url' => '',
187f7f4b984SGreg Roach                'title'      => I18N::plural('%s message', '%s messages', $count, I18N::number($count)),
1889c6524dcSGreg Roach                'content'    => $content,
1899c6524dcSGreg Roach            ]);
1908c2e8227SGreg Roach        }
191b2ce94c6SRico Sonntag
192b2ce94c6SRico Sonntag        return $content;
1938c2e8227SGreg Roach    }
1948c2e8227SGreg Roach
1958c2e8227SGreg Roach    /** {@inheritdoc} */
196c1010edaSGreg Roach    public function loadAjax(): bool
197c1010edaSGreg Roach    {
1988c2e8227SGreg Roach        return false;
1998c2e8227SGreg Roach    }
2008c2e8227SGreg Roach
2018c2e8227SGreg Roach    /** {@inheritdoc} */
202c1010edaSGreg Roach    public function isUserBlock(): bool
203c1010edaSGreg Roach    {
2048c2e8227SGreg Roach        return true;
2058c2e8227SGreg Roach    }
2068c2e8227SGreg Roach
2078c2e8227SGreg Roach    /** {@inheritdoc} */
208c1010edaSGreg Roach    public function isGedcomBlock(): bool
209c1010edaSGreg Roach    {
2108c2e8227SGreg Roach        return false;
2118c2e8227SGreg Roach    }
2128c2e8227SGreg Roach
21376692c8bSGreg Roach    /**
214a45f9889SGreg Roach     * Update the configuration for a block.
215a45f9889SGreg Roach     *
216a45f9889SGreg Roach     * @param Request $request
217a45f9889SGreg Roach     * @param int     $block_id
218a45f9889SGreg Roach     *
219a45f9889SGreg Roach     * @return void
220a45f9889SGreg Roach     */
221a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
222a45f9889SGreg Roach    {
223a45f9889SGreg Roach    }
224a45f9889SGreg Roach
225a45f9889SGreg Roach    /**
22676692c8bSGreg Roach     * An HTML form to edit block settings
22776692c8bSGreg Roach     *
228e490cd80SGreg Roach     * @param Tree $tree
22976692c8bSGreg Roach     * @param int  $block_id
230a9430be8SGreg Roach     *
231a9430be8SGreg Roach     * @return void
23276692c8bSGreg Roach     */
233a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
234c1010edaSGreg Roach    {
2358c2e8227SGreg Roach    }
2368c2e8227SGreg Roach}
237