namespace wcf\data\user;
use wcf\data\DatabaseObjectDecorator;
+use wcf\data\file\File;
+use wcf\data\file\thumbnail\FileThumbnailList;
use wcf\data\ITitledLinkObject;
use wcf\data\trophy\Trophy;
use wcf\data\trophy\TrophyCache;
*/
public function getAvatar()
{
+ // TODO simplify this function?
if ($this->avatar === null) {
if (!$this->disableAvatar) {
if ($this->canSeeAvatar()) {
- if ($this->avatarID) {
+ if ($this->avatarFileID !== null) {
+ $data = UserStorageHandler::getInstance()->getField('avatar', $this->userID);
+ if ($data === null) {
+ $this->avatar = new File($this->avatarFileID);
+
+ $thumbnailList = new FileThumbnailList();
+ $thumbnailList->getConditionBuilder()->add("fileID = ?", [$this->avatarFileID]);
+ $thumbnailList->readObjects();
+ foreach ($thumbnailList as $thumbnail) {
+ $this->avatar->addThumbnail($thumbnail);
+ }
+
+ UserStorageHandler::getInstance()->update(
+ $this->userID,
+ 'avatar',
+ \serialize($this->avatar)
+ );
+ } else {
+ $this->avatar = \unserialize($data);
+ }
+ } elseif ($this->avatarID) {
if (!$this->fileHash) {
$data = UserStorageHandler::getInstance()->getField('avatar', $this->userID);
if ($data === null) {
return $this->avatar;
}
+ public function setFileAvatar(File $file): void
+ {
+ $this->avatar = new AvatarDecorator($file);
+ }
+
/**
* Returns true if the active user can view the avatar of this user.
*
namespace wcf\data\user;
+use wcf\data\file\FileList;
+use wcf\data\file\thumbnail\FileThumbnailList;
+
/**
* Represents a list of user profiles.
*
*/
public $decoratorClassName = UserProfile::class;
+ public bool $loadAvatarFiles = true;
+
/**
* @inheritDoc
*/
if (!empty($this->sqlSelects)) {
$this->sqlSelects .= ',';
}
- $this->sqlSelects .= "user_avatar.*";
- $this->sqlJoins .= "
- LEFT JOIN wcf1_user_avatar user_avatar
- ON user_avatar.avatarID = user_table.avatarID";
// get current location
- $this->sqlSelects .= ", session.pageID, session.pageObjectID, session.lastActivityTime AS sessionLastActivityTime";
+ $this->sqlSelects .= "session.pageID, session.pageObjectID, session.lastActivityTime AS sessionLastActivityTime";
$this->sqlJoins .= "
LEFT JOIN wcf1_session session
ON session.userID = user_table.userID";
}
parent::readObjects();
+
+ $this->loadAvatarFiles();
+ }
+
+ protected function loadAvatarFiles(): void
+ {
+ if (!$this->loadAvatarFiles) {
+ return;
+ }
+
+ $avatarFileIDs = [];
+ foreach ($this->objects as $user) {
+ if ($user->avatarFileID !== null) {
+ $avatarFileIDs[] = $user->avatarFileID;
+ }
+ }
+ if ($avatarFileIDs === []) {
+ return;
+ }
+
+ $fileList = new FileList();
+ $fileList->setObjectIDs($avatarFileIDs);
+ $fileList->readObjects();
+ $files = $fileList->getObjects();
+
+ $thumbnailList = new FileThumbnailList();
+ $thumbnailList->getConditionBuilder()->add("fileID IN (?)", [$avatarFileIDs]);
+ $thumbnailList->readObjects();
+ foreach ($thumbnailList as $thumbnail) {
+ $files[$thumbnail->fileID]->addThumbnail($thumbnail);
+ }
+
+ foreach ($this->objects as $user) {
+ if ($user->avatarFileID !== null) {
+ $user->setFileAvatar($files[$user->avatarFileID]);
+ }
+ }
}
}
namespace wcf\data\user\avatar;
+use wcf\data\file\File;
+use wcf\util\StringUtil;
+
/**
* Wraps avatars to provide compatibility layers.
*
*/
final class AvatarDecorator implements IUserAvatar, ISafeFormatAvatar
{
- /**
- * @var IUserAvatar
- */
- private $avatar;
+ private IUserAvatar | File $avatar;
- public function __construct(IUserAvatar $avatar)
+ public function __construct(IUserAvatar | File $avatar)
{
$this->avatar = $avatar;
}
*/
public function getSafeURL(?int $size = null): string
{
- if ($this->avatar instanceof ISafeFormatAvatar) {
+ if ($this->avatar instanceof File) {
+ return $this->getURL($size);
+ } elseif ($this->avatar instanceof ISafeFormatAvatar) {
return $this->avatar->getSafeURL($size);
}
*/
public function getSafeImageTag(?int $size = null): string
{
- if ($this->avatar instanceof ISafeFormatAvatar) {
+ if ($this->avatar instanceof File) {
+ return $this->getImageTag($size);
+ } elseif ($this->avatar instanceof ISafeFormatAvatar) {
return $this->avatar->getSafeImageTag($size);
}
*/
public function getURL($size = null)
{
- return $this->avatar->getURL();
+ if ($this->avatar instanceof File) {
+ $thumbnail = $this->avatar->getThumbnail($size ?? '');
+ if ($thumbnail !== null) {
+ return $thumbnail->getLink();
+ }
+
+ return $this->avatar->getLink();
+ } else {
+ return $this->avatar->getURL();
+ }
}
/**
*/
public function getImageTag($size = null, bool $lazyLoading = true)
{
- return $this->avatar->getImageTag($size, $lazyLoading);
+ if ($this->avatar instanceof File) {
+ return \sprintf(
+ '<img src="%s" width="%d" height="%d" alt="" class="userAvatarImage" loading="%s">',
+ StringUtil::encodeHTML($this->getSafeURL($size)),
+ $size,
+ $size,
+ $lazyLoading ? 'lazy' : 'eager'
+ );
+ } else {
+ return $this->avatar->getImageTag($size, $lazyLoading);
+ }
}
/**
*/
public function getWidth()
{
- return $this->avatar->getWidth();
+ if ($this->avatar instanceof File) {
+ return $this->avatar->width;
+ } else {
+ return $this->avatar->getWidth();
+ }
}
/**
*/
public function getHeight()
{
- return $this->avatar->getHeight();
+ if ($this->avatar instanceof File) {
+ return $this->avatar->height;
+ } else {
+ return $this->avatar->getHeight();
+ }
}
}