1e73e1e2935e5c39070a1b08c9aa32548959423b
[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')) unless $self->background_job;
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_edit_as_new {
56   my ($self) = @_;
57
58   $self->background_job($self->background_job->clone_and_reset);
59   $self->action_new;
60 }
61
62 sub action_show {
63   my ($self) = @_;
64
65   if ($::request->type eq 'json') {
66     $self->render(\ SL::JSON::to_json($self->background_job->as_tree), { type => 'json' });
67   } else {
68     $self->action_edit;
69   }
70 }
71
72 sub action_create {
73   my ($self) = @_;
74
75   $self->background_job(SL::DB::BackgroundJob->new);
76   $self->create_or_update;
77 }
78
79 sub action_update {
80   my ($self) = @_;
81   $self->create_or_update;
82 }
83
84 sub action_destroy {
85   my ($self) = @_;
86
87   if (eval { $self->background_job->delete; 1; }) {
88     flash_later('info',  $::locale->text('The background job has been deleted.'));
89   } else {
90     flash_later('error', $::locale->text('The background job could not be destroyed.'));
91   }
92
93   $self->redirect_to($self->back_to);
94 }
95
96 sub action_save_and_execute {
97   my ($self) = @_;
98
99   $self->background_job(SL::DB::BackgroundJob->new) if !$self->background_job;
100   return unless $self->create_or_update;
101   $self->action_execute;
102 }
103
104 sub action_execute {
105   my ($self) = @_;
106
107   my $history = $self->background_job->run;
108   if ($history->status eq 'success') {
109     flash_later('info', $::locale->text('The background job was executed successfully.'));
110   } else {
111     flash_later('error', $::locale->text('There was an error executing the background job.'));
112   }
113
114   $self->redirect_to(controller => 'BackgroundJobHistory',
115                      action     => 'show',
116                      id         => $history->id,
117                      back_to    => $self->url_for(action => 'edit', id => $self->background_job->id));
118 }
119
120 #
121 # filters
122 #
123
124 sub check_auth {
125   $::auth->assert('admin');
126 }
127
128 #
129 # helpers
130 #
131
132 sub create_or_update {
133   my $self   = shift;
134   my $return = shift;
135   my $is_new = !$self->background_job->id;
136   my $params = delete($::form->{background_job}) || { };
137
138   $self->background_job->assign_attributes(%{ $params });
139
140   my @errors = $self->background_job->validate;
141
142   if (@errors) {
143     flash('error', @errors);
144     $self->render('background_job/form', title => $is_new ? $::locale->text('Create a new background job') : $::locale->text('Edit background job'));
145     return;
146   }
147
148   $self->background_job->update_next_run_at;
149   $self->background_job->save;
150
151   flash_later('info', $is_new ? $::locale->text('The background job has been created.') : $::locale->text('The background job has been saved.'));
152   return if $return;
153
154   $self->redirect_to($self->back_to);
155 }
156
157 sub init_background_job {
158   return $::form->{id} ? SL::DB::BackgroundJob->new(id => $::form->{id})->load : undef;
159 }
160
161 sub init_task_server {
162   return SL::System::TaskServer->new;
163 }
164
165 sub check_task_server {
166   my ($self) = @_;
167   flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;
168 }
169
170 sub init_back_to {
171   my ($self) = @_;
172   return $::form->{back_to} || $self->url_for(action => 'list');
173 }
174
175 sub init_models {
176   SL::Controller::Helper::GetModels->new(
177     controller => $_[0],
178     filtered => 0,
179     sorted => {
180       package_name => t8('Package name'),
181       type         => t8('Execution type'),
182       active       => t8('Active'),
183       cron_spec    => t8('Execution schedule'),
184       last_run_at  => t8('Last run at'),
185       next_run_at  => t8('Next run at'),
186     },
187     query => [
188       package_name => [ SL::BackgroundJob::Base->get_known_job_classes ],
189     ],
190   );
191 }
192
193 sub setup_list_action_bar {
194   my ($self) = @_;
195
196   for my $bar ($::request->layout->get('actionbar')) {
197     $bar->add(
198       link => [
199         t8('Add'),
200         link      => $self->url_for(action => 'new'),
201         accesskey => 'enter',
202       ],
203       link => [
204         t8('Server control'),
205         link => $self->url_for(controller => 'TaskServer', action => 'show'),
206       ],
207       link => [
208         t8('Job history'),
209         link => $self->url_for(controller => 'BackgroundJobHistory', action => 'list'),
210       ],
211     );
212   }
213 }
214
215 sub setup_form_action_bar {
216   my ($self) = @_;
217
218   my $is_new = !$self->background_job->id;
219
220   for my $bar ($::request->layout->get('actionbar')) {
221     $bar->add(
222       combobox => [
223         action => [
224           t8('Save'),
225           submit    => [ '#form', { action => 'BackgroundJob/' . ($is_new ? 'create' : 'update') } ],
226           accesskey => 'enter',
227         ],
228         action => [
229           t8('Save and execute'),
230           submit => [ '#form', { action => 'BackgroundJob/save_and_execute' } ],
231         ],
232         action => [
233           t8('Use as new'),
234           submit   => [ '#form', { action => 'BackgroundJob/edit_as_new' } ],
235           disabled => $is_new ? t8('The object has not been saved yet.') : undef,
236         ],
237       ], # end of combobox "Save"
238
239       action => [
240         t8('Delete'),
241         submit   => [ '#form', { action => 'BackgroundJob/destroy' } ],
242         confirm  => t8('Do you really want to delete this object?'),
243         disabled => $is_new ? t8('This object has not been saved yet.') : undef,
244       ],
245
246       link => [
247         t8('Abort'),
248         link => $self->url_for(action => 'list'),
249       ],
250
251       link => [
252         t8('Job history'),
253         link     => $self->url_for(controller => 'BackgroundJobHistory', action => 'list', 'filter.package_name:substr::ilike' => $self->background_job->package_name),
254         disabled => $is_new ? t8('This object has not been saved yet.') : undef,
255       ],
256     );
257   }
258 }
259
260 1;