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

namespace Icinga\Module\Director\Forms;

use gipfl\Translation\TranslationHelper;
use gipfl\Web\Form;
use Icinga\Module\Director\Data\Db\DbObjectStore;
use Icinga\Module\Director\Import\Sync;
use Icinga\Module\Director\Objects\SyncRule;

class SyncRunForm extends Form
{
    use TranslationHelper;

    protected $defaultDecoratorClass = null;

    /** @var ?string */
    protected $successMessage = null;

    /** @var SyncRule */
    protected $rule;

    /** @var DbObjectStore */
    protected $store;

    public function __construct(SyncRule $rule, DbObjectStore $store)
    {
        $this->rule = $rule;
        $this->store = $store;
    }

    public function assemble()
    {
        if ($this->store->getBranch()->isBranch()) {
            $label = sprintf($this->translate('Sync to Branch: %s'), $this->store->getBranch()->getName());
        } else {
            $label = $this->translate('Trigger this Sync');
        }
        $this->addElement('submit', 'submit', [
            'label' => $label,
        ]);
    }

    /**
     * @return string|null
     */
    public function getSuccessMessage()
    {
        return $this->successMessage;
    }

    public function onSuccess()
    {
        $sync = new Sync($this->rule, $this->store);
        if ($sync->hasModifications()) {
            if ($sync->apply()) {
                // and changed
                $this->successMessage = $this->translate(('Source has successfully been synchronized'));
            } else {
                $this->successMessage = $this->translate('Nothing changed, rule is in sync');
            }
        } else {
            // Used to be $rule->get('sync_state') === 'in-sync', $changed = $rule->applyChanges();
            $this->successMessage = $this->translate('Nothing to do, rule is in sync');
        }
    }
}