Hintergrundjobs: »Speichern & Ausführen« darf keinen neuen Job anlegen
[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 --get_set_init' => [ qw(task_server back_to models background_job) ],
17 );
18
19 __PACKAGE__->run_before('check_auth');
20 __PACKAGE__->run_before('check_task_server');
21
22 #
23 # actions
24 #
25
26 sub action_list {
27   my ($self) = @_;
28
29   $self->setup_list_action_bar;
30   $self->render('background_job/list',
31                 title           => $::locale->text('Background jobs'),
32                 BACKGROUND_JOBS => $self->models->get,
33                 MODELS          => $self->models);
34 }
35
36 sub action_new {
37   my ($self) = @_;
38
39   $self->background_job(SL::DB::BackgroundJob->new(cron_spec => '* * * * *',  package_name => 'Test'));
40   $self->setup_form_action_bar;
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->setup_form_action_bar;
50   $self->render('background_job/form',
51                 title       => $::locale->text('Edit background job'),
52                 JOB_CLASSES => [ SL::BackgroundJob::Base->get_known_job_classes ]);
53 }
54
55 sub action_show {
56   my ($self) = @_;
57
58   if ($::request->type eq 'json') {
59     $self->render(\ SL::JSON::to_json($self->background_job->as_tree), { type => 'json' });
60   } else {
61     $self->action_edit;
62   }
63 }
64
65 sub action_create {
66   my ($self) = @_;
67
68   $self->background_job(SL::DB::BackgroundJob->new);
69   $self->create_or_update;
70 }
71
72 sub action_update {
73   my ($self) = @_;
74   $self->create_or_update;
75 }
76
77 sub action_destroy {
78   my ($self) = @_;
79
80   if (eval { $self->background_job->delete; 1; }) {
81     flash_later('info',  $::locale->text('The background job has been deleted.'));
82   } else {
83     flash_later('error', $::locale->text('The background job could not be destroyed.'));
84   }
85
86   $self->redirect_to($self->back_to);
87 }
88
89 sub action_save_and_execute {
90   my ($self) = @_;
91
92   $self->background_job(SL::DB::BackgroundJob->new) if !$self->background_job;
93   return unless $self->create_or_update;
94   $self->action_execute;
95 }
96
97 sub action_execute {
98   my ($self) = @_;
99
100   my $history = $self->background_job->run;
101   if ($history->status eq 'success') {
102     flash_later('info', $::locale->text('The background job was executed successfully.'));
103   } else {
104     flash_later('error', $::locale->text('There was an error executing the background job.'));
105   }
106
107   $self->redirect_to(controller => 'BackgroundJobHistory',
108                      action     => 'show',
109                      id         => $history->id,
110                      back_to    => $self->url_for(action => 'edit', id => $self->background_job->id));
111 }
112
113 #
114 # filters
115 #
116
117 sub check_auth {
118   $::auth->assert('admin');
119 }
120
121 #
122 # helpers
123 #
124
125 sub create_or_update {
126   my $self   = shift;
127   my $return = shift;
128   my $is_new = !$self->background_job->id;
129   my $params = delete($::form->{background_job}) || { };
130
131   $self->background_job->assign_attributes(%{ $params });
132
133   my @errors = $self->background_job->validate;
134
135   if (@errors) {
136     flash('error', @errors);
137     $self->render('background_job/form', title => $is_new ? $::locale->text('Create a new background job') : $::locale->text('Edit background job'));
138     return;
139   }
140
141   $self->background_job->update_next_run_at;
142   $self->background_job->save;
143
144   flash_later('info', $is_new ? $::locale->text('The background job has been created.') : $::locale->text('The background job has been saved.'));
145   return if $return;
146
147   $self->redirect_to($self->back_to);
148 }
149
150 sub init_background_job {
151   return $::form->{id} ? SL::DB::BackgroundJob->new(id => $::form->{id})->load : undef;
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 sub setup_list_action_bar {
187   my ($self) = @_;
188
189   for my $bar ($::request->layout->get('actionbar')) {
190     $bar->add(
191       link => [
192         t8('Add'),
193         link      => $self->url_for(action => 'new'),
194         accesskey => 'enter',
195       ],
196       link => [
197         t8('Server control'),
198         link => $self->url_for(controller => 'TaskServer', action => 'show'),
199       ],
200       link => [
201         t8('Job history'),
202         link => $self->url_for(controller => 'BackgroundJobHistory', action => 'list'),
203       ],
204     );
205   }
206 }
207
208 sub setup_form_action_bar {
209   my ($self) = @_;
210
211   my $is_new = !$self->background_job->id;
212
213   for my $bar ($::request->layout->get('actionbar')) {
214     $bar->add(
215       combobox => [
216         action => [
217           t8('Save'),
218           submit    => [ '#form', { action => 'BackgroundJob/' . ($is_new ? 'create' : 'update') } ],
219           accesskey => 'enter',
220         ],
221         action => [
222           t8('Save and execute'),
223           submit => [ '#form', { action => 'BackgroundJob/save_and_execute' } ],
224         ],
225       ], # end of combobox "Save"
226
227       action => [
228         t8('Delete'),
229         submit   => [ '#form', { action => 'BackgroundJob/destroy' } ],
230         confirm  => t8('Do you really want to delete this object?'),
231         disabled => $is_new ? t8('This object has not been saved yet.') : undef,
232       ],
233
234       link => [
235         t8('Abort'),
236         link => $self->url_for(action => 'list'),
237       ],
238
239       link => [
240         t8('Job history'),
241         link     => $self->url_for(controller => 'BackgroundJobHistory', action => 'list', 'filter.package_name:substr::ilike' => $self->background_job->package_name),
242         disabled => $is_new ? t8('This object has not been saved yet.') : undef,
243       ],
244     );
245   }
246 }
247
248 1;