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