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

namespace Icinga\Module\Director\Forms;

use Icinga\Module\Director\Objects\DirectorActivityLog;
use Icinga\Module\Director\Objects\SyncRule;
use Icinga\Module\Director\Web\Form\DirectorForm;

class SyncCheckForm extends DirectorForm
{
    /** @var SyncRule */
    protected $rule;

    public function setSyncRule(SyncRule $rule)
    {
        $this->rule = $rule;
        return $this;
    }

    public function setup()
    {
        $this->submitLabel = false;
        $this->addElement('submit', 'submit', array(
            'label' => $this->translate('Check for changes'),
            'decorators' => array('ViewHelper')
        ));
    }

    public function onSuccess()
    {
        if ($this->rule->checkForChanges()) {
            $this->notifySuccess(
                $this->translate(('This Sync Rule would apply new changes'))
            );
            $sum = [
                DirectorActivityLog::ACTION_CREATE => 0,
                DirectorActivityLog::ACTION_MODIFY => 0,
                DirectorActivityLog::ACTION_DELETE => 0
            ];

            // TODO: Preview them? Like "hosta, hostb and 4 more would be...
            foreach ($this->rule->getExpectedModifications() as $object) {
                if ($object->shouldBeRemoved()) {
                    $sum[DirectorActivityLog::ACTION_DELETE]++;
                } elseif (! $object->hasBeenLoadedFromDb()) {
                    $sum[DirectorActivityLog::ACTION_CREATE]++;
                } elseif ($object->hasBeenModified()) {
                    $sum[DirectorActivityLog::ACTION_MODIFY]++;
                }
            }

            /**
            if ($sum['modify'] === 1) {
                $html .= $this->translate('One object would be modified'
            } elseif ($sum['modify'] > 1) {
            }
            */
            $html = '<pre>' . print_r($sum, true) . '</pre>';

            $this->addHtml($html);
        } elseif ($this->rule->get('sync_state') === 'in-sync') {
            $this->notifySuccess(
                $this->translate('Nothing would change, this rule is still in sync')
            );
        } else {
            $this->addError($this->translate('Checking this sync rule failed'));
        }
    }
}