aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/AvatarMenu.php
blob: 91bb865765c3ef5da4834bb61ffe3122a3b03408 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
 * This class represents the a more flexible menu used to group actions.
 *
 * @author  Timo Hartge <hartge@data-quest.de>
 * @license GPL2 or any later version
 * @since   Stud.IP 4.0
 */
final class AvatarMenu extends ActionMenu
{
    /**
     * Creates the avatar menu for a specific user
     */
    public static function forUser(User $user): self
    {
        $menu = self::get();
        $menu->addCSSClass('avatar-menu');
        $menu->addAttribute('data-action-menu-reposition', 'false');
        $menu->setContext($user->getFullName());
        $menu->setTitle(_('Profilmenü'));
        $menu->setImage(
            Avatar::getAvatar($user->id)->getImageTag(),
            ['id' => 'header_avatar_image_link']
        );

        return $menu;
    }

    /**
     * Adds the menu items from a given navigation path
     */
    public function withNavigation(string $path): self
    {
        foreach (Navigation::getItem($path) as $subpath => $subnav) {
            if ($subnav->getRenderAsButton()) {
                $this->addButton(
                    $subpath,
                    $subnav->getTitle(),
                    $subnav->getImage(),
                    array_merge(
                        $subnav->getLinkAttributes(),
                        ['formaction' => URLHelper::getURL($subnav->getURL(), [], true)]
                    )
                );
            } else {
                $this->addLink(
                    URLHelper::getURL($subnav->getURL(), [], true),
                    $subnav->getTitle(),
                    $subnav->getImage(),
                    $subnav->getLinkAttributes()
                );
            }
        }

        return $this;
    }

    /**
     * Ensures that the menu is always rendered as a menu.
     */
    #[Override]
    public function getRenderingMode(): string
    {
        return self::RENDERING_MODE_MENU;
    }

    /**
     * Sets the image of the menu
     */
    public function setImage(string $image_html, array $image_attributes = []): self
    {
        $this->image = $image_html;
        $this->image_attributes = $image_attributes;

        return $this;
    }
}