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