summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Web/Form/Decorator/Help.php
blob: 9e30e863c9c27c4839a836c37f71cec148fe9a39 (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
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Web\Form\Decorator;

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

/**
 * Decorator to add helptext to a form element
 */
class Help extends Zend_Form_Decorator_Abstract
{
    /**
     * Whether a hidden <span> should be created to describe the decorated form element
     *
     * @var bool
     */
    protected $accessible = false;

    /**
     * The id used to identify the description associated with the decorated form element
     *
     * @var string
     */
    protected $descriptionId;

    /**
     * Set whether a hidden <span> should be created to describe the decorated form element
     *
     * @param   bool    $state
     *
     * @return  Help
     */
    public function setAccessible($state = true)
    {
        $this->accessible = (bool) $state;
        return $this;
    }

    /**
     * Return the id used to identify the description associated with the decorated element
     *
     * @param   Zend_Form_Element   $element    The element for which to generate a id
     *
     * @return  string
     */
    public function getDescriptionId(Zend_Form_Element $element = null)
    {
        if ($this->descriptionId === null) {
            $element = $element ?: $this->getElement();
            $this->descriptionId = 'desc_' . $element->getId();
        }

        return $this->descriptionId;
    }

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

    /**
     * Add a help icon to the left of an element
     *
     * @param   string      $content    The html rendered so far
     *
     * @return  ?string                  The updated html
     */
    public function render($content = '')
    {
        $element = $this->getElement();
        $description = $element->getDescription();
        $requirement = $element->getAttrib('requirement');
        unset($element->requirement);

        $helpContent = '';
        if ($description || $requirement) {
            if ($this->accessible) {
                $helpContent = '<span id="'
                    . $this->getDescriptionId()
                    . '" class="sr-only">'
                    . $description
                    . ($description && $requirement ? ' ' : '')
                    . $requirement
                    . '</span>';
            }

            $helpContent = $this->getView()->icon(
                'info-circled',
                $description . ($description && $requirement ? ' ' : '') . $requirement,
                array(
                    'class'         => 'control-info',
                    'aria-hidden'   => $this->accessible ? 'true' : 'false'
                )
            ) . $helpContent;
        }

        switch ($this->getPlacement()) {
            case self::APPEND:
                return $content . $helpContent;
            case self::PREPEND:
                return $helpContent . $content;
        }
    }
}