summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Web/Form/Decorator/FormNotifications.php
blob: 46734dfc66121d8d2d121c457eaf4100c7053ac9 (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
116
117
118
119
120
121
122
123
124
125
<?php
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */

namespace Icinga\Web\Form\Decorator;

use Icinga\Application\Icinga;
use Icinga\Exception\ProgrammingError;
use Icinga\Web\Form;
use Zend_Form_Decorator_Abstract;

/**
 * Decorator to add a list of notifications at the top or bottom of a form
 */
class FormNotifications extends Zend_Form_Decorator_Abstract
{
    /**
     * Render form notifications
     *
     * @param   string      $content    The html rendered so far
     *
     * @return  string                  The updated html
     */
    public function render($content = '')
    {
        $form = $this->getElement();
        if (! $form instanceof Form) {
            return $content;
        }

        $view = $form->getView();
        if ($view === null) {
            return $content;
        }

        $notifications = $this->recurseForm($form);
        if (empty($notifications)) {
            return $content;
        }

        $html = '<ul class="form-notification-list">';
        foreach (array(Form::NOTIFICATION_ERROR, Form::NOTIFICATION_WARNING, Form::NOTIFICATION_INFO) as $type) {
            if (isset($notifications[$type])) {
                $html .= '<li><ul class="notification-' . $this->getNotificationTypeName($type) . '">';
                foreach ($notifications[$type] as $message) {
                    if (is_array($message)) {
                        list($message, $properties) = $message;
                        $html .= '<li' . $view->propertiesToString($properties) . '>'
                            . $view->escape($message)
                            . '</li>';
                    } else {
                        $html .= '<li>' . $view->escape($message) . '</li>';
                    }
                }

                $html .= '</ul></li>';
            }
        }

        if (isset($notifications[Form::NOTIFICATION_ERROR])) {
            $icon = 'cancel';
            $class = 'error';
        } elseif (isset($notifications[Form::NOTIFICATION_WARNING])) {
            $icon = 'warning-empty';
            $class = 'warning';
        } else {
            $icon = 'info';
            $class = 'info';
        }

        $html = "<div class=\"form-notifications $class\">"
        . Icinga::app()->getViewRenderer()->view->icon($icon, '', ['class' => 'form-notification-icon'])
        . $html;

        switch ($this->getPlacement()) {
            case self::APPEND:
                return $content . $html . '</ul></div>';
            case self::PREPEND:
                return $html . '</ul></div>' . $content;
        }
    }

    /**
     * Recurse the given form and return the notifications for it and all of its subforms
     *
     * @param   Form    $form   The form to recurse
     *
     * @return  array
     */
    protected function recurseForm(Form $form)
    {
        $notifications = $form->getNotifications();
        foreach ($form->getSubForms() as $subForm) {
            foreach ($this->recurseForm($subForm) as $type => $messages) {
                foreach ($messages as $message) {
                    $notifications[$type][] = $message;
                }
            }
        }

        return $notifications;
    }

    /**
     * Return the name for the given notification type
     *
     * @param   int     $type
     *
     * @return  string
     *
     * @throws  ProgrammingError    In case the given type is invalid
     */
    protected function getNotificationTypeName($type)
    {
        switch ($type) {
            case Form::NOTIFICATION_ERROR:
                return 'error';
            case Form::NOTIFICATION_WARNING:
                return 'warning';
            case Form::NOTIFICATION_INFO:
                return 'info';
            default:
                throw new ProgrammingError('Invalid notification type "%s" provided', $type);
        }
    }
}