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