summaryrefslogtreecommitdiffstats
path: root/application/controllers/JobController.php
blob: 7655a74a0eeac94e0bb840b54ee7eba6d58f0709 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php

/* Icinga Web 2 X.509 Module | (c) 2023 Icinga GmbH | GPLv2 */

namespace Icinga\Module\X509\Controllers;

use Icinga\Module\X509\Common\Database;
use Icinga\Module\X509\Common\Links;
use Icinga\Module\X509\Forms\Jobs\JobConfigForm;
use Icinga\Module\X509\Model\X509Job;
use Icinga\Module\X509\Model\X509Schedule;
use Icinga\Module\X509\Forms\Jobs\ScheduleForm;
use Icinga\Module\X509\Widget\JobDetails;
use Icinga\Module\X509\Widget\Schedules;
use Icinga\Util\Json;
use ipl\Html\Contract\FormSubmitElement;
use ipl\Html\ValidHtml;
use ipl\Scheduler\Contract\Frequency;
use ipl\Stdlib\Filter;
use ipl\Web\Compat\CompatController;
use ipl\Web\Url;
use ipl\Web\Widget\ActionBar;
use ipl\Web\Widget\ActionLink;
use ipl\Web\Widget\ButtonLink;
use stdClass;

class JobController extends CompatController
{
    /** @var X509Job */
    protected $job;

    public function init()
    {
        parent::init();

        $this->getTabs()->disableLegacyExtensions();

        /** @var int $jobId */
        $jobId = $this->params->getRequired('id');

        /** @var X509Job $job */
        $job = X509Job::on(Database::get())
            ->filter(Filter::equal('id', $jobId))
            ->first();

        if ($job === null) {
            $this->httpNotFound($this->translate('Job not found'));
        }

        $this->job = $job;
    }

    public function indexAction(): void
    {
        $this->assertPermission('config/x509');

        $this->initTabs();
        $this->getTabs()->activate('job-activities');

        $jobRuns = $this->job->job_run->with(['job', 'schedule']);

        $limitControl = $this->createLimitControl();
        $sortControl = $this->createSortControl($jobRuns, [
            'schedule.name'    => $this->translate('Schedule Name'),
            'schedule.author'  => $this->translate('Author'),
            'total_targets'    => $this->translate('Total Targets'),
            'finished_targets' => $this->translate('Finished Targets'),
            'start_time desc'  => $this->translate('Started At'),
            'end_time'         => $this->translate('Ended At')
        ]);

        $this->controls->getAttributes()->add('class', 'default-layout');
        $this->addControl($sortControl);
        $this->addControl($limitControl);
        $this->addControl($this->createActionBar());

        $this->addContent(new JobDetails($jobRuns));
    }

    public function updateAction(): void
    {
        $this->assertPermission('config/x509');

        $this->addTitleTab($this->translate('Update Job'));

        $form = (new JobConfigForm($this->job))
            ->setAction((string) Url::fromRequest())
            ->populate([
                'name'            => $this->job->name,
                'cidrs'           => $this->job->cidrs,
                'ports'           => $this->job->ports,
                'exclude_targets' => $this->job->exclude_targets
            ])
            ->on(JobConfigForm::ON_SUCCESS, function (JobConfigForm $form) {
                /** @var FormSubmitElement $button */
                $button = $form->getPressedSubmitElement();
                if ($button->getName() === 'btn_remove') {
                    $this->switchToSingleColumnLayout();
                } else {
                    $this->closeModalAndRefreshRelatedView(Links::job($this->job));
                }
            })
            ->handleRequest($this->getServerRequest());

        $this->addContent($form);
    }

    public function schedulesAction(): void
    {
        $this->assertPermission('config/x509');

        $this->initTabs();
        $this->getTabs()->activate('schedules');

        $schedules = $this->job->schedule->with(['job']);

        $sortControl = $this->createSortControl($schedules, [
            'name'   => $this->translate('Name'),
            'author' => $this->translate('Author'),
            'ctime'  => $this->translate('Date Created'),
            'mtime'  => $this->translate('Date Modified')
        ]);

        $this->controls->getAttributes()->add('class', 'default-layout');
        $this->addControl(
            (new ButtonLink($this->translate('New Schedule'), Links::scheduleJob($this->job), 'plus'))
                ->openInModal()
        );
        $this->addControl($sortControl);

        $this->addContent(new Schedules($schedules));
    }

    public function scheduleAction(): void
    {
        $this->assertPermission('config/x509');

        $this->addTitleTab($this->translate('Schedule Job'));

        $form = (new ScheduleForm())
            ->setAction((string) Url::fromRequest())
            ->setJobId($this->job->id)
            ->on(JobConfigForm::ON_SUCCESS, function () {
                $this->redirectNow(Links::schedules($this->job));
            })
            ->handleRequest($this->getServerRequest());

        $parts = $form->getPartUpdates();
        if (! empty($parts)) {
            $this->sendMultipartUpdate(...$parts);
        }

        $this->addContent($form);
    }

    public function updateScheduleAction(): void
    {
        $this->assertPermission('config/x509');

        $this->addTitleTab($this->translate('Update Schedule'));

        /** @var int $id */
        $id = $this->params->getRequired('scheduleId');
        /** @var X509Schedule $schedule */
        $schedule = X509Schedule::on(Database::get())
            ->filter(Filter::equal('id', $id))
            ->first();
        if ($schedule === null) {
            $this->httpNotFound($this->translate('Schedule not found'));
        }

        /** @var stdClass $config */
        $config = Json::decode($schedule->config);
        /** @var Frequency $type */
        $type = $config->type;
        $frequency = $type::fromJson($config->frequency);

        $form = (new ScheduleForm($schedule))
            ->setAction((string) Url::fromRequest())
            ->populate([
                'name'             => $schedule->name,
                'full_scan'        => $config->full_scan ?? 'n',
                'rescan'           => $config->rescan ?? 'n',
                'since_last_scan'  => $config->since_last_scan ?? null,
                'schedule_element' => $frequency
            ])
            ->on(JobConfigForm::ON_SUCCESS, function () {
                $this->redirectNow('__BACK__');
            })
            ->handleRequest($this->getServerRequest());

        $parts = $form->getPartUpdates();
        if (! empty($parts)) {
            $this->sendMultipartUpdate(...$parts);
        }

        $this->addContent($form);
    }

    protected function createActionBar(): ValidHtml
    {
        $actions = new ActionBar();
        $actions->addHtml(
            (new ActionLink($this->translate('Modify'), Links::updateJob($this->job), 'edit'))
                ->openInModal(),
            (new ActionLink($this->translate('Schedule'), Links::scheduleJob($this->job), 'calendar'))
                ->openInModal()
        );

        return $actions;
    }

    protected function initTabs(): void
    {
        $tabs = $this->getTabs();
        $tabs
            ->add('job-activities', [
                'label' => $this->translate('Job Activities'),
                'url'   => Links::job($this->job)
            ])
            ->add('schedules', [
                'label' => $this->translate('Schedules'),
                'url'   => Links::schedules($this->job)
            ]);
    }
}