summaryrefslogtreecommitdiffstats
path: root/application/forms/ImportRunForm.php
blob: 9f6494f9d899137684a35f061fa0ac637a7bc199 (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
<?php

namespace Icinga\Module\Director\Forms;

use Icinga\Module\Director\Objects\ImportSource;
use Icinga\Module\Director\Web\Form\DirectorForm;

class ImportRunForm extends DirectorForm
{
    /** @var  ImportSource */
    protected $source;

    public function setImportSource(ImportSource $source)
    {
        $this->source = $source;
        return $this;
    }

    public function setup()
    {
        $this->submitLabel = false;
        $this->addElement('submit', 'submit', [
            'label' => $this->translate('Trigger Import Run'),
            'decorators' => ['ViewHelper']
        ]);
    }

    public function onSuccess()
    {
        $source = $this->source;
        if ($source->runImport()) {
            $this->setSuccessMessage(
                $this->translate('Imported new data from this Import Source')
            );
        } else {
            $this->setSuccessMessage(
                $this->translate(
                    'Nothing to do, data provided by this Import Source'
                    . " didn't change since the last import run"
                )
            );
        }

        if ($source->get('import_state') === 'failing') {
            $this->addError($this->translate('Triggering this Import Source failed'));
        } else {
            parent::onSuccess();
        }
    }
}