xref: /webtrees/app/Module/LoggedInUsersModule.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 LoggedInUsersModule
21 */
22class LoggedInUsersModule extends Module implements ModuleBlockInterface {
23	/** {@inheritdoc} */
24	public function getTitle() {
25		return /* I18N: Name of a module. (A list of users who are online now) */ I18N::translate('Who is online');
26	}
27
28	/** {@inheritdoc} */
29	public function getDescription() {
30		return /* I18N: Description of the “Who is online” module */ I18N::translate('A list of users and visitors who are currently online.');
31	}
32
33	/** {@inheritdoc} */
34	public function getBlock($block_id, $template = true, $cfg = null) {
35		$id        = $this->getName() . $block_id;
36		$class     = $this->getName() . '_block';
37		$title     = $this->getTitle();
38		$anonymous = 0;
39		$logged_in = array();
40		$content   = '';
41		foreach (User::allLoggedIn() as $user) {
42			if (Auth::isAdmin() || $user->getPreference('visibleonline')) {
43				$logged_in[] = $user;
44			} else {
45				$anonymous++;
46			}
47		}
48		$count_logged_in = count($logged_in);
49		$content .= '<div class="logged_in_count">';
50		if ($anonymous) {
51			$content .= I18N::plural('%d anonymous logged-in user', '%d anonymous logged-in users', $anonymous, $anonymous);
52			if ($count_logged_in) {
53				$content .= '&nbsp;|&nbsp;';
54			}
55		}
56		if ($count_logged_in) {
57			$content .= I18N::plural('%d logged-in user', '%d logged-in users', $count_logged_in, $count_logged_in);
58		}
59		$content .= '</div>';
60		$content .= '<div class="logged_in_list">';
61		if (Auth::check()) {
62			foreach ($logged_in as $user) {
63				$content .= '<div class="logged_in_name">';
64				$content .= Filter::escapeHtml($user->getRealName()) . ' - ' . Filter::escapeHtml($user->getUserName());
65				if (Auth::id() != $user->getUserId() && $user->getPreference('contactmethod') != 'none') {
66					$content .= ' <a class="icon-email" href="#" onclick="return message(\'' . Filter::escapeHtml($user->getUserName()) . '\', \'\', \'' . Filter::escapeHtml(get_query_url()) . '\');" title="' . I18N::translate('Send a message') . '"></a>';
67				}
68				$content .= '</div>';
69			}
70		}
71		$content .= '</div>';
72
73		if ($anonymous === 0 && $count_logged_in === 0) {
74			return '';
75		}
76
77		if ($template) {
78			return Theme::theme()->formatBlock($id, $title, $class, $content);
79		} else {
80			return $content;
81		}
82	}
83
84	/** {@inheritdoc} */
85	public function loadAjax() {
86		return false;
87	}
88
89	/** {@inheritdoc} */
90	public function isUserBlock() {
91		return true;
92	}
93
94	/** {@inheritdoc} */
95	public function isGedcomBlock() {
96		return true;
97	}
98
99	/** {@inheritdoc} */
100	public function configureBlock($block_id) {
101	}
102}
103