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