Revert "Buchungsliste: Kontonamen werden nicht angezeigt."
[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) ]);
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_create {
55   my ($self) = @_;
56
57   $self->background_job(SL::DB::BackgroundJob->new);
58   $self->create_or_update;
59 }
60
61 sub action_update {
62   my ($self) = @_;
63   $self->create_or_update;
64 }
65
66 sub action_destroy {
67   my ($self) = @_;
68
69   if (eval { $self->background_job->delete; 1; }) {
70     flash_later('info',  $::locale->text('The background job has been deleted.'));
71   } else {
72     flash_later('error', $::locale->text('The background job could not be destroyed.'));
73   }
74
75   $self->redirect_to($self->back_to);
76 }
77
78 sub action_save_and_execute {
79   my ($self) = @_;
80
81   $self->background_job(SL::DB::BackgroundJob->new) if !$self->background_job;
82   return unless $self->create_or_update;
83   $self->action_execute;
84 }
85
86 sub action_execute {
87   my ($self) = @_;
88
89   my $history = $self->background_job->run;
90   if ($history->status eq 'success') {
91     flash_later('info', $::locale->text('The background job was executed successfully.'));
92   } else {
93     flash_later('error', $::locale->text('There was an error executing the background job.'));
94   }
95
96   $self->redirect_to(controller => 'BackgroundJobHistory',
97                      action     => 'show',
98                      id         => $history->id,
99                      back_to    => $self->url_for(action => 'edit', id => $self->background_job->id));
100 }
101
102 #
103 # filters
104 #
105
106 sub check_auth {
107   $::auth->assert('admin');
108 }
109
110 #
111 # helpers
112 #
113
114 sub create_or_update {
115   my $self   = shift;
116   my $return = shift;
117   my $is_new = !$self->background_job->id;
118   my $params = delete($::form->{background_job}) || { };
119
120   $self->background_job->assign_attributes(%{ $params });
121
122   my @errors = $self->background_job->validate;
123
124   if (@errors) {
125     flash('error', @errors);
126     $self->render('background_job/form', title => $is_new ? $::locale->text('Create a new background job') : $::locale->text('Edit background job'));
127     return;
128   }
129
130   $self->background_job->update_next_run_at;
131   $self->background_job->save;
132
133   flash_later('info', $is_new ? $::locale->text('The background job has been created.') : $::locale->text('The background job has been saved.'));
134   return if $return;
135
136   $self->redirect_to($self->back_to);
137 }
138
139 sub load_background_job {
140   my ($self) = @_;
141   $self->background_job(SL::DB::BackgroundJob->new(id => $::form->{id})->load);
142 }
143
144 sub init_task_server {
145   return SL::System::TaskServer->new;
146 }
147
148 sub check_task_server {
149   my ($self) = @_;
150   flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;
151 }
152
153 sub init_back_to {
154   my ($self) = @_;
155   return $::form->{back_to} || $self->url_for(action => 'list');
156 }
157
158 sub init_models {
159   SL::Controller::Helper::GetModels->new(
160     controller => $_[0],
161     filtered => 0,
162     sorted => {
163       package_name => t8('Package name'),
164       type         => t8('Execution type'),
165       active       => t8('Active'),
166       cron_spec    => t8('Execution schedule'),
167       last_run_at  => t8('Last run at'),
168       next_run_at  => t8('Next run at'),
169     },
170     query => [
171       package_name => [ SL::BackgroundJob::Base->get_known_job_classes ],
172     ],
173   );
174 }
175
176 1;