xref: /webtrees/app/Statistics/Repository/UserRepository.php (revision 0dcd9387ba2c042491548d2300fa2bf341235d8d)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2020 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     * @return string
145     */
146    public function usersLoggedIn(): string
147    {
148        return $this->usersLoggedInQuery('nolist');
149    }
150
151    /**
152     * @return string
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     * @return int
173     */
174    public function usersLoggedInTotal(): int
175    {
176        return count($this->user_service->allLoggedIn());
177    }
178
179    /**
180     * @return int
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     * @return int
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     * @return string
213     */
214    public function userId(): string
215    {
216        return (string) Auth::id();
217    }
218
219    /**
220     * @param string $visitor_text
221     *
222     * @return string
223     */
224    public function userName(string $visitor_text = ''): string
225    {
226        if (Auth::check()) {
227            return e(Auth::user()->userName());
228        }
229
230        // if #username:visitor# was specified, then "visitor" will be returned when the user is not logged in
231        return e($visitor_text);
232    }
233
234    /**
235     * @return string
236     */
237    public function userFullName(): string
238    {
239        return Auth::check() ? '<span dir="auto">' . e(Auth::user()->realName()) . '</span>' : '';
240    }
241
242    /**
243     * Returns the user count.
244     *
245     * @return int
246     */
247    private function getUserCount(): int
248    {
249        return count($this->user_service->all());
250    }
251
252    /**
253     * Returns the administrator count.
254     *
255     * @return int
256     */
257    private function getAdminCount(): int
258    {
259        return count($this->user_service->administrators());
260    }
261
262    /**
263     * @return string
264     */
265    public function totalUsers(): string
266    {
267        return I18N::number($this->getUserCount());
268    }
269
270    /**
271     * @return string
272     */
273    public function totalAdmins(): string
274    {
275        return I18N::number($this->getAdminCount());
276    }
277
278    /**
279     * @return string
280     */
281    public function totalNonAdmins(): string
282    {
283        return I18N::number($this->getUserCount() - $this->getAdminCount());
284    }
285}
286