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

namespace Icinga\Module\Director\Forms;

use Icinga\Exception\NotFoundError;
use Icinga\Exception\NotImplementedError;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Web\Form\DirectorForm;

class RestoreObjectForm extends DirectorForm
{
    /** @var IcingaObject */
    protected $object;

    public function setup()
    {
        $this->addSubmitButton($this->translate('Restore former object'));
    }

    public function onSuccess()
    {
        $object = $this->object;
        $name = $object->getObjectName();
        $db = $this->db;

        $keyParams = $object->getKeyParams();

        if ($object->supportsApplyRules() && $object->get('object_type') === 'apply') {
            // TODO: not all apply should be considered unique by name + object_type
            $query = $db->getDbAdapter()
                ->select()
                ->from($object->getTableName())
                ->where('object_type = ?', 'apply')
                ->where('object_name = ?', $name);

            $rules = $object::loadAll($db, $query);

            if (empty($rules)) {
                $existing = null;
            } elseif (count($rules) === 1) {
                $existing = current($rules);
            } else {
                // TODO: offer drop down?
                throw new NotImplementedError(
                    "Found multiple apply rule matching name '%s', can not restore!",
                    $name
                );
            }
        } else {
            try {
                $existing = $object::load($keyParams, $db);
            } catch (NotFoundError $e) {
                $existing = null;
            }
        }

        if ($existing !== null) {
            $typeExisting = $existing->get('object_type');
            $typeObject = $object->get('object_type');
            if ($typeExisting !== $typeObject) {
                // Not sure when that may occur
                throw new NotImplementedError(
                    'Found existing object has a mismatching object_type: %s != %s',
                    $typeExisting,
                    $typeObject
                );
            }

            $existing->replaceWith($object);

            if ($existing->hasBeenModified()) {
                $msg = $this->translate('Object has been restored');
                $existing->store();
            } else {
                $msg = $this->translate(
                    'Nothing to do, restore would not modify the current object'
                );
            }
        } else {
            $msg = $this->translate('Object has been re-created');
            $object->store($db);
        }

        $this->redirectOnSuccess($msg);
    }

    public function setObject(IcingaObject $object)
    {
        $this->object = $object;
        return $this;
    }
}