xref: /webtrees/app/Module/UserMessagesModule.php (revision 4cb8d1f0eb760216ff1402f3415ef3e289b4d8b3)
1<?php
2namespace Fisharebest\Webtrees;
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 */
18
19/**
20 * Class UserMessagesModule
21 */
22class UserMessagesModule extends Module implements ModuleBlockInterface {
23	/** {@inheritdoc} */
24	public function getTitle() {
25		return /* I18N: Name of a module */ I18N::translate('Messages');
26	}
27
28	/** {@inheritdoc} */
29	public function getDescription() {
30		return /* I18N: Description of the “Messages” module */ I18N::translate('Communicate directly with other users, using private messages.');
31	}
32
33	/** {@inheritdoc} */
34	public function getBlock($block_id, $template = true, $cfg = null) {
35		// Block actions
36		$action      = Filter::post('action');
37		$message_ids = Filter::postArray('message_id');
38		if ($action === 'deletemessage') {
39			foreach ($message_ids as $message_id) {
40				Database::prepare("DELETE FROM `##message` WHERE message_id=?")->execute(array($message_id));
41			}
42		}
43		$block = get_block_setting($block_id, 'block', '1');
44		if ($cfg) {
45			foreach (array('block') as $name) {
46				if (array_key_exists($name, $cfg)) {
47					$$name = $cfg[$name];
48				}
49			}
50		}
51		$messages = Database::prepare("SELECT message_id, sender, subject, body, UNIX_TIMESTAMP(created) AS created FROM `##message` WHERE user_id=? ORDER BY message_id DESC")
52			->execute(array(Auth::id()))
53			->fetchAll();
54
55		$count = count($messages);
56		$id    = $this->getName() . $block_id;
57		$class = $this->getName() . '_block';
58		$title = I18N::plural('%s message', '%s messages', $count, I18N::number($count));
59		$users = array_filter(User::all(), function(User $user) {
60			return $user->getUserId() !== Auth::id() && $user->getPreference('verified_by_admin') && $user->getPreference('contactmethod') !== 'none';
61		});
62
63		$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.') . '\');">';
64		if ($users) {
65			$content .= '<label for="touser">' . I18N::translate('Send a message') . '</label>';
66			$content .= '<select id="touser" name="touser">';
67			$content .= '<option value="">' . I18N::translate('&lt;select&gt;') . '</option>';
68			foreach ($users as $user) {
69				$content .= sprintf('<option value="%1$s">%2$s - %1$s</option>', Filter::escapeHtml($user->getUserName()), Filter::escapeHtml($user->getRealName()));
70			}
71			$content .= '</select>';
72			$content .= '<input type="button" value="' . I18N::translate('Send') . '" onclick="message(document.messageform.touser.options[document.messageform.touser.selectedIndex].value, \'messaging2\', \'\'); return false;"><br><br>';
73		}
74		if ($messages) {
75			$content .= '<input type="hidden" name="action" value="deletemessage">';
76			$content .= '<table class="list_table"><tr>';
77			$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>';
78			$content .= '<th class="list_label">' . I18N::translate('Subject:') . '</th>';
79			$content .= '<th class="list_label">' . I18N::translate('Date sent:') . '</th>';
80			$content .= '<th class="list_label">' . I18N::translate('Email address:') . '</th>';
81			$content .= '</tr>';
82			foreach ($messages as $message) {
83				$content .= '<tr>';
84				$content .= '<td class="list_value_wrap"><input type="checkbox" id="cb_message' . $message->message_id . '" name="message_id[]" value="' . $message->message_id . '"></td>';
85				$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>';
86				$content .= '<td class="list_value_wrap">' . format_timestamp($message->created) . '</td>';
87				$content .= '<td class="list_value_wrap">';
88				$user = User::findByIdentifier($message->sender);
89				if ($user) {
90					$content .= '<span dir="auto">' . $user->getRealName() . '</span>';
91					$content .= '  - <span dir="auto">' . $user->getEmail() . '</span>';
92				} else {
93					$content .= '<a href="mailto:' . Filter::escapeHtml($message->sender) . '">' . Filter::escapeHtml($message->sender) . '</a>';
94				}
95				$content .= '</td>';
96				$content .= '</tr>';
97				$content .= '<tr><td class="list_value_wrap" colspan="4"><div id="message' . $message->message_id . '" style="display:none;">';
98				$content .= '<div dir="auto" style="white-space: pre-wrap;">' . Filter::expandUrls($message->body) . '</div><br>';
99				if (strpos($message->subject, /* I18N: When replying to an email, the subject becomes “RE: <subject>” */ I18N::translate('RE: ')) !== 0) {
100					$message->subject = I18N::translate('RE: ') . $message->subject;
101				}
102				if ($user) {
103					$content .= '<a href="#" onclick="reply(\'' . Filter::escapeJs($message->sender) . '\', \'' . Filter::escapeJs($message->subject) . '\'); return false;">' . I18N::translate('Reply') . '</a> | ';
104				}
105				$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>';
106			}
107			$content .= '</table>';
108			$content .= '<input type="submit" value="' . I18N::translate('Delete selected messages') . '"><br>';
109		}
110		$content .= '</form>';
111
112		if ($template) {
113			if ($block) {
114				$class .= ' small_inner_block';
115			}
116			return Theme::theme()->formatBlock($id, $title, $class, $content);
117		} else {
118			return $content;
119		}
120	}
121
122	/** {@inheritdoc} */
123	public function loadAjax() {
124		return false;
125	}
126
127	/** {@inheritdoc} */
128	public function isUserBlock() {
129		return true;
130	}
131
132	/** {@inheritdoc} */
133	public function isGedcomBlock() {
134		return false;
135	}
136
137	/** {@inheritdoc} */
138	public function configureBlock($block_id) {
139		if (Filter::postBool('save') && Filter::checkCsrf()) {
140			set_block_setting($block_id, 'block', Filter::postBool('block'));
141		}
142
143		$block = get_block_setting($block_id, 'block', '1');
144		echo '<tr><td class="descriptionbox wrap width33">';
145		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
146		echo '</td><td class="optionbox">';
147		echo edit_field_yes_no('block', $block);
148		echo '</td></tr>';
149	}
150}
151