18c2e8227SGreg Roach<?php 20e62c4b8SGreg Roachnamespace Fisharebest\Webtrees\Module; 38c2e8227SGreg Roach 48c2e8227SGreg Roach/** 58c2e8227SGreg Roach * webtrees: online genealogy 68c2e8227SGreg Roach * Copyright (C) 2015 webtrees development team 78c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 88c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 98c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 108c2e8227SGreg Roach * (at your option) any later version. 118c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 128c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 138c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 148c2e8227SGreg Roach * GNU General Public License for more details. 158c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 168c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 178c2e8227SGreg Roach */ 180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database; 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter; 21*3d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDate; 22*3d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsEdit; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Theme; 250e62c4b8SGreg Roachuse Fisharebest\Webtrees\User; 268c2e8227SGreg Roach 278c2e8227SGreg Roach/** 288c2e8227SGreg Roach * Class UserMessagesModule 298c2e8227SGreg Roach */ 30e2a378d3SGreg Roachclass UserMessagesModule extends AbstractModule implements ModuleBlockInterface { 318c2e8227SGreg Roach /** {@inheritdoc} */ 328c2e8227SGreg Roach public function getTitle() { 338c2e8227SGreg Roach return /* I18N: Name of a module */ I18N::translate('Messages'); 348c2e8227SGreg Roach } 358c2e8227SGreg Roach 368c2e8227SGreg Roach /** {@inheritdoc} */ 378c2e8227SGreg Roach public function getDescription() { 388c2e8227SGreg Roach return /* I18N: Description of the “Messages” module */ I18N::translate('Communicate directly with other users, using private messages.'); 398c2e8227SGreg Roach } 408c2e8227SGreg Roach 418c2e8227SGreg Roach /** {@inheritdoc} */ 428c2e8227SGreg Roach public function getBlock($block_id, $template = true, $cfg = null) { 438c2e8227SGreg Roach // Block actions 448c2e8227SGreg Roach $action = Filter::post('action'); 458c2e8227SGreg Roach $message_ids = Filter::postArray('message_id'); 468c2e8227SGreg Roach if ($action === 'deletemessage') { 478c2e8227SGreg Roach foreach ($message_ids as $message_id) { 488c2e8227SGreg Roach Database::prepare("DELETE FROM `##message` WHERE message_id=?")->execute(array($message_id)); 498c2e8227SGreg Roach } 508c2e8227SGreg Roach } 51e2a378d3SGreg Roach $block = $this->getBlockSetting($block_id, 'block', '1'); 528c2e8227SGreg Roach if ($cfg) { 538c2e8227SGreg Roach foreach (array('block') as $name) { 548c2e8227SGreg Roach if (array_key_exists($name, $cfg)) { 558c2e8227SGreg Roach $$name = $cfg[$name]; 568c2e8227SGreg Roach } 578c2e8227SGreg Roach } 588c2e8227SGreg Roach } 598c2e8227SGreg Roach $messages = Database::prepare("SELECT message_id, sender, subject, body, UNIX_TIMESTAMP(created) AS created FROM `##message` WHERE user_id=? ORDER BY message_id DESC") 608c2e8227SGreg Roach ->execute(array(Auth::id())) 618c2e8227SGreg Roach ->fetchAll(); 628c2e8227SGreg Roach 638c2e8227SGreg Roach $count = count($messages); 648c2e8227SGreg Roach $id = $this->getName() . $block_id; 658c2e8227SGreg Roach $class = $this->getName() . '_block'; 668c2e8227SGreg Roach $title = I18N::plural('%s message', '%s messages', $count, I18N::number($count)); 678c2e8227SGreg Roach $users = array_filter(User::all(), function (User $user) { 688c2e8227SGreg Roach return $user->getUserId() !== Auth::id() && $user->getPreference('verified_by_admin') && $user->getPreference('contactmethod') !== 'none'; 698c2e8227SGreg Roach }); 708c2e8227SGreg Roach 718c2e8227SGreg Roach $content = '<form name="messageform" method="post" onsubmit="return confirm(\'' . I18N::translate('Are you sure you want to delete this message? It cannot be retrieved later.') . '\');">'; 728c2e8227SGreg Roach if ($users) { 738c2e8227SGreg Roach $content .= '<label for="touser">' . I18N::translate('Send a message') . '</label>'; 748c2e8227SGreg Roach $content .= '<select id="touser" name="touser">'; 758c2e8227SGreg Roach $content .= '<option value="">' . I18N::translate('<select>') . '</option>'; 768c2e8227SGreg Roach foreach ($users as $user) { 778c2e8227SGreg Roach $content .= sprintf('<option value="%1$s">%2$s - %1$s</option>', Filter::escapeHtml($user->getUserName()), Filter::escapeHtml($user->getRealName())); 788c2e8227SGreg Roach } 798c2e8227SGreg Roach $content .= '</select>'; 808c2e8227SGreg Roach $content .= '<input type="button" value="' . I18N::translate('Send') . '" onclick="message(document.messageform.touser.options[document.messageform.touser.selectedIndex].value, \'messaging2\', \'\'); return false;"><br><br>'; 818c2e8227SGreg Roach } 828c2e8227SGreg Roach if ($messages) { 838c2e8227SGreg Roach $content .= '<input type="hidden" name="action" value="deletemessage">'; 848c2e8227SGreg Roach $content .= '<table class="list_table"><tr>'; 858c2e8227SGreg Roach $content .= '<th class="list_label">' . I18N::translate('Delete') . '<br><a href="#" onclick="jQuery(\'#' . $this->getName() . $block_id . ' :checkbox\').prop(\'checked\', true); return false;">' . I18N::translate('All') . '</a></th>'; 86e284b8e1SGreg Roach $content .= '<th class="list_label">' . I18N::translate('Subject') . '</th>'; 87e284b8e1SGreg Roach $content .= '<th class="list_label">' . I18N::translate('Date sent') . '</th>'; 88e284b8e1SGreg Roach $content .= '<th class="list_label">' . I18N::translate('Email address') . '</th>'; 898c2e8227SGreg Roach $content .= '</tr>'; 908c2e8227SGreg Roach foreach ($messages as $message) { 918c2e8227SGreg Roach $content .= '<tr>'; 928c2e8227SGreg Roach $content .= '<td class="list_value_wrap"><input type="checkbox" id="cb_message' . $message->message_id . '" name="message_id[]" value="' . $message->message_id . '"></td>'; 938c2e8227SGreg Roach $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">' . Filter::escapeHtml($message->subject) . '</b></a></td>'; 94*3d7a8a4cSGreg Roach $content .= '<td class="list_value_wrap">' . FunctionsDate::formatTimestamp($message->created + WT_TIMESTAMP_OFFSET) . '</td>'; 958c2e8227SGreg Roach $content .= '<td class="list_value_wrap">'; 968c2e8227SGreg Roach $user = User::findByIdentifier($message->sender); 978c2e8227SGreg Roach if ($user) { 98524c0fabSGreg Roach $content .= $user->getRealNameHtml(); 998c2e8227SGreg Roach $content .= ' - <span dir="auto">' . $user->getEmail() . '</span>'; 1008c2e8227SGreg Roach } else { 1018c2e8227SGreg Roach $content .= '<a href="mailto:' . Filter::escapeHtml($message->sender) . '">' . Filter::escapeHtml($message->sender) . '</a>'; 1028c2e8227SGreg Roach } 1038c2e8227SGreg Roach $content .= '</td>'; 1048c2e8227SGreg Roach $content .= '</tr>'; 1058c2e8227SGreg Roach $content .= '<tr><td class="list_value_wrap" colspan="4"><div id="message' . $message->message_id . '" style="display:none;">'; 1068c2e8227SGreg Roach $content .= '<div dir="auto" style="white-space: pre-wrap;">' . Filter::expandUrls($message->body) . '</div><br>'; 1078c2e8227SGreg Roach if (strpos($message->subject, /* I18N: When replying to an email, the subject becomes “RE: <subject>” */ I18N::translate('RE: ')) !== 0) { 1088c2e8227SGreg Roach $message->subject = I18N::translate('RE: ') . $message->subject; 1098c2e8227SGreg Roach } 1108c2e8227SGreg Roach if ($user) { 1118c2e8227SGreg Roach $content .= '<a href="#" onclick="reply(\'' . Filter::escapeJs($message->sender) . '\', \'' . Filter::escapeJs($message->subject) . '\'); return false;">' . I18N::translate('Reply') . '</a> | '; 1128c2e8227SGreg Roach } 1138c2e8227SGreg Roach $content .= '<a href="index.php?action=deletemessage&message_id%5B%5D=' . $message->message_id . '" onclick="return confirm(\'' . I18N::translate('Are you sure you want to delete this message? It cannot be retrieved later.') . '\');">' . I18N::translate('Delete') . '</a></div></td></tr>'; 1148c2e8227SGreg Roach } 1158c2e8227SGreg Roach $content .= '</table>'; 1168c2e8227SGreg Roach $content .= '<input type="submit" value="' . I18N::translate('Delete selected messages') . '"><br>'; 1178c2e8227SGreg Roach } 1188c2e8227SGreg Roach $content .= '</form>'; 1198c2e8227SGreg Roach 1208c2e8227SGreg Roach if ($template) { 1218c2e8227SGreg Roach if ($block) { 1228c2e8227SGreg Roach $class .= ' small_inner_block'; 1238c2e8227SGreg Roach } 124cbc1590aSGreg Roach 1258c2e8227SGreg Roach return Theme::theme()->formatBlock($id, $title, $class, $content); 1268c2e8227SGreg Roach } else { 1278c2e8227SGreg Roach return $content; 1288c2e8227SGreg Roach } 1298c2e8227SGreg Roach } 1308c2e8227SGreg Roach 1318c2e8227SGreg Roach /** {@inheritdoc} */ 1328c2e8227SGreg Roach public function loadAjax() { 1338c2e8227SGreg Roach return false; 1348c2e8227SGreg Roach } 1358c2e8227SGreg Roach 1368c2e8227SGreg Roach /** {@inheritdoc} */ 1378c2e8227SGreg Roach public function isUserBlock() { 1388c2e8227SGreg Roach return true; 1398c2e8227SGreg Roach } 1408c2e8227SGreg Roach 1418c2e8227SGreg Roach /** {@inheritdoc} */ 1428c2e8227SGreg Roach public function isGedcomBlock() { 1438c2e8227SGreg Roach return false; 1448c2e8227SGreg Roach } 1458c2e8227SGreg Roach 1468c2e8227SGreg Roach /** {@inheritdoc} */ 1478c2e8227SGreg Roach public function configureBlock($block_id) { 1488c2e8227SGreg Roach if (Filter::postBool('save') && Filter::checkCsrf()) { 149e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'block', Filter::postBool('block')); 1508c2e8227SGreg Roach } 1518c2e8227SGreg Roach 152e2a378d3SGreg Roach $block = $this->getBlockSetting($block_id, 'block', '1'); 1538c2e8227SGreg Roach echo '<tr><td class="descriptionbox wrap width33">'; 1548c2e8227SGreg Roach echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow'); 1558c2e8227SGreg Roach echo '</td><td class="optionbox">'; 156*3d7a8a4cSGreg Roach echo FunctionsEdit::editFieldYesNo('block', $block); 1578c2e8227SGreg Roach echo '</td></tr>'; 1588c2e8227SGreg Roach } 1598c2e8227SGreg Roach} 160