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