summaryrefslogtreecommitdiffstats
path: root/application/forms/Config/TtsIntegrationConfigForm.php
blob: 2bdb5627687aceef9df8f430e785180d116e4487 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php

/* Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Generictts\Forms\Config;

use Zend_Validate_Callback;
use Icinga\Exception\AlreadyExistsException;
use Icinga\Exception\IcingaException;
use Icinga\Exception\NotFoundError;
use Icinga\Forms\ConfigForm;
use Icinga\Web\Form\Validator\UrlValidator;
use Icinga\Web\Notification;

/**
 * Form for managing trouble ticket system integrations
 */
class TtsIntegrationConfigForm extends ConfigForm
{
    /**
     * Name of the integration if the form is bound to one
     *
     * @var string
     */
    protected $boundIntegration;

    /**
     * {@inheritdoc}
     */
    public function init()
    {
        $this->setName('form_config_generictts_tts_integrations');
    }

    /**
     * {@inheritdoc}
     */
    public function createElements(array $formData)
    {
        $this->addElement(
            'text',
            'name',
            array(
                'description'   => $this->translate('The name of the TTS integration'),
                'label'         => $this->translate('Name'),
                'required'      => true
            )
        );

        $patternValidator = new Zend_Validate_Callback(function ($value) {
            return @preg_match($value, '') !== false;
        });
        $patternValidator->setMessage(
            $this->translate('"%value%" is not a valid regular expression.'),
            Zend_Validate_Callback::INVALID_VALUE
        );
        $this->addElement(
            'text',
            'pattern',
            array(
                'value'         => '/this-will-not-match(\d{3-6})/',
                'label'         => $this->translate('Ticket Pattern'),
                'description'   => $this->translate(
                    'The pattern to extract ticket IDs from comments.'
                    . ' The ticket pattern must be a valid regular expression'
                ),
                'required'      => true,
                'validators'    => array($patternValidator)
            )
        );

        $urlValidator = new Zend_Validate_Callback(function ($value) {
            return strpos($value, '$1') !== false;
        });
        $urlValidator->setMessage(
            $this->translate('The URL must contain the placeholder $1 to be substituted with a ticket ID.'),
            Zend_Validate_Callback::INVALID_VALUE
        );
        $this->addElement(
            'text',
            'url',
            array(
                'value'         => 'http://no-such-domain.example.com/ticket?id=$1',
                'label'         => $this->translate('TTS Ticket URL'),
                'description'   => $this->translate(
                    'The URL pointing to the TTS with $1 as the placeholder for the ticket ID'
                ),
                'required'      => true,
                'validators'    => array($urlValidator, new UrlValidator())
            )
        );
    }

    /**
     * {@inheritdoc}
     */
    public function getSubmitLabel()
    {
        if (($submitLabel = parent::getSubmitLabel()) === null) {
            if ($this->boundIntegration === null) {
                $submitLabel = $this->translate('Integrate');
            } else {
                $submitLabel = $this->translate('Update Integration');
            }
        }
        return $submitLabel;
    }

    /**
     * {@inheritdoc}
     */
    public function onRequest()
    {
        // The base class implementation does not make sense here. We're not populating the whole configuration but
        // only a section
        return;
    }

    /**
     * {@inheritdoc}
     */
    public function onSuccess()
    {
        $name = $this->getElement('name')->getValue();
        $values = array(
            'pattern'   => $this->getElement('pattern')->getValue(),
            'url'       => $this->getElement('url')->getValue()
        );
        if ($this->boundIntegration === null) {
            $successNotification = $this->translate('TTS integrated');
            try {
                $this->add($name, $values);
            } catch (AlreadyExistsException $e) {
                $this->addError($e->getMessage());
                return false;
            }
        } else {
            $successNotification = $this->translate('TTS integration updated');
            try {
                $this->update($name, $values, $this->boundIntegration);
            } catch (IcingaException $e) {
                // Exception may be AlreadyExistsException or NotFoundError
                $this->addError($e->getMessage());
                return false;
            }
        }
        if ($this->save()) {
            Notification::success($successNotification);
            return true;
        }
        return false;
    }

    /**
     * Add a TTS integration
     *
     * @param   string  $name           The name of the integration
     * @param   array   $values
     *
     * @return  $this
     *
     * @throws  AlreadyExistsException  If the integration to add already exists
     */
    public function add($name, array $values)
    {
        if ($this->config->hasSection($name)) {
            throw new AlreadyExistsException(
                $this->translate('Can\'t add integration \'%s\'. Integration already exists'),
                $name
            );
        }
        $this->config->setSection($name, $values);
        return $this;
    }

    /**
     * Bind integration to this form
     *
     * @param   string  $name   The name of the integration
     *
     * @return  $this
     *
     * @throws  NotFoundError   If the given integration does not exist
     */
    public function bind($name)
    {
        if (! $this->config->hasSection($name)) {
            throw new NotFoundError(
                $this->translate('Can\'t load integration \'%s\'. Integration does not exist'),
                $name
            );
        }
        $this->boundIntegration = $name;
        $integration = $this->config->getSection($name)->toArray();
        $integration['name'] = $name;
        $this->populate($integration);
        return $this;
    }

    /**
     * Remove a TTS integration
     *
     * @param   string  $name   The name of the integration
     *
     * @return  $this
     *
     * @throws  NotFoundError   If the role does not exist
     */
    public function remove($name)
    {
        if (! $this->config->hasSection($name)) {
            throw new NotFoundError(
                $this->translate('Can\'t remove integration \'%s\'. Integration does not exist'),
                $name
            );
        }
        $this->config->removeSection($name);
        return $this;
    }

    /**
     * Update a TTS integration
     *
     * @param   string  $name       The possibly new name of the integration
     * @param   array   $values
     * @param   string  $oldName    The name of the integration to update
     *
     * @return  $this
     *
     * @throws  NotFoundError       If the integration to update does not exist
     */
    public function update($name, array $values, $oldName)
    {
        if ($name !== $oldName) {
            // The integration got a new name
            $this->remove($oldName);
            $this->add($name, $values);
        } else {
            if (! $this->config->hasSection($name)) {
                throw new NotFoundError(
                    $this->translate('Can\'t update integration \'%s\'. Integration does not exist'),
                    $name
                );
            }
            $this->config->setSection($name, $values);
        }
        return $this;
    }
}