summaryrefslogtreecommitdiffstats
path: root/application/forms/Navigation/ActionForm.php
blob: 08cba3f8223b7515f808f599202ae26df46365a4 (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
<?php

/* Icinga DB Web | (c) 2021 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Forms\Navigation;

use Icinga\Exception\ConfigurationError;
use Icinga\Forms\Navigation\NavigationItemForm;
use Icinga\Module\Icingadb\Common\Auth;

class ActionForm extends NavigationItemForm
{
    use Auth;

    /**
     * The name of the restriction to which the filter should be applied
     *
     * @var string
     */
    protected $restriction;

    public function createElements(array $formData)
    {
        parent::createElements($formData);

        $this->addElement(
            'text',
            'filter',
            array(
                'allowEmpty'    => true,
                'label'         => $this->translate('Filter'),
                'description'   => $this->translate(
                    'Display this action only for objects matching this filter. Leave it blank'
                    . ' if you want this action being displayed regardless of the object'
                )
            )
        );
    }

    public function isValid($formData): bool
    {
        if (! parent::isValid($formData)) {
            return false;
        }

        if (($filterString = $this->getValue('filter')) !== null) {
            try {
                $this->parseRestriction($filterString, $this->restriction);
            } catch (ConfigurationError $err) {
                $this->getElement('filter')->addError($err->getMessage());

                return false;
            }
        }

        return true;
    }
}