xref: /webtrees/app/Module/UserMessagesModule.php (revision e364afe4ae4e316fc4ebd53eccbaff2d29e419a5)
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;
214459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
22e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
25e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
26cf700360SGreg Roachuse Fisharebest\Webtrees\Tree;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\User;
28d39b150cSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
29ca52a408SGreg Roachuse stdClass;
30cf700360SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse;
31cf700360SGreg Roachuse Symfony\Component\HttpFoundation\Request;
32cf700360SGreg Roachuse Symfony\Component\HttpFoundation\Response;
338c2e8227SGreg Roach
348c2e8227SGreg Roach/**
358c2e8227SGreg Roach * Class UserMessagesModule
368c2e8227SGreg Roach */
3737eb8894SGreg Roachclass UserMessagesModule extends AbstractModule implements ModuleBlockInterface
38c1010edaSGreg Roach{
3949a243cbSGreg Roach    use ModuleBlockTrait;
4049a243cbSGreg Roach
41961ec755SGreg Roach    /**
42e5a6b4d4SGreg Roach     * @var UserService
43e5a6b4d4SGreg Roach     */
44e5a6b4d4SGreg Roach    private $user_service;
45e5a6b4d4SGreg Roach
46e5a6b4d4SGreg Roach    /**
47e5a6b4d4SGreg Roach     * UserMessagesModule constructor.
48e5a6b4d4SGreg Roach     *
49e5a6b4d4SGreg Roach     * @param UserService $user_service
50e5a6b4d4SGreg Roach     */
51e2cbf57aSGreg Roach    public function __construct(UserService $user_service)
52e2cbf57aSGreg Roach    {
53e5a6b4d4SGreg Roach        $this->user_service = $user_service;
54e5a6b4d4SGreg Roach    }
55e5a6b4d4SGreg Roach
56e5a6b4d4SGreg Roach    /**
570cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
58961ec755SGreg Roach     *
59961ec755SGreg Roach     * @return string
60961ec755SGreg Roach     */
6149a243cbSGreg Roach    public function title(): string
62c1010edaSGreg Roach    {
63bbb76c12SGreg Roach        /* I18N: Name of a module */
64bbb76c12SGreg Roach        return I18N::translate('Messages');
658c2e8227SGreg Roach    }
668c2e8227SGreg Roach
67961ec755SGreg Roach    /**
68961ec755SGreg Roach     * A sentence describing what this module does.
69961ec755SGreg Roach     *
70961ec755SGreg Roach     * @return string
71961ec755SGreg Roach     */
7249a243cbSGreg Roach    public function description(): string
73c1010edaSGreg Roach    {
74bbb76c12SGreg Roach        /* I18N: Description of the “Messages” module */
75bbb76c12SGreg Roach        return I18N::translate('Communicate directly with other users, using private messages.');
768c2e8227SGreg Roach    }
778c2e8227SGreg Roach
7876692c8bSGreg Roach    /**
79cf700360SGreg Roach     * Delete one or messages belonging to a user.
802da2404eSGreg Roach     *
81cf700360SGreg Roach     * @param Request $request
82b6db7c1fSGreg Roach     * @param Tree    $tree
83cf700360SGreg Roach     *
84cf700360SGreg Roach     * @return Response
852da2404eSGreg Roach     */
86b6db7c1fSGreg Roach    public function postDeleteMessageAction(Request $request, Tree $tree): Response
87c1010edaSGreg Roach    {
88cf700360SGreg Roach        $message_ids = (array) $request->get('message_id', []);
89cf700360SGreg Roach
90d39b150cSGreg Roach        DB::table('message')
91d39b150cSGreg Roach            ->where('user_id', '=', Auth::id())
92d39b150cSGreg Roach            ->whereIn('message_id', $message_ids)
93d39b150cSGreg Roach            ->delete();
94cf700360SGreg Roach
95cf700360SGreg Roach        if ($request->get('ctype') === 'user') {
96aa6f03bbSGreg Roach            $url = route('user-page', ['ged' => $tree->name()]);
97cf700360SGreg Roach        } else {
98aa6f03bbSGreg Roach            $url = route('tree-page', ['ged' => $tree->name()]);
992da2404eSGreg Roach        }
1002da2404eSGreg Roach
101cf700360SGreg Roach        return new RedirectResponse($url);
1022da2404eSGreg Roach    }
1032da2404eSGreg Roach
1042da2404eSGreg Roach    /**
10576692c8bSGreg Roach     * Generate the HTML content of this block.
10676692c8bSGreg Roach     *
107e490cd80SGreg Roach     * @param Tree     $tree
10876692c8bSGreg Roach     * @param int      $block_id
1095f2ae573SGreg Roach     * @param string   $ctype
110727f238cSGreg Roach     * @param string[] $cfg
11176692c8bSGreg Roach     *
11276692c8bSGreg Roach     * @return string
11376692c8bSGreg Roach     */
1145f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
115c1010edaSGreg Roach    {
116d39b150cSGreg Roach        $messages = DB::table('message')
117d39b150cSGreg Roach            ->where('user_id', '=', Auth::id())
118d39b150cSGreg Roach            ->orderByDesc('message_id')
119ca52a408SGreg Roach            ->get()
120ca52a408SGreg Roach            ->map(function (stdClass $row): stdClass {
1214459dc9aSGreg Roach                $row->created = Carbon::make($row->created);
122ca52a408SGreg Roach
123ca52a408SGreg Roach                return $row;
124ca52a408SGreg Roach            });
1258c2e8227SGreg Roach
126e5a6b4d4SGreg Roach        $users = $this->user_service->all()->filter(function (UserInterface $user) use ($tree): bool {
12776a8e0f3SGreg Roach            $public_tree  = $tree->getPreference('REQUIRE_AUTHENTICATION') !== '1';
12876a8e0f3SGreg Roach            $can_see_tree = $public_tree || Auth::accessLevel($tree, $user) <= Auth::PRIV_USER;
12976a8e0f3SGreg Roach
13076a8e0f3SGreg Roach            return
131895230eeSGreg Roach                $user->id() !== Auth::id() &&
13276a8e0f3SGreg Roach                $user->getPreference('verified_by_admin') &&
13376a8e0f3SGreg Roach                $can_see_tree &&
13476a8e0f3SGreg Roach                $user->getPreference('contactmethod') !== 'none';
1358c2e8227SGreg Roach        });
1368c2e8227SGreg Roach
1377bab8982SGreg Roach        $content = '';
1388b67c11aSGreg Roach        if ($users->isNotEmpty()) {
139aa6f03bbSGreg Roach            $url = route('user-page', ['ged' => $tree->name()]);
140d39b150cSGreg Roach
14146bb661fSGreg Roach            $content .= '<form onsubmit="return $(&quot;#to&quot;).val() !== &quot;&quot;">';
14246bb661fSGreg Roach            $content .= '<input type="hidden" name="route" value="message">';
143aa6f03bbSGreg Roach            $content .= '<input type="hidden" name="ged" value="' . e($tree->name()) . '">';
144d53324c9SGreg Roach            $content .= '<input type="hidden" name="url" value="' . e($url) . '">';
1457bab8982SGreg Roach            $content .= '<label for="to">' . I18N::translate('Send a message') . '</label>';
1467bab8982SGreg Roach            $content .= '<select id="to" name="to">';
1478c2e8227SGreg Roach            $content .= '<option value="">' . I18N::translate('&lt;select&gt;') . '</option>';
1488c2e8227SGreg Roach            foreach ($users as $user) {
149e5a6b4d4SGreg Roach                $content .= sprintf('<option value="%1$s">%2$s - %1$s</option>', e($user->userName()), e($user->realName()));
1508c2e8227SGreg Roach            }
1518c2e8227SGreg Roach            $content .= '</select>';
1527bab8982SGreg Roach            $content .= '<button type="submit">' . I18N::translate('Send') . '</button><br><br>';
1537bab8982SGreg Roach            $content .= '</form>';
1548c2e8227SGreg Roach        }
155c1010edaSGreg Roach        $content .= '<form id="messageform" name="messageform" method="post" action="' . e(route('module', [
156c1010edaSGreg Roach                'action' => 'DeleteMessage',
15726684e68SGreg Roach                'module' => $this->name(),
158c1010edaSGreg Roach                'ctype'  => $ctype,
159aa6f03bbSGreg Roach                'ged'    => $tree->name(),
160c1010edaSGreg 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);">';
161cf700360SGreg Roach        $content .= csrf_field();
162cf700360SGreg Roach
163d39b150cSGreg Roach        if ($messages->isNotEmpty()) {
16446bb661fSGreg Roach            $content .= '<table class="list_table w-100"><tr>';
165bc6628fbSRico 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>';
166e284b8e1SGreg Roach            $content .= '<th class="list_label">' . I18N::translate('Subject') . '</th>';
167e284b8e1SGreg Roach            $content .= '<th class="list_label">' . I18N::translate('Date sent') . '</th>';
168e284b8e1SGreg Roach            $content .= '<th class="list_label">' . I18N::translate('Email address') . '</th>';
1698c2e8227SGreg Roach            $content .= '</tr>';
1708c2e8227SGreg Roach            foreach ($messages as $message) {
1718c2e8227SGreg Roach                $content .= '<tr>';
172a33af35aSmakitso                $content .= '<td class="list_value_wrap center"><input type="checkbox" name="message_id[]" value="' . $message->message_id . '" id="cb_message' . $message->message_id . '"></td>';
173d53324c9SGreg 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>';
1744459dc9aSGreg Roach                $content .= '<td class="list_value_wrap">' . view('components/datetime', ['timestamp' => $message->created]) . '</td>';
1758c2e8227SGreg Roach                $content .= '<td class="list_value_wrap">';
176d39b150cSGreg Roach
177e5a6b4d4SGreg Roach                $user = $this->user_service->findByIdentifier($message->sender);
178d39b150cSGreg Roach
17926eae716SGreg Roach                if ($user instanceof User) {
180e5a6b4d4SGreg Roach                    $content .= '<span dir="auto">' . e($user->realName()) . '</span> - <span dir="auto">' . $user->email() . '</span>';
1818c2e8227SGreg Roach                } else {
182d53324c9SGreg Roach                    $content .= '<a href="mailto:' . e($message->sender) . '">' . e($message->sender) . '</a>';
1838c2e8227SGreg Roach                }
184d39b150cSGreg Roach
1858c2e8227SGreg Roach                $content .= '</td>';
1868c2e8227SGreg Roach                $content .= '</tr>';
1878c2e8227SGreg Roach                $content .= '<tr><td class="list_value_wrap" colspan="4"><div id="message' . $message->message_id . '" style="display:none;">';
188e490cd80SGreg Roach                $content .= '<div dir="auto" style="white-space: pre-wrap;">' . Filter::expandUrls($message->body, $tree) . '</div><br>';
189d39b150cSGreg Roach
190bbb76c12SGreg Roach                /* I18N: When replying to an email, the subject becomes “RE: <subject>” */
191bbb76c12SGreg Roach                if (strpos($message->subject, I18N::translate('RE: ')) !== 0) {
1928c2e8227SGreg Roach                    $message->subject = I18N::translate('RE: ') . $message->subject;
1938c2e8227SGreg Roach                }
19446bb661fSGreg Roach
19546bb661fSGreg Roach                // If this user still exists, show a reply link.
1968c2e8227SGreg Roach                if ($user) {
197c1010edaSGreg Roach                    $reply_url = route('message', [
198e5a6b4d4SGreg Roach                        'to'      => $user->userName(),
199c1010edaSGreg Roach                        'subject' => $message->subject,
200aa6f03bbSGreg Roach                        'ged'     => $tree->name(),
201c1010edaSGreg Roach                    ]);
202d39b150cSGreg Roach
20346bb661fSGreg Roach                    $content .= '<a class="btn btn-primary" href="' . e($reply_url) . '" title="' . I18N::translate('Reply') . '">' . I18N::translate('Reply') . '</a> ';
2048c2e8227SGreg Roach                }
20546bb661fSGreg 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>';
2068c2e8227SGreg Roach            }
2078c2e8227SGreg Roach            $content .= '</table>';
2082da2404eSGreg Roach            $content .= '<p><button type="submit">' . I18N::translate('Delete selected messages') . '</button></p>';
2098c2e8227SGreg Roach        }
2108c2e8227SGreg Roach        $content .= '</form>';
2118c2e8227SGreg Roach
2126a8879feSGreg Roach        if ($ctype !== '') {
213d39b150cSGreg Roach            $count = $messages->count();
214d39b150cSGreg Roach
215147e99aaSGreg Roach            return view('modules/block-template', [
21626684e68SGreg Roach                'block'      => str_replace('_', '-', $this->name()),
2179c6524dcSGreg Roach                'id'         => $block_id,
2189c6524dcSGreg Roach                'config_url' => '',
219f7f4b984SGreg Roach                'title'      => I18N::plural('%s message', '%s messages', $count, I18N::number($count)),
2209c6524dcSGreg Roach                'content'    => $content,
2219c6524dcSGreg Roach            ]);
2228c2e8227SGreg Roach        }
223b2ce94c6SRico Sonntag
224b2ce94c6SRico Sonntag        return $content;
2258c2e8227SGreg Roach    }
2268c2e8227SGreg Roach
2278c2e8227SGreg Roach    /** {@inheritdoc} */
228c1010edaSGreg Roach    public function loadAjax(): bool
229c1010edaSGreg Roach    {
2308c2e8227SGreg Roach        return false;
2318c2e8227SGreg Roach    }
2328c2e8227SGreg Roach
2338c2e8227SGreg Roach    /** {@inheritdoc} */
234c1010edaSGreg Roach    public function isUserBlock(): bool
235c1010edaSGreg Roach    {
2368c2e8227SGreg Roach        return true;
2378c2e8227SGreg Roach    }
2388c2e8227SGreg Roach
2398c2e8227SGreg Roach    /** {@inheritdoc} */
24063276d8fSGreg Roach    public function isTreeBlock(): bool
241c1010edaSGreg Roach    {
2428c2e8227SGreg Roach        return false;
2438c2e8227SGreg Roach    }
2448c2e8227SGreg Roach
24576692c8bSGreg Roach    /**
246a45f9889SGreg Roach     * Update the configuration for a block.
247a45f9889SGreg Roach     *
248a45f9889SGreg Roach     * @param Request $request
249a45f9889SGreg Roach     * @param int     $block_id
250a45f9889SGreg Roach     *
251a45f9889SGreg Roach     * @return void
252a45f9889SGreg Roach     */
253*e364afe4SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id): void
254a45f9889SGreg Roach    {
255a45f9889SGreg Roach    }
256a45f9889SGreg Roach
257a45f9889SGreg Roach    /**
25876692c8bSGreg Roach     * An HTML form to edit block settings
25976692c8bSGreg Roach     *
260e490cd80SGreg Roach     * @param Tree $tree
26176692c8bSGreg Roach     * @param int  $block_id
262a9430be8SGreg Roach     *
263a9430be8SGreg Roach     * @return void
26476692c8bSGreg Roach     */
265*e364afe4SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): void
266c1010edaSGreg Roach    {
2678c2e8227SGreg Roach    }
2688c2e8227SGreg Roach}
269