‪TYPO3CMS  11.5
Avatar.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
25 
32 class ‪Avatar
33 {
39  protected ‪$avatarProviders = [];
40 
50  public function ‪render(array $backendUser = null, int $size = 32, bool $showIcon = false)
51  {
52  if (!is_array($backendUser)) {
53  $backendUser = $this->‪getBackendUser()->user;
54  }
55 
56  $cacheId = 'avatar_' . md5(
57  $backendUser['uid'] . '/' .
58  (string)$size . '/' .
59  (string)$showIcon
60  );
61 
62  $avatar = $this->‪getCache()->‪get($cacheId);
63 
64  if (!$avatar) {
66  $view = $this->‪getFluidTemplateObject();
67 
68  $view->assignMultiple([
69  'image' => $this->‪getImgTag($backendUser, $size),
70  'showIcon' => $showIcon,
71  'backendUser' => $backendUser,
72  ]);
73  $avatar = $view->render();
74  $this->‪getCache()->‪set($cacheId, $avatar);
75  }
76 
77  return $avatar;
78  }
79 
87  public function ‪getImgTag(array $backendUser = null, $size = 32)
88  {
89  if (!is_array($backendUser)) {
90  $backendUser = $this->‪getBackendUser()->user;
91  }
92 
93  $avatarImage = false;
94  if ($backendUser !== null) {
95  $avatarImage = $this->‪getImage($backendUser, $size);
96  }
97 
98  if (!$avatarImage) {
99  $avatarImage = GeneralUtility::makeInstance(
100  Image::class,
101  ‪PathUtility::getPublicResourceWebPath('EXT:core/Resources/Public/Icons/T3Icons/svgs/avatar/avatar-default.svg'),
102  $size,
103  $size
104  );
105  }
106  $imageTag = '<img src="' . htmlspecialchars($avatarImage->getUrl()) . '" ' .
107  'width="' . (int)$avatarImage->getWidth() . '" ' .
108  'height="' . (int)$avatarImage->getHeight() . '" />';
109 
110  return $imageTag;
111  }
112 
120  public function ‪getImage(array $backendUser, $size)
121  {
122  foreach ($this->avatarProviders as $provider) {
123  $avatarImage = $provider->getImage($backendUser, $size);
124  if (!empty($avatarImage)) {
125  return $avatarImage;
126  }
127  }
128  return null;
129  }
130 
136  protected function ‪validateSortAndInitiateAvatarProviders()
137  {
139  $providers = ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'] ?? [];
140  if (empty($providers)) {
141  return;
142  }
143  foreach ($providers as $identifier => $configuration) {
144  if (empty($configuration) || !is_array($configuration)) {
145  throw new \RuntimeException(
146  'Missing configuration for avatar provider "' . $identifier . '".',
147  1439317801
148  );
149  }
150  if (!is_string($configuration['provider']) || empty($configuration['provider']) || !class_exists($configuration['provider']) || !is_subclass_of(
151  $configuration['provider'],
152  AvatarProviderInterface::class
153  )) {
154  throw new \RuntimeException(
155  'The avatar provider "' . $identifier . '" defines an invalid provider. Ensure the class exists and implements the "' . AvatarProviderInterface::class . '".',
156  1439317802
157  );
158  }
159  }
160 
161  $orderedProviders = GeneralUtility::makeInstance(DependencyOrderingService::class)->orderByDependencies($providers);
162 
163  // Initializes providers
164  foreach ($orderedProviders as $configuration) {
166  $avatarProvider = GeneralUtility::makeInstance($configuration['provider']);
167  $this->avatarProviders[] = $avatarProvider;
168  }
169  }
170 
176  protected function ‪getBackendUser()
177  {
178  return ‪$GLOBALS['BE_USER'];
179  }
180 
187  protected function ‪getFluidTemplateObject(string $filename = null): ‪StandaloneView
188  {
189  $view = GeneralUtility::makeInstance(StandaloneView::class);
190  $view->setLayoutRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Layouts')]);
191  $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
192  $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
193 
194  if ($filename === null) {
195  $filename = 'Main.html';
196  }
197 
198  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/Avatar/' . $filename));
199 
200  $view->getRequest()->setControllerExtensionName('Backend');
201  return $view;
202  }
203 
207  protected function ‪getCache()
208  {
209  return GeneralUtility::makeInstance(CacheManager::class)->getCache('runtime');
210  }
211 }
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\render
‪string render(array $backendUser=null, int $size=32, bool $showIcon=false)
Definition: Avatar.php:49
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:25
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\$avatarProviders
‪AvatarProviderInterface[] $avatarProviders
Definition: Avatar.php:38
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar
Definition: Avatar.php:33
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\getCache
‪FrontendInterface getCache()
Definition: Avatar.php:206
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\getImgTag
‪string getImgTag(array $backendUser=null, $size=32)
Definition: Avatar.php:86
‪TYPO3\CMS\Core\Utility\PathUtility\getPublicResourceWebPath
‪static string getPublicResourceWebPath(string $resourcePath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:98
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface\get
‪mixed get($entryIdentifier)
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\validateSortAndInitiateAvatarProviders
‪validateSortAndInitiateAvatarProviders()
Definition: Avatar.php:135
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪TYPO3\CMS\Core\Service\DependencyOrderingService
Definition: DependencyOrderingService.php:32
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\getFluidTemplateObject
‪StandaloneView getFluidTemplateObject(string $filename=null)
Definition: Avatar.php:186
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Backend\Backend\Avatar
Definition: Avatar.php:16
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:31
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: Avatar.php:175
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Backend\Avatar\AvatarProviderInterface
Definition: AvatarProviderInterface.php:22
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\getImage
‪Image null getImage(array $backendUser, $size)
Definition: Avatar.php:119
‪TYPO3\CMS\Backend\Backend\Avatar\Image
Definition: Image.php:23
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface\set
‪set($entryIdentifier, $data, array $tags=[], $lifetime=null)
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50