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