TYPO3  7.6
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 
21 
25 class Avatar
26 {
32  protected $avatarProviders = [];
33 
37  public function __construct()
38  {
40  }
41 
50  public function render(array $backendUser = null, $size = 32, $showIcon = false)
51  {
52  if (!is_array($backendUser)) {
53  $backendUser = $this->getBackendUser()->user;
54  }
55 
56  // Icon
57  $icon = '';
58  if ($showIcon) {
59  $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
60  $icon = '<span class="avatar-icon">' . $iconFactory->getIconForRecord('be_users', $backendUser, Icon::SIZE_SMALL)->render() . '</span>';
61  }
62 
63  $image = $this->getImgTag($backendUser, $size);
64 
65  return '<span class="avatar"><span class="avatar-image">' . $image . '</span>' . $icon . '</span>';
66  }
67 
75  public function getImgTag(array $backendUser = null, $size = 32)
76  {
77  if (!is_array($backendUser)) {
78  $backendUser = $this->getBackendUser()->user;
79  }
80 
81  $imageTag = '';
82  $avatarImage = $this->getImage($backendUser, $size);
83 
84  if ($avatarImage) {
85  $imageTag = '<img src="' . htmlspecialchars($avatarImage->getUrl(true)) . '" ' .
86  'width="' . (int)$avatarImage->getWidth() . '" ' .
87  'height="' . (int)$avatarImage->getHeight() . '" />';
88  }
89 
90  return $imageTag;
91  }
92 
100  public function getImage(array $backendUser, $size)
101  {
102  foreach ($this->avatarProviders as $provider) {
103  $avatarImage = $provider->getImage($backendUser, $size);
104  if (!empty($avatarImage)) {
105  return $avatarImage;
106  }
107  }
108  return null;
109  }
110 
118  {
119  if (
120  empty($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'])
121  || !is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'])
122  ) {
123  return;
124  }
125  $providers = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'];
126  foreach ($providers as $identifier => $configuration) {
127  if (empty($configuration) || !is_array($configuration)) {
128  throw new \RuntimeException('Missing configuration for avatar provider "' . $identifier . '".', 1439317801);
129  }
130  if (!is_string($configuration['provider']) || empty($configuration['provider']) || !class_exists($configuration['provider']) || !is_subclass_of($configuration['provider'], AvatarProviderInterface::class)) {
131  throw new \RuntimeException('The avatar provider "' . $identifier . '" defines an invalid provider. Ensure the class exists and implements the "' . AvatarProviderInterface::class . '".', 1439317802);
132  }
133  }
134 
135  $orderedProviders = GeneralUtility::makeInstance(DependencyOrderingService::class)->orderByDependencies($providers);
136 
137  // Initiate providers
138  foreach ($orderedProviders as $configuration) {
139  $this->avatarProviders[] = GeneralUtility::makeInstance($configuration['provider']);
140  }
141  }
142 
148  protected function getBackendUser()
149  {
150  return $GLOBALS['BE_USER'];
151  }
152 }