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