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