05ec2ca72e6b2869947c3e3427ad103c5f6d6cca
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Backend / Avatar / DefaultAvatarProvider.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\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
19 use TYPO3\CMS\Core\Resource\ProcessedFile;
20 use TYPO3\CMS\Core\Resource\ResourceFactory;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23 /**
24 * Avatar Provider used for rendering avatars based on local files (based on FAL), stored in the be_users.avatar
25 * relation field with sys_file_reference.
26 */
27 class DefaultAvatarProvider implements AvatarProviderInterface
28 {
29 /**
30 * Return an Image object for rendering the avatar, based on a FAL-based file
31 *
32 * @param array $backendUser be_users record
33 * @param int $size
34 * @return Image|NULL
35 */
36 public function getImage(array $backendUser, $size)
37 {
38 $fileUid = $this->getAvatarFileUid($backendUser['uid']);
39
40 // Get file object
41 try {
42 $file = ResourceFactory::getInstance()->getFileObject($fileUid);
43 $processedImage = $file->process(
44 ProcessedFile::CONTEXT_IMAGECROPSCALEMASK,
45 ['width' => $size . 'c', 'height' => $size . 'c']
46 );
47
48 $image = GeneralUtility::makeInstance(
49 Image::class,
50 $processedImage->getPublicUrl(),
51 $processedImage->getProperty('width'),
52 $processedImage->getProperty('height')
53 );
54 } catch (FileDoesNotExistException $e) {
55 // No image found
56 $image = null;
57 }
58
59 return $image;
60 }
61
62 /**
63 * Get the sys_file UID of the avatar of the given backend user ID
64 *
65 * @param int $backendUserId the UID of the be_users record
66 * @return int the sys_file UID or 0 if none found
67 */
68 protected function getAvatarFileUid($backendUserId)
69 {
70 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file_reference');
71 $fileUid = $queryBuilder
72 ->select('uid_local')
73 ->from('sys_file_reference')
74 ->where(
75 $queryBuilder->expr()->eq(
76 'tablenames',
77 $queryBuilder->createNamedParameter('be_users', \PDO::PARAM_STR)
78 ),
79 $queryBuilder->expr()->eq(
80 'fieldname',
81 $queryBuilder->createNamedParameter('avatar', \PDO::PARAM_STR)
82 ),
83 $queryBuilder->expr()->eq(
84 'table_local',
85 $queryBuilder->createNamedParameter('sys_file', \PDO::PARAM_STR)
86 ),
87 $queryBuilder->expr()->eq(
88 'uid_foreign',
89 $queryBuilder->createNamedParameter((int)$backendUserId, \PDO::PARAM_INT)
90 )
91 )
92 ->execute()
93 ->fetchColumn();
94
95 return (int)$fileUid;
96 }
97 }