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; 21*e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter; 233d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDate; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 25*e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService; 26cf700360SGreg Roachuse Fisharebest\Webtrees\Tree; 270e62c4b8SGreg Roachuse Fisharebest\Webtrees\User; 28d39b150cSGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 29cf700360SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse; 30cf700360SGreg Roachuse Symfony\Component\HttpFoundation\Request; 31cf700360SGreg Roachuse Symfony\Component\HttpFoundation\Response; 328c2e8227SGreg Roach 338c2e8227SGreg Roach/** 348c2e8227SGreg Roach * Class UserMessagesModule 358c2e8227SGreg Roach */ 3637eb8894SGreg Roachclass UserMessagesModule extends AbstractModule implements ModuleBlockInterface 37c1010edaSGreg Roach{ 3849a243cbSGreg Roach use ModuleBlockTrait; 3949a243cbSGreg Roach 40961ec755SGreg Roach /** 41*e5a6b4d4SGreg Roach * @var UserService 42*e5a6b4d4SGreg Roach */ 43*e5a6b4d4SGreg Roach private $user_service; 44*e5a6b4d4SGreg Roach 45*e5a6b4d4SGreg Roach /** 46*e5a6b4d4SGreg Roach * UserMessagesModule constructor. 47*e5a6b4d4SGreg Roach * 48*e5a6b4d4SGreg Roach * @param UserService $user_service 49*e5a6b4d4SGreg Roach */ 50*e5a6b4d4SGreg Roach public function __construct(UserService $user_service) { 51*e5a6b4d4SGreg Roach $this->user_service = $user_service; 52*e5a6b4d4SGreg Roach } 53*e5a6b4d4SGreg Roach 54*e5a6b4d4SGreg Roach /** 55961ec755SGreg Roach * How should this module be labelled on tabs, menus, etc.? 56961ec755SGreg Roach * 57961ec755SGreg Roach * @return string 58961ec755SGreg Roach */ 5949a243cbSGreg Roach public function title(): string 60c1010edaSGreg Roach { 61bbb76c12SGreg Roach /* I18N: Name of a module */ 62bbb76c12SGreg Roach return I18N::translate('Messages'); 638c2e8227SGreg Roach } 648c2e8227SGreg Roach 65961ec755SGreg Roach /** 66961ec755SGreg Roach * A sentence describing what this module does. 67961ec755SGreg Roach * 68961ec755SGreg Roach * @return string 69961ec755SGreg Roach */ 7049a243cbSGreg Roach public function description(): string 71c1010edaSGreg Roach { 72bbb76c12SGreg Roach /* I18N: Description of the “Messages” module */ 73bbb76c12SGreg Roach return I18N::translate('Communicate directly with other users, using private messages.'); 748c2e8227SGreg Roach } 758c2e8227SGreg Roach 7676692c8bSGreg Roach /** 77cf700360SGreg Roach * Delete one or messages belonging to a user. 782da2404eSGreg Roach * 79cf700360SGreg Roach * @param Request $request 80b6db7c1fSGreg Roach * @param Tree $tree 81cf700360SGreg Roach * 82cf700360SGreg Roach * @return Response 832da2404eSGreg Roach */ 84b6db7c1fSGreg Roach public function postDeleteMessageAction(Request $request, Tree $tree): Response 85c1010edaSGreg Roach { 86cf700360SGreg Roach $message_ids = (array) $request->get('message_id', []); 87cf700360SGreg Roach 88d39b150cSGreg Roach DB::table('message') 89d39b150cSGreg Roach ->where('user_id', '=', Auth::id()) 90d39b150cSGreg Roach ->whereIn('message_id', $message_ids) 91d39b150cSGreg Roach ->delete(); 92cf700360SGreg Roach 93cf700360SGreg Roach if ($request->get('ctype') === 'user') { 94aa6f03bbSGreg Roach $url = route('user-page', ['ged' => $tree->name()]); 95cf700360SGreg Roach } else { 96aa6f03bbSGreg Roach $url = route('tree-page', ['ged' => $tree->name()]); 972da2404eSGreg Roach } 982da2404eSGreg Roach 99cf700360SGreg Roach return new RedirectResponse($url); 1002da2404eSGreg Roach } 1012da2404eSGreg Roach 1022da2404eSGreg Roach /** 10376692c8bSGreg Roach * Generate the HTML content of this block. 10476692c8bSGreg Roach * 105e490cd80SGreg Roach * @param Tree $tree 10676692c8bSGreg Roach * @param int $block_id 1075f2ae573SGreg Roach * @param string $ctype 108727f238cSGreg Roach * @param string[] $cfg 10976692c8bSGreg Roach * 11076692c8bSGreg Roach * @return string 11176692c8bSGreg Roach */ 1125f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 113c1010edaSGreg Roach { 114d39b150cSGreg Roach $messages = DB::table('message') 115d39b150cSGreg Roach ->where('user_id', '=', Auth::id()) 116d39b150cSGreg Roach ->orderByDesc('message_id') 117d39b150cSGreg Roach ->select(['message_id', 'sender', 'subject', 'body', DB::raw('UNIX_TIMESTAMP(created) AS created')]) 118d39b150cSGreg Roach ->get(); 1198c2e8227SGreg Roach 120*e5a6b4d4SGreg Roach $users = $this->user_service->all()->filter(function (UserInterface $user) use ($tree): bool { 12176a8e0f3SGreg Roach $public_tree = $tree->getPreference('REQUIRE_AUTHENTICATION') !== '1'; 12276a8e0f3SGreg Roach $can_see_tree = $public_tree || Auth::accessLevel($tree, $user) <= Auth::PRIV_USER; 12376a8e0f3SGreg Roach 12476a8e0f3SGreg Roach return 125895230eeSGreg Roach $user->id() !== Auth::id() && 12676a8e0f3SGreg Roach $user->getPreference('verified_by_admin') && 12776a8e0f3SGreg Roach $can_see_tree && 12876a8e0f3SGreg Roach $user->getPreference('contactmethod') !== 'none'; 1298c2e8227SGreg Roach }); 1308c2e8227SGreg Roach 1317bab8982SGreg Roach $content = ''; 1328b67c11aSGreg Roach if ($users->isNotEmpty()) { 133aa6f03bbSGreg Roach $url = route('user-page', ['ged' => $tree->name()]); 134d39b150cSGreg Roach 13546bb661fSGreg Roach $content .= '<form onsubmit="return $("#to").val() !== """>'; 13646bb661fSGreg Roach $content .= '<input type="hidden" name="route" value="message">'; 137aa6f03bbSGreg Roach $content .= '<input type="hidden" name="ged" value="' . e($tree->name()) . '">'; 138d53324c9SGreg Roach $content .= '<input type="hidden" name="url" value="' . e($url) . '">'; 1397bab8982SGreg Roach $content .= '<label for="to">' . I18N::translate('Send a message') . '</label>'; 1407bab8982SGreg Roach $content .= '<select id="to" name="to">'; 1418c2e8227SGreg Roach $content .= '<option value="">' . I18N::translate('<select>') . '</option>'; 1428c2e8227SGreg Roach foreach ($users as $user) { 143*e5a6b4d4SGreg Roach $content .= sprintf('<option value="%1$s">%2$s - %1$s</option>', e($user->userName()), e($user->realName())); 1448c2e8227SGreg Roach } 1458c2e8227SGreg Roach $content .= '</select>'; 1467bab8982SGreg Roach $content .= '<button type="submit">' . I18N::translate('Send') . '</button><br><br>'; 1477bab8982SGreg Roach $content .= '</form>'; 1488c2e8227SGreg Roach } 149c1010edaSGreg Roach $content .= '<form id="messageform" name="messageform" method="post" action="' . e(route('module', [ 150c1010edaSGreg Roach 'action' => 'DeleteMessage', 15126684e68SGreg Roach 'module' => $this->name(), 152c1010edaSGreg Roach 'ctype' => $ctype, 153aa6f03bbSGreg Roach 'ged' => $tree->name(), 154c1010edaSGreg 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);">'; 155cf700360SGreg Roach $content .= csrf_field(); 156cf700360SGreg Roach 157d39b150cSGreg Roach if ($messages->isNotEmpty()) { 15846bb661fSGreg Roach $content .= '<table class="list_table w-100"><tr>'; 159bc6628fbSRico 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>'; 160e284b8e1SGreg Roach $content .= '<th class="list_label">' . I18N::translate('Subject') . '</th>'; 161e284b8e1SGreg Roach $content .= '<th class="list_label">' . I18N::translate('Date sent') . '</th>'; 162e284b8e1SGreg Roach $content .= '<th class="list_label">' . I18N::translate('Email address') . '</th>'; 1638c2e8227SGreg Roach $content .= '</tr>'; 1648c2e8227SGreg Roach foreach ($messages as $message) { 1658c2e8227SGreg Roach $content .= '<tr>'; 166a33af35aSmakitso $content .= '<td class="list_value_wrap center"><input type="checkbox" name="message_id[]" value="' . $message->message_id . '" id="cb_message' . $message->message_id . '"></td>'; 167d53324c9SGreg 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>'; 1681b367094SGreg Roach $content .= '<td class="list_value_wrap">' . FunctionsDate::formatTimestamp((int) $message->created) . '</td>'; 1698c2e8227SGreg Roach $content .= '<td class="list_value_wrap">'; 170d39b150cSGreg Roach 171*e5a6b4d4SGreg Roach $user = $this->user_service->findByIdentifier($message->sender); 172d39b150cSGreg Roach 17326eae716SGreg Roach if ($user instanceof User) { 174*e5a6b4d4SGreg Roach $content .= '<span dir="auto">' . e($user->realName()) . '</span> - <span dir="auto">' . $user->email() . '</span>'; 1758c2e8227SGreg Roach } else { 176d53324c9SGreg Roach $content .= '<a href="mailto:' . e($message->sender) . '">' . e($message->sender) . '</a>'; 1778c2e8227SGreg Roach } 178d39b150cSGreg Roach 1798c2e8227SGreg Roach $content .= '</td>'; 1808c2e8227SGreg Roach $content .= '</tr>'; 1818c2e8227SGreg Roach $content .= '<tr><td class="list_value_wrap" colspan="4"><div id="message' . $message->message_id . '" style="display:none;">'; 182e490cd80SGreg Roach $content .= '<div dir="auto" style="white-space: pre-wrap;">' . Filter::expandUrls($message->body, $tree) . '</div><br>'; 183d39b150cSGreg Roach 184bbb76c12SGreg Roach /* I18N: When replying to an email, the subject becomes “RE: <subject>” */ 185bbb76c12SGreg Roach if (strpos($message->subject, I18N::translate('RE: ')) !== 0) { 1868c2e8227SGreg Roach $message->subject = I18N::translate('RE: ') . $message->subject; 1878c2e8227SGreg Roach } 18846bb661fSGreg Roach 18946bb661fSGreg Roach // If this user still exists, show a reply link. 1908c2e8227SGreg Roach if ($user) { 191c1010edaSGreg Roach $reply_url = route('message', [ 192*e5a6b4d4SGreg Roach 'to' => $user->userName(), 193c1010edaSGreg Roach 'subject' => $message->subject, 194aa6f03bbSGreg Roach 'ged' => $tree->name(), 195c1010edaSGreg Roach ]); 196d39b150cSGreg Roach 19746bb661fSGreg Roach $content .= '<a class="btn btn-primary" href="' . e($reply_url) . '" title="' . I18N::translate('Reply') . '">' . I18N::translate('Reply') . '</a> '; 1988c2e8227SGreg Roach } 19946bb661fSGreg 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>'; 2008c2e8227SGreg Roach } 2018c2e8227SGreg Roach $content .= '</table>'; 2022da2404eSGreg Roach $content .= '<p><button type="submit">' . I18N::translate('Delete selected messages') . '</button></p>'; 2038c2e8227SGreg Roach } 2048c2e8227SGreg Roach $content .= '</form>'; 2058c2e8227SGreg Roach 2066a8879feSGreg Roach if ($ctype !== '') { 207d39b150cSGreg Roach $count = $messages->count(); 208d39b150cSGreg Roach 209147e99aaSGreg Roach return view('modules/block-template', [ 21026684e68SGreg Roach 'block' => str_replace('_', '-', $this->name()), 2119c6524dcSGreg Roach 'id' => $block_id, 2129c6524dcSGreg Roach 'config_url' => '', 213f7f4b984SGreg Roach 'title' => I18N::plural('%s message', '%s messages', $count, I18N::number($count)), 2149c6524dcSGreg Roach 'content' => $content, 2159c6524dcSGreg Roach ]); 2168c2e8227SGreg Roach } 217b2ce94c6SRico Sonntag 218b2ce94c6SRico Sonntag return $content; 2198c2e8227SGreg Roach } 2208c2e8227SGreg Roach 2218c2e8227SGreg Roach /** {@inheritdoc} */ 222c1010edaSGreg Roach public function loadAjax(): bool 223c1010edaSGreg Roach { 2248c2e8227SGreg Roach return false; 2258c2e8227SGreg Roach } 2268c2e8227SGreg Roach 2278c2e8227SGreg Roach /** {@inheritdoc} */ 228c1010edaSGreg Roach public function isUserBlock(): bool 229c1010edaSGreg Roach { 2308c2e8227SGreg Roach return true; 2318c2e8227SGreg Roach } 2328c2e8227SGreg Roach 2338c2e8227SGreg Roach /** {@inheritdoc} */ 23463276d8fSGreg Roach public function isTreeBlock(): bool 235c1010edaSGreg Roach { 2368c2e8227SGreg Roach return false; 2378c2e8227SGreg Roach } 2388c2e8227SGreg Roach 23976692c8bSGreg Roach /** 240a45f9889SGreg Roach * Update the configuration for a block. 241a45f9889SGreg Roach * 242a45f9889SGreg Roach * @param Request $request 243a45f9889SGreg Roach * @param int $block_id 244a45f9889SGreg Roach * 245a45f9889SGreg Roach * @return void 246a45f9889SGreg Roach */ 247a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 248a45f9889SGreg Roach { 249a45f9889SGreg Roach } 250a45f9889SGreg Roach 251a45f9889SGreg Roach /** 25276692c8bSGreg Roach * An HTML form to edit block settings 25376692c8bSGreg Roach * 254e490cd80SGreg Roach * @param Tree $tree 25576692c8bSGreg Roach * @param int $block_id 256a9430be8SGreg Roach * 257a9430be8SGreg Roach * @return void 25876692c8bSGreg Roach */ 259a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 260c1010edaSGreg Roach { 2618c2e8227SGreg Roach } 2628c2e8227SGreg Roach} 263