summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Web/Form/Decorator/Autosubmit.php
blob: 4405d0b23e4ec261786a978bf254ba7c6384a356 (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
126
127
128
129
130
131
132
133
<?php
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */

namespace Icinga\Web\Form\Decorator;

use Zend_Form_Decorator_Abstract;
use Icinga\Application\Icinga;
use Icinga\Web\View;
use Icinga\Web\Form;

/**
 * Decorator to add an icon and a submit button encapsulated in noscript-tags
 *
 * The icon is shown in JS environments to indicate that a specific form field does automatically request an update
 * of its form upon it has changed. The button allows users in non-JS environments to trigger the update manually.
 */
class Autosubmit extends Zend_Form_Decorator_Abstract
{
    /**
     * Whether a hidden <span> should be created with the same warning as in the icon label
     *
     * @var bool
     */
    protected $accessible;

    /**
     * The id used to identify the auto-submit warning associated with the decorated form element
     *
     * @var string
     */
    protected $warningId;

    /**
     * Set whether a hidden <span> should be created with the same warning as in the icon label
     *
     * @param   bool    $state
     *
     * @return  Autosubmit
     */
    public function setAccessible($state = true)
    {
        $this->accessible = (bool) $state;
        return $this;
    }

    /**
     * Return whether a hidden <span> is being created with the same warning as in the icon label
     *
     * @return  bool
     */
    public function getAccessible()
    {
        if ($this->accessible === null) {
            $this->accessible = $this->getOption('accessible') ?: false;
        }

        return $this->accessible;
    }

    /**
     * Return the id used to identify the auto-submit warning associated with the decorated element
     *
     * @param   mixed   $element    The element for which to generate a id
     *
     * @return  string
     */
    public function getWarningId($element = null)
    {
        if ($this->warningId === null) {
            $element = $element ?: $this->getElement();
            $this->warningId = 'autosubmit_warning_' . $element->getId();
        }

        return $this->warningId;
    }

    /**
     * Return the current view
     *
     * @return  View
     */
    protected function getView()
    {
        return Icinga::app()->getViewRenderer()->view;
    }

    /**
     * Add a auto-submit icon and submit button encapsulated in noscript-tags to the element
     *
     * @param   string      $content    The html rendered so far
     *
     * @return  string                  The updated html
     */
    public function render($content = '')
    {
        if ($content) {
            $isForm = $this->getElement() instanceof Form;
            $warning = $isForm
                ? t('This page will be automatically updated upon change of any of this form\'s fields')
                : t('This page will be automatically updated upon change of the value');
            $content .= $this->getView()->icon('cw', $warning, array(
                'aria-hidden'   => $isForm ? 'false' : 'true',
                'class'         => 'spinner autosubmit-info'
            ));
            if (! $isForm && $this->getAccessible()) {
                $content = '<span id="'
                    . $this->getWarningId()
                    . '" class="sr-only">'
                    . $warning
                    . '</span>'
                    . $content;
            }

            $content .= sprintf(
                '<noscript><button'
                . ' name="noscript_apply"'
                . ' class="noscript-apply"'
                . ' type="submit"'
                . ' value="1"'
                . ($this->getAccessible() ? ' aria-label="%1$s"' : '')
                . ' title="%1$s"'
                . '>%2$s</button></noscript>',
                $isForm
                    ? t('Push this button to update the form to reflect the changes that were made below')
                    : t('Push this button to update the form to reflect the change'
                        . ' that was made in the field on the left'),
                $this->getView()->icon('cw') . t('Apply')
            );
        }

        return $content;
    }
}