1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Zikula package. |
7
|
|
|
* |
8
|
|
|
* Copyright Zikula - https://ziku.la/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Zikula\UsersModule\Twig\Runtime; |
15
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
use function Symfony\Component\String\s; |
18
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
19
|
|
|
use Twig\Extension\RuntimeExtensionInterface; |
20
|
|
|
use Zikula\UsersModule\Collector\ProfileModuleCollector; |
21
|
|
|
use Zikula\UsersModule\Entity\RepositoryInterface\UserRepositoryInterface; |
22
|
|
|
use Zikula\UsersModule\Entity\UserEntity; |
23
|
|
|
|
24
|
|
|
class ProfileRuntime implements RuntimeExtensionInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var UserRepositoryInterface |
28
|
|
|
*/ |
29
|
|
|
private $userRepository; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ProfileModuleCollector |
33
|
|
|
*/ |
34
|
|
|
private $profileModuleCollector; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var TranslatorInterface |
38
|
|
|
*/ |
39
|
|
|
private $translator; |
40
|
|
|
|
41
|
|
|
public function __construct( |
42
|
|
|
UserRepositoryInterface $userRepository, |
43
|
|
|
ProfileModuleCollector $profileModuleCollector, |
44
|
|
|
TranslatorInterface $translator |
45
|
|
|
) { |
46
|
|
|
$this->userRepository = $userRepository; |
47
|
|
|
$this->profileModuleCollector = $profileModuleCollector; |
48
|
|
|
$this->translator = $translator; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Displays the avatar of a given user. |
53
|
|
|
* |
54
|
|
|
* @param int|string $userId The user's id or name |
55
|
|
|
*/ |
56
|
|
|
public function getUserAvatar($userId = 0, array $parameters = []): string |
57
|
|
|
{ |
58
|
|
|
return $this->profileModuleCollector->getSelected()->getAvatar($userId, $parameters); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Create a link to a users profile from the UID. |
63
|
|
|
* |
64
|
|
|
* Examples |
65
|
|
|
* |
66
|
|
|
* Simple version, shows the username |
67
|
|
|
* {{ uid|profileLinkByUserId() }} |
68
|
|
|
* Simple version, shows username, using class="classname" |
69
|
|
|
* {{ uid|profileLinkByUserId(class='classname') }} |
70
|
|
|
* Using profile.gif instead of username, no class |
71
|
|
|
* {{ uid|profileLinkByUserId(image='images/profile.gif') }} |
72
|
|
|
*/ |
73
|
|
|
public function profileLinkByUserId( |
74
|
|
|
int $userId, |
75
|
|
|
string $class = '', |
76
|
|
|
string $image = '', |
77
|
|
|
int $maxLength = 0, |
78
|
|
|
string $title = '' |
79
|
|
|
): string { |
80
|
|
|
if (empty($userId) || 1 > $userId) { |
81
|
|
|
return (string) $userId; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->determineProfileLink($userId, null, $class, $image, $maxLength, $title); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Create a link to a users profile from the username. |
89
|
|
|
* |
90
|
|
|
* Examples |
91
|
|
|
* |
92
|
|
|
* Simple version, shows the username |
93
|
|
|
* {{ username|profileLinkByUserName() }} |
94
|
|
|
* Simple version, shows username, using class="classname" |
95
|
|
|
* {{ username|profileLinkByUserName(class='classname') }} |
96
|
|
|
* Using profile.gif instead of username, no class |
97
|
|
|
* {{ username|profileLinkByUserName('image'='images/profile.gif') }} |
98
|
|
|
*/ |
99
|
|
|
public function profileLinkByUserName( |
100
|
|
|
string $userName, |
101
|
|
|
string $class = '', |
102
|
|
|
string $image = '', |
103
|
|
|
int $maxLength = 0, |
104
|
|
|
string $title = '' |
105
|
|
|
): string { |
106
|
|
|
if (empty($userName)) { |
107
|
|
|
return $userName; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this->determineProfileLink(null, $userName, $class, $image, $maxLength, $title); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Internal function used by profileLinkByUserId() and profileLinkByUserName(). |
115
|
|
|
*/ |
116
|
|
|
private function determineProfileLink( |
117
|
|
|
int $userId = null, |
118
|
|
|
string $userName = null, |
119
|
|
|
string $class = '', |
120
|
|
|
string $imagePath = '', |
121
|
|
|
int $maxLength = 0, |
122
|
|
|
string $title = '' |
123
|
|
|
): string { |
124
|
|
|
if (!isset($userId) && !isset($userName)) { |
125
|
|
|
throw new InvalidArgumentException(); |
126
|
|
|
} |
127
|
|
|
/** @var UserEntity $user */ |
128
|
|
|
if (null !== $userId) { |
129
|
|
|
$user = $this->userRepository->find($userId); |
130
|
|
|
} else { |
131
|
|
|
$user = $this->userRepository->findOneBy(['uname' => $userName]); |
132
|
|
|
} |
133
|
|
|
if (!$user) { |
134
|
|
|
return $userId . $userName; // one or the other is empty |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$userDisplayName = $this->profileModuleCollector->getSelected()->getDisplayName($user->getUid()); |
138
|
|
|
if (!$userDisplayName) { |
139
|
|
|
$userDisplayName = $user->getUname(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$class = !empty($class) ? ' class="' . htmlspecialchars($class, ENT_QUOTES) . '"' : ''; |
143
|
|
|
|
144
|
|
|
if (!empty($imagePath)) { |
145
|
|
|
$show = '<img src="' . htmlspecialchars($imagePath, ENT_QUOTES) . '" alt="' . htmlspecialchars($userDisplayName, ENT_QUOTES) . '" />'; |
146
|
|
|
} elseif (0 < $maxLength) { |
147
|
|
|
// truncate the user name to $maxLength chars |
148
|
|
|
$length = mb_strlen($userDisplayName); |
149
|
|
|
$truncEnd = ($maxLength > $length) ? $length : $maxLength; |
150
|
|
|
$show = htmlspecialchars(s($userDisplayName)->slice(0, $truncEnd)->toString(), ENT_QUOTES); |
151
|
|
|
} else { |
152
|
|
|
$show = htmlspecialchars($userDisplayName, ENT_QUOTES); |
153
|
|
|
} |
154
|
|
|
$href = $this->profileModuleCollector->getSelected()->getProfileUrl($user->getUid()); |
155
|
|
|
if ('#' === $href) { |
156
|
|
|
return $userDisplayName; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if (empty($title)) { |
160
|
|
|
$title = $this->translator->trans('Profile') . ': ' . $userDisplayName; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return '<a' . $class . ' title="' . htmlspecialchars($title, ENT_QUOTES) . '" href="' . $href . '">' . $show . '</a>'; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|