TYPO3 CMS  TYPO3_8-7
Avatar.php
Go to the documentation of this file.
1 <?php
2 
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
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  ExtensionManagementUtility::siteRelPath('core') . 'Resources/Public/Icons/T3Icons/avatar/avatar-default.svg',
102  $size,
103  $size
104  );
105  }
106  $imageTag = '<img src="' . htmlspecialchars($avatarImage->getUrl(true)) . '" ' .
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 
137  {
138  if (
139  empty($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'])
140  || !is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'])
141  ) {
142  return;
143  }
144  $providers = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'];
145  foreach ($providers as $identifier => $configuration) {
146  if (empty($configuration) || !is_array($configuration)) {
147  throw new \RuntimeException(
148  'Missing configuration for avatar provider "' . $identifier . '".',
149  1439317801
150  );
151  }
152  if (!is_string($configuration['provider']) || empty($configuration['provider']) || !class_exists($configuration['provider']) || !is_subclass_of(
153  $configuration['provider'],
154  AvatarProviderInterface::class
155  )) {
156  throw new \RuntimeException(
157  'The avatar provider "' . $identifier . '" defines an invalid provider. Ensure the class exists and implements the "' . AvatarProviderInterface::class . '".',
158  1439317802
159  );
160  }
161  }
162 
163  $orderedProviders = GeneralUtility::makeInstance(DependencyOrderingService::class)->orderByDependencies($providers);
164 
165  // Initializes providers
166  foreach ($orderedProviders as $configuration) {
167  $this->avatarProviders[] = GeneralUtility::makeInstance($configuration['provider']);
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('cache_runtime');
210  }
211 }
getImgTag(array $backendUser=null, $size=32)
Definition: Avatar.php:87
render(array $backendUser=null, int $size=32, bool $showIcon=false)
Definition: Avatar.php:50
static getFileAbsFileName($filename, $_=null, $_2=null)
static makeInstance($className,... $constructorArguments)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
getImage(array $backendUser, $size)
Definition: Avatar.php:120
getFluidTemplateObject(string $filename=null)
Definition: Avatar.php:187