diff --git a/wcfsetup/install/files/lib/data/TCollectionEmbeddedObjects.class.php b/wcfsetup/install/files/lib/data/TCollectionEmbeddedObjects.class.php new file mode 100644 index 00000000000..5a9b8007482 --- /dev/null +++ b/wcfsetup/install/files/lib/data/TCollectionEmbeddedObjects.class.php @@ -0,0 +1,50 @@ + + * @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) + ); + } +} diff --git a/wcfsetup/install/files/lib/data/TCollectionUserProfiles.class.php b/wcfsetup/install/files/lib/data/TCollectionUserProfiles.class.php new file mode 100644 index 00000000000..8aa260afcbb --- /dev/null +++ b/wcfsetup/install/files/lib/data/TCollectionUserProfiles.class.php @@ -0,0 +1,55 @@ + + * @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); + } + } +}