|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Bone\User\View\Helper; |
|
4
|
|
|
|
|
5
|
|
|
use Del\Image; |
|
6
|
|
|
use Del\Service\UserService; |
|
7
|
|
|
use Del\SessionManager; |
|
8
|
|
|
use League\Plates\Engine; |
|
9
|
|
|
use League\Plates\Extension\ExtensionInterface; |
|
10
|
|
|
use Laminas\I18n\Translator\Translator; |
|
11
|
|
|
use League\Plates\Template\Template; |
|
12
|
|
|
|
|
13
|
|
|
class LoginWidget implements ExtensionInterface |
|
14
|
|
|
{ |
|
15
|
|
|
public ?Template $template = null; |
|
16
|
|
|
|
|
17
|
1 |
|
public function __construct( |
|
18
|
|
|
private UserService $userService, |
|
19
|
|
|
private Translator $translator, |
|
20
|
|
|
private SessionManager $sessionManager, |
|
21
|
|
|
private string $uploadFolder |
|
22
|
|
|
) { |
|
23
|
1 |
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param Engine $engine |
|
27
|
|
|
*/ |
|
28
|
|
|
public function register(Engine $engine) |
|
29
|
|
|
{ |
|
30
|
|
|
$engine->registerFunction('loginWidget', [$this, 'loginWidget']); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return string |
|
35
|
|
|
* @throws Image\Exception\NothingLoadedException |
|
36
|
|
|
*/ |
|
37
|
|
|
public function loginWidget(): string |
|
38
|
|
|
{ |
|
39
|
|
|
$locale = $this->translator->getLocale(); |
|
40
|
|
|
|
|
41
|
|
|
if ($id = $this->sessionManager->get('user')) { |
|
42
|
|
|
$user = $this->userService->findUserById($id); |
|
43
|
|
|
$person = $user->getPerson(); |
|
44
|
|
|
$html = '<li class="nav-item dropdown">'; |
|
45
|
|
|
|
|
46
|
|
|
if ($person->getImage()) { |
|
47
|
|
|
$image = new Image($this->uploadFolder . $person->getImage()); |
|
48
|
|
|
$html .= '<a id="user-dropdown" href="#" class="nav-link dropdown-toggle avatar" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"> |
|
49
|
|
|
<img id="user-avatar" src="' . $image->outputBase64Src() . '" class="img-rounded img-circle" />'; |
|
50
|
|
|
$html .= $person->getAka() ?: $user->getEmail() . '</a>'; |
|
51
|
|
|
} else { |
|
52
|
|
|
$html .= '<a id="user-dropdown" href="#" class="nav-link dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">' . $user->getEmail() . '</a>'; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$html .= '<div class="dropdown-menu login-widget" aria-labelledby="user-dropdown"> |
|
56
|
|
|
<a class="dropdown-item" href="/' . $locale . '/user/change-email">Change Email</a> |
|
57
|
|
|
<a class="dropdown-item" href="/' . $locale . '/user/change-password">Change Password</a> |
|
58
|
|
|
<a class="dropdown-item" href="/' . $locale . '/user/edit-profile">Edit Profile</a> |
|
59
|
|
|
<a class="dropdown-item" href="/' . $locale . '/user/logout">Logout</a> |
|
60
|
|
|
</div> |
|
61
|
|
|
</li>'; |
|
62
|
|
|
|
|
63
|
|
|
return $html; |
|
64
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return '<li class="nav-item"><a class="nav-link" href="/' . $locale . '/user/login">Login</a></li>'; |
|
68
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|