18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 98c2e8227SGreg Roach * (at your option) any later version. 108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 138c2e8227SGreg Roach * GNU General Public License for more details. 148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 168c2e8227SGreg Roach */ 17e7f56f2aSGreg Roachdeclare(strict_types=1); 18e7f56f2aSGreg Roach 1976692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 2076692c8bSGreg Roach 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 224459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon; 23e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter; 250e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 26e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService; 27cf700360SGreg Roachuse Fisharebest\Webtrees\Tree; 280e62c4b8SGreg Roachuse Fisharebest\Webtrees\User; 29d39b150cSGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 301e7a7a28SGreg Roachuse Illuminate\Support\Str; 316ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 326ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 33ca52a408SGreg Roachuse stdClass; 3471378461SGreg Roach 3583615acfSGreg Roachuse function e; 3683615acfSGreg Roachuse function route; 378c2e8227SGreg Roach 388c2e8227SGreg Roach/** 398c2e8227SGreg Roach * Class UserMessagesModule 408c2e8227SGreg Roach */ 4137eb8894SGreg Roachclass UserMessagesModule extends AbstractModule implements ModuleBlockInterface 42c1010edaSGreg Roach{ 4349a243cbSGreg Roach use ModuleBlockTrait; 4449a243cbSGreg Roach 45961ec755SGreg Roach /** 46e5a6b4d4SGreg Roach * @var UserService 47e5a6b4d4SGreg Roach */ 48e5a6b4d4SGreg Roach private $user_service; 49e5a6b4d4SGreg Roach 50e5a6b4d4SGreg Roach /** 51e5a6b4d4SGreg Roach * UserMessagesModule constructor. 52e5a6b4d4SGreg Roach * 53e5a6b4d4SGreg Roach * @param UserService $user_service 54e5a6b4d4SGreg Roach */ 55e2cbf57aSGreg Roach public function __construct(UserService $user_service) 56e2cbf57aSGreg Roach { 57e5a6b4d4SGreg Roach $this->user_service = $user_service; 58e5a6b4d4SGreg Roach } 59e5a6b4d4SGreg Roach 60e5a6b4d4SGreg Roach /** 610cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 62961ec755SGreg Roach * 63961ec755SGreg Roach * @return string 64961ec755SGreg Roach */ 6549a243cbSGreg Roach public function title(): string 66c1010edaSGreg Roach { 67bbb76c12SGreg Roach /* I18N: Name of a module */ 68bbb76c12SGreg Roach return I18N::translate('Messages'); 698c2e8227SGreg Roach } 708c2e8227SGreg Roach 71961ec755SGreg Roach /** 72961ec755SGreg Roach * A sentence describing what this module does. 73961ec755SGreg Roach * 74961ec755SGreg Roach * @return string 75961ec755SGreg Roach */ 7649a243cbSGreg Roach public function description(): string 77c1010edaSGreg Roach { 78bbb76c12SGreg Roach /* I18N: Description of the “Messages” module */ 79bbb76c12SGreg Roach return I18N::translate('Communicate directly with other users, using private messages.'); 808c2e8227SGreg Roach } 818c2e8227SGreg Roach 8276692c8bSGreg Roach /** 83cf700360SGreg Roach * Delete one or messages belonging to a user. 842da2404eSGreg Roach * 856ccdf4f0SGreg Roach * @param ServerRequestInterface $request 86cf700360SGreg Roach * 876ccdf4f0SGreg Roach * @return ResponseInterface 882da2404eSGreg Roach */ 8957ab2231SGreg Roach public function postDeleteMessageAction(ServerRequestInterface $request): ResponseInterface 90c1010edaSGreg Roach { 9157ab2231SGreg Roach $tree = $request->getAttribute('tree'); 92c50a90beSGreg Roach $message_ids = $request->getParsedBody()['message_id'] ?? []; 93cf700360SGreg Roach 94d39b150cSGreg Roach DB::table('message') 95d39b150cSGreg Roach ->where('user_id', '=', Auth::id()) 96d39b150cSGreg Roach ->whereIn('message_id', $message_ids) 97d39b150cSGreg Roach ->delete(); 98cf700360SGreg Roach 993caaa4d2SGreg Roach if ($request->getQueryParams()['context'] === ModuleBlockInterface::CONTEXT_USER_PAGE) { 100*d72b284aSGreg Roach $url = route('user-page', ['tree' => $tree->name()]); 101cf700360SGreg Roach } else { 102*d72b284aSGreg Roach $url = route('tree-page', ['tree' => $tree->name()]); 1032da2404eSGreg Roach } 1042da2404eSGreg Roach 1056ccdf4f0SGreg Roach return redirect($url); 1062da2404eSGreg Roach } 1072da2404eSGreg Roach 1082da2404eSGreg Roach /** 10976692c8bSGreg Roach * Generate the HTML content of this block. 11076692c8bSGreg Roach * 111e490cd80SGreg Roach * @param Tree $tree 11276692c8bSGreg Roach * @param int $block_id 1133caaa4d2SGreg Roach * @param string $context 1143caaa4d2SGreg Roach * @param string[] $config 11576692c8bSGreg Roach * 11676692c8bSGreg Roach * @return string 11776692c8bSGreg Roach */ 1183caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 119c1010edaSGreg Roach { 120d39b150cSGreg Roach $messages = DB::table('message') 121d39b150cSGreg Roach ->where('user_id', '=', Auth::id()) 122d39b150cSGreg Roach ->orderByDesc('message_id') 123ca52a408SGreg Roach ->get() 1240b5fd0a6SGreg Roach ->map(static function (stdClass $row): stdClass { 1254459dc9aSGreg Roach $row->created = Carbon::make($row->created); 126ca52a408SGreg Roach 127ca52a408SGreg Roach return $row; 128ca52a408SGreg Roach }); 1298c2e8227SGreg Roach 1300b5fd0a6SGreg Roach $users = $this->user_service->all()->filter(static function (UserInterface $user) use ($tree): bool { 13176a8e0f3SGreg Roach $public_tree = $tree->getPreference('REQUIRE_AUTHENTICATION') !== '1'; 13276a8e0f3SGreg Roach $can_see_tree = $public_tree || Auth::accessLevel($tree, $user) <= Auth::PRIV_USER; 13376a8e0f3SGreg Roach 13476a8e0f3SGreg Roach return 135895230eeSGreg Roach $user->id() !== Auth::id() && 13676a8e0f3SGreg Roach $user->getPreference('verified_by_admin') && 13776a8e0f3SGreg Roach $can_see_tree && 13876a8e0f3SGreg Roach $user->getPreference('contactmethod') !== 'none'; 1398c2e8227SGreg Roach }); 1408c2e8227SGreg Roach 1417bab8982SGreg Roach $content = ''; 1428b67c11aSGreg Roach if ($users->isNotEmpty()) { 143*d72b284aSGreg Roach $url = route('user-page', ['tree' => $tree->name()]); 144d39b150cSGreg Roach 145*d72b284aSGreg Roach $content .= '<form method="get" action="' . e(route('message', ['tree' => $tree->name()])) . '" onsubmit="return $("#to").val() !== """>'; 14646bb661fSGreg Roach $content .= '<input type="hidden" name="route" value="message">'; 147*d72b284aSGreg Roach $content .= '<input type="hidden" name="tree" value="' . e($tree->name()) . '">'; 148d53324c9SGreg Roach $content .= '<input type="hidden" name="url" value="' . e($url) . '">'; 1497bab8982SGreg Roach $content .= '<label for="to">' . I18N::translate('Send a message') . '</label>'; 1507bab8982SGreg Roach $content .= '<select id="to" name="to">'; 1518c2e8227SGreg Roach $content .= '<option value="">' . I18N::translate('<select>') . '</option>'; 1528c2e8227SGreg Roach foreach ($users as $user) { 153e5a6b4d4SGreg Roach $content .= sprintf('<option value="%1$s">%2$s - %1$s</option>', e($user->userName()), e($user->realName())); 1548c2e8227SGreg Roach } 1558c2e8227SGreg Roach $content .= '</select>'; 1567bab8982SGreg Roach $content .= '<button type="submit">' . I18N::translate('Send') . '</button><br><br>'; 1577bab8982SGreg Roach $content .= '</form>'; 1588c2e8227SGreg Roach } 15983615acfSGreg Roach $content .= '<form method="post" action="' . e(route('module', [ 160c1010edaSGreg Roach 'action' => 'DeleteMessage', 16126684e68SGreg Roach 'module' => $this->name(), 1623caaa4d2SGreg Roach 'context' => $context, 163*d72b284aSGreg Roach 'tree' => $tree->name(), 16483615acfSGreg 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);" id="messageform" name="messageform">'; 165cf700360SGreg Roach $content .= csrf_field(); 166cf700360SGreg Roach 167d39b150cSGreg Roach if ($messages->isNotEmpty()) { 16846bb661fSGreg Roach $content .= '<table class="list_table w-100"><tr>'; 169bc6628fbSRico 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>'; 170e284b8e1SGreg Roach $content .= '<th class="list_label">' . I18N::translate('Subject') . '</th>'; 171e284b8e1SGreg Roach $content .= '<th class="list_label">' . I18N::translate('Date sent') . '</th>'; 172e284b8e1SGreg Roach $content .= '<th class="list_label">' . I18N::translate('Email address') . '</th>'; 1738c2e8227SGreg Roach $content .= '</tr>'; 1748c2e8227SGreg Roach foreach ($messages as $message) { 1758c2e8227SGreg Roach $content .= '<tr>'; 176a33af35aSmakitso $content .= '<td class="list_value_wrap center"><input type="checkbox" name="message_id[]" value="' . $message->message_id . '" id="cb_message' . $message->message_id . '"></td>'; 177d53324c9SGreg 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>'; 1784459dc9aSGreg Roach $content .= '<td class="list_value_wrap">' . view('components/datetime', ['timestamp' => $message->created]) . '</td>'; 1798c2e8227SGreg Roach $content .= '<td class="list_value_wrap">'; 180d39b150cSGreg Roach 181e5a6b4d4SGreg Roach $user = $this->user_service->findByIdentifier($message->sender); 182d39b150cSGreg Roach 18326eae716SGreg Roach if ($user instanceof User) { 184e5a6b4d4SGreg Roach $content .= '<span dir="auto">' . e($user->realName()) . '</span> - <span dir="auto">' . $user->email() . '</span>'; 1858c2e8227SGreg Roach } else { 186d53324c9SGreg Roach $content .= '<a href="mailto:' . e($message->sender) . '">' . e($message->sender) . '</a>'; 1878c2e8227SGreg Roach } 188d39b150cSGreg Roach 1898c2e8227SGreg Roach $content .= '</td>'; 1908c2e8227SGreg Roach $content .= '</tr>'; 1918c2e8227SGreg Roach $content .= '<tr><td class="list_value_wrap" colspan="4"><div id="message' . $message->message_id . '" style="display:none;">'; 192e490cd80SGreg Roach $content .= '<div dir="auto" style="white-space: pre-wrap;">' . Filter::expandUrls($message->body, $tree) . '</div><br>'; 193d39b150cSGreg Roach 194bbb76c12SGreg Roach /* I18N: When replying to an email, the subject becomes “RE: <subject>” */ 195bbb76c12SGreg Roach if (strpos($message->subject, I18N::translate('RE: ')) !== 0) { 1968c2e8227SGreg Roach $message->subject = I18N::translate('RE: ') . $message->subject; 1978c2e8227SGreg Roach } 19846bb661fSGreg Roach 19946bb661fSGreg Roach // If this user still exists, show a reply link. 2008c2e8227SGreg Roach if ($user) { 201c1010edaSGreg Roach $reply_url = route('message', [ 202c1010edaSGreg Roach 'subject' => $message->subject, 203*d72b284aSGreg Roach 'to' => $user->userName(), 204*d72b284aSGreg Roach 'tree' => $tree->name(), 205c1010edaSGreg Roach ]); 206d39b150cSGreg Roach 20746bb661fSGreg Roach $content .= '<a class="btn btn-primary" href="' . e($reply_url) . '" title="' . I18N::translate('Reply') . '">' . I18N::translate('Reply') . '</a> '; 2088c2e8227SGreg Roach } 20946bb661fSGreg 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>'; 2108c2e8227SGreg Roach } 2118c2e8227SGreg Roach $content .= '</table>'; 2122da2404eSGreg Roach $content .= '<p><button type="submit">' . I18N::translate('Delete selected messages') . '</button></p>'; 2138c2e8227SGreg Roach } 2148c2e8227SGreg Roach $content .= '</form>'; 2158c2e8227SGreg Roach 2163caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 217d39b150cSGreg Roach $count = $messages->count(); 218d39b150cSGreg Roach 219147e99aaSGreg Roach return view('modules/block-template', [ 2201e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 2219c6524dcSGreg Roach 'id' => $block_id, 2229c6524dcSGreg Roach 'config_url' => '', 223f7f4b984SGreg Roach 'title' => I18N::plural('%s message', '%s messages', $count, I18N::number($count)), 2249c6524dcSGreg Roach 'content' => $content, 2259c6524dcSGreg Roach ]); 2268c2e8227SGreg Roach } 227b2ce94c6SRico Sonntag 228b2ce94c6SRico Sonntag return $content; 2298c2e8227SGreg Roach } 2308c2e8227SGreg Roach 2313caaa4d2SGreg Roach /** 2323caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 2333caaa4d2SGreg Roach * 2343caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 2353caaa4d2SGreg Roach * 2363caaa4d2SGreg Roach * @return bool 2373caaa4d2SGreg Roach */ 238c1010edaSGreg Roach public function loadAjax(): bool 239c1010edaSGreg Roach { 2408c2e8227SGreg Roach return false; 2418c2e8227SGreg Roach } 2428c2e8227SGreg Roach 2433caaa4d2SGreg Roach /** 2443caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 2453caaa4d2SGreg Roach * 2463caaa4d2SGreg Roach * @return bool 2473caaa4d2SGreg Roach */ 248c1010edaSGreg Roach public function isUserBlock(): bool 249c1010edaSGreg Roach { 2508c2e8227SGreg Roach return true; 2518c2e8227SGreg Roach } 2528c2e8227SGreg Roach 2533caaa4d2SGreg Roach /** 2543caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 2553caaa4d2SGreg Roach * 2563caaa4d2SGreg Roach * @return bool 2573caaa4d2SGreg Roach */ 25863276d8fSGreg Roach public function isTreeBlock(): bool 259c1010edaSGreg Roach { 2608c2e8227SGreg Roach return false; 2618c2e8227SGreg Roach } 2628c2e8227SGreg Roach} 263