1<?php 2 3/** 4 * webtrees: online genealogy 5 * 'Copyright (C) 2023 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\MessageService; 29use Fisharebest\Webtrees\Services\UserService; 30use Fisharebest\Webtrees\Tree; 31use Fisharebest\Webtrees\Validator; 32use Illuminate\Database\Capsule\Manager as DB; 33use Illuminate\Support\Str; 34use Psr\Http\Message\ResponseInterface; 35use Psr\Http\Message\ServerRequestInterface; 36 37use function route; 38use function view; 39 40/** 41 * Class UserMessagesModule 42 */ 43class UserMessagesModule extends AbstractModule implements ModuleBlockInterface 44{ 45 use ModuleBlockTrait; 46 47 private UserService $user_service; 48 49 /** 50 * UserMessagesModule constructor. 51 * 52 * @param UserService $user_service 53 */ 54 public function __construct(UserService $user_service) 55 { 56 $this->user_service = $user_service; 57 } 58 59 /** 60 * How should this module be identified in the control panel, etc.? 61 * 62 * @return string 63 */ 64 public function title(): string 65 { 66 /* I18N: Name of a module */ 67 return I18N::translate('Messages'); 68 } 69 70 /** 71 * A sentence describing what this module does. 72 * 73 * @return string 74 */ 75 public function description(): string 76 { 77 /* I18N: Description of the “Messages” module */ 78 return I18N::translate('Communicate directly with other users, using private messages.'); 79 } 80 81 /** 82 * Delete one or messages belonging to a user. 83 * 84 * @param ServerRequestInterface $request 85 * 86 * @return ResponseInterface 87 */ 88 public function postDeleteMessageAction(ServerRequestInterface $request): ResponseInterface 89 { 90 $tree = Validator::attributes($request)->tree(); 91 $context = Validator::queryParams($request)->string('context'); 92 $message_ids = Validator::parsedBody($request)->array('message_id'); 93 94 DB::table('message') 95 ->where('user_id', '=', Auth::id()) 96 ->whereIn('message_id', $message_ids) 97 ->delete(); 98 99 if ($context === ModuleBlockInterface::CONTEXT_USER_PAGE) { 100 $url = route(UserPage::class, ['tree' => $tree->name()]); 101 } else { 102 $url = route(TreePage::class, ['tree' => $tree->name()]); 103 } 104 105 return redirect($url); 106 } 107 108 /** 109 * Generate the HTML content of this block. 110 * 111 * @param Tree $tree 112 * @param int $block_id 113 * @param string $context 114 * @param array<string,string> $config 115 * 116 * @return string 117 */ 118 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 119 { 120 $messages = DB::table('message') 121 ->where('user_id', '=', Auth::id()) 122 ->orderByDesc('message_id') 123 ->get() 124 ->map(static function (object $row): object { 125 $row->created = Registry::timestampFactory()->fromString($row->created); 126 127 return $row; 128 }); 129 130 $users = $this->user_service->all()->filter(static function (UserInterface $user) use ($tree): bool { 131 $public_tree = $tree->getPreference('REQUIRE_AUTHENTICATION') !== '1'; 132 $can_see_tree = $public_tree || Auth::accessLevel($tree, $user) <= Auth::PRIV_USER; 133 134 return 135 $user->id() !== Auth::id() && 136 $user->getPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED) && 137 $can_see_tree && 138 $user->getPreference(UserInterface::PREF_CONTACT_METHOD) !== MessageService::CONTACT_METHOD_NONE; 139 }); 140 141 $content = view('modules/user-messages/user-messages', [ 142 'block_id' => $block_id, 143 'context' => $context, 144 'messages' => $messages, 145 'module' => $this, 146 'tree' => $tree, 147 'user_service' => $this->user_service, 148 'users' => $users, 149 ]); 150 151 if ($context !== self::CONTEXT_EMBED) { 152 $count = $messages->count(); 153 154 return view('modules/block-template', [ 155 'block' => Str::kebab($this->name()), 156 'id' => $block_id, 157 'config_url' => '', 158 'title' => I18N::plural('%s message', '%s messages', $count, I18N::number($count)), 159 'content' => $content, 160 ]); 161 } 162 163 return $content; 164 } 165 166 /** 167 * Should this block load asynchronously using AJAX? 168 * 169 * Simple blocks are faster in-line, more complex ones can be loaded later. 170 * 171 * @return bool 172 */ 173 public function loadAjax(): bool 174 { 175 return false; 176 } 177 178 /** 179 * Can this block be shown on the user’s home page? 180 * 181 * @return bool 182 */ 183 public function isUserBlock(): bool 184 { 185 return true; 186 } 187 188 /** 189 * Can this block be shown on the tree’s home page? 190 * 191 * @return bool 192 */ 193 public function isTreeBlock(): bool 194 { 195 return false; 196 } 197} 198