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