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