TYPO3 CMS  TYPO3_7-6
Avatar.php
Go to the documentation of this file.
1 <?php
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 
22 
26 class Avatar
27 {
33  protected $avatarProviders = [];
34 
38  public function __construct()
39  {
41  }
42 
51  public function render(array $backendUser = null, $size = 32, $showIcon = false)
52  {
53  if (!is_array($backendUser)) {
54  $backendUser = $this->getBackendUser()->user;
55  }
56 
57  // Icon
58  $icon = '';
59  if ($showIcon) {
60  $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
61  $icon = '<span class="avatar-icon">' . $iconFactory->getIconForRecord('be_users', $backendUser, Icon::SIZE_SMALL)->render() . '</span>';
62  }
63 
64  $image = $this->getImgTag($backendUser, $size);
65 
66  return '<span class="avatar"><span class="avatar-image">' . $image . '</span>' . $icon . '</span>';
67  }
68 
76  public function getImgTag(array $backendUser = null, $size = 32)
77  {
78  if (!is_array($backendUser)) {
79  $backendUser = $this->getBackendUser()->user;
80  }
81 
82  $avatarImage = $this->getImage($backendUser, $size);
83 
84  if (!$avatarImage) {
85  $avatarImage = GeneralUtility::makeInstance(
86  Image::class,
87  ExtensionManagementUtility::siteRelPath('core') . 'Resources/Public/Icons/T3Icons/avatar/avatar-default.svg',
88  $size,
89  $size
90  );
91  }
92  $imageTag = '<img src="' . htmlspecialchars($avatarImage->getUrl(true)) . '" ' .
93  'width="' . (int)$avatarImage->getWidth() . '" ' .
94  'height="' . (int)$avatarImage->getHeight() . '" />';
95 
96  return $imageTag;
97  }
98 
106  public function getImage(array $backendUser, $size)
107  {
108  foreach ($this->avatarProviders as $provider) {
109  $avatarImage = $provider->getImage($backendUser, $size);
110  if (!empty($avatarImage)) {
111  return $avatarImage;
112  }
113  }
114  return null;
115  }
116 
124  {
125  if (
126  empty($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'])
127  || !is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'])
128  ) {
129  return;
130  }
131  $providers = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'];
132  foreach ($providers as $identifier => $configuration) {
133  if (empty($configuration) || !is_array($configuration)) {
134  throw new \RuntimeException('Missing configuration for avatar provider "' . $identifier . '".', 1439317801);
135  }
136  if (!is_string($configuration['provider']) || empty($configuration['provider']) || !class_exists($configuration['provider']) || !is_subclass_of($configuration['provider'], AvatarProviderInterface::class)) {
137  throw new \RuntimeException('The avatar provider "' . $identifier . '" defines an invalid provider. Ensure the class exists and implements the "' . AvatarProviderInterface::class . '".', 1439317802);
138  }
139  }
140 
141  $orderedProviders = GeneralUtility::makeInstance(DependencyOrderingService::class)->orderByDependencies($providers);
142 
143  // Initiate providers
144  foreach ($orderedProviders as $configuration) {
145  $this->avatarProviders[] = GeneralUtility::makeInstance($configuration['provider']);
146  }
147  }
148 
154  protected function getBackendUser()
155  {
156  return $GLOBALS['BE_USER'];
157  }
158 }
getImgTag(array $backendUser=null, $size=32)
Definition: Avatar.php:76
render(array $backendUser=null, $size=32, $showIcon=false)
Definition: Avatar.php:51
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
getImage(array $backendUser, $size)
Definition: Avatar.php:106