1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 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\Database; 22use Fisharebest\Webtrees\Filter; 23use Fisharebest\Webtrees\Functions\FunctionsDate; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Tree; 26use Fisharebest\Webtrees\User; 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 $stmt = Database::prepare("DELETE FROM `##message` WHERE user_id = :user_id AND message_id = :message_id"); 63 64 foreach ($message_ids as $message_id) { 65 $stmt->execute([ 66 'message_id' => $message_id, 67 'user_id' => Auth::id(), 68 ]); 69 } 70 71 if ($request->get('ctype') === 'user') { 72 $url = route('user-page', ['ged' => $tree->name()]); 73 } else { 74 $url = route('tree-page', ['ged' => $tree->name()]); 75 } 76 77 return new RedirectResponse($url); 78 } 79 80 /** 81 * Generate the HTML content of this block. 82 * 83 * @param Tree $tree 84 * @param int $block_id 85 * @param string $ctype 86 * @param string[] $cfg 87 * 88 * @return string 89 */ 90 public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 91 { 92 $messages = Database::prepare("SELECT message_id, sender, subject, body, UNIX_TIMESTAMP(created) AS created FROM `##message` WHERE user_id=? ORDER BY message_id DESC") 93 ->execute([Auth::id()]) 94 ->fetchAll(); 95 96 $count = count($messages); 97 $users = array_filter(User::all(), function (User $user) use ($tree): bool { 98 $public_tree = $tree->getPreference('REQUIRE_AUTHENTICATION') !== '1'; 99 $can_see_tree = $public_tree || Auth::accessLevel($tree, $user) <= Auth::PRIV_USER; 100 101 return 102 $user->getUserId() !== Auth::id() && 103 $user->getPreference('verified_by_admin') && 104 $can_see_tree && 105 $user->getPreference('contactmethod') !== 'none'; 106 }); 107 108 $content = ''; 109 if (!empty($users)) { 110 $url = route('user-page', ['ged' => $tree->name()]); 111 $content .= '<form onsubmit="return $("#to").val() !== """>'; 112 $content .= '<input type="hidden" name="route" value="message">'; 113 $content .= '<input type="hidden" name="ged" value="' . e($tree->name()) . '">'; 114 $content .= '<input type="hidden" name="url" value="' . e($url) . '">'; 115 $content .= '<label for="to">' . I18N::translate('Send a message') . '</label>'; 116 $content .= '<select id="to" name="to">'; 117 $content .= '<option value="">' . I18N::translate('<select>') . '</option>'; 118 foreach ($users as $user) { 119 $content .= sprintf('<option value="%1$s">%2$s - %1$s</option>', e($user->getUserName()), e($user->getRealName())); 120 } 121 $content .= '</select>'; 122 $content .= '<button type="submit">' . I18N::translate('Send') . '</button><br><br>'; 123 $content .= '</form>'; 124 } 125 $content .= '<form id="messageform" name="messageform" method="post" action="' . e(route('module', [ 126 'action' => 'DeleteMessage', 127 'module' => $this->getName(), 128 'ctype' => $ctype, 129 'ged' => $tree->name(), 130 ])) . '" data-confirm="' . I18N::translate('Are you sure you want to delete this message? It cannot be retrieved later.') . '" onsubmit="return confirm(this.dataset.confirm);">'; 131 $content .= csrf_field(); 132 133 if (!empty($messages)) { 134 $content .= '<table class="list_table w-100"><tr>'; 135 $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>'; 136 $content .= '<th class="list_label">' . I18N::translate('Subject') . '</th>'; 137 $content .= '<th class="list_label">' . I18N::translate('Date sent') . '</th>'; 138 $content .= '<th class="list_label">' . I18N::translate('Email address') . '</th>'; 139 $content .= '</tr>'; 140 foreach ($messages as $message) { 141 $content .= '<tr>'; 142 $content .= '<td class="list_value_wrap center"><input type="checkbox" name="message_id[]" value="' . $message->message_id . '" id="cb_message' . $message->message_id . '"></td>'; 143 $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>'; 144 $content .= '<td class="list_value_wrap">' . FunctionsDate::formatTimestamp($message->created + WT_TIMESTAMP_OFFSET) . '</td>'; 145 $content .= '<td class="list_value_wrap">'; 146 $user = User::findByIdentifier($message->sender); 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 $content .= '</td>'; 153 $content .= '</tr>'; 154 $content .= '<tr><td class="list_value_wrap" colspan="4"><div id="message' . $message->message_id . '" style="display:none;">'; 155 $content .= '<div dir="auto" style="white-space: pre-wrap;">' . Filter::expandUrls($message->body, $tree) . '</div><br>'; 156 /* I18N: When replying to an email, the subject becomes “RE: <subject>” */ 157 if (strpos($message->subject, I18N::translate('RE: ')) !== 0) { 158 $message->subject = I18N::translate('RE: ') . $message->subject; 159 } 160 161 // If this user still exists, show a reply link. 162 if ($user) { 163 $reply_url = route('message', [ 164 'to' => $user->getUserName(), 165 'subject' => $message->subject, 166 'ged' => $tree->name(), 167 ]); 168 $content .= '<a class="btn btn-primary" href="' . e($reply_url) . '" title="' . I18N::translate('Reply') . '">' . I18N::translate('Reply') . '</a> '; 169 } 170 $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>'; 171 } 172 $content .= '</table>'; 173 $content .= '<p><button type="submit">' . I18N::translate('Delete selected messages') . '</button></p>'; 174 } 175 $content .= '</form>'; 176 177 if ($ctype) { 178 return view('modules/block-template', [ 179 'block' => str_replace('_', '-', $this->getName()), 180 'id' => $block_id, 181 'config_url' => '', 182 'title' => I18N::plural('%s message', '%s messages', $count, I18N::number($count)), 183 'content' => $content, 184 ]); 185 } 186 187 return $content; 188 } 189 190 /** {@inheritdoc} */ 191 public function loadAjax(): bool 192 { 193 return false; 194 } 195 196 /** {@inheritdoc} */ 197 public function isUserBlock(): bool 198 { 199 return true; 200 } 201 202 /** {@inheritdoc} */ 203 public function isGedcomBlock(): bool 204 { 205 return false; 206 } 207 208 /** 209 * Update the configuration for a block. 210 * 211 * @param Request $request 212 * @param int $block_id 213 * 214 * @return void 215 */ 216 public function saveBlockConfiguration(Request $request, int $block_id) 217 { 218 } 219 220 /** 221 * An HTML form to edit block settings 222 * 223 * @param Tree $tree 224 * @param int $block_id 225 * 226 * @return void 227 */ 228 public function editBlockConfiguration(Tree $tree, int $block_id) 229 { 230 } 231} 232