summaryrefslogtreecommitdiffstats
path: root/library/Cube/Web/ActionLinks.php
blob: 4b84fac49e0ebcc083de932250396d35b18b6bf8 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php

// Icinga Web 2 Cube Module | (c) 2016 Icinga GmbH | GPLv2

namespace Icinga\Module\Cube\Web;

use Exception;
use Icinga\Application\Hook;
use Icinga\Module\Cube\Cube;
use Icinga\Module\Cube\Hook\ActionsHook;
use Icinga\Web\View;

/**
 * ActionLink
 *
 * ActionsHook implementations return instances of this class
 *
 * @package Icinga\Module\Cube\Web
 */
class ActionLinks
{
    /** @var ActionLink[] */
    protected $links = array();

    /**
     * Get all links for all Hook implementations
     *
     * This is what the Cube calls when rendering details
     *
     * @param Cube $cube
     * @param View $view
     *
     * @return string
     */
    public static function renderAll(Cube $cube, View $view)
    {
        $html = array();

        /** @var ActionsHook $hook */
        foreach (Hook::all('Cube/Actions') as $hook) {
            try {
                $hook->prepareActionLinks($cube, $view);
            } catch (Exception $e) {
                $html[] = self::renderErrorItem($e, $view);
            }

            foreach ($hook->getActionLinks()->getLinks() as $link) {
                $html[] = '<li>' . $link->render($view) . '</li>';
            }
        }

        if (empty($html)) {
            $html[] = self::renderErrorItem(
                $view->translate('No action links have been provided for this cube'),
                $view
            );
        }

        return implode("\n", $html) . "\n";
    }

    /**
     * @param Exception|string $error
     * @param View $view
     * @return string
     */
    private static function renderErrorItem($error, View $view)
    {
        if ($error instanceof Exception) {
            $error = $error->getMessage();
        }
        return '<li class="error">' . $view->escape($error) . '</li>';
    }

    /**
     * Add an ActionLink to this set of actions
     *
     * @param ActionLink $link
     * @return $this
     */
    public function add(ActionLink $link)
    {
        $this->links[] = $link;
        return $this;
    }

    /**
     * @return ActionLink[]
     */
    public function getLinks()
    {
        return $this->links;
    }

    /**
     * @param View $view
     *
     * @return string
     */
    public function render(View $view)
    {
        $links = $this->getLinks();
        if (empty($links)) {
            return '';
        }

        $html = '<ul class="action-links">';
        foreach ($links as $link) {
            $html .= $link->render($view);
        }
        $html .= '</ul>';

        return $html;
    }
}