18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 51fe542e9SGreg Roach * Copyright (C) 2021 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 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 168c2e8227SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 2176692c8bSGreg Roach 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 23e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 248e0e1b25SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\TreePage; 258e0e1b25SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\UserPage; 260e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 27*d97083feSGreg Roachuse Fisharebest\Webtrees\Registry; 28e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService; 29cf700360SGreg Roachuse Fisharebest\Webtrees\Tree; 30d39b150cSGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 311e7a7a28SGreg Roachuse Illuminate\Support\Str; 326ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 336ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 3471378461SGreg Roach 354ea62551SGreg Roachuse function assert; 3683615acfSGreg Roachuse function route; 37a427f55aSGreg Roachuse function view; 388c2e8227SGreg Roach 398c2e8227SGreg Roach/** 408c2e8227SGreg Roach * Class UserMessagesModule 418c2e8227SGreg Roach */ 4237eb8894SGreg Roachclass UserMessagesModule extends AbstractModule implements ModuleBlockInterface 43c1010edaSGreg Roach{ 4449a243cbSGreg Roach use ModuleBlockTrait; 4549a243cbSGreg Roach 4643f2f523SGreg Roach private UserService $user_service; 47e5a6b4d4SGreg Roach 48e5a6b4d4SGreg Roach /** 49e5a6b4d4SGreg Roach * UserMessagesModule constructor. 50e5a6b4d4SGreg Roach * 51e5a6b4d4SGreg Roach * @param UserService $user_service 52e5a6b4d4SGreg Roach */ 53e2cbf57aSGreg Roach public function __construct(UserService $user_service) 54e2cbf57aSGreg Roach { 55e5a6b4d4SGreg Roach $this->user_service = $user_service; 56e5a6b4d4SGreg Roach } 57e5a6b4d4SGreg Roach 58e5a6b4d4SGreg Roach /** 590cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 60961ec755SGreg Roach * 61961ec755SGreg Roach * @return string 62961ec755SGreg Roach */ 6349a243cbSGreg Roach public function title(): string 64c1010edaSGreg Roach { 65bbb76c12SGreg Roach /* I18N: Name of a module */ 66bbb76c12SGreg Roach return I18N::translate('Messages'); 678c2e8227SGreg Roach } 688c2e8227SGreg Roach 69961ec755SGreg Roach /** 70961ec755SGreg Roach * A sentence describing what this module does. 71961ec755SGreg Roach * 72961ec755SGreg Roach * @return string 73961ec755SGreg Roach */ 7449a243cbSGreg Roach public function description(): string 75c1010edaSGreg Roach { 76bbb76c12SGreg Roach /* I18N: Description of the “Messages” module */ 77bbb76c12SGreg Roach return I18N::translate('Communicate directly with other users, using private messages.'); 788c2e8227SGreg Roach } 798c2e8227SGreg Roach 8076692c8bSGreg Roach /** 81cf700360SGreg Roach * Delete one or messages belonging to a user. 822da2404eSGreg Roach * 836ccdf4f0SGreg Roach * @param ServerRequestInterface $request 84cf700360SGreg Roach * 856ccdf4f0SGreg Roach * @return ResponseInterface 862da2404eSGreg Roach */ 8757ab2231SGreg Roach public function postDeleteMessageAction(ServerRequestInterface $request): ResponseInterface 88c1010edaSGreg Roach { 8957ab2231SGreg Roach $tree = $request->getAttribute('tree'); 904ea62551SGreg Roach assert($tree instanceof Tree); 914ea62551SGreg Roach 92b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 93b46c87bdSGreg Roach 94b46c87bdSGreg Roach $message_ids = $params['message_id'] ?? []; 95cf700360SGreg Roach 96d39b150cSGreg Roach DB::table('message') 97d39b150cSGreg Roach ->where('user_id', '=', Auth::id()) 98d39b150cSGreg Roach ->whereIn('message_id', $message_ids) 99d39b150cSGreg Roach ->delete(); 100cf700360SGreg Roach 1013caaa4d2SGreg Roach if ($request->getQueryParams()['context'] === ModuleBlockInterface::CONTEXT_USER_PAGE) { 1028e0e1b25SGreg Roach $url = route(UserPage::class, ['tree' => $tree->name()]); 103cf700360SGreg Roach } else { 1048e0e1b25SGreg Roach $url = route(TreePage::class, ['tree' => $tree->name()]); 1052da2404eSGreg Roach } 1062da2404eSGreg Roach 1076ccdf4f0SGreg Roach return redirect($url); 1082da2404eSGreg Roach } 1092da2404eSGreg Roach 1102da2404eSGreg Roach /** 11176692c8bSGreg Roach * Generate the HTML content of this block. 11276692c8bSGreg Roach * 113e490cd80SGreg Roach * @param Tree $tree 11476692c8bSGreg Roach * @param int $block_id 1153caaa4d2SGreg Roach * @param string $context 11609482a55SGreg Roach * @param array<string> $config 11776692c8bSGreg Roach * 11876692c8bSGreg Roach * @return string 11976692c8bSGreg Roach */ 1203caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 121c1010edaSGreg Roach { 122d39b150cSGreg Roach $messages = DB::table('message') 123d39b150cSGreg Roach ->where('user_id', '=', Auth::id()) 124d39b150cSGreg Roach ->orderByDesc('message_id') 125ca52a408SGreg Roach ->get() 126f70bcff5SGreg Roach ->map(static function (object $row): object { 127*d97083feSGreg Roach $row->created = Registry::timestampFactory()->fromString($row->created); 128ca52a408SGreg Roach 129ca52a408SGreg Roach return $row; 130ca52a408SGreg Roach }); 1318c2e8227SGreg Roach 1320b5fd0a6SGreg Roach $users = $this->user_service->all()->filter(static function (UserInterface $user) use ($tree): bool { 13376a8e0f3SGreg Roach $public_tree = $tree->getPreference('REQUIRE_AUTHENTICATION') !== '1'; 13476a8e0f3SGreg Roach $can_see_tree = $public_tree || Auth::accessLevel($tree, $user) <= Auth::PRIV_USER; 13576a8e0f3SGreg Roach 13676a8e0f3SGreg Roach return 137895230eeSGreg Roach $user->id() !== Auth::id() && 1381fe542e9SGreg Roach $user->getPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED) && 13976a8e0f3SGreg Roach $can_see_tree && 1401fe542e9SGreg Roach $user->getPreference(UserInterface::PREF_CONTACT_METHOD) !== 'none'; 1418c2e8227SGreg Roach }); 1428c2e8227SGreg Roach 143b47837c4SGreg Roach $content = view('modules/user-messages/user-messages', [ 144b47837c4SGreg Roach 'block_id' => $block_id, 1453caaa4d2SGreg Roach 'context' => $context, 146b47837c4SGreg Roach 'messages' => $messages, 147b47837c4SGreg Roach 'module' => $this, 148b47837c4SGreg Roach 'tree' => $tree, 149b47837c4SGreg Roach 'user_service' => $this->user_service, 150b47837c4SGreg Roach 'users' => $users, 151c1010edaSGreg Roach ]); 152d39b150cSGreg Roach 1533caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 154d39b150cSGreg Roach $count = $messages->count(); 155d39b150cSGreg Roach 156147e99aaSGreg Roach return view('modules/block-template', [ 1571e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1589c6524dcSGreg Roach 'id' => $block_id, 1599c6524dcSGreg Roach 'config_url' => '', 160f7f4b984SGreg Roach 'title' => I18N::plural('%s message', '%s messages', $count, I18N::number($count)), 1619c6524dcSGreg Roach 'content' => $content, 1629c6524dcSGreg Roach ]); 1638c2e8227SGreg Roach } 164b2ce94c6SRico Sonntag 165b2ce94c6SRico Sonntag return $content; 1668c2e8227SGreg Roach } 1678c2e8227SGreg Roach 1683caaa4d2SGreg Roach /** 1693caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 1703caaa4d2SGreg Roach * 1713caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 1723caaa4d2SGreg Roach * 1733caaa4d2SGreg Roach * @return bool 1743caaa4d2SGreg Roach */ 175c1010edaSGreg Roach public function loadAjax(): bool 176c1010edaSGreg Roach { 1778c2e8227SGreg Roach return false; 1788c2e8227SGreg Roach } 1798c2e8227SGreg Roach 1803caaa4d2SGreg Roach /** 1813caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 1823caaa4d2SGreg Roach * 1833caaa4d2SGreg Roach * @return bool 1843caaa4d2SGreg Roach */ 185c1010edaSGreg Roach public function isUserBlock(): bool 186c1010edaSGreg Roach { 1878c2e8227SGreg Roach return true; 1888c2e8227SGreg Roach } 1898c2e8227SGreg Roach 1903caaa4d2SGreg Roach /** 1913caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 1923caaa4d2SGreg Roach * 1933caaa4d2SGreg Roach * @return bool 1943caaa4d2SGreg Roach */ 19563276d8fSGreg Roach public function isTreeBlock(): bool 196c1010edaSGreg Roach { 1978c2e8227SGreg Roach return false; 1988c2e8227SGreg Roach } 1998c2e8227SGreg Roach} 200