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