[TASK] Fluidification of Avatar.php
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Backend / Avatar / Avatar.php
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
17 use TYPO3\CMS\Core\Imaging\Icon;
18 use TYPO3\CMS\Core\Imaging\IconFactory;
19 use TYPO3\CMS\Core\Service\DependencyOrderingService;
20 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Fluid\View\StandaloneView;
23
24 /**
25 * Avatar renderer class
26 */
27 class Avatar
28 {
29 /**
30 * Array of sorted and initiated avatar providers
31 *
32 * @var AvatarProviderInterface[]
33 */
34 protected $avatarProviders = [];
35
36 /**
37 * Construct
38 */
39 public function __construct()
40 {
41 $this->validateSortAndInitiateAvatarProviders();
42 $this->view = $this->getFluidTemplateObject();
43 }
44
45 /**
46 * @var StandaloneView
47 */
48 protected $view;
49
50 /**
51 * Render avatar tag
52 *
53 * @param array $backendUser be_users record
54 * @param int $size width and height of the image
55 * @param bool $showIcon show the record icon
56 * @return string
57 */
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
82 /**
83 * Get avatar img tag
84 *
85 * @param array $backendUser be_users record
86 * @param int $size
87 * @return string
88 */
89 public function getImgTag(array $backendUser = null, $size = 32)
90 {
91 if (!is_array($backendUser)) {
92 $backendUser = $this->getBackendUser()->user;
93 }
94
95 $avatarImage = $this->getImage($backendUser, $size);
96
97 if (!$avatarImage) {
98 $avatarImage = GeneralUtility::makeInstance(
99 Image::class,
100 ExtensionManagementUtility::siteRelPath('core') . 'Resources/Public/Icons/T3Icons/avatar/avatar-default.svg',
101 $size,
102 $size
103 );
104 }
105 $imageTag = '<img src="' . htmlspecialchars($avatarImage->getUrl(true)) . '" ' .
106 'width="' . (int)$avatarImage->getWidth() . '" ' .
107 'height="' . (int)$avatarImage->getHeight() . '" />';
108
109 return $imageTag;
110 }
111
112 /**
113 * Get Image from first provider that returns one
114 *
115 * @param array $backendUser be_users record
116 * @param int $size
117 * @return Image|NULL
118 */
119 public function getImage(array $backendUser, $size)
120 {
121 foreach ($this->avatarProviders as $provider) {
122 $avatarImage = $provider->getImage($backendUser, $size);
123 if (!empty($avatarImage)) {
124 return $avatarImage;
125 }
126 }
127 return null;
128 }
129
130 /**
131 * Validates the registered avatar providers
132 *
133 * @return void
134 * @throws \RuntimeException
135 */
136 protected function validateSortAndInitiateAvatarProviders()
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('Missing configuration for avatar provider "' . $identifier . '".', 1439317801);
148 }
149 if (!is_string($configuration['provider']) || empty($configuration['provider']) || !class_exists($configuration['provider']) || !is_subclass_of($configuration['provider'], AvatarProviderInterface::class)) {
150 throw new \RuntimeException('The avatar provider "' . $identifier . '" defines an invalid provider. Ensure the class exists and implements the "' . AvatarProviderInterface::class . '".', 1439317802);
151 }
152 }
153
154 $orderedProviders = GeneralUtility::makeInstance(DependencyOrderingService::class)->orderByDependencies($providers);
155
156 // Initiate providers
157 foreach ($orderedProviders as $configuration) {
158 $this->avatarProviders[] = GeneralUtility::makeInstance($configuration['provider']);
159 }
160 }
161
162 /**
163 * Returns the current BE user.
164 *
165 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
166 */
167 protected function getBackendUser()
168 {
169 return $GLOBALS['BE_USER'];
170 }
171
172 /**
173 * Returns a new standalone view, shorthand function
174 *
175 * @param string $filename Which templateFile should be used.
176 *
177 * @return StandaloneView
178 */
179 protected function getFluidTemplateObject(string $filename = null):StandaloneView
180 {
181 /** @var StandaloneView $view */
182 $view = GeneralUtility::makeInstance(StandaloneView::class);
183 $view->setLayoutRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Layouts')]);
184 $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
185 $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
186
187 if (is_null($filename)) {
188 $filename = 'Main.html';
189 }
190
191 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/Avatar/' . $filename));
192
193 $view->getRequest()->setControllerExtensionName('Backend');
194 return $view;
195 }
196 }