marei: headline/colspec keys for SimpleTabular
[kivitendo-erp.git] / SL / Controller / BackgroundJobHistory.pm
1 package SL::Controller::BackgroundJobHistory;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use SL::Controller::Helper::GetModels;
8 use SL::DB::BackgroundJobHistory;
9 use SL::Helper::Flash;
10 use SL::Locale::String;
11 use SL::System::TaskServer;
12
13 use Rose::Object::MakeMethods::Generic
14 (
15   scalar                  => [ qw(history filter_summary) ],
16   'scalar --get_set_init' => [ qw(task_server models) ],
17 );
18
19 __PACKAGE__->run_before('check_auth');
20 __PACKAGE__->run_before('add_stylesheet');
21 __PACKAGE__->run_before('check_task_server');
22
23 #
24 # actions
25 #
26
27 sub action_list {
28   my ($self) = @_;
29
30   $self->make_filter_summary;
31
32   $self->setup_list_action_bar;
33   $self->render('background_job_history/list',
34                 title   => $::locale->text('Background job history'),
35                 ENTRIES => $self->models->get,
36                 MODELS  => $self->models);
37 }
38
39 sub action_show {
40   my ($self) = @_;
41
42   my $back_to = $::form->{back_to} || $self->url_for(action => 'list');
43
44   $self->history(SL::DB::BackgroundJobHistory->new(id => $::form->{id})->load);
45   $self->setup_show_action_bar;
46   $self->render('background_job_history/show',
47                 title   => $::locale->text('View background job execution result'),
48                 back_to => $back_to);
49 }
50
51 #
52 # filters
53 #
54
55 sub check_auth {
56   $::auth->assert('admin');
57 }
58
59 #
60 # helpers
61 #
62
63 sub init_task_server {
64   return SL::System::TaskServer->new;
65 }
66
67 sub check_task_server {
68   my ($self) = @_;
69   flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;
70 }
71
72 sub add_stylesheet {
73   $::request->{layout}->use_stylesheet('background_jobs.css');
74 }
75
76 sub make_filter_summary {
77   my ($self)  = @_;
78
79   my $filter  = $::form->{filter} || {};
80   my @filters = (
81     [ "package_name:substr::ilike", $::locale->text('Package name')                                ],
82     [ "result:substr::ilike",       $::locale->text('Result')                                      ],
83     [ "error:substr::ilike",        $::locale->text('Error')                                       ],
84     [ "run_at:date::ge",            $::locale->text('Run at') . " " . $::locale->text('From Date') ],
85     [ "run_at:date::le",            $::locale->text('Run at') . " " . $::locale->text('To Date')   ],
86   );
87
88   my @filter_strings = grep { $_ }
89                        map  { $filter->{ $_->[0] } ? $_->[1] . ' ' . $filter->{ $_->[0] } : undef }
90                        @filters;
91
92   my %status = (
93     failure  => $::locale->text('failed'),
94     success  => $::locale->text('succeeded'),
95   );
96   push @filter_strings, $status{ $filter->{'status:eq_ignore_empty'} } if $filter->{'status:eq_ignore_empty'};
97
98   $self->filter_summary(join(', ', @filter_strings));
99 }
100
101 sub init_models {
102   my ($self) = @_;
103
104   SL::Controller::Helper::GetModels->new(
105     controller => $self,
106     sorted => {
107       package_name => t8('Package name'),
108       run_at       => t8('Run at'),
109       status       => t8('Execution status'),
110       result       => t8('Result'),
111       error        => t8('Error'),
112     },
113   );
114 }
115
116 sub setup_list_action_bar {
117   my ($self) = @_;
118
119   for my $bar ($::request->layout->get('actionbar')) {
120     $bar->add(
121       link => [
122         t8('Server control'),
123         link => $self->url_for(controller => 'TaskServer', action => 'show'),
124       ],
125       link => [
126         t8('List of jobs'),
127         link => $self->url_for(controller => 'BackgroundJob', action => 'list'),
128       ],
129     );
130   }
131 }
132
133 sub setup_show_action_bar {
134   my ($self) = @_;
135
136   for my $bar ($::request->layout->get('actionbar')) {
137     $bar->add(
138       link => [
139         t8('Back'),
140         link => $self->url_for(action => 'list'),
141       ],
142     );
143   }
144 }
145
146 1;