[CLEANUP] Streamline Avatar-related code in EXT:backend
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / ViewHelpers / AvatarViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Backend\ViewHelpers;
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\Backend\Backend\Avatar\Avatar;
18 use TYPO3\CMS\Core\Database\ConnectionPool;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
21 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
23
24 /**
25 * Render the avatar img tag for a given backend user
26 */
27 class AvatarViewHelper extends AbstractViewHelper
28 {
29 use CompileWithRenderStatic;
30
31 /**
32 * As this ViewHelper renders HTML, the output must not be escaped.
33 *
34 * @var bool
35 */
36 protected $escapeOutput = false;
37
38 /**
39 * Initializes the arguments
40 */
41 public function initializeArguments()
42 {
43 parent::initializeArguments();
44 $this->registerArgument('backendUser', 'int', 'uid of the backend user', false, 0);
45 $this->registerArgument('size', 'int', 'width and height of the image', false, 32);
46 $this->registerArgument('showIcon', 'bool', 'show the record icon as well', false, false);
47 }
48
49 /**
50 * Resolve user avatar from a given backend user id.
51 *
52 * @param array $arguments
53 * @param \Closure $renderChildrenClosure
54 * @param RenderingContextInterface $renderingContext
55 * @return string
56 */
57 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
58 {
59 if ($arguments['backendUser'] > 0) {
60 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_users');
61 $queryBuilder->getRestrictions()->removeAll();
62 $backendUser = $queryBuilder
63 ->select('*')
64 ->from('be_users')
65 ->where(
66 $queryBuilder->expr()->eq(
67 'uid',
68 $queryBuilder->createNamedParameter($arguments['backendUser'], \PDO::PARAM_INT)
69 )
70 )
71 ->execute()
72 ->fetch();
73 } else {
74 $backendUser = $GLOBALS['BE_USER']->user;
75 }
76 if ($backendUser === false) {
77 // no BE user can be retrieved from DB, probably deleted
78 return '';
79 }
80 $avatar = GeneralUtility::makeInstance(Avatar::class);
81 return $avatar->render($backendUser, $arguments['size'], $arguments['showIcon']);
82 }
83 }