summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Web/Form/Element/Button.php
blob: 307247e0898b1706c9f0c591caec68bf4689ebf8 (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
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Web\Form\Element;

use Icinga\Web\Request;
use Icinga\Application\Icinga;
use Icinga\Web\Form\FormElement;
use Zend_Config;

/**
 * A button
 */
class Button extends FormElement
{
    /**
     * Use formButton view helper by default
     *
     * @var string
     */
    public $helper = 'formButton';

    /**
     * Constructor
     *
     * @param   string|array|Zend_Config    $spec       Element name or configuration
     * @param   string|array|Zend_Config    $options    Element value or configuration
     */
    public function __construct($spec, $options = null)
    {
        if (is_string($spec) && ((null !== $options) && is_string($options))) {
            $options = array('label' => $options);
        }

        if (!isset($options['ignore'])) {
            $options['ignore'] = true;
        }

        parent::__construct($spec, $options);

        if ($label = $this->getLabel()) {
            // Necessary to get the label shown on the generated HTML
            $this->content = $label;
        }
    }

    /**
     * Validate element value (pseudo)
     *
     * There is no need to reset the value
     *
     * @param   mixed   $value      Is always ignored
     * @param   mixed   $context    Is always ignored
     *
     * @return  bool                Returns always TRUE
     */
    public function isValid($value, $context = null)
    {
        return true;
    }

    /**
     * Has this button been selected?
     *
     * @return  bool
     */
    public function isChecked()
    {
        return $this->getRequest()->getParam($this->getName()) === $this->getValue();
    }

    /**
     * Return the current request
     *
     * @return  Request
     */
    protected function getRequest()
    {
        return Icinga::app()->getRequest();
    }
}