xref: /webtrees/app/Module/UserMessagesModule.php (revision 3d7a8a4ca809135634f38216b734b15acff479f7)
1<?php
2namespace Fisharebest\Webtrees\Module;
3
4/**
5 * webtrees: online genealogy
6 * Copyright (C) 2015 webtrees development team
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Database;
20use Fisharebest\Webtrees\Filter;
21use Fisharebest\Webtrees\Functions\FunctionsDate;
22use Fisharebest\Webtrees\Functions\FunctionsEdit;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Theme;
25use Fisharebest\Webtrees\User;
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	/** {@inheritdoc} */
42	public function getBlock($block_id, $template = true, $cfg = null) {
43		// Block actions
44		$action      = Filter::post('action');
45		$message_ids = Filter::postArray('message_id');
46		if ($action === 'deletemessage') {
47			foreach ($message_ids as $message_id) {
48				Database::prepare("DELETE FROM `##message` WHERE message_id=?")->execute(array($message_id));
49			}
50		}
51		$block = $this->getBlockSetting($block_id, 'block', '1');
52		if ($cfg) {
53			foreach (array('block') as $name) {
54				if (array_key_exists($name, $cfg)) {
55					$$name = $cfg[$name];
56				}
57			}
58		}
59		$messages = Database::prepare("SELECT message_id, sender, subject, body, UNIX_TIMESTAMP(created) AS created FROM `##message` WHERE user_id=? ORDER BY message_id DESC")
60			->execute(array(Auth::id()))
61			->fetchAll();
62
63		$count = count($messages);
64		$id    = $this->getName() . $block_id;
65		$class = $this->getName() . '_block';
66		$title = I18N::plural('%s message', '%s messages', $count, I18N::number($count));
67		$users = array_filter(User::all(), function (User $user) {
68			return $user->getUserId() !== Auth::id() && $user->getPreference('verified_by_admin') && $user->getPreference('contactmethod') !== 'none';
69		});
70
71		$content = '<form name="messageform" method="post" onsubmit="return confirm(\'' . I18N::translate('Are you sure you want to delete this message?  It cannot be retrieved later.') . '\');">';
72		if ($users) {
73			$content .= '<label for="touser">' . I18N::translate('Send a message') . '</label>';
74			$content .= '<select id="touser" name="touser">';
75			$content .= '<option value="">' . I18N::translate('&lt;select&gt;') . '</option>';
76			foreach ($users as $user) {
77				$content .= sprintf('<option value="%1$s">%2$s - %1$s</option>', Filter::escapeHtml($user->getUserName()), Filter::escapeHtml($user->getRealName()));
78			}
79			$content .= '</select>';
80			$content .= '<input type="button" value="' . I18N::translate('Send') . '" onclick="message(document.messageform.touser.options[document.messageform.touser.selectedIndex].value, \'messaging2\', \'\'); return false;"><br><br>';
81		}
82		if ($messages) {
83			$content .= '<input type="hidden" name="action" value="deletemessage">';
84			$content .= '<table class="list_table"><tr>';
85			$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>';
86			$content .= '<th class="list_label">' . I18N::translate('Subject') . '</th>';
87			$content .= '<th class="list_label">' . I18N::translate('Date sent') . '</th>';
88			$content .= '<th class="list_label">' . I18N::translate('Email address') . '</th>';
89			$content .= '</tr>';
90			foreach ($messages as $message) {
91				$content .= '<tr>';
92				$content .= '<td class="list_value_wrap"><input type="checkbox" id="cb_message' . $message->message_id . '" name="message_id[]" value="' . $message->message_id . '"></td>';
93				$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>';
94				$content .= '<td class="list_value_wrap">' . FunctionsDate::formatTimestamp($message->created + WT_TIMESTAMP_OFFSET) . '</td>';
95				$content .= '<td class="list_value_wrap">';
96				$user = User::findByIdentifier($message->sender);
97				if ($user) {
98					$content .= $user->getRealNameHtml();
99					$content .= '  - <span dir="auto">' . $user->getEmail() . '</span>';
100				} else {
101					$content .= '<a href="mailto:' . Filter::escapeHtml($message->sender) . '">' . Filter::escapeHtml($message->sender) . '</a>';
102				}
103				$content .= '</td>';
104				$content .= '</tr>';
105				$content .= '<tr><td class="list_value_wrap" colspan="4"><div id="message' . $message->message_id . '" style="display:none;">';
106				$content .= '<div dir="auto" style="white-space: pre-wrap;">' . Filter::expandUrls($message->body) . '</div><br>';
107				if (strpos($message->subject, /* I18N: When replying to an email, the subject becomes “RE: <subject>” */ I18N::translate('RE: ')) !== 0) {
108					$message->subject = I18N::translate('RE: ') . $message->subject;
109				}
110				if ($user) {
111					$content .= '<a href="#" onclick="reply(\'' . Filter::escapeJs($message->sender) . '\', \'' . Filter::escapeJs($message->subject) . '\'); return false;">' . I18N::translate('Reply') . '</a> | ';
112				}
113				$content .= '<a href="index.php?action=deletemessage&amp;message_id%5B%5D=' . $message->message_id . '" onclick="return confirm(\'' . I18N::translate('Are you sure you want to delete this message?  It cannot be retrieved later.') . '\');">' . I18N::translate('Delete') . '</a></div></td></tr>';
114			}
115			$content .= '</table>';
116			$content .= '<input type="submit" value="' . I18N::translate('Delete selected messages') . '"><br>';
117		}
118		$content .= '</form>';
119
120		if ($template) {
121			if ($block) {
122				$class .= ' small_inner_block';
123			}
124
125			return Theme::theme()->formatBlock($id, $title, $class, $content);
126		} else {
127			return $content;
128		}
129	}
130
131	/** {@inheritdoc} */
132	public function loadAjax() {
133		return false;
134	}
135
136	/** {@inheritdoc} */
137	public function isUserBlock() {
138		return true;
139	}
140
141	/** {@inheritdoc} */
142	public function isGedcomBlock() {
143		return false;
144	}
145
146	/** {@inheritdoc} */
147	public function configureBlock($block_id) {
148		if (Filter::postBool('save') && Filter::checkCsrf()) {
149			$this->setBlockSetting($block_id, 'block', Filter::postBool('block'));
150		}
151
152		$block = $this->getBlockSetting($block_id, 'block', '1');
153		echo '<tr><td class="descriptionbox wrap width33">';
154		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
155		echo '</td><td class="optionbox">';
156		echo FunctionsEdit::editFieldYesNo('block', $block);
157		echo '</td></tr>';
158	}
159}
160