xref: /webtrees/app/Statistics/Repository/UserRepository.php (revision eb414fd9c6104ae2de3f67b9fe3bd4d7877a8121)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Statistics\Repository;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Contracts\UserInterface;
24use Fisharebest\Webtrees\Http\RequestHandlers\MessagePage;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Individual;
27use Fisharebest\Webtrees\Services\UserService;
28use Fisharebest\Webtrees\Statistics\Repository\Interfaces\UserRepositoryInterface;
29use Fisharebest\Webtrees\Tree;
30use Fisharebest\Webtrees\User;
31
32use function count;
33
34/**
35 * A repository providing methods for user related statistics.
36 */
37class UserRepository implements UserRepositoryInterface
38{
39    /**
40     * @var Tree
41     */
42    private $tree;
43    /**
44     * @var UserService
45     */
46    private $user_service;
47
48    /**
49     * Constructor.
50     *
51     * @param Tree        $tree
52     * @param UserService $user_service
53     */
54    public function __construct(Tree $tree, UserService $user_service)
55    {
56        $this->tree         = $tree;
57        $this->user_service = $user_service;
58    }
59
60    /**
61     * Who is currently logged in?
62     *
63     * @param string $type "list" or "nolist"
64     *
65     * @return string
66     */
67    private function usersLoggedInQuery($type = 'nolist'): string
68    {
69        $content   = '';
70        $anonymous = 0;
71        $logged_in = [];
72
73        foreach ($this->user_service->allLoggedIn() as $user) {
74            if (Auth::isAdmin() || $user->getPreference(User::PREF_IS_VISIBLE_ONLINE) === '1') {
75                $logged_in[] = $user;
76            } else {
77                $anonymous++;
78            }
79        }
80
81        $count_logged_in = count($logged_in);
82
83        if ($count_logged_in === 0 && $anonymous === 0) {
84            $content .= I18N::translate('No signed-in and no anonymous users');
85        }
86
87        if ($anonymous > 0) {
88            $content .= '<b>' . I18N::plural('%s anonymous signed-in user', '%s anonymous signed-in users', $anonymous, I18N::number($anonymous)) . '</b>';
89        }
90
91        if ($count_logged_in > 0) {
92            if ($anonymous) {
93                if ($type === 'list') {
94                    $content .= '<br><br>';
95                } else {
96                    $content .= ' ' . I18N::translate('and') . ' ';
97                }
98            }
99            $content .= '<b>' . I18N::plural('%s signed-in user', '%s signed-in users', $count_logged_in, I18N::number($count_logged_in)) . '</b>';
100            if ($type === 'list') {
101                $content .= '<ul>';
102            } else {
103                $content .= ': ';
104            }
105        }
106
107        if (Auth::check()) {
108            foreach ($logged_in as $user) {
109                if ($type === 'list') {
110                    $content .= '<li>';
111                }
112
113                $individual = Individual::getInstance($this->tree->getUserPreference($user, User::PREF_TREE_ACCOUNT_XREF), $this->tree);
114
115                if ($individual instanceof Individual && $individual->canShow()) {
116                    $content .= '<a href="' . e($individual->url()) . '">' . e($user->realName()) . '</a>';
117                } else {
118                    $content .= e($user->realName());
119                }
120
121                $content .= ' - ' . e($user->userName());
122
123                if (($user->getPreference(User::PREF_CONTACT_METHOD) !== 'none') && (Auth::id() !== $user->id())) {
124                    if ($type === 'list') {
125                        $content .= '<br>';
126                    }
127                    $content .= '<a href="' . e(route(MessagePage::class, ['to' => $user->userName(), 'tree' => $this->tree->name()])) . '" class="btn btn-link" title="' . I18N::translate('Send a message') . '">' . view('icons/email') . '</a>';
128                }
129
130                if ($type === 'list') {
131                    $content .= '</li>';
132                }
133            }
134        }
135
136        if ($type === 'list') {
137            $content .= '</ul>';
138        }
139
140        return $content;
141    }
142
143    /**
144     * @inheritDoc
145     */
146    public function usersLoggedIn(): string
147    {
148        return $this->usersLoggedInQuery('nolist');
149    }
150
151    /**
152     * @inheritDoc
153     */
154    public function usersLoggedInList(): string
155    {
156        return $this->usersLoggedInQuery('list');
157    }
158
159    /**
160     * Returns true if the given user is visible to others.
161     *
162     * @param UserInterface $user
163     *
164     * @return bool
165     */
166    private function isUserVisible(UserInterface $user): bool
167    {
168        return Auth::isAdmin() || $user->getPreference(User::PREF_IS_VISIBLE_ONLINE) === '1';
169    }
170
171    /**
172     * @inheritDoc
173     */
174    public function usersLoggedInTotal(): int
175    {
176        return count($this->user_service->allLoggedIn());
177    }
178
179    /**
180     * @inheritDoc
181     */
182    public function usersLoggedInTotalAnon(): int
183    {
184        $anonymous = 0;
185
186        foreach ($this->user_service->allLoggedIn() as $user) {
187            if (!$this->isUserVisible($user)) {
188                ++$anonymous;
189            }
190        }
191
192        return $anonymous;
193    }
194
195    /**
196     * @inheritDoc
197     */
198    public function usersLoggedInTotalVisible(): int
199    {
200        $visible = 0;
201
202        foreach ($this->user_service->allLoggedIn() as $user) {
203            if ($this->isUserVisible($user)) {
204                ++$visible;
205            }
206        }
207
208        return $visible;
209    }
210
211    /**
212     * @inheritDoc
213     */
214    public function userId(): string
215    {
216        return (string) Auth::id();
217    }
218
219    /**
220     * @inheritDoc
221     */
222    public function userName(string $visitor_text = ''): string
223    {
224        if (Auth::check()) {
225            return e(Auth::user()->userName());
226        }
227
228        // if #username:visitor# was specified, then "visitor" will be returned when the user is not logged in
229        return e($visitor_text);
230    }
231
232    /**
233     * @inheritDoc
234     */
235    public function userFullName(): string
236    {
237        return Auth::check() ? '<span dir="auto">' . e(Auth::user()->realName()) . '</span>' : '';
238    }
239
240    /**
241     * Returns the user count.
242     *
243     * @return int
244     */
245    private function getUserCount(): int
246    {
247        return count($this->user_service->all());
248    }
249
250    /**
251     * Returns the administrator count.
252     *
253     * @return int
254     */
255    private function getAdminCount(): int
256    {
257        return count($this->user_service->administrators());
258    }
259
260    /**
261     * @inheritDoc
262     */
263    public function totalUsers(): string
264    {
265        return I18N::number($this->getUserCount());
266    }
267
268    /**
269     * @inheritDoc
270     */
271    public function totalAdmins(): string
272    {
273        return I18N::number($this->getAdminCount());
274    }
275
276    /**
277     * @inheritDoc
278     */
279    public function totalNonAdmins(): string
280    {
281        return I18N::number($this->getUserCount() - $this->getAdminCount());
282    }
283}
284