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