summaryrefslogtreecommitdiffstats
path: root/application/forms/RestoreBasketForm.php
blob: 90d5b3890ac48da077bb8c4c17082d0fc821755b (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
<?php

namespace Icinga\Module\Director\Forms;

use Icinga\Application\Config;
use Icinga\Authentication\Auth;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\DirectorObject\Automation\BasketSnapshot;
use Icinga\Module\Director\Web\Controller\Extension\DirectorDb;
use Icinga\Module\Director\Web\Form\QuickForm;

class RestoreBasketForm extends QuickForm
{
    use DirectorDb;

    /** @var BasketSnapshot */
    private $snapshot;

    public function setSnapshot(BasketSnapshot $snapshot)
    {
        $this->snapshot = $snapshot;

        return $this;
    }

    /**
     * @codingStandardsIgnoreStart
     * @return Auth
     */
    protected function Auth()
    {
        return Auth::getInstance();
    }

    /**
     * @return Config
     */
    protected function Config()
    {
        // @codingStandardsIgnoreEnd
        return Config::module('director');
    }

    /**
     * @throws \Zend_Form_Exception
     */
    public function setup()
    {
        $allowedDbs = $this->listAllowedDbResourceNames();
        $this->addElement('select', 'target_db', [
            'label'        => $this->translate('Target DB'),
            'description'  => $this->translate('Restore to this target Director DB'),
            'multiOptions' => $allowedDbs,
            'value'        => $this->getRequest()->getParam('target_db', $this->getFirstDbResourceName()),
            'class'        => 'autosubmit',
        ]);

        $this->setSubmitLabel($this->translate('Restore'));
    }

    public function getDb()
    {
        return Db::fromResourceName($this->getValue('target_db'));
    }

    /**
     * @throws \Icinga\Exception\NotFoundError
     */
    public function onSuccess()
    {
        $this->snapshot->restoreTo($this->getDb());
        $this->setSuccessUrl($this->getSuccessUrl()->with('target_db', $this->getValue('target_db')));
        $this->setSuccessMessage(sprintf('Restored to %s', $this->getValue('target_db')));

        parent::onSuccess();
    }
}