|
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
|
|
|
|
|
18
|
|
|
declare(strict_types=1); |
|
19
|
|
|
|
|
20
|
|
|
namespace Fisharebest\Webtrees\Statistics\Repository; |
|
21
|
|
|
|
|
22
|
|
|
use Fisharebest\Webtrees\Auth; |
|
23
|
|
|
use Fisharebest\Webtrees\Contracts\UserInterface; |
|
24
|
|
|
use Fisharebest\Webtrees\Registry; |
|
25
|
|
|
use Fisharebest\Webtrees\Http\RequestHandlers\MessagePage; |
|
26
|
|
|
use Fisharebest\Webtrees\I18N; |
|
27
|
|
|
use Fisharebest\Webtrees\Individual; |
|
28
|
|
|
use Fisharebest\Webtrees\Services\UserService; |
|
29
|
|
|
use Fisharebest\Webtrees\Statistics\Repository\Interfaces\UserRepositoryInterface; |
|
30
|
|
|
use Fisharebest\Webtrees\Tree; |
|
31
|
|
|
use Fisharebest\Webtrees\User; |
|
32
|
|
|
|
|
33
|
|
|
use function count; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* A repository providing methods for user related statistics. |
|
37
|
|
|
*/ |
|
38
|
|
|
class UserRepository implements UserRepositoryInterface |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* @var Tree |
|
42
|
|
|
*/ |
|
43
|
|
|
private $tree; |
|
44
|
|
|
/** |
|
45
|
|
|
* @var UserService |
|
46
|
|
|
*/ |
|
47
|
|
|
private $user_service; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Constructor. |
|
51
|
|
|
* |
|
52
|
|
|
* @param Tree $tree |
|
53
|
|
|
* @param UserService $user_service |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct(Tree $tree, UserService $user_service) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->tree = $tree; |
|
58
|
|
|
$this->user_service = $user_service; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Who is currently logged in? |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $type "list" or "nolist" |
|
65
|
|
|
* |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
private function usersLoggedInQuery($type = 'nolist'): string |
|
69
|
|
|
{ |
|
70
|
|
|
$content = ''; |
|
71
|
|
|
$anonymous = 0; |
|
72
|
|
|
$logged_in = []; |
|
73
|
|
|
|
|
74
|
|
|
foreach ($this->user_service->allLoggedIn() as $user) { |
|
75
|
|
|
if (Auth::isAdmin() || $user->getPreference(User::PREF_IS_VISIBLE_ONLINE) === '1') { |
|
76
|
|
|
$logged_in[] = $user; |
|
77
|
|
|
} else { |
|
78
|
|
|
$anonymous++; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$count_logged_in = count($logged_in); |
|
83
|
|
|
|
|
84
|
|
|
if ($count_logged_in === 0 && $anonymous === 0) { |
|
85
|
|
|
$content .= I18N::translate('No signed-in and no anonymous users'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if ($anonymous > 0) { |
|
89
|
|
|
$content .= '<b>' . I18N::plural('%s anonymous signed-in user', '%s anonymous signed-in users', $anonymous, I18N::number($anonymous)) . '</b>'; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if ($count_logged_in > 0) { |
|
93
|
|
|
if ($anonymous) { |
|
94
|
|
|
if ($type === 'list') { |
|
95
|
|
|
$content .= '<br><br>'; |
|
96
|
|
|
} else { |
|
97
|
|
|
$content .= ' ' . I18N::translate('and') . ' '; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
$content .= '<b>' . I18N::plural('%s signed-in user', '%s signed-in users', $count_logged_in, I18N::number($count_logged_in)) . '</b>'; |
|
101
|
|
|
if ($type === 'list') { |
|
102
|
|
|
$content .= '<ul>'; |
|
103
|
|
|
} else { |
|
104
|
|
|
$content .= ': '; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if (Auth::check()) { |
|
109
|
|
|
foreach ($logged_in as $user) { |
|
110
|
|
|
if ($type === 'list') { |
|
111
|
|
|
$content .= '<li>'; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$individual = Registry::individualFactory()->make($this->tree->getUserPreference($user, User::PREF_TREE_ACCOUNT_XREF), $this->tree); |
|
115
|
|
|
|
|
116
|
|
|
if ($individual instanceof Individual && $individual->canShow()) { |
|
117
|
|
|
$content .= '<a href="' . e($individual->url()) . '">' . e($user->realName()) . '</a>'; |
|
118
|
|
|
} else { |
|
119
|
|
|
$content .= e($user->realName()); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$content .= ' - ' . e($user->userName()); |
|
123
|
|
|
|
|
124
|
|
|
if ($user->getPreference(User::PREF_CONTACT_METHOD) !== 'none' && Auth::id() !== $user->id()) { |
|
125
|
|
|
$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>'; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
if ($type === 'list') { |
|
129
|
|
|
$content .= '</li>'; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if ($type === 'list') { |
|
135
|
|
|
$content .= '</ul>'; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $content; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return string |
|
143
|
|
|
*/ |
|
144
|
|
|
public function usersLoggedIn(): string |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->usersLoggedInQuery('nolist'); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return string |
|
151
|
|
|
*/ |
|
152
|
|
|
public function usersLoggedInList(): string |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->usersLoggedInQuery('list'); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Returns true if the given user is visible to others. |
|
159
|
|
|
* |
|
160
|
|
|
* @param UserInterface $user |
|
161
|
|
|
* |
|
162
|
|
|
* @return bool |
|
163
|
|
|
*/ |
|
164
|
|
|
private function isUserVisible(UserInterface $user): bool |
|
165
|
|
|
{ |
|
166
|
|
|
return Auth::isAdmin() || $user->getPreference(User::PREF_IS_VISIBLE_ONLINE) === '1'; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @return int |
|
171
|
|
|
*/ |
|
172
|
|
|
public function usersLoggedInTotal(): int |
|
173
|
|
|
{ |
|
174
|
|
|
return count($this->user_service->allLoggedIn()); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @return int |
|
179
|
|
|
*/ |
|
180
|
|
|
public function usersLoggedInTotalAnon(): int |
|
181
|
|
|
{ |
|
182
|
|
|
$anonymous = 0; |
|
183
|
|
|
|
|
184
|
|
|
foreach ($this->user_service->allLoggedIn() as $user) { |
|
185
|
|
|
if (!$this->isUserVisible($user)) { |
|
186
|
|
|
++$anonymous; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return $anonymous; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @return int |
|
195
|
|
|
*/ |
|
196
|
|
|
public function usersLoggedInTotalVisible(): int |
|
197
|
|
|
{ |
|
198
|
|
|
$visible = 0; |
|
199
|
|
|
|
|
200
|
|
|
foreach ($this->user_service->allLoggedIn() as $user) { |
|
201
|
|
|
if ($this->isUserVisible($user)) { |
|
202
|
|
|
++$visible; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
return $visible; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @return string |
|
211
|
|
|
*/ |
|
212
|
|
|
public function userId(): string |
|
213
|
|
|
{ |
|
214
|
|
|
return (string) Auth::id(); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @param string $visitor_text |
|
219
|
|
|
* |
|
220
|
|
|
* @return string |
|
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
|
|
|
* @return string |
|
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
|
|
|
* @return string |
|
262
|
|
|
*/ |
|
263
|
|
|
public function totalUsers(): string |
|
264
|
|
|
{ |
|
265
|
|
|
return I18N::number($this->getUserCount()); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @return string |
|
270
|
|
|
*/ |
|
271
|
|
|
public function totalAdmins(): string |
|
272
|
|
|
{ |
|
273
|
|
|
return I18N::number($this->getAdminCount()); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* @return string |
|
278
|
|
|
*/ |
|
279
|
|
|
public function totalNonAdmins(): string |
|
280
|
|
|
{ |
|
281
|
|
|
return I18N::number($this->getUserCount() - $this->getAdminCount()); |
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
|