summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Web/Widget/ApplicationStateMessages.php
blob: 99d3bb2aeb61e0ae4c6adbd27f96d868fd0a825b (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
<?php
/* Icinga Web 2 | (c) 2018 Icinga Development Team | GPLv2+ */

namespace Icinga\Web\Widget;

use Icinga\Application\Config;
use Icinga\Application\Hook\ApplicationStateHook;
use Icinga\Authentication\Auth;
use Icinga\Forms\AcknowledgeApplicationStateMessageForm;
use Icinga\Web\ApplicationStateCookie;
use Icinga\Web\Helper\Markdown;

/**
 * Render application state messages
 */
class ApplicationStateMessages extends AbstractWidget
{
    protected function getMessages()
    {
        $cookie = new ApplicationStateCookie();

        $acked = array_flip($cookie->getAcknowledgedMessages());
        $messages = ApplicationStateHook::getAllMessages();

        $active = array_diff_key($messages, $acked);

        return $active;
    }

    public function render()
    {
        $enabled = Auth::getInstance()
            ->getUser()
            ->getPreferences()
            ->getValue('icingaweb', 'show_application_state_messages', 'system');

        if ($enabled === 'system') {
            $enabled = Config::app()->get('global', 'show_application_state_messages', true);
        }

        if (! (bool) $enabled) {
            return '<div hidden></div>';
        }

        $active = $this->getMessages();

        if (empty($active)) {
            // Force container update on XHR
            return '<div hidden></div>';
        }

        $html = '<div>';

        reset($active);

        $id = key($active);
        $spec = current($active);
        $message = array_pop($spec); // We don't use state and timestamp here


        $ackForm = new AcknowledgeApplicationStateMessageForm();
        $ackForm->populate(['id' => $id]);

        $html .= '<section class="markdown">';
        $html .= Markdown::text($message);
        $html .= '</section>';

        $html .= $ackForm;

        $html .= '</div>';

        return $html;
    }
}