GDPDU: DATEV-ähnlicher Buchungsexport Rohversion
[kivitendo-erp.git] / SL / Controller / BackgroundJob.pm
1 package SL::Controller::BackgroundJob;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use SL::BackgroundJob::Base;
8 use SL::Controller::Helper::GetModels;
9 use SL::DB::BackgroundJob;
10 use SL::Helper::Flash;
11 use SL::Locale::String;
12 use SL::System::TaskServer;
13
14 use Rose::Object::MakeMethods::Generic
15 (
16   scalar                  => [ qw(background_job) ],
17   'scalar --get_set_init' => [ qw(task_server back_to models) ],
18 );
19
20 __PACKAGE__->run_before('check_auth');
21 __PACKAGE__->run_before('check_task_server');
22 __PACKAGE__->run_before('load_background_job', only => [ qw(edit update destroy execute show) ]);
23
24 #
25 # actions
26 #
27
28 sub action_list {
29   my ($self) = @_;
30
31   $self->render('background_job/list',
32                 title           => $::locale->text('Background jobs'),
33                 BACKGROUND_JOBS => $self->models->get,
34                 MODELS          => $self->models);
35 }
36
37 sub action_new {
38   my ($self) = @_;
39
40   $self->background_job(SL::DB::BackgroundJob->new(cron_spec => '* * * * *',  package_name => 'Test'));
41   $self->render('background_job/form',
42                 title       => $::locale->text('Create a new background job'),
43                 JOB_CLASSES => [ SL::BackgroundJob::Base->get_known_job_classes ]);
44 }
45
46 sub action_edit {
47   my ($self) = @_;
48
49   $self->render('background_job/form',
50                 title       => $::locale->text('Edit background job'),
51                 JOB_CLASSES => [ SL::BackgroundJob::Base->get_known_job_classes ]);
52 }
53
54 sub action_show {
55   my ($self) = @_;
56
57   if ($::request->type eq 'json') {
58     $self->render(\ SL::JSON::to_json($self->background_job->as_tree), { type => 'json' });
59   } else {
60     $self->action_edit;
61   }
62 }
63
64 sub action_create {
65   my ($self) = @_;
66
67   $self->background_job(SL::DB::BackgroundJob->new);
68   $self->create_or_update;
69 }
70
71 sub action_update {
72   my ($self) = @_;
73   $self->create_or_update;
74 }
75
76 sub action_destroy {
77   my ($self) = @_;
78
79   if (eval { $self->background_job->delete; 1; }) {
80     flash_later('info',  $::locale->text('The background job has been deleted.'));
81   } else {
82     flash_later('error', $::locale->text('The background job could not be destroyed.'));
83   }
84
85   $self->redirect_to($self->back_to);
86 }
87
88 sub action_save_and_execute {
89   my ($self) = @_;
90
91   $self->background_job(SL::DB::BackgroundJob->new) if !$self->background_job;
92   return unless $self->create_or_update;
93   $self->action_execute;
94 }
95
96 sub action_execute {
97   my ($self) = @_;
98
99   my $history = $self->background_job->run;
100   if ($history->status eq 'success') {
101     flash_later('info', $::locale->text('The background job was executed successfully.'));
102   } else {
103     flash_later('error', $::locale->text('There was an error executing the background job.'));
104   }
105
106   $self->redirect_to(controller => 'BackgroundJobHistory',
107                      action     => 'show',
108                      id         => $history->id,
109                      back_to    => $self->url_for(action => 'edit', id => $self->background_job->id));
110 }
111
112 #
113 # filters
114 #
115
116 sub check_auth {
117   $::auth->assert('admin');
118 }
119
120 #
121 # helpers
122 #
123
124 sub create_or_update {
125   my $self   = shift;
126   my $return = shift;
127   my $is_new = !$self->background_job->id;
128   my $params = delete($::form->{background_job}) || { };
129
130   $self->background_job->assign_attributes(%{ $params });
131
132   my @errors = $self->background_job->validate;
133
134   if (@errors) {
135     flash('error', @errors);
136     $self->render('background_job/form', title => $is_new ? $::locale->text('Create a new background job') : $::locale->text('Edit background job'));
137     return;
138   }
139
140   $self->background_job->update_next_run_at;
141   $self->background_job->save;
142
143   flash_later('info', $is_new ? $::locale->text('The background job has been created.') : $::locale->text('The background job has been saved.'));
144   return if $return;
145
146   $self->redirect_to($self->back_to);
147 }
148
149 sub load_background_job {
150   my ($self) = @_;
151   $self->background_job(SL::DB::BackgroundJob->new(id => $::form->{id})->load);
152 }
153
154 sub init_task_server {
155   return SL::System::TaskServer->new;
156 }
157
158 sub check_task_server {
159   my ($self) = @_;
160   flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;
161 }
162
163 sub init_back_to {
164   my ($self) = @_;
165   return $::form->{back_to} || $self->url_for(action => 'list');
166 }
167
168 sub init_models {
169   SL::Controller::Helper::GetModels->new(
170     controller => $_[0],
171     filtered => 0,
172     sorted => {
173       package_name => t8('Package name'),
174       type         => t8('Execution type'),
175       active       => t8('Active'),
176       cron_spec    => t8('Execution schedule'),
177       last_run_at  => t8('Last run at'),
178       next_run_at  => t8('Next run at'),
179     },
180     query => [
181       package_name => [ SL::BackgroundJob::Base->get_known_job_classes ],
182     ],
183   );
184 }
185
186 1;