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