xref: /webtrees/app/Module/UserMessagesModule.php (revision 13abd6f3a37322f885d85df150e105d27ad81f8d)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
4369c0ce6SGreg Roach * Copyright (C) 2016 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDate;
223d7a8a4cSGreg 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
4176692c8bSGreg Roach	/**
422da2404eSGreg Roach	 * This is a general purpose hook, allowing modules to respond to routes
432da2404eSGreg Roach	 * of the form module.php?mod=FOO&mod_action=BAR
442da2404eSGreg Roach	 *
452da2404eSGreg Roach	 * @param string $mod_action
462da2404eSGreg Roach	 */
472da2404eSGreg Roach	public function modAction($mod_action) {
482da2404eSGreg Roach		switch ($mod_action) {
492da2404eSGreg Roach		case 'delete':
502da2404eSGreg Roach			$stmt = Database::prepare("DELETE FROM `##message` WHERE user_id = :user_id AND message_id = :message_id");
512da2404eSGreg Roach
522da2404eSGreg Roach			foreach (Filter::postArray('message_id') as $id) {
53*13abd6f3SGreg Roach				$stmt->execute([
542da2404eSGreg Roach					'message_id' => $id,
552da2404eSGreg Roach					'user_id'    => Auth::id(),
56*13abd6f3SGreg Roach				]);
572da2404eSGreg Roach			}
582da2404eSGreg Roach		}
592da2404eSGreg Roach
602da2404eSGreg Roach		$ged   = Filter::post('ged');
612da2404eSGreg Roach		$ctype = Filter::post('ctype', 'user|gedcom', 'user');
622da2404eSGreg Roach
632da2404eSGreg Roach		header('Location: ' . WT_BASE_URL . 'index.php?ged=' . Filter::escapeUrl($ged) . '&ctype=' . $ctype);
642da2404eSGreg Roach	}
652da2404eSGreg Roach
662da2404eSGreg Roach	/**
6776692c8bSGreg Roach	 * Generate the HTML content of this block.
6876692c8bSGreg Roach	 *
6976692c8bSGreg Roach	 * @param int      $block_id
7076692c8bSGreg Roach	 * @param bool     $template
71727f238cSGreg Roach	 * @param string[] $cfg
7276692c8bSGreg Roach	 *
7376692c8bSGreg Roach	 * @return string
7476692c8bSGreg Roach	 */
75*13abd6f3SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []) {
762da2404eSGreg Roach		global $ctype, $WT_TREE;
772da2404eSGreg Roach
78e2a378d3SGreg Roach		$block = $this->getBlockSetting($block_id, 'block', '1');
79*13abd6f3SGreg Roach		foreach (['block'] as $name) {
808c2e8227SGreg Roach			if (array_key_exists($name, $cfg)) {
818c2e8227SGreg Roach				$$name = $cfg[$name];
828c2e8227SGreg Roach			}
838c2e8227SGreg Roach		}
848c2e8227SGreg 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")
85*13abd6f3SGreg Roach			->execute([Auth::id()])
868c2e8227SGreg Roach			->fetchAll();
878c2e8227SGreg Roach
888c2e8227SGreg Roach		$count = count($messages);
898c2e8227SGreg Roach		$id    = $this->getName() . $block_id;
908c2e8227SGreg Roach		$class = $this->getName() . '_block';
918c2e8227SGreg Roach		$title = I18N::plural('%s message', '%s messages', $count, I18N::number($count));
928c2e8227SGreg Roach		$users = array_filter(User::all(), function (User $user) {
938c2e8227SGreg Roach			return $user->getUserId() !== Auth::id() && $user->getPreference('verified_by_admin') && $user->getPreference('contactmethod') !== 'none';
948c2e8227SGreg Roach		});
958c2e8227SGreg Roach
962da2404eSGreg Roach		$content = '<form id="messageform" name="messageform" method="post" action="module.php?mod=user_messages&mod_action=delete" onsubmit="return confirm(\'' . I18N::translate('Are you sure you want to delete this message? It cannot be retrieved later.') . '\');">';
972da2404eSGreg Roach		$content .= '<input type="hidden" name="ged" value="' . $ctype . '">';
982da2404eSGreg Roach		$content .= '<input type="hidden" name="ctype" value="' . $WT_TREE->getNameHtml() . '">';
998c2e8227SGreg Roach		if ($users) {
1008c2e8227SGreg Roach			$content .= '<label for="touser">' . I18N::translate('Send a message') . '</label>';
1018c2e8227SGreg Roach			$content .= '<select id="touser" name="touser">';
1028c2e8227SGreg Roach			$content .= '<option value="">' . I18N::translate('&lt;select&gt;') . '</option>';
1038c2e8227SGreg Roach			foreach ($users as $user) {
1048c2e8227SGreg Roach				$content .= sprintf('<option value="%1$s">%2$s - %1$s</option>', Filter::escapeHtml($user->getUserName()), Filter::escapeHtml($user->getRealName()));
1058c2e8227SGreg Roach			}
1068c2e8227SGreg Roach			$content .= '</select>';
1072da2404eSGreg Roach			$content .= '<input type="button" value="' . I18N::translate('Send') . '" onclick="return message(document.messageform.touser.options[document.messageform.touser.selectedIndex].value, \'messaging2\', \'\');"><br><br>';
1088c2e8227SGreg Roach		}
1098c2e8227SGreg Roach		if ($messages) {
1108c2e8227SGreg Roach			$content .= '<table class="list_table"><tr>';
1118c2e8227SGreg 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>';
112e284b8e1SGreg Roach			$content .= '<th class="list_label">' . I18N::translate('Subject') . '</th>';
113e284b8e1SGreg Roach			$content .= '<th class="list_label">' . I18N::translate('Date sent') . '</th>';
114e284b8e1SGreg Roach			$content .= '<th class="list_label">' . I18N::translate('Email address') . '</th>';
1158c2e8227SGreg Roach			$content .= '</tr>';
1168c2e8227SGreg Roach			foreach ($messages as $message) {
1178c2e8227SGreg Roach				$content .= '<tr>';
1182da2404eSGreg Roach				$content .= '<td class="list_value_wrap"><input type="checkbox" name="message_id[]" value="' . $message->message_id . '" id="cb_message' . $message->message_id . '"></td>';
1198c2e8227SGreg 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>';
1203d7a8a4cSGreg Roach				$content .= '<td class="list_value_wrap">' . FunctionsDate::formatTimestamp($message->created + WT_TIMESTAMP_OFFSET) . '</td>';
1218c2e8227SGreg Roach				$content .= '<td class="list_value_wrap">';
1228c2e8227SGreg Roach				$user = User::findByIdentifier($message->sender);
1238c2e8227SGreg Roach				if ($user) {
124524c0fabSGreg Roach					$content .= $user->getRealNameHtml();
1258c2e8227SGreg Roach					$content .= '  - <span dir="auto">' . $user->getEmail() . '</span>';
1268c2e8227SGreg Roach				} else {
1278c2e8227SGreg Roach					$content .= '<a href="mailto:' . Filter::escapeHtml($message->sender) . '">' . Filter::escapeHtml($message->sender) . '</a>';
1288c2e8227SGreg Roach				}
1298c2e8227SGreg Roach				$content .= '</td>';
1308c2e8227SGreg Roach				$content .= '</tr>';
1318c2e8227SGreg Roach				$content .= '<tr><td class="list_value_wrap" colspan="4"><div id="message' . $message->message_id . '" style="display:none;">';
1328c2e8227SGreg Roach				$content .= '<div dir="auto" style="white-space: pre-wrap;">' . Filter::expandUrls($message->body) . '</div><br>';
1338c2e8227SGreg Roach				if (strpos($message->subject, /* I18N: When replying to an email, the subject becomes “RE: <subject>” */ I18N::translate('RE: ')) !== 0) {
1348c2e8227SGreg Roach					$message->subject = I18N::translate('RE: ') . $message->subject;
1358c2e8227SGreg Roach				}
1368c2e8227SGreg Roach				if ($user) {
1372da2404eSGreg Roach					$content .= '<button type="button" onclick="reply(\'' . Filter::escapeJs($message->sender) . '\', \'' . Filter::escapeJs($message->subject) . '\'); return false;">' . I18N::translate('Reply') . '</button> ';
1388c2e8227SGreg Roach				}
1392da2404eSGreg Roach				$content .= '<button type="button" onclick="if (confirm(\'' . I18N::translate('Are you sure you want to delete this message? It cannot be retrieved later.') . '\')) {jQuery(\'#messageform :checkbox\').prop(\'checked\', false); jQuery(\'#cb_message' . $message->message_id . '\').prop(\'checked\', true); document.messageform.submit();}">' . I18N::translate('Delete') . '</button></div></td></tr>';
1408c2e8227SGreg Roach			}
1418c2e8227SGreg Roach			$content .= '</table>';
1422da2404eSGreg Roach			$content .= '<p><button type="submit">' . I18N::translate('Delete selected messages') . '</button></p>';
1438c2e8227SGreg Roach		}
1448c2e8227SGreg Roach		$content .= '</form>';
1458c2e8227SGreg Roach
1468c2e8227SGreg Roach		if ($template) {
1478c2e8227SGreg Roach			if ($block) {
1488c2e8227SGreg Roach				$class .= ' small_inner_block';
1498c2e8227SGreg Roach			}
150cbc1590aSGreg Roach
1518c2e8227SGreg Roach			return Theme::theme()->formatBlock($id, $title, $class, $content);
1528c2e8227SGreg Roach		} else {
1538c2e8227SGreg Roach			return $content;
1548c2e8227SGreg Roach		}
1558c2e8227SGreg Roach	}
1568c2e8227SGreg Roach
1578c2e8227SGreg Roach	/** {@inheritdoc} */
1588c2e8227SGreg Roach	public function loadAjax() {
1598c2e8227SGreg Roach		return false;
1608c2e8227SGreg Roach	}
1618c2e8227SGreg Roach
1628c2e8227SGreg Roach	/** {@inheritdoc} */
1638c2e8227SGreg Roach	public function isUserBlock() {
1648c2e8227SGreg Roach		return true;
1658c2e8227SGreg Roach	}
1668c2e8227SGreg Roach
1678c2e8227SGreg Roach	/** {@inheritdoc} */
1688c2e8227SGreg Roach	public function isGedcomBlock() {
1698c2e8227SGreg Roach		return false;
1708c2e8227SGreg Roach	}
1718c2e8227SGreg Roach
17276692c8bSGreg Roach	/**
17376692c8bSGreg Roach	 * An HTML form to edit block settings
17476692c8bSGreg Roach	 *
17576692c8bSGreg Roach	 * @param int $block_id
17676692c8bSGreg Roach	 */
1778c2e8227SGreg Roach	public function configureBlock($block_id) {
1788c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
179e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'block', Filter::postBool('block'));
1808c2e8227SGreg Roach		}
1818c2e8227SGreg Roach
182e2a378d3SGreg Roach		$block = $this->getBlockSetting($block_id, 'block', '1');
1838c2e8227SGreg Roach		echo '<tr><td class="descriptionbox wrap width33">';
1848c2e8227SGreg Roach		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
1858c2e8227SGreg Roach		echo '</td><td class="optionbox">';
1863d7a8a4cSGreg Roach		echo FunctionsEdit::editFieldYesNo('block', $block);
1878c2e8227SGreg Roach		echo '</td></tr>';
1888c2e8227SGreg Roach	}
1898c2e8227SGreg Roach}
190