xref: /webtrees/app/Statistics/Repository/UserRepository.php (revision 1f1ffa65b3b51df2b95b5c68894525436855964a)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 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 function count;
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
29/**
30 * A repository providing methods for user related statistics.
31 */
32class UserRepository implements UserRepositoryInterface
33{
34    /**
35     * @var Tree
36     */
37    private $tree;
38    /**
39     * @var UserService
40     */
41    private $user_service;
42
43    /**
44     * Constructor.
45     *
46     * @param Tree        $tree
47     * @param UserService $user_service
48     */
49    public function __construct(Tree $tree, UserService $user_service)
50    {
51        $this->tree         = $tree;
52        $this->user_service = $user_service;
53    }
54
55    /**
56     * Who is currently logged in?
57     *
58     * @param string $type "list" or "nolist"
59     *
60     * @return string
61     */
62    private function usersLoggedInQuery($type = 'nolist'): string
63    {
64        $content   = '';
65        $anonymous = 0;
66        $logged_in = [];
67
68        foreach ($this->user_service->allLoggedIn() as $user) {
69            if (Auth::isAdmin() || $user->getPreference('visibleonline')) {
70                $logged_in[] = $user;
71            } else {
72                $anonymous++;
73            }
74        }
75
76        $count_logged_in = count($logged_in);
77
78        if ($count_logged_in === 0 && $anonymous === 0) {
79            $content .= I18N::translate('No signed-in and no anonymous users');
80        }
81
82        if ($anonymous > 0) {
83            $content .= '<b>' . I18N::plural('%s anonymous signed-in user', '%s anonymous signed-in users', $anonymous, I18N::number($anonymous)) . '</b>';
84        }
85
86        if ($count_logged_in > 0) {
87            if ($anonymous) {
88                if ($type === 'list') {
89                    $content .= '<br><br>';
90                } else {
91                    $content .= ' ' . I18N::translate('and') . ' ';
92                }
93            }
94            $content .= '<b>' . I18N::plural('%s signed-in user', '%s signed-in users', $count_logged_in, I18N::number($count_logged_in)) . '</b>';
95            if ($type === 'list') {
96                $content .= '<ul>';
97            } else {
98                $content .= ': ';
99            }
100        }
101
102        if (Auth::check()) {
103            foreach ($logged_in as $user) {
104                if ($type === 'list') {
105                    $content .= '<li>';
106                }
107
108                $individual = Individual::getInstance($this->tree->getUserPreference($user, 'gedcomid'), $this->tree);
109
110                if ($individual instanceof Individual && $individual->canShow()) {
111                    $content .= '<a href="' . e($individual->url()) . '">' . e($user->realName()) . '</a>';
112                } else {
113                    $content .= e($user->realName());
114                }
115
116                $content .= ' - ' . e($user->userName());
117
118                if (($user->getPreference('contactmethod') !== 'none')
119                    && (Auth::id() !== $user->id())
120                ) {
121                    if ($type === 'list') {
122                        $content .= '<br>';
123                    }
124                    $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>';
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