TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
Avatar.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Backend\Backend\Avatar;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
23 
27 class Avatar
28 {
34  protected $avatarProviders = [];
35 
39  public function __construct()
40  {
42  $this->view = $this->getFluidTemplateObject();
43  }
44 
48  protected $view;
49 
58  public function render(array $backendUser = null, int $size = 32, bool $showIcon = false)
59  {
60  if (!is_array($backendUser)) {
61  $backendUser = $this->getBackendUser()->user;
62  }
63 
64  // Icon
65  $icon = '';
66  if ($showIcon) {
67  $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
68  $icon = $iconFactory->getIconForRecord('be_users', $backendUser, Icon::SIZE_SMALL)->render();
69  }
70 
71  $image = $this->getImgTag($backendUser, $size);
72 
73  $this->view->assignMultiple([
74  'image' => $image,
75  'icon' => $icon
76  ]
77  );
78 
79  return $this->view->render();
80  }
81 
89  public function getImgTag(array $backendUser = null, $size = 32)
90  {
91  if (!is_array($backendUser)) {
92  $backendUser = $this->getBackendUser()->user;
93  }
94 
95  $avatarImage = false;
96  if ($backendUser !== null) {
97  $avatarImage = $this->getImage($backendUser, $size);
98  }
99 
100  if (!$avatarImage) {
101  $avatarImage = GeneralUtility::makeInstance(
102  Image::class,
103  ExtensionManagementUtility::siteRelPath('core') . 'Resources/Public/Icons/T3Icons/avatar/avatar-default.svg',
104  $size,
105  $size
106  );
107  }
108  $imageTag = '<img src="' . htmlspecialchars($avatarImage->getUrl(true)) . '" ' .
109  'width="' . (int)$avatarImage->getWidth() . '" ' .
110  'height="' . (int)$avatarImage->getHeight() . '" />';
111 
112  return $imageTag;
113  }
114 
122  public function getImage(array $backendUser, $size)
123  {
124  foreach ($this->avatarProviders as $provider) {
125  $avatarImage = $provider->getImage($backendUser, $size);
126  if (!empty($avatarImage)) {
127  return $avatarImage;
128  }
129  }
130  return null;
131  }
132 
140  {
141  if (
142  empty($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'])
143  || !is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'])
144  ) {
145  return;
146  }
147  $providers = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'];
148  foreach ($providers as $identifier => $configuration) {
149  if (empty($configuration) || !is_array($configuration)) {
150  throw new \RuntimeException('Missing configuration for avatar provider "' . $identifier . '".', 1439317801);
151  }
152  if (!is_string($configuration['provider']) || empty($configuration['provider']) || !class_exists($configuration['provider']) || !is_subclass_of($configuration['provider'], AvatarProviderInterface::class)) {
153  throw new \RuntimeException('The avatar provider "' . $identifier . '" defines an invalid provider. Ensure the class exists and implements the "' . AvatarProviderInterface::class . '".', 1439317802);
154  }
155  }
156 
157  $orderedProviders = GeneralUtility::makeInstance(DependencyOrderingService::class)->orderByDependencies($providers);
158 
159  // Initiate providers
160  foreach ($orderedProviders as $configuration) {
161  $this->avatarProviders[] = GeneralUtility::makeInstance($configuration['provider']);
162  }
163  }
164 
170  protected function getBackendUser()
171  {
172  return $GLOBALS['BE_USER'];
173  }
174 
182  protected function getFluidTemplateObject(string $filename = null):StandaloneView
183  {
185  $view = GeneralUtility::makeInstance(StandaloneView::class);
186  $view->setLayoutRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Layouts')]);
187  $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
188  $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
189 
190  if (is_null($filename)) {
191  $filename = 'Main.html';
192  }
193 
194  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/Avatar/' . $filename));
195 
196  $view->getRequest()->setControllerExtensionName('Backend');
197  return $view;
198  }
199 }
render(array $backendUser=null, int $size=32, bool $showIcon=false)
Definition: Avatar.php:58
getImage(array $backendUser, $size)
Definition: Avatar.php:122
getImgTag(array $backendUser=null, $size=32)
Definition: Avatar.php:89
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static makeInstance($className,...$constructorArguments)
static getFileAbsFileName($filename, $_=null, $_2=null)