xref: /webtrees/app/Module/LoggedInUsersModule.php (revision cdc90107d0170c768f6b75a14987716f307363cc)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2016 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\Filter;
20use Fisharebest\Webtrees\Functions\Functions;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Individual;
23use Fisharebest\Webtrees\Theme;
24use Fisharebest\Webtrees\User;
25
26/**
27 * Class LoggedInUsersModule
28 */
29class LoggedInUsersModule extends AbstractModule implements ModuleBlockInterface {
30	/** {@inheritdoc} */
31	public function getTitle() {
32		return /* I18N: Name of a module. (A list of users who are online now) */ I18N::translate('Who is online');
33	}
34
35	/** {@inheritdoc} */
36	public function getDescription() {
37		return /* I18N: Description of the “Who is online” module */ I18N::translate('A list of users and visitors who are currently online.');
38	}
39
40	/**
41	 * Generate the HTML content of this block.
42	 *
43	 * @param int      $block_id
44	 * @param bool     $template
45	 * @param string[] $cfg
46	 *
47	 * @return string
48	 */
49	public function getBlock($block_id, $template = true, $cfg = array()) {
50		global $WT_TREE;
51
52		$id        = $this->getName() . $block_id;
53		$class     = $this->getName() . '_block';
54		$title     = $this->getTitle();
55		$anonymous = 0;
56		$logged_in = array();
57		$content   = '';
58		foreach (User::allLoggedIn() as $user) {
59			if (Auth::isAdmin() || $user->getPreference('visibleonline')) {
60				$logged_in[] = $user;
61			} else {
62				$anonymous++;
63			}
64		}
65		$count_logged_in = count($logged_in);
66		$content .= '<div class="logged_in_count">';
67		if ($anonymous) {
68			$content .= I18N::plural('%s anonymous signed-in user', '%s anonymous signed-in users', $anonymous, I18N::number($anonymous));
69			if ($count_logged_in) {
70				$content .= '&nbsp;|&nbsp;';
71			}
72		}
73		if ($count_logged_in) {
74			$content .= I18N::plural('%s signed-in user', '%s signed-in users', $count_logged_in, I18N::number($count_logged_in));
75		}
76		$content .= '</div>';
77		$content .= '<div class="logged_in_list">';
78		if (Auth::check()) {
79			foreach ($logged_in as $user) {
80				$individual = Individual::getInstance($WT_TREE->getUserPreference($user, 'gedcomid'), $WT_TREE);
81
82				$content .= '<div class="logged_in_name">';
83				if ($individual) {
84					$content .= '<a href="' . $individual->getHtmlUrl() . '">' . $user->getRealNameHtml() . '</a>';
85				} else {
86					$content .= $user->getRealNameHtml();
87				}
88				$content .= ' - ' . Filter::escapeHtml($user->getUserName());
89				if (Auth::id() != $user->getUserId() && $user->getPreference('contactmethod') != 'none') {
90					$content .= ' <a class="icon-email" href="#" onclick="return message(\'' . Filter::escapeHtml($user->getUserName()) . '\', \'\', \'' . Filter::escapeHtml(Functions::getQueryUrl()) . '\');" title="' . I18N::translate('Send a message') . '"></a>';
91				}
92				$content .= '</div>';
93			}
94		}
95		$content .= '</div>';
96
97		if ($anonymous === 0 && $count_logged_in === 0) {
98			return '';
99		}
100
101		if ($template) {
102			return Theme::theme()->formatBlock($id, $title, $class, $content);
103		} else {
104			return $content;
105		}
106	}
107
108	/** {@inheritdoc} */
109	public function loadAjax() {
110		return false;
111	}
112
113	/** {@inheritdoc} */
114	public function isUserBlock() {
115		return true;
116	}
117
118	/** {@inheritdoc} */
119	public function isGedcomBlock() {
120		return true;
121	}
122
123	/**
124	 * An HTML form to edit block settings
125	 *
126	 * @param int $block_id
127	 */
128	public function configureBlock($block_id) {
129	}
130}
131