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