jquery.checkall.js immer laden lassen
[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::Controller::Helper::Paginated;
10 use SL::Controller::Helper::Sorted;
11 use SL::DB::BackgroundJob;
12 use SL::Helper::Flash;
13 use SL::Locale::String;
14 use SL::System::TaskServer;
15
16 use Rose::Object::MakeMethods::Generic
17 (
18   scalar                  => [ qw(background_job) ],
19   'scalar --get_set_init' => [ qw(task_server back_to) ],
20 );
21
22 __PACKAGE__->run_before('check_auth');
23 __PACKAGE__->run_before('check_task_server');
24 __PACKAGE__->run_before('load_background_job', only => [ qw(edit update destroy execute) ]);
25
26 __PACKAGE__->make_paginated(ONLY => [ qw(list) ]);
27
28 __PACKAGE__->make_sorted(
29   ONLY         => [ qw(list) ],
30
31   package_name => t8('Package name'),
32   type         => t8('Execution type'),
33   active       => t8('Active'),
34   cron_spec    => t8('Execution schedule'),
35   last_run_at  => t8('Last run at'),
36   next_run_at  => t8('Next run at'),
37 );
38
39 #
40 # actions
41 #
42
43 sub action_list {
44   my ($self) = @_;
45
46   $self->render('background_job/list',
47                 title           => $::locale->text('Background jobs'),
48                 BACKGROUND_JOBS => $self->get_models);
49 }
50
51 sub action_new {
52   my ($self) = @_;
53
54   $self->background_job(SL::DB::BackgroundJob->new(cron_spec => '* * * * *',  package_name => 'Test'));
55   $self->render('background_job/form',
56                 title       => $::locale->text('Create a new background job'),
57                 JOB_CLASSES => [ SL::BackgroundJob::Base->get_known_job_classes ]);
58 }
59
60 sub action_edit {
61   my ($self) = @_;
62
63   $self->render('background_job/form',
64                 title       => $::locale->text('Edit background job'),
65                 JOB_CLASSES => [ SL::BackgroundJob::Base->get_known_job_classes ]);
66 }
67
68 sub action_create {
69   my ($self) = @_;
70
71   $self->background_job(SL::DB::BackgroundJob->new);
72   $self->create_or_update;
73 }
74
75 sub action_update {
76   my ($self) = @_;
77   $self->create_or_update;
78 }
79
80 sub action_destroy {
81   my ($self) = @_;
82
83   if (eval { $self->background_job->delete; 1; }) {
84     flash_later('info',  $::locale->text('The background job has been deleted.'));
85   } else {
86     flash_later('error', $::locale->text('The background job could not be destroyed.'));
87   }
88
89   $self->redirect_to($self->back_to);
90 }
91
92 sub action_save_and_execute {
93   my ($self) = @_;
94
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 1;