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 158c2e8227SGreg Roach * along with this program. If not, see <http://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; 234459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon; 24e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter; 26e381f98dSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\MessagePage; 27e381f98dSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\MessageSelect; 288e0e1b25SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\TreePage; 298e0e1b25SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\UserPage; 300e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 31e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService; 32cf700360SGreg Roachuse Fisharebest\Webtrees\Tree; 330e62c4b8SGreg Roachuse Fisharebest\Webtrees\User; 34d39b150cSGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 351e7a7a28SGreg Roachuse Illuminate\Support\Str; 366ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 376ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 38ca52a408SGreg Roachuse stdClass; 3971378461SGreg Roach 404ea62551SGreg Roachuse function assert; 4183615acfSGreg Roachuse function e; 4283615acfSGreg Roachuse function route; 43dec352c1SGreg Roachuse function str_starts_with; 44a427f55aSGreg Roachuse function view; 458c2e8227SGreg Roach 468c2e8227SGreg Roach/** 478c2e8227SGreg Roach * Class UserMessagesModule 488c2e8227SGreg Roach */ 4937eb8894SGreg Roachclass UserMessagesModule extends AbstractModule implements ModuleBlockInterface 50c1010edaSGreg Roach{ 5149a243cbSGreg Roach use ModuleBlockTrait; 5249a243cbSGreg Roach 53961ec755SGreg Roach /** 54e5a6b4d4SGreg Roach * @var UserService 55e5a6b4d4SGreg Roach */ 56e5a6b4d4SGreg Roach private $user_service; 57e5a6b4d4SGreg Roach 58e5a6b4d4SGreg Roach /** 59e5a6b4d4SGreg Roach * UserMessagesModule constructor. 60e5a6b4d4SGreg Roach * 61e5a6b4d4SGreg Roach * @param UserService $user_service 62e5a6b4d4SGreg Roach */ 63e2cbf57aSGreg Roach public function __construct(UserService $user_service) 64e2cbf57aSGreg Roach { 65e5a6b4d4SGreg Roach $this->user_service = $user_service; 66e5a6b4d4SGreg Roach } 67e5a6b4d4SGreg Roach 68e5a6b4d4SGreg Roach /** 690cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 70961ec755SGreg Roach * 71961ec755SGreg Roach * @return string 72961ec755SGreg Roach */ 7349a243cbSGreg Roach public function title(): string 74c1010edaSGreg Roach { 75bbb76c12SGreg Roach /* I18N: Name of a module */ 76bbb76c12SGreg Roach return I18N::translate('Messages'); 778c2e8227SGreg Roach } 788c2e8227SGreg Roach 79961ec755SGreg Roach /** 80961ec755SGreg Roach * A sentence describing what this module does. 81961ec755SGreg Roach * 82961ec755SGreg Roach * @return string 83961ec755SGreg Roach */ 8449a243cbSGreg Roach public function description(): string 85c1010edaSGreg Roach { 86bbb76c12SGreg Roach /* I18N: Description of the “Messages” module */ 87bbb76c12SGreg Roach return I18N::translate('Communicate directly with other users, using private messages.'); 888c2e8227SGreg Roach } 898c2e8227SGreg Roach 9076692c8bSGreg Roach /** 91cf700360SGreg Roach * Delete one or messages belonging to a user. 922da2404eSGreg Roach * 936ccdf4f0SGreg Roach * @param ServerRequestInterface $request 94cf700360SGreg Roach * 956ccdf4f0SGreg Roach * @return ResponseInterface 962da2404eSGreg Roach */ 9757ab2231SGreg Roach public function postDeleteMessageAction(ServerRequestInterface $request): ResponseInterface 98c1010edaSGreg Roach { 9957ab2231SGreg Roach $tree = $request->getAttribute('tree'); 1004ea62551SGreg Roach assert($tree instanceof Tree); 1014ea62551SGreg Roach 102b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 103b46c87bdSGreg Roach 104b46c87bdSGreg Roach $message_ids = $params['message_id'] ?? []; 105cf700360SGreg Roach 106d39b150cSGreg Roach DB::table('message') 107d39b150cSGreg Roach ->where('user_id', '=', Auth::id()) 108d39b150cSGreg Roach ->whereIn('message_id', $message_ids) 109d39b150cSGreg Roach ->delete(); 110cf700360SGreg Roach 1113caaa4d2SGreg Roach if ($request->getQueryParams()['context'] === ModuleBlockInterface::CONTEXT_USER_PAGE) { 1128e0e1b25SGreg Roach $url = route(UserPage::class, ['tree' => $tree->name()]); 113cf700360SGreg Roach } else { 1148e0e1b25SGreg Roach $url = route(TreePage::class, ['tree' => $tree->name()]); 1152da2404eSGreg Roach } 1162da2404eSGreg Roach 1176ccdf4f0SGreg Roach return redirect($url); 1182da2404eSGreg Roach } 1192da2404eSGreg Roach 1202da2404eSGreg Roach /** 12176692c8bSGreg Roach * Generate the HTML content of this block. 12276692c8bSGreg Roach * 123e490cd80SGreg Roach * @param Tree $tree 12476692c8bSGreg Roach * @param int $block_id 1253caaa4d2SGreg Roach * @param string $context 1263caaa4d2SGreg Roach * @param string[] $config 12776692c8bSGreg Roach * 12876692c8bSGreg Roach * @return string 12976692c8bSGreg Roach */ 1303caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 131c1010edaSGreg Roach { 132d39b150cSGreg Roach $messages = DB::table('message') 133d39b150cSGreg Roach ->where('user_id', '=', Auth::id()) 134d39b150cSGreg Roach ->orderByDesc('message_id') 135ca52a408SGreg Roach ->get() 1360b5fd0a6SGreg Roach ->map(static function (stdClass $row): stdClass { 1374459dc9aSGreg Roach $row->created = Carbon::make($row->created); 138ca52a408SGreg Roach 139ca52a408SGreg Roach return $row; 140ca52a408SGreg Roach }); 1418c2e8227SGreg Roach 1420b5fd0a6SGreg Roach $users = $this->user_service->all()->filter(static function (UserInterface $user) use ($tree): bool { 14376a8e0f3SGreg Roach $public_tree = $tree->getPreference('REQUIRE_AUTHENTICATION') !== '1'; 14476a8e0f3SGreg Roach $can_see_tree = $public_tree || Auth::accessLevel($tree, $user) <= Auth::PRIV_USER; 14576a8e0f3SGreg Roach 14676a8e0f3SGreg Roach return 147895230eeSGreg Roach $user->id() !== Auth::id() && 1481fe542e9SGreg Roach $user->getPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED) && 14976a8e0f3SGreg Roach $can_see_tree && 1501fe542e9SGreg Roach $user->getPreference(UserInterface::PREF_CONTACT_METHOD) !== 'none'; 1518c2e8227SGreg Roach }); 1528c2e8227SGreg Roach 153*b47837c4SGreg Roach $content = view('modules/user-messages/user-messages', [ 154*b47837c4SGreg Roach 'block_id' => $block_id, 1553caaa4d2SGreg Roach 'context' => $context, 156*b47837c4SGreg Roach 'messages' => $messages, 157*b47837c4SGreg Roach 'module' => $this, 158*b47837c4SGreg Roach 'tree' => $tree, 159*b47837c4SGreg Roach 'user_service' => $this->user_service, 160*b47837c4SGreg Roach 'users' => $users, 161c1010edaSGreg Roach ]); 162d39b150cSGreg Roach 1633caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 164d39b150cSGreg Roach $count = $messages->count(); 165d39b150cSGreg Roach 166147e99aaSGreg Roach return view('modules/block-template', [ 1671e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1689c6524dcSGreg Roach 'id' => $block_id, 1699c6524dcSGreg Roach 'config_url' => '', 170f7f4b984SGreg Roach 'title' => I18N::plural('%s message', '%s messages', $count, I18N::number($count)), 1719c6524dcSGreg Roach 'content' => $content, 1729c6524dcSGreg Roach ]); 1738c2e8227SGreg Roach } 174b2ce94c6SRico Sonntag 175b2ce94c6SRico Sonntag return $content; 1768c2e8227SGreg Roach } 1778c2e8227SGreg Roach 1783caaa4d2SGreg Roach /** 1793caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 1803caaa4d2SGreg Roach * 1813caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 1823caaa4d2SGreg Roach * 1833caaa4d2SGreg Roach * @return bool 1843caaa4d2SGreg Roach */ 185c1010edaSGreg Roach public function loadAjax(): bool 186c1010edaSGreg Roach { 1878c2e8227SGreg Roach return false; 1888c2e8227SGreg Roach } 1898c2e8227SGreg Roach 1903caaa4d2SGreg Roach /** 1913caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 1923caaa4d2SGreg Roach * 1933caaa4d2SGreg Roach * @return bool 1943caaa4d2SGreg Roach */ 195c1010edaSGreg Roach public function isUserBlock(): bool 196c1010edaSGreg Roach { 1978c2e8227SGreg Roach return true; 1988c2e8227SGreg Roach } 1998c2e8227SGreg Roach 2003caaa4d2SGreg Roach /** 2013caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 2023caaa4d2SGreg Roach * 2033caaa4d2SGreg Roach * @return bool 2043caaa4d2SGreg Roach */ 20563276d8fSGreg Roach public function isTreeBlock(): bool 206c1010edaSGreg Roach { 2078c2e8227SGreg Roach return false; 2088c2e8227SGreg Roach } 2098c2e8227SGreg Roach} 210