1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\Carbon; 24use Fisharebest\Webtrees\Contracts\UserInterface; 25use Fisharebest\Webtrees\Filter; 26use Fisharebest\Webtrees\Http\Controllers\HomePageController; 27use Fisharebest\Webtrees\Http\RequestHandlers\MessagePage; 28use Fisharebest\Webtrees\Http\RequestHandlers\MessageSelect; 29use Fisharebest\Webtrees\I18N; 30use Fisharebest\Webtrees\Services\UserService; 31use Fisharebest\Webtrees\Tree; 32use Fisharebest\Webtrees\User; 33use Illuminate\Database\Capsule\Manager as DB; 34use Illuminate\Support\Str; 35use Psr\Http\Message\ResponseInterface; 36use Psr\Http\Message\ServerRequestInterface; 37use stdClass; 38 39use function assert; 40use function e; 41use function route; 42 43/** 44 * Class UserMessagesModule 45 */ 46class UserMessagesModule extends AbstractModule implements ModuleBlockInterface 47{ 48 use ModuleBlockTrait; 49 50 /** 51 * @var UserService 52 */ 53 private $user_service; 54 55 /** 56 * UserMessagesModule constructor. 57 * 58 * @param UserService $user_service 59 */ 60 public function __construct(UserService $user_service) 61 { 62 $this->user_service = $user_service; 63 } 64 65 /** 66 * How should this module be identified in the control panel, etc.? 67 * 68 * @return string 69 */ 70 public function title(): string 71 { 72 /* I18N: Name of a module */ 73 return I18N::translate('Messages'); 74 } 75 76 /** 77 * A sentence describing what this module does. 78 * 79 * @return string 80 */ 81 public function description(): string 82 { 83 /* I18N: Description of the “Messages” module */ 84 return I18N::translate('Communicate directly with other users, using private messages.'); 85 } 86 87 /** 88 * Delete one or messages belonging to a user. 89 * 90 * @param ServerRequestInterface $request 91 * 92 * @return ResponseInterface 93 */ 94 public function postDeleteMessageAction(ServerRequestInterface $request): ResponseInterface 95 { 96 $tree = $request->getAttribute('tree'); 97 assert($tree instanceof Tree); 98 99 $params = (array) $request->getParsedBody(); 100 101 $message_ids = $params['message_id'] ?? []; 102 103 DB::table('message') 104 ->where('user_id', '=', Auth::id()) 105 ->whereIn('message_id', $message_ids) 106 ->delete(); 107 108 if ($request->getQueryParams()['context'] === ModuleBlockInterface::CONTEXT_USER_PAGE) { 109 $url = route('user-page', ['tree' => $tree->name()]); 110 } else { 111 $url = route('tree-page', ['tree' => $tree->name()]); 112 } 113 114 return redirect($url); 115 } 116 117 /** 118 * Generate the HTML content of this block. 119 * 120 * @param Tree $tree 121 * @param int $block_id 122 * @param string $context 123 * @param string[] $config 124 * 125 * @return string 126 */ 127 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 128 { 129 $messages = DB::table('message') 130 ->where('user_id', '=', Auth::id()) 131 ->orderByDesc('message_id') 132 ->get() 133 ->map(static function (stdClass $row): stdClass { 134 $row->created = Carbon::make($row->created); 135 136 return $row; 137 }); 138 139 $users = $this->user_service->all()->filter(static function (UserInterface $user) use ($tree): bool { 140 $public_tree = $tree->getPreference('REQUIRE_AUTHENTICATION') !== '1'; 141 $can_see_tree = $public_tree || Auth::accessLevel($tree, $user) <= Auth::PRIV_USER; 142 143 return 144 $user->id() !== Auth::id() && 145 $user->getPreference(User::PREF_IS_ACCOUNT_APPROVED) && 146 $can_see_tree && 147 $user->getPreference(User::PREF_CONTACT_METHOD) !== 'none'; 148 }); 149 150 $content = ''; 151 if ($users->isNotEmpty()) { 152 $url = route('user-page', ['tree' => $tree->name()]); 153 154 $content .= '<form method="post" action="' . e(route(MessageSelect::class, ['tree' => $tree->name()])) . '">'; 155 $content .= csrf_field(); 156 $content .= '<input type="hidden" name="url" value="' . e($url) . '">'; 157 $content .= '<label for="to">' . I18N::translate('Send a message') . '</label>'; 158 $content .= '<select id="to" name="to" required>'; 159 $content .= '<option value="">' . I18N::translate('<select>') . '</option>'; 160 foreach ($users as $user) { 161 $content .= sprintf('<option value="%1$s">%2$s - %1$s</option>', e($user->userName()), e($user->realName())); 162 } 163 $content .= '</select>'; 164 $content .= '<button type="submit">' . I18N::translate('Send') . '</button><br><br>'; 165 $content .= '</form>'; 166 } 167 $content .= '<form method="post" action="' . e(route('module', [ 168 'action' => 'DeleteMessage', 169 'module' => $this->name(), 170 'context' => $context, 171 'tree' => $tree->name(), 172 ])) . '" 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">'; 173 $content .= csrf_field(); 174 175 if ($messages->isNotEmpty()) { 176 $content .= '<div class="table-responsive">'; 177 $content .= '<table class="table table-sm w-100"><tr>'; 178 $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>'; 179 $content .= '<th class="list_label">' . I18N::translate('Subject') . '</th>'; 180 $content .= '<th class="list_label">' . I18N::translate('Date sent') . '</th>'; 181 $content .= '<th class="list_label">' . I18N::translate('Email address') . '</th>'; 182 $content .= '</tr>'; 183 foreach ($messages as $message) { 184 $content .= '<tr>'; 185 $content .= '<td class="list_value_wrap center"><input type="checkbox" name="message_id[]" value="' . $message->message_id . '" id="cb_message' . $message->message_id . '"></td>'; 186 $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>'; 187 $content .= '<td class="list_value_wrap">' . view('components/datetime', ['timestamp' => $message->created]) . '</td>'; 188 $content .= '<td class="list_value_wrap">'; 189 190 $user = $this->user_service->findByIdentifier($message->sender); 191 192 if ($user instanceof User) { 193 $content .= '<span dir="auto">' . e($user->realName()) . '</span> - <span dir="auto">' . $user->email() . '</span>'; 194 } else { 195 $content .= '<a href="mailto:' . e($message->sender) . '">' . e($message->sender) . '</a>'; 196 } 197 198 $content .= '</td>'; 199 $content .= '</tr>'; 200 $content .= '<tr><td class="list_value_wrap" colspan="4"><div id="message' . $message->message_id . '" style="display:none;">'; 201 $content .= '<div dir="auto" style="white-space: pre-wrap;">' . Filter::expandUrls($message->body, $tree) . '</div><br>'; 202 203 /* I18N: When replying to an email, the subject becomes “RE: <subject>” */ 204 if (strpos($message->subject, I18N::translate('RE: ')) !== 0) { 205 $message->subject = I18N::translate('RE: ') . $message->subject; 206 } 207 208 // If this user still exists, show a reply link. 209 if ($user instanceof User) { 210 $reply_url = route(MessagePage::class, [ 211 'subject' => $message->subject, 212 'to' => $user->userName(), 213 'tree' => $tree->name(), 214 'url' => route('user-page', ['tree' => $tree->name()]), 215 ]); 216 217 $content .= '<a class="btn btn-primary" href="' . e($reply_url) . '" title="' . I18N::translate('Reply') . '">' . I18N::translate('Reply') . '</a> '; 218 } 219 $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>'; 220 } 221 $content .= '</table>'; 222 $content .= '</div>'; 223 $content .= '<p><button type="submit">' . I18N::translate('Delete selected messages') . '</button></p>'; 224 } 225 $content .= '</form>'; 226 227 if ($context !== self::CONTEXT_EMBED) { 228 $count = $messages->count(); 229 230 return view('modules/block-template', [ 231 'block' => Str::kebab($this->name()), 232 'id' => $block_id, 233 'config_url' => '', 234 'title' => I18N::plural('%s message', '%s messages', $count, I18N::number($count)), 235 'content' => $content, 236 ]); 237 } 238 239 return $content; 240 } 241 242 /** 243 * Should this block load asynchronously using AJAX? 244 * 245 * Simple blocks are faster in-line, more complex ones can be loaded later. 246 * 247 * @return bool 248 */ 249 public function loadAjax(): bool 250 { 251 return false; 252 } 253 254 /** 255 * Can this block be shown on the user’s home page? 256 * 257 * @return bool 258 */ 259 public function isUserBlock(): bool 260 { 261 return true; 262 } 263 264 /** 265 * Can this block be shown on the tree’s home page? 266 * 267 * @return bool 268 */ 269 public function isTreeBlock(): bool 270 { 271 return false; 272 } 273} 274