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