1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Module; 19 20use Fisharebest\Webtrees\Auth; 21use Fisharebest\Webtrees\Carbon; 22use Fisharebest\Webtrees\Contracts\UserInterface; 23use Fisharebest\Webtrees\Filter; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Services\UserService; 26use Fisharebest\Webtrees\Tree; 27use Fisharebest\Webtrees\User; 28use Illuminate\Database\Capsule\Manager as DB; 29use stdClass; 30use Symfony\Component\HttpFoundation\RedirectResponse; 31use Symfony\Component\HttpFoundation\Request; 32use Symfony\Component\HttpFoundation\Response; 33 34/** 35 * Class UserMessagesModule 36 */ 37class UserMessagesModule extends AbstractModule implements ModuleBlockInterface 38{ 39 use ModuleBlockTrait; 40 41 /** 42 * @var UserService 43 */ 44 private $user_service; 45 46 /** 47 * UserMessagesModule constructor. 48 * 49 * @param UserService $user_service 50 */ 51 public function __construct(UserService $user_service) 52 { 53 $this->user_service = $user_service; 54 } 55 56 /** 57 * How should this module be identified in the control panel, etc.? 58 * 59 * @return string 60 */ 61 public function title(): string 62 { 63 /* I18N: Name of a module */ 64 return I18N::translate('Messages'); 65 } 66 67 /** 68 * A sentence describing what this module does. 69 * 70 * @return string 71 */ 72 public function description(): string 73 { 74 /* I18N: Description of the “Messages” module */ 75 return I18N::translate('Communicate directly with other users, using private messages.'); 76 } 77 78 /** 79 * Delete one or messages belonging to a user. 80 * 81 * @param Request $request 82 * @param Tree $tree 83 * 84 * @return Response 85 */ 86 public function postDeleteMessageAction(Request $request, Tree $tree): Response 87 { 88 $message_ids = (array) $request->get('message_id', []); 89 90 DB::table('message') 91 ->where('user_id', '=', Auth::id()) 92 ->whereIn('message_id', $message_ids) 93 ->delete(); 94 95 if ($request->get('ctype') === 'user') { 96 $url = route('user-page', ['ged' => $tree->name()]); 97 } else { 98 $url = route('tree-page', ['ged' => $tree->name()]); 99 } 100 101 return new RedirectResponse($url); 102 } 103 104 /** 105 * Generate the HTML content of this block. 106 * 107 * @param Tree $tree 108 * @param int $block_id 109 * @param string $ctype 110 * @param string[] $cfg 111 * 112 * @return string 113 */ 114 public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 115 { 116 $messages = DB::table('message') 117 ->where('user_id', '=', Auth::id()) 118 ->orderByDesc('message_id') 119 ->get() 120 ->map(function (stdClass $row): stdClass { 121 $row->created = Carbon::make($row->created); 122 123 return $row; 124 }); 125 126 $users = $this->user_service->all()->filter(function (UserInterface $user) use ($tree): bool { 127 $public_tree = $tree->getPreference('REQUIRE_AUTHENTICATION') !== '1'; 128 $can_see_tree = $public_tree || Auth::accessLevel($tree, $user) <= Auth::PRIV_USER; 129 130 return 131 $user->id() !== Auth::id() && 132 $user->getPreference('verified_by_admin') && 133 $can_see_tree && 134 $user->getPreference('contactmethod') !== 'none'; 135 }); 136 137 $content = ''; 138 if ($users->isNotEmpty()) { 139 $url = route('user-page', ['ged' => $tree->name()]); 140 141 $content .= '<form onsubmit="return $("#to").val() !== """>'; 142 $content .= '<input type="hidden" name="route" value="message">'; 143 $content .= '<input type="hidden" name="ged" value="' . e($tree->name()) . '">'; 144 $content .= '<input type="hidden" name="url" value="' . e($url) . '">'; 145 $content .= '<label for="to">' . I18N::translate('Send a message') . '</label>'; 146 $content .= '<select id="to" name="to">'; 147 $content .= '<option value="">' . I18N::translate('<select>') . '</option>'; 148 foreach ($users as $user) { 149 $content .= sprintf('<option value="%1$s">%2$s - %1$s</option>', e($user->userName()), e($user->realName())); 150 } 151 $content .= '</select>'; 152 $content .= '<button type="submit">' . I18N::translate('Send') . '</button><br><br>'; 153 $content .= '</form>'; 154 } 155 $content .= '<form id="messageform" name="messageform" method="post" action="' . e(route('module', [ 156 'action' => 'DeleteMessage', 157 'module' => $this->name(), 158 'ctype' => $ctype, 159 'ged' => $tree->name(), 160 ])) . '" data-confirm="' . I18N::translate('Are you sure you want to delete this message? It cannot be retrieved later.') . '" onsubmit="return confirm(this.dataset.confirm);">'; 161 $content .= csrf_field(); 162 163 if ($messages->isNotEmpty()) { 164 $content .= '<table class="list_table w-100"><tr>'; 165 $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>'; 166 $content .= '<th class="list_label">' . I18N::translate('Subject') . '</th>'; 167 $content .= '<th class="list_label">' . I18N::translate('Date sent') . '</th>'; 168 $content .= '<th class="list_label">' . I18N::translate('Email address') . '</th>'; 169 $content .= '</tr>'; 170 foreach ($messages as $message) { 171 $content .= '<tr>'; 172 $content .= '<td class="list_value_wrap center"><input type="checkbox" name="message_id[]" value="' . $message->message_id . '" id="cb_message' . $message->message_id . '"></td>'; 173 $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>'; 174 $content .= '<td class="list_value_wrap">' . view('components/datetime', ['timestamp' => $message->created]) . '</td>'; 175 $content .= '<td class="list_value_wrap">'; 176 177 $user = $this->user_service->findByIdentifier($message->sender); 178 179 if ($user instanceof User) { 180 $content .= '<span dir="auto">' . e($user->realName()) . '</span> - <span dir="auto">' . $user->email() . '</span>'; 181 } else { 182 $content .= '<a href="mailto:' . e($message->sender) . '">' . e($message->sender) . '</a>'; 183 } 184 185 $content .= '</td>'; 186 $content .= '</tr>'; 187 $content .= '<tr><td class="list_value_wrap" colspan="4"><div id="message' . $message->message_id . '" style="display:none;">'; 188 $content .= '<div dir="auto" style="white-space: pre-wrap;">' . Filter::expandUrls($message->body, $tree) . '</div><br>'; 189 190 /* I18N: When replying to an email, the subject becomes “RE: <subject>” */ 191 if (strpos($message->subject, I18N::translate('RE: ')) !== 0) { 192 $message->subject = I18N::translate('RE: ') . $message->subject; 193 } 194 195 // If this user still exists, show a reply link. 196 if ($user) { 197 $reply_url = route('message', [ 198 'to' => $user->userName(), 199 'subject' => $message->subject, 200 'ged' => $tree->name(), 201 ]); 202 203 $content .= '<a class="btn btn-primary" href="' . e($reply_url) . '" title="' . I18N::translate('Reply') . '">' . I18N::translate('Reply') . '</a> '; 204 } 205 $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>'; 206 } 207 $content .= '</table>'; 208 $content .= '<p><button type="submit">' . I18N::translate('Delete selected messages') . '</button></p>'; 209 } 210 $content .= '</form>'; 211 212 if ($ctype !== '') { 213 $count = $messages->count(); 214 215 return view('modules/block-template', [ 216 'block' => str_replace('_', '-', $this->name()), 217 'id' => $block_id, 218 'config_url' => '', 219 'title' => I18N::plural('%s message', '%s messages', $count, I18N::number($count)), 220 'content' => $content, 221 ]); 222 } 223 224 return $content; 225 } 226 227 /** {@inheritdoc} */ 228 public function loadAjax(): bool 229 { 230 return false; 231 } 232 233 /** {@inheritdoc} */ 234 public function isUserBlock(): bool 235 { 236 return true; 237 } 238 239 /** {@inheritdoc} */ 240 public function isTreeBlock(): bool 241 { 242 return false; 243 } 244 245 /** 246 * Update the configuration for a block. 247 * 248 * @param Request $request 249 * @param int $block_id 250 * 251 * @return void 252 */ 253 public function saveBlockConfiguration(Request $request, int $block_id): void 254 { 255 } 256 257 /** 258 * An HTML form to edit block settings 259 * 260 * @param Tree $tree 261 * @param int $block_id 262 * 263 * @return void 264 */ 265 public function editBlockConfiguration(Tree $tree, int $block_id): void 266 { 267 } 268} 269