TYPO3 v15 dev-main snapshot ()
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Backend\Backend\Avatar;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
|
||||
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
|
||||
use TYPO3\CMS\Core\Imaging\IconFactory;
|
||||
use TYPO3\CMS\Core\Imaging\IconSize;
|
||||
use TYPO3\CMS\Core\Service\DependencyOrderingService;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Core\Utility\PathUtility;
|
||||
|
||||
/**
|
||||
* Main class to render an avatar image of a certain Backend user, resolving any avatar provider
|
||||
* that takes care of fetching the image.
|
||||
*
|
||||
* See render() and getImgTag() as main entry points
|
||||
*/
|
||||
#[Autoconfigure(public: true)]
|
||||
readonly class Avatar
|
||||
{
|
||||
/**
|
||||
* @param list<AvatarProviderInterface> $avatarProviders
|
||||
*/
|
||||
public function __construct(
|
||||
#[Autowire(service: 'cache.runtime')]
|
||||
protected FrontendInterface $cache,
|
||||
protected DependencyOrderingService $dependencyOrderingService,
|
||||
protected IconFactory $iconFactory,
|
||||
protected array $avatarProviders = [],
|
||||
) {
|
||||
$this->validateAvatarProviders();
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders an avatar based on a Fluid template which contains some base wrapper css classes.
|
||||
* Has a simple caching functionality. Used in Avatar ViewHelper for instance.
|
||||
* Renders avatar of a given backend user record, or of current logged-in backend user.
|
||||
*/
|
||||
public function render(?array $backendUser = null, int $size = 32, bool $showIcon = false): string
|
||||
{
|
||||
if (!is_array($backendUser)) {
|
||||
/** @var array $backendUser */
|
||||
$backendUser = $this->getBackendUser()->user;
|
||||
}
|
||||
$cacheId = 'avatar_' . sha1($backendUser['uid'] . $size . $showIcon);
|
||||
$avatar = $this->cache->get($cacheId);
|
||||
if (!$avatar) {
|
||||
$icon = $showIcon ? $this->iconFactory->getIconForRecord('be_users', $backendUser, IconSize::SMALL)->render() : '';
|
||||
$avatar
|
||||
= '<span class="avatar" style="--avatar-size: ' . $size . 'px;">'
|
||||
. '<span class="avatar-image">' . $this->getImgTag($backendUser, $size) . '</span>'
|
||||
. ($showIcon ? '<span class="avatar-icon">' . $icon . '</span>' : '')
|
||||
. '</span>';
|
||||
$this->cache->set($cacheId, $avatar);
|
||||
}
|
||||
return $avatar;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an HTML <img> tag of given backend users avatar.
|
||||
*/
|
||||
protected function getImgTag(array $backendUser, int $size = 32): string
|
||||
{
|
||||
$avatarImage = $this->getImage($backendUser, $size);
|
||||
return '<img src="' . htmlspecialchars($avatarImage->getUrl()) . '" '
|
||||
. 'width="' . (int)$avatarImage->getWidth() . '" '
|
||||
. 'height="' . (int)$avatarImage->getHeight() . '" '
|
||||
. 'alt="" '
|
||||
. 'aria-hidden="true" '
|
||||
. 'loading="lazy" />';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Image from first provider that returns one.
|
||||
*/
|
||||
protected function getImage(array $backendUser, int $size): Image
|
||||
{
|
||||
foreach ($this->avatarProviders as $provider) {
|
||||
$avatarImage = $provider->getImage($backendUser, $size);
|
||||
if (!empty($avatarImage)) {
|
||||
return $avatarImage;
|
||||
}
|
||||
}
|
||||
return GeneralUtility::makeInstance(
|
||||
Image::class,
|
||||
(string)PathUtility::getSystemResourceUri('EXT:core/Resources/Public/Icons/T3Icons/svgs/avatar/avatar-default.svg'),
|
||||
$size,
|
||||
$size
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the registered avatar providers
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
protected function validateAvatarProviders(): void
|
||||
{
|
||||
foreach ($this->avatarProviders as $provider) {
|
||||
if (!($provider instanceof AvatarProviderInterface)) {
|
||||
throw new \RuntimeException(
|
||||
sprintf(
|
||||
'Avatar provider must implement interface "%s", "%s" given.',
|
||||
AvatarProviderInterface::class,
|
||||
get_debug_type($provider),
|
||||
),
|
||||
1439317802,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function getBackendUser(): BackendUserAuthentication
|
||||
{
|
||||
return $GLOBALS['BE_USER'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Backend\Backend\Avatar;
|
||||
|
||||
/**
|
||||
* Contract for avatar providers that ensure how an avatar should be rendered for a given Backend User
|
||||
*/
|
||||
interface AvatarProviderInterface
|
||||
{
|
||||
/**
|
||||
* Returns an Image object, prepared for output, based on a given be_users record
|
||||
*
|
||||
* @param array $backendUser be_users record
|
||||
* @param int $size
|
||||
* @return Image|null
|
||||
*/
|
||||
public function getImage(array $backendUser, $size);
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Backend\Backend\Avatar;
|
||||
|
||||
use TYPO3\CMS\Backend\Attribute\AsAvatarProvider;
|
||||
use TYPO3\CMS\Core\Database\Connection;
|
||||
use TYPO3\CMS\Core\Database\ConnectionPool;
|
||||
use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
|
||||
use TYPO3\CMS\Core\Resource\ProcessedFile;
|
||||
use TYPO3\CMS\Core\Resource\ResourceFactory;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* Avatar Provider used for rendering avatars based on local files (based on FAL), stored in the be_users.avatar
|
||||
* relation field with sys_file_reference.
|
||||
*/
|
||||
#[AsAvatarProvider('defaultAvatarProvider')]
|
||||
class DefaultAvatarProvider implements AvatarProviderInterface
|
||||
{
|
||||
/**
|
||||
* Return an Image object for rendering the avatar, based on a FAL-based file
|
||||
*
|
||||
* @param array $backendUser be_users record
|
||||
* @param int $size
|
||||
* @return Image|null
|
||||
*/
|
||||
public function getImage(array $backendUser, $size)
|
||||
{
|
||||
$fileUid = $this->getAvatarFileUid($backendUser['uid']);
|
||||
if ($fileUid === 0) {
|
||||
// Early return if there is no valid image file UID
|
||||
return null;
|
||||
}
|
||||
// Get file object
|
||||
try {
|
||||
$file = GeneralUtility::makeInstance(ResourceFactory::class)->getFileObject($fileUid);
|
||||
$processedImage = $file->process(
|
||||
ProcessedFile::CONTEXT_IMAGECROPSCALEMASK,
|
||||
['width' => $size . 'c', 'height' => $size . 'c']
|
||||
);
|
||||
|
||||
$publicUrl = $processedImage->getPublicUrl();
|
||||
if ($publicUrl) {
|
||||
$image = GeneralUtility::makeInstance(
|
||||
Image::class,
|
||||
$publicUrl,
|
||||
$processedImage->getProperty('width'),
|
||||
$processedImage->getProperty('height')
|
||||
);
|
||||
} else {
|
||||
$image = null;
|
||||
}
|
||||
} catch (FileDoesNotExistException $e) {
|
||||
// No image found
|
||||
$image = null;
|
||||
}
|
||||
|
||||
return $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the sys_file UID of the avatar of the given backend user ID
|
||||
*
|
||||
* @param int $backendUserId the UID of the be_users record
|
||||
* @return int the sys_file UID or 0 if none found
|
||||
*/
|
||||
protected function getAvatarFileUid($backendUserId)
|
||||
{
|
||||
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file_reference');
|
||||
$fileUid = $queryBuilder
|
||||
->select('uid_local')
|
||||
->from('sys_file_reference')
|
||||
->where(
|
||||
$queryBuilder->expr()->eq(
|
||||
'tablenames',
|
||||
$queryBuilder->createNamedParameter('be_users')
|
||||
),
|
||||
$queryBuilder->expr()->eq(
|
||||
'fieldname',
|
||||
$queryBuilder->createNamedParameter('avatar')
|
||||
),
|
||||
$queryBuilder->expr()->eq(
|
||||
'uid_foreign',
|
||||
$queryBuilder->createNamedParameter((int)$backendUserId, Connection::PARAM_INT)
|
||||
)
|
||||
)
|
||||
->executeQuery()
|
||||
->fetchOne();
|
||||
|
||||
return (int)$fileUid;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Backend\Backend\Avatar;
|
||||
|
||||
/**
|
||||
* Acts as a pseudo model for holding all information of an avatar image
|
||||
* Holds url + dimensions of avatar image
|
||||
*/
|
||||
class Image
|
||||
{
|
||||
/**
|
||||
* Url of avatar image. Needs to be relative to the website root or an absolute URL.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $url;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $width;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $height;
|
||||
|
||||
/**
|
||||
* @param string $url url of image. Needs to be relative to the website root or an absolute URL.
|
||||
* @param int $width width of image
|
||||
* @param int $height height of image
|
||||
*/
|
||||
public function __construct($url, $width, $height)
|
||||
{
|
||||
$this->url = $url;
|
||||
$this->width = (int)$width;
|
||||
$this->height = (int)$height;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the URL to the avatar image
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUrl()
|
||||
{
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getWidth()
|
||||
{
|
||||
return $this->width;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getHeight()
|
||||
{
|
||||
return $this->height;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user