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