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