xref: /webtrees/app/Module/LoggedInUsersModule.php (revision e3fb86df7b59efbf6fe979d9e13092cc1bfebf9b)
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 AbstractModule 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		global $WT_TREE;
36
37		$id        = $this->getName() . $block_id;
38		$class     = $this->getName() . '_block';
39		$title     = $this->getTitle();
40		$anonymous = 0;
41		$logged_in = array();
42		$content   = '';
43		foreach (User::allLoggedIn() as $user) {
44			if (Auth::isAdmin() || $user->getPreference('visibleonline')) {
45				$logged_in[] = $user;
46			} else {
47				$anonymous++;
48			}
49		}
50		$count_logged_in = count($logged_in);
51		$content .= '<div class="logged_in_count">';
52		if ($anonymous) {
53			$content .= I18N::plural('%s anonymous logged-in user', '%s anonymous logged-in users', $anonymous, I18N::number($anonymous));
54			if ($count_logged_in) {
55				$content .= '&nbsp;|&nbsp;';
56			}
57		}
58		if ($count_logged_in) {
59			$content .= I18N::plural('%s logged-in user', '%s logged-in users', $count_logged_in, I18N::number($count_logged_in));
60		}
61		$content .= '</div>';
62		$content .= '<div class="logged_in_list">';
63		if (Auth::check()) {
64			foreach ($logged_in as $user) {
65				$individual = Individual::getInstance($WT_TREE->getUserPreference($user, 'gedcomid'), $WT_TREE);
66
67				$content .= '<div class="logged_in_name">';
68				if ($individual) {
69					$content .= '<a href="' . $individual->getHtmlUrl() . '">' . $user->getRealNameHtml() . '</a>';
70				} else {
71					$content .= $user->getRealNameHtml();
72				}
73				$content .= ' - ' . Filter::escapeHtml($user->getUserName());
74				if (Auth::id() != $user->getUserId() && $user->getPreference('contactmethod') != 'none') {
75					$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>';
76				}
77				$content .= '</div>';
78			}
79		}
80		$content .= '</div>';
81
82		if ($anonymous === 0 && $count_logged_in === 0) {
83			return '';
84		}
85
86		if ($template) {
87			return Theme::theme()->formatBlock($id, $title, $class, $content);
88		} else {
89			return $content;
90		}
91	}
92
93	/** {@inheritdoc} */
94	public function loadAjax() {
95		return false;
96	}
97
98	/** {@inheritdoc} */
99	public function isUserBlock() {
100		return true;
101	}
102
103	/** {@inheritdoc} */
104	public function isGedcomBlock() {
105		return true;
106	}
107
108	/** {@inheritdoc} */
109	public function configureBlock($block_id) {
110	}
111}
112