xref: /webtrees/app/Module/UserMessagesModule.php (revision 15d603e7c7c15d20f055d3d9c38d6b133453c5be)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 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 */
16namespace Fisharebest\Webtrees\Module;
17
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Database;
20use Fisharebest\Webtrees\Filter;
21use Fisharebest\Webtrees\Functions\FunctionsDate;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Theme;
24use Fisharebest\Webtrees\User;
25
26/**
27 * Class UserMessagesModule
28 */
29class UserMessagesModule extends AbstractModule implements ModuleBlockInterface {
30	/** {@inheritdoc} */
31	public function getTitle() {
32		return /* I18N: Name of a module */ I18N::translate('Messages');
33	}
34
35	/** {@inheritdoc} */
36	public function getDescription() {
37		return /* I18N: Description of the “Messages” module */ I18N::translate('Communicate directly with other users, using private messages.');
38	}
39
40	/**
41	 * This is a general purpose hook, allowing modules to respond to routes
42	 * of the form module.php?mod=FOO&mod_action=BAR
43	 *
44	 * @param string $mod_action
45	 */
46	public function modAction($mod_action) {
47		switch ($mod_action) {
48		case 'delete':
49			$stmt = Database::prepare("DELETE FROM `##message` WHERE user_id = :user_id AND message_id = :message_id");
50
51			foreach (Filter::postArray('message_id') as $id) {
52				$stmt->execute([
53					'message_id' => $id,
54					'user_id'    => Auth::id(),
55				]);
56			}
57		}
58
59		$ged   = Filter::post('ged');
60		$ctype = Filter::post('ctype', 'user|gedcom', 'user');
61
62		header('Location: ' . WT_BASE_URL . 'index.php?ged=' . Filter::escapeUrl($ged) . '&ctype=' . $ctype);
63	}
64
65	/**
66	 * Generate the HTML content of this block.
67	 *
68	 * @param int      $block_id
69	 * @param bool     $template
70	 * @param string[] $cfg
71	 *
72	 * @return string
73	 */
74	public function getBlock($block_id, $template = true, $cfg = []) {
75		global $ctype, $WT_TREE;
76
77		$messages = Database::prepare("SELECT message_id, sender, subject, body, UNIX_TIMESTAMP(created) AS created FROM `##message` WHERE user_id=? ORDER BY message_id DESC")
78			->execute([Auth::id()])
79			->fetchAll();
80
81		$count = count($messages);
82		$id    = $this->getName() . $block_id;
83		$class = $this->getName() . '_block';
84		$title = I18N::plural('%s message', '%s messages', $count, I18N::number($count));
85		$users = array_filter(User::all(), function (User $user) {
86			return $user->getUserId() !== Auth::id() && $user->getPreference('verified_by_admin') && $user->getPreference('contactmethod') !== 'none';
87		});
88
89		$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.') . '\');">';
90		$content .= '<input type="hidden" name="ged" value="' . $ctype . '">';
91		$content .= '<input type="hidden" name="ctype" value="' . $WT_TREE->getNameHtml() . '">';
92		if ($users) {
93			$content .= '<label for="touser">' . I18N::translate('Send a message') . '</label>';
94			$content .= '<select id="touser" name="touser">';
95			$content .= '<option value="">' . I18N::translate('&lt;select&gt;') . '</option>';
96			foreach ($users as $user) {
97				$content .= sprintf('<option value="%1$s">%2$s - %1$s</option>', Filter::escapeHtml($user->getUserName()), Filter::escapeHtml($user->getRealName()));
98			}
99			$content .= '</select>';
100			$content .= '<input type="button" value="' . I18N::translate('Send') . '" onclick="return message(document.messageform.touser.options[document.messageform.touser.selectedIndex].value, \'messaging2\', \'\');"><br><br>';
101		}
102		if ($messages) {
103			$content .= '<table class="list_table"><tr>';
104			$content .= '<th class="list_label">' . I18N::translate('Delete') . '<br><a href="#" onclick="$(\'#' . $this->getName() . $block_id . ' :checkbox\').prop(\'checked\', true); return false;">' . I18N::translate('All') . '</a></th>';
105			$content .= '<th class="list_label">' . I18N::translate('Subject') . '</th>';
106			$content .= '<th class="list_label">' . I18N::translate('Date sent') . '</th>';
107			$content .= '<th class="list_label">' . I18N::translate('Email address') . '</th>';
108			$content .= '</tr>';
109			foreach ($messages as $message) {
110				$content .= '<tr>';
111				$content .= '<td class="list_value_wrap"><input type="checkbox" name="message_id[]" value="' . $message->message_id . '" id="cb_message' . $message->message_id . '"></td>';
112				$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>';
113				$content .= '<td class="list_value_wrap">' . FunctionsDate::formatTimestamp($message->created + WT_TIMESTAMP_OFFSET) . '</td>';
114				$content .= '<td class="list_value_wrap">';
115				$user = User::findByIdentifier($message->sender);
116				if ($user) {
117					$content .= $user->getRealNameHtml();
118					$content .= '  - <span dir="auto">' . $user->getEmail() . '</span>';
119				} else {
120					$content .= '<a href="mailto:' . Filter::escapeHtml($message->sender) . '">' . Filter::escapeHtml($message->sender) . '</a>';
121				}
122				$content .= '</td>';
123				$content .= '</tr>';
124				$content .= '<tr><td class="list_value_wrap" colspan="4"><div id="message' . $message->message_id . '" style="display:none;">';
125				$content .= '<div dir="auto" style="white-space: pre-wrap;">' . Filter::expandUrls($message->body) . '</div><br>';
126				if (strpos($message->subject, /* I18N: When replying to an email, the subject becomes “RE: <subject>” */ I18N::translate('RE: ')) !== 0) {
127					$message->subject = I18N::translate('RE: ') . $message->subject;
128				}
129				if ($user) {
130					$content .= '<a class="btn btn-secondary" href="message.php?to=' . Filter::escapeUrl($message->sender) . '&amp;subject=' . Filter::escapeUrl($message->subject) . '&amp;ged=' . $WT_TREE->getNameUrl() .'" title="' . I18N::translate('Reply') .'">' . I18N::translate('Reply') . '</a> ';
131				}
132				$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>';
133			}
134			$content .= '</table>';
135			$content .= '<p><button type="submit">' . I18N::translate('Delete selected messages') . '</button></p>';
136		}
137		$content .= '</form>';
138
139		if ($template) {
140			return Theme::theme()->formatBlock($id, $title, $class, $content);
141		} else {
142			return $content;
143		}
144	}
145
146	/** {@inheritdoc} */
147	public function loadAjax() {
148		return false;
149	}
150
151	/** {@inheritdoc} */
152	public function isUserBlock() {
153		return true;
154	}
155
156	/** {@inheritdoc} */
157	public function isGedcomBlock() {
158		return false;
159	}
160
161	/**
162	 * An HTML form to edit block settings
163	 *
164	 * @param int $block_id
165	 */
166	public function configureBlock($block_id) {
167	}
168}
169