summaryrefslogtreecommitdiffstats
path: root/library/Director/PropertyModifier/PropertyModifierRejectOrSelect.php
blob: 04c49c57f17958ab9cd7223772852fac5e94a19b (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php

namespace Icinga\Module\Director\PropertyModifier;

use Icinga\Data\Filter\FilterExpression;
use Icinga\Exception\ConfigurationError;
use Icinga\Module\Director\Hook\PropertyModifierHook;
use Icinga\Module\Director\Web\Form\QuickForm;

class PropertyModifierRejectOrSelect extends PropertyModifierHook
{
    /** @var FilterExpression */
    private $filterExpression;

    public function getName()
    {
        return mt('director', 'Reject or keep rows based on property value');
    }

    /**
     * @inheritdoc
     * @throws \Zend_Form_Exception
     */
    public static function addSettingsFormFields(QuickForm $form)
    {
        $form->addElement('select', 'filter_method', [
            'label'       => $form->translate('Filter method'),
            'required'    => true,
            'value'        => 'wildcard',
            'multiOptions' => $form->optionalEnum([
                'wildcard' => $form->translate('Simple match with wildcards (*)'),
                'regex'    => $form->translate('Regular Expression'),
                'is_true'  => $form->translate('Match boolean TRUE'),
                'is_false' => $form->translate('Match boolean FALSE'),
                'is_null'  => $form->translate('Match NULL value columns'),
            ]),
            'class' => 'autosubmit',
        ]);

        $method = $form->getSetting('filter_method');
        switch ($method) {
            case 'wildcard':
                $form->addElement('text', 'filter_string', [
                    'label'       => $form->translate('Filter'),
                    'description' => $form->translate(
                        'The string/pattern you want to search for, use wildcard'
                        . ' matches like www.* or *linux*'
                    ),
                    'required'    => true,
                ]);
                break;
            case 'regex':
                $form->addElement('text', 'filter_string', [
                    'label'       => $form->translate('Filter'),
                    'description' => $form->translate(
                        'The string/pattern you want to search for, use regular'
                        . ' expression like /^www\d+\./'
                    ),
                    'required'    => true,
                ]);
                break;
        }

        $form->addElement('select', 'policy', [
            'label'       => $form->translate('Policy'),
            'required'    => true,
            'description' => $form->translate(
                'What should happen with the row, when this property matches the given expression?'
            ),
            'value'        => 'reject',
            'multiOptions' => [
                'reject' => $form->translate('Reject the whole row'),
                'keep'   => $form->translate('Keep only matching rows'),
            ],
        ]);
    }

    public function matchesRegexp($string, $expression)
    {
        return preg_match($expression, $string);
    }

    public function isNull($string, $expression)
    {
        return $string === null;
    }

    public function isTrue($string, $expression)
    {
        return $string === true;
    }

    public function isFalse($string, $expression)
    {
        return $string === false;
    }

    public function matchesWildcard($string, $expression)
    {
        return $this->filterExpression->matches(
            (object) ['value' => $string]
        );
    }

    public function transform($value)
    {
        $method = $this->getSetting('filter_method');
        $filter = $this->getSetting('filter_string');
        $policy = $this->getSetting('policy');

        switch ($method) {
            case 'wildcard':
                $func = 'matchesWildcard';
                $this->filterExpression = new FilterExpression('value', '=', $filter);
                break;
            case 'regex':
                $func = 'matchesRegexp';
                break;
            case 'is_null':
                $func = 'isNull';
                break;
            case 'is_true':
                $func = 'isTrue';
                break;
            case 'is_false':
                $func = 'isFalse';
                break;
            default:
                throw new ConfigurationError(
                    '%s is not a valid value for an ArrayFilter filter_method',
                    var_export($method, true)
                );
        }

        if ($this->$func($value, $filter)) {
            if ($policy === 'reject') {
                $this->rejectRow();
            }
        } else {
            if ($policy === 'keep') {
                $this->rejectRow();
            }
        }

        return $value;
    }
}