summaryrefslogtreecommitdiffstats
path: root/library/Director/Job/SyncJob.php
blob: 0a5aa37f9e11489c5f909e62c989fdb4e3179394 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php

namespace Icinga\Module\Director\Job;

use Icinga\Module\Director\Hook\JobHook;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Module\Director\Web\Form\QuickForm;
use Icinga\Module\Director\Objects\SyncRule;

class SyncJob extends JobHook
{
    protected $rule;

    /**
     * @throws \Icinga\Exception\NotFoundError
     * @throws \Icinga\Module\Director\Exception\DuplicateKeyException
     */
    public function run()
    {
        $db = $this->db();
        $id = $this->getSetting('rule_id');
        if ($id === '__ALL__') {
            foreach (SyncRule::loadAll($db) as $rule) {
                $this->runForRule($rule);
            }
        } else {
            $this->runForRule(SyncRule::loadWithAutoIncId((int) $id, $db));
        }
    }

    /**
     * @return array
     * @throws \Icinga\Exception\NotFoundError
     */
    public function exportSettings()
    {
        $settings = [
            'apply_changes' => $this->getSetting('apply_changes') === 'y'
        ];
        $id = $this->getSetting('rule_id');
        if ($id !== '__ALL__') {
            $settings['rule'] = SyncRule::loadWithAutoIncId((int) $id, $this->db())
                ->get('rule_name');
        }

        return $settings;
    }

    /**
     * @param SyncRule $rule
     * @throws \Icinga\Module\Director\Exception\DuplicateKeyException
     */
    protected function runForRule(SyncRule $rule)
    {
        if ($this->getSetting('apply_changes') === 'y') {
            $rule->applyChanges();
        } else {
            $rule->checkForChanges();
        }
    }

    public static function getDescription(QuickForm $form)
    {
        return $form->translate(
            'The "Sync" job allows to run sync actions at regular intervals'
        );
    }

    /**
     * @param QuickForm $form
     * @return DirectorObjectForm|QuickForm
     * @throws \Zend_Form_Exception
     */
    public static function addSettingsFormFields(QuickForm $form)
    {
        /** @var DirectorObjectForm $form */
        $rules = self::enumSyncRules($form);

        $form->addElement('select', 'rule_id', array(
            'label'        => $form->translate('Synchronization rule'),
            'description'  => $form->translate(
                'Please choose your synchronization rule that should be executed.'
                . ' You could create different schedules for different rules or also'
                . ' opt for running all of them at once.'
            ),
            'required'     => true,
            'class'        => 'autosubmit',
            'multiOptions' => $rules
        ));

        $form->addElement('select', 'apply_changes', array(
            'label'        => $form->translate('Apply changes'),
            'description'  => $form->translate(
                'You could immediately apply eventual changes or just learn about them.'
                . ' In case you do not want them to be applied immediately, defining a'
                . ' job still makes sense. You will be made aware of available changes'
                . ' in your Director GUI.'
            ),
            'value'        => 'n',
            'multiOptions' => array(
                'y'  => $form->translate('Yes'),
                'n'  => $form->translate('No'),
            )
        ));

        if ((string) $form->getSentOrObjectValue('job_name') !== '') {
            if (($ruleId = $form->getSentValue('rule_id')) && array_key_exists($ruleId, $rules)) {
                $name = sprintf('Sync job: %s', $rules[$ruleId]);
                $form->getElement('job_name')->setValue($name);
                ///$form->getObject()->set('job_name', $name);
            }
        }

        return $form;
    }

    protected static function enumSyncRules(QuickForm $form)
    {
        /** @var DirectorObjectForm $form */
        $db = $form->getDb();
        $query = $db->select()->from('sync_rule', array('id', 'rule_name'))->order('rule_name');
        $res = $db->fetchPairs($query);
        return array(
            null      => $form->translate('- please choose -'),
            '__ALL__' => $form->translate('Run all rules at once')
        ) + $res;
    }
}