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