-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathTCollectionUserProfiles.class.php
More file actions
55 lines (45 loc) · 1.47 KB
/
TCollectionUserProfiles.class.php
File metadata and controls
55 lines (45 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
namespace wcf\data;
use wcf\data\user\UserProfile;
use wcf\system\cache\runtime\UserProfileRuntimeCache;
/**
* Trait for dbo collections with user profiles.
*
* @author Marcel Werk
* @copyright 2001-2025 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.2
*/
trait TCollectionUserProfiles
{
private bool $userProfilesLoaded = false;
public function getUserProfile(
DatabaseObject $object,
string $userIdProperty = 'userID',
string $usernameProperty = 'username'
): UserProfile {
$this->loadUserProfiles($userIdProperty);
if ($object->{$userIdProperty}) {
return UserProfileRuntimeCache::getInstance()->getObject($object->{$userIdProperty});
} else {
return UserProfile::getGuestUserProfile($object->{$usernameProperty});
}
}
private function loadUserProfiles(string $userIdProperty): void
{
if ($this->userProfilesLoaded) {
return;
}
\assert($this instanceof DatabaseObjectCollection);
$this->userProfilesLoaded = true;
$userIDs = [];
foreach ($this->getObjects() as $object) {
if ($object->{$userIdProperty}) {
$userIDs[] = $object->{$userIdProperty};
}
}
if ($userIDs !== []) {
UserProfileRuntimeCache::getInstance()->cacheObjectIDs($userIDs);
}
}
}