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