xref: /webtrees/app/Module/UserMessagesModule.php (revision 225e381f36d59b49c8cdac0060465fa5af2fc308)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\User;
248c2e8227SGreg Roach
258c2e8227SGreg Roach/**
268c2e8227SGreg Roach * Class UserMessagesModule
278c2e8227SGreg Roach */
28e2a378d3SGreg Roachclass UserMessagesModule extends AbstractModule implements ModuleBlockInterface {
298c2e8227SGreg Roach	/** {@inheritdoc} */
308c2e8227SGreg Roach	public function getTitle() {
318c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('Messages');
328c2e8227SGreg Roach	}
338c2e8227SGreg Roach
348c2e8227SGreg Roach	/** {@inheritdoc} */
358c2e8227SGreg Roach	public function getDescription() {
368c2e8227SGreg Roach		return /* I18N: Description of the “Messages” module */ I18N::translate('Communicate directly with other users, using private messages.');
378c2e8227SGreg Roach	}
388c2e8227SGreg Roach
3976692c8bSGreg Roach	/**
402da2404eSGreg Roach	 * This is a general purpose hook, allowing modules to respond to routes
412da2404eSGreg Roach	 * of the form module.php?mod=FOO&mod_action=BAR
422da2404eSGreg Roach	 *
432da2404eSGreg Roach	 * @param string $mod_action
442da2404eSGreg Roach	 */
452da2404eSGreg Roach	public function modAction($mod_action) {
462da2404eSGreg Roach		switch ($mod_action) {
472da2404eSGreg Roach			case 'delete':
482da2404eSGreg Roach				$stmt = Database::prepare("DELETE FROM `##message` WHERE user_id = :user_id AND message_id = :message_id");
492da2404eSGreg Roach
502da2404eSGreg Roach				foreach (Filter::postArray('message_id') as $id) {
5113abd6f3SGreg Roach					$stmt->execute([
522da2404eSGreg Roach						'message_id' => $id,
532da2404eSGreg Roach						'user_id'    => Auth::id(),
5413abd6f3SGreg Roach					]);
552da2404eSGreg Roach				}
562da2404eSGreg Roach		}
572da2404eSGreg Roach
582da2404eSGreg Roach		$ged   = Filter::post('ged');
592da2404eSGreg Roach		$ctype = Filter::post('ctype', 'user|gedcom', 'user');
602da2404eSGreg Roach
6185ccaa18SGreg Roach		header('Location: index.php?ged=' . rawurlencode($ged) . '&ctype=' . $ctype);
622da2404eSGreg Roach	}
632da2404eSGreg Roach
642da2404eSGreg Roach	/**
6576692c8bSGreg Roach	 * Generate the HTML content of this block.
6676692c8bSGreg Roach	 *
6776692c8bSGreg Roach	 * @param int      $block_id
6876692c8bSGreg Roach	 * @param bool     $template
69727f238cSGreg Roach	 * @param string[] $cfg
7076692c8bSGreg Roach	 *
7176692c8bSGreg Roach	 * @return string
7276692c8bSGreg Roach	 */
73a9430be8SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []): string {
742da2404eSGreg Roach		global $ctype, $WT_TREE;
752da2404eSGreg Roach
768c2e8227SGreg 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")
7713abd6f3SGreg Roach			->execute([Auth::id()])
788c2e8227SGreg Roach			->fetchAll();
798c2e8227SGreg Roach
808c2e8227SGreg Roach		$count = count($messages);
818c2e8227SGreg Roach		$users = array_filter(User::all(), function (User $user) {
828c2e8227SGreg Roach			return $user->getUserId() !== Auth::id() && $user->getPreference('verified_by_admin') && $user->getPreference('contactmethod') !== 'none';
838c2e8227SGreg Roach		});
848c2e8227SGreg Roach
857bab8982SGreg Roach		$content = '';
86e92490bcSGreg Roach		if (!empty($users)) {
877bab8982SGreg Roach			$url = route('user-page', ['ged' => $WT_TREE->getName()]);
887bab8982SGreg Roach			$content .= '<form action="message.php" onsubmit="return $(&quot;#to&quot;).val() !== &quot;&quot;">';
89d53324c9SGreg Roach			$content .= '<input type="hidden" name="ged" value="' . e($WT_TREE->getName()) . '">';
90d53324c9SGreg Roach			$content .= '<input type="hidden" name="url" value="' . e($url) . '">';
917bab8982SGreg Roach			$content .= '<label for="to">' . I18N::translate('Send a message') . '</label>';
927bab8982SGreg Roach			$content .= '<select id="to" name="to">';
938c2e8227SGreg Roach			$content .= '<option value="">' . I18N::translate('&lt;select&gt;') . '</option>';
948c2e8227SGreg Roach			foreach ($users as $user) {
95d53324c9SGreg Roach				$content .= sprintf('<option value="%1$s">%2$s - %1$s</option>', e($user->getUserName()), e($user->getRealName()));
968c2e8227SGreg Roach			}
978c2e8227SGreg Roach			$content .= '</select>';
987bab8982SGreg Roach			$content .= '<button type="submit">' . I18N::translate('Send') . '</button><br><br>';
997bab8982SGreg Roach			$content .= '</form>';
1008c2e8227SGreg Roach		}
1017bab8982SGreg 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.') . '\');">';
1027bab8982SGreg Roach		$content .= '<input type="hidden" name="ged" value="' . $ctype . '">';
1037bab8982SGreg Roach		$content .= '<input type="hidden" name="ctype" value="' . $WT_TREE->getNameHtml() . '">';
104e92490bcSGreg Roach		if (!empty($messages)) {
1058c2e8227SGreg Roach			$content .= '<table class="list_table"><tr>';
106bc6628fbSRico Sonntag			$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>';
107e284b8e1SGreg Roach			$content .= '<th class="list_label">' . I18N::translate('Subject') . '</th>';
108e284b8e1SGreg Roach			$content .= '<th class="list_label">' . I18N::translate('Date sent') . '</th>';
109e284b8e1SGreg Roach			$content .= '<th class="list_label">' . I18N::translate('Email address') . '</th>';
1108c2e8227SGreg Roach			$content .= '</tr>';
1118c2e8227SGreg Roach			foreach ($messages as $message) {
1128c2e8227SGreg Roach				$content .= '<tr>';
113a33af35aSmakitso				$content .= '<td class="list_value_wrap center"><input type="checkbox" name="message_id[]" value="' . $message->message_id . '" id="cb_message' . $message->message_id . '"></td>';
114d53324c9SGreg 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">' . e($message->subject) . '</b></a></td>';
1153d7a8a4cSGreg Roach				$content .= '<td class="list_value_wrap">' . FunctionsDate::formatTimestamp($message->created + WT_TIMESTAMP_OFFSET) . '</td>';
1168c2e8227SGreg Roach				$content .= '<td class="list_value_wrap">';
1178c2e8227SGreg Roach				$user = User::findByIdentifier($message->sender);
1188c2e8227SGreg Roach				if ($user) {
119524c0fabSGreg Roach					$content .= $user->getRealNameHtml();
1208c2e8227SGreg Roach					$content .= '  - <span dir="auto">' . $user->getEmail() . '</span>';
1218c2e8227SGreg Roach				} else {
122d53324c9SGreg Roach					$content .= '<a href="mailto:' . e($message->sender) . '">' . e($message->sender) . '</a>';
1238c2e8227SGreg Roach				}
1248c2e8227SGreg Roach				$content .= '</td>';
1258c2e8227SGreg Roach				$content .= '</tr>';
1268c2e8227SGreg Roach				$content .= '<tr><td class="list_value_wrap" colspan="4"><div id="message' . $message->message_id . '" style="display:none;">';
127ff05914fSGreg Roach				$content .= '<div dir="auto" style="white-space: pre-wrap;">' . Filter::expandUrls($message->body, $WT_TREE) . '</div><br>';
1288c2e8227SGreg Roach				if (strpos($message->subject, /* I18N: When replying to an email, the subject becomes “RE: <subject>” */ I18N::translate('RE: ')) !== 0) {
1298c2e8227SGreg Roach					$message->subject = I18N::translate('RE: ') . $message->subject;
1308c2e8227SGreg Roach				}
1318c2e8227SGreg Roach				if ($user) {
13285ccaa18SGreg Roach					$content .= '<a class="btn btn-secondary" href="message.php?to=' . rawurlencode($message->sender) . '&amp;subject=' . rawurlencode($message->subject) . '&amp;ged=' . $WT_TREE->getNameUrl() . '" title="' . I18N::translate('Reply') . '">' . I18N::translate('Reply') . '</a> ';
1338c2e8227SGreg Roach				}
13415d603e7SGreg Roach				$content .= '<button type="button" onclick="if (confirm(\'' . I18N::translate('Are you sure you want to delete this message? It cannot be retrieved later.') . '\')) {$(\'#messageform :checkbox\').prop(\'checked\', false); $(\'#cb_message' . $message->message_id . '\').prop(\'checked\', true); document.messageform.submit();}">' . I18N::translate('Delete') . '</button></div></td></tr>';
1358c2e8227SGreg Roach			}
1368c2e8227SGreg Roach			$content .= '</table>';
1372da2404eSGreg Roach			$content .= '<p><button type="submit">' . I18N::translate('Delete selected messages') . '</button></p>';
1388c2e8227SGreg Roach		}
1398c2e8227SGreg Roach		$content .= '</form>';
1408c2e8227SGreg Roach
1418c2e8227SGreg Roach		if ($template) {
142*225e381fSGreg Roach			return view('blocks/template', [
1439c6524dcSGreg Roach				'block'      => str_replace('_', '-', $this->getName()),
1449c6524dcSGreg Roach				'id'         => $block_id,
1459c6524dcSGreg Roach				'config_url' => '',
146f7f4b984SGreg Roach				'title'      => I18N::plural('%s message', '%s messages', $count, I18N::number($count)),
1479c6524dcSGreg Roach				'content'    => $content,
1489c6524dcSGreg Roach			]);
1498c2e8227SGreg Roach		} else {
1508c2e8227SGreg Roach			return $content;
1518c2e8227SGreg Roach		}
1528c2e8227SGreg Roach	}
1538c2e8227SGreg Roach
1548c2e8227SGreg Roach	/** {@inheritdoc} */
155a9430be8SGreg Roach	public function loadAjax(): bool {
1568c2e8227SGreg Roach		return false;
1578c2e8227SGreg Roach	}
1588c2e8227SGreg Roach
1598c2e8227SGreg Roach	/** {@inheritdoc} */
160a9430be8SGreg Roach	public function isUserBlock(): bool {
1618c2e8227SGreg Roach		return true;
1628c2e8227SGreg Roach	}
1638c2e8227SGreg Roach
1648c2e8227SGreg Roach	/** {@inheritdoc} */
165a9430be8SGreg Roach	public function isGedcomBlock(): bool {
1668c2e8227SGreg Roach		return false;
1678c2e8227SGreg Roach	}
1688c2e8227SGreg Roach
16976692c8bSGreg Roach	/**
17076692c8bSGreg Roach	 * An HTML form to edit block settings
17176692c8bSGreg Roach	 *
17276692c8bSGreg Roach	 * @param int $block_id
173a9430be8SGreg Roach	 *
174a9430be8SGreg Roach	 * @return void
17576692c8bSGreg Roach	 */
176be9a728cSGreg Roach	public function configureBlock($block_id) {
1778c2e8227SGreg Roach	}
1788c2e8227SGreg Roach}
179