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 Carbon\Carbon; 21use Fisharebest\Webtrees\Auth; 22use Fisharebest\Webtrees\Contracts\UserInterface; 23use Fisharebest\Webtrees\Filter; 24use Fisharebest\Webtrees\Functions\FunctionsDate; 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 stdClass; 31use Symfony\Component\HttpFoundation\RedirectResponse; 32use Symfony\Component\HttpFoundation\Request; 33use Symfony\Component\HttpFoundation\Response; 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 labelled on tabs, menus, 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 Request $request 83 * @param Tree $tree 84 * 85 * @return Response 86 */ 87 public function postDeleteMessageAction(Request $request, Tree $tree): Response 88 { 89 $message_ids = (array) $request->get('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->get('ctype') === 'user') { 97 $url = route('user-page', ['ged' => $tree->name()]); 98 } else { 99 $url = route('tree-page', ['ged' => $tree->name()]); 100 } 101 102 return new RedirectResponse($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 $ctype 111 * @param string[] $cfg 112 * 113 * @return string 114 */ 115 public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 116 { 117 $messages = DB::table('message') 118 ->where('user_id', '=', Auth::id()) 119 ->orderByDesc('message_id') 120 ->get() 121 ->map(function (stdClass $row): stdClass { 122 $row->created = new Carbon($row->created); 123 124 return $row; 125 }); 126 127 $users = $this->user_service->all()->filter(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 'ctype' => $ctype, 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">' . I18N::localTime($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 ($ctype !== '') { 214 $count = $messages->count(); 215 216 return view('modules/block-template', [ 217 'block' => str_replace('_', '-', $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 /** {@inheritdoc} */ 229 public function loadAjax(): bool 230 { 231 return false; 232 } 233 234 /** {@inheritdoc} */ 235 public function isUserBlock(): bool 236 { 237 return true; 238 } 239 240 /** {@inheritdoc} */ 241 public function isTreeBlock(): bool 242 { 243 return false; 244 } 245 246 /** 247 * Update the configuration for a block. 248 * 249 * @param Request $request 250 * @param int $block_id 251 * 252 * @return void 253 */ 254 public function saveBlockConfiguration(Request $request, int $block_id) 255 { 256 } 257 258 /** 259 * An HTML form to edit block settings 260 * 261 * @param Tree $tree 262 * @param int $block_id 263 * 264 * @return void 265 */ 266 public function editBlockConfiguration(Tree $tree, int $block_id) 267 { 268 } 269} 270