[TASK] Follow-up only display active Avatar files
[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\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Backend\Utility\IconUtility;
19 use TYPO3\CMS\Core\Resource\ResourceFactory;
20 use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
21 use TYPO3\CMS\Core\Resource\ProcessedFile;
22
23 /**
24 * Default Avatar class
25 */
26 class Avatar {
27
28 /**
29 * @var array
30 */
31 protected $defaultConfiguration = array();
32
33 /**
34 * Render avatar tag
35 *
36 * @param array $backendUser
37 * @param int $size width and height of the image
38 * @param bool $showIcon show the record icon
39 * @return string
40 */
41 public function render(array $backendUser = NULL, $size = 32, $showIcon = FALSE) {
42 if (!is_array($backendUser)) {
43 $backendUser = $this->getBackendUser()->user;
44 }
45
46 $fileUid = $this->getAvatarFileUid($backendUser['uid']);
47
48 // Get file object
49 try {
50 $file = ResourceFactory::getInstance()->getFileObject($fileUid);
51 $processedImage = $file->process(
52 ProcessedFile::CONTEXT_IMAGECROPSCALEMASK,
53 array('width' => $size . 'c', 'height' => $size . 'c')
54 );
55 $imageUri = $processedImage->getPublicUrl(TRUE);
56
57 // Image
58 $image = '<img src="' . htmlspecialchars($imageUri) . '"' .
59 'width="' . $processedImage->getProperty('width') . '" ' .
60 'height="' . $processedImage->getProperty('height') . '">';
61 } catch (FileDoesNotExistException $e) {
62 // don't show an image
63 $image = '';
64 }
65
66 // Icon
67 $icon = '';
68 if ($showIcon) {
69 $icon = '<span class="avatar-icon">' . IconUtility::getSpriteIconForRecord('be_users', $backendUser) . '</span>';
70 }
71
72 return '<span class="avatar"><span class="avatar-image">' . $image . '</span>' . $icon . '</span>';
73 }
74
75 /**
76 * Get Avatar fileUid
77 *
78 * @param int $beUserId
79 * @return int
80 */
81 protected function getAvatarFileUid($beUserId) {
82 $file = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
83 'uid_local',
84 'sys_file_reference',
85 'tablenames = \'be_users\' AND fieldname = \'avatar\' AND ' .
86 'table_local = \'sys_file\' AND uid_foreign = ' . (int)$beUserId .
87 BackendUtility::BEenableFields('sys_file_reference') . BackendUtility::deleteClause('sys_file_reference')
88 );
89 return $file ? $file['uid_local'] : 0;
90 }
91
92 /**
93 * Returns the current BE user.
94 *
95 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
96 */
97 protected function getBackendUser() {
98 return $GLOBALS['BE_USER'];
99 }
100
101 /**
102 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
103 */
104 protected function getDatabaseConnection() {
105 return $GLOBALS['TYPO3_DB'];
106 }
107 }