1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\Contracts\UserInterface; 24use Fisharebest\Webtrees\Http\RequestHandlers\TreePage; 25use Fisharebest\Webtrees\Http\RequestHandlers\UserPage; 26use Fisharebest\Webtrees\I18N; 27use Fisharebest\Webtrees\Registry; 28use Fisharebest\Webtrees\Services\UserService; 29use Fisharebest\Webtrees\Tree; 30use Illuminate\Database\Capsule\Manager as DB; 31use Illuminate\Support\Str; 32use Psr\Http\Message\ResponseInterface; 33use Psr\Http\Message\ServerRequestInterface; 34 35use function assert; 36use function route; 37use function view; 38 39/** 40 * Class UserMessagesModule 41 */ 42class UserMessagesModule extends AbstractModule implements ModuleBlockInterface 43{ 44 use ModuleBlockTrait; 45 46 private UserService $user_service; 47 48 /** 49 * UserMessagesModule constructor. 50 * 51 * @param UserService $user_service 52 */ 53 public function __construct(UserService $user_service) 54 { 55 $this->user_service = $user_service; 56 } 57 58 /** 59 * How should this module be identified in the control panel, etc.? 60 * 61 * @return string 62 */ 63 public function title(): string 64 { 65 /* I18N: Name of a module */ 66 return I18N::translate('Messages'); 67 } 68 69 /** 70 * A sentence describing what this module does. 71 * 72 * @return string 73 */ 74 public function description(): string 75 { 76 /* I18N: Description of the “Messages” module */ 77 return I18N::translate('Communicate directly with other users, using private messages.'); 78 } 79 80 /** 81 * Delete one or messages belonging to a user. 82 * 83 * @param ServerRequestInterface $request 84 * 85 * @return ResponseInterface 86 */ 87 public function postDeleteMessageAction(ServerRequestInterface $request): ResponseInterface 88 { 89 $tree = $request->getAttribute('tree'); 90 assert($tree instanceof Tree); 91 92 $params = (array) $request->getParsedBody(); 93 94 $message_ids = $params['message_id'] ?? []; 95 96 DB::table('message') 97 ->where('user_id', '=', Auth::id()) 98 ->whereIn('message_id', $message_ids) 99 ->delete(); 100 101 if ($request->getQueryParams()['context'] === ModuleBlockInterface::CONTEXT_USER_PAGE) { 102 $url = route(UserPage::class, ['tree' => $tree->name()]); 103 } else { 104 $url = route(TreePage::class, ['tree' => $tree->name()]); 105 } 106 107 return redirect($url); 108 } 109 110 /** 111 * Generate the HTML content of this block. 112 * 113 * @param Tree $tree 114 * @param int $block_id 115 * @param string $context 116 * @param array<string,string> $config 117 * 118 * @return string 119 */ 120 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 121 { 122 $messages = DB::table('message') 123 ->where('user_id', '=', Auth::id()) 124 ->orderByDesc('message_id') 125 ->get() 126 ->map(static function (object $row): object { 127 $row->created = Registry::timestampFactory()->fromString($row->created); 128 129 return $row; 130 }); 131 132 $users = $this->user_service->all()->filter(static function (UserInterface $user) use ($tree): bool { 133 $public_tree = $tree->getPreference('REQUIRE_AUTHENTICATION') !== '1'; 134 $can_see_tree = $public_tree || Auth::accessLevel($tree, $user) <= Auth::PRIV_USER; 135 136 return 137 $user->id() !== Auth::id() && 138 $user->getPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED) && 139 $can_see_tree && 140 $user->getPreference(UserInterface::PREF_CONTACT_METHOD) !== 'none'; 141 }); 142 143 $content = view('modules/user-messages/user-messages', [ 144 'block_id' => $block_id, 145 'context' => $context, 146 'messages' => $messages, 147 'module' => $this, 148 'tree' => $tree, 149 'user_service' => $this->user_service, 150 'users' => $users, 151 ]); 152 153 if ($context !== self::CONTEXT_EMBED) { 154 $count = $messages->count(); 155 156 return view('modules/block-template', [ 157 'block' => Str::kebab($this->name()), 158 'id' => $block_id, 159 'config_url' => '', 160 'title' => I18N::plural('%s message', '%s messages', $count, I18N::number($count)), 161 'content' => $content, 162 ]); 163 } 164 165 return $content; 166 } 167 168 /** 169 * Should this block load asynchronously using AJAX? 170 * 171 * Simple blocks are faster in-line, more complex ones can be loaded later. 172 * 173 * @return bool 174 */ 175 public function loadAjax(): bool 176 { 177 return false; 178 } 179 180 /** 181 * Can this block be shown on the user’s home page? 182 * 183 * @return bool 184 */ 185 public function isUserBlock(): bool 186 { 187 return true; 188 } 189 190 /** 191 * Can this block be shown on the tree’s home page? 192 * 193 * @return bool 194 */ 195 public function isTreeBlock(): bool 196 { 197 return false; 198 } 199} 200