Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace wcf\data;

use wcf\system\message\embedded\object\MessageEmbeddedObjectManager;

/**
* Trait for dbo collections with message embedded objects.
*
* @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 TCollectionEmbeddedObjects
{
private bool $embeddedObjectsLoaded = false;

public function loadEmbeddedObjects(string $messageObjectType): void
{
if ($this->embeddedObjectsLoaded) {
return;
}

$this->embeddedObjectsLoaded = true;

$objectIDs = $this->getEmbeddedObjectIDs();
if ($objectIDs === []) {
return;
}

MessageEmbeddedObjectManager::getInstance()->loadObjects(
$messageObjectType,
$objectIDs
);
}

/**
* @return int[]
*/
private function getEmbeddedObjectIDs(): array
{
\assert($this instanceof DatabaseObjectCollection);

return \array_map(
static fn($object) => $object->getObjectID(),
\array_filter($this->getObjects(), fn($object) => $object->hasEmbeddedObjects === 1)
);
}
}
55 changes: 55 additions & 0 deletions wcfsetup/install/files/lib/data/TCollectionUserProfiles.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);
}
}
}