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