Refactoring
[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->render('background_job_history/list',
33                 title   => $::locale->text('Background job history'),
34                 ENTRIES => $self->models->get,
35                 MODELS  => $self->models);
36 }
37
38 sub action_show {
39   my ($self) = @_;
40
41   my $back_to = $::form->{back_to} || $self->url_for(action => 'list');
42
43   $self->history(SL::DB::BackgroundJobHistory->new(id => $::form->{id})->load);
44   $self->render('background_job_history/show',
45                 title   => $::locale->text('View background job execution result'),
46                 back_to => $back_to);
47 }
48
49 #
50 # filters
51 #
52
53 sub check_auth {
54   $::auth->assert('admin');
55 }
56
57 #
58 # helpers
59 #
60
61 sub init_task_server {
62   return SL::System::TaskServer->new;
63 }
64
65 sub check_task_server {
66   my ($self) = @_;
67   flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;
68 }
69
70 sub add_stylesheet {
71   $::request->{layout}->use_stylesheet('lx-office-erp/background_jobs.css');
72 }
73
74 sub make_filter_summary {
75   my ($self)  = @_;
76
77   my $filter  = $::form->{filter} || {};
78   my @filters = (
79     [ "package_name:substr::ilike", $::locale->text('Package name')                                ],
80     [ "result:substr::ilike",       $::locale->text('Result')                                      ],
81     [ "error:substr::ilike",        $::locale->text('Error')                                       ],
82     [ "run_at:date::ge",            $::locale->text('Run at') . " " . $::locale->text('From Date') ],
83     [ "run_at:date::le",            $::locale->text('Run at') . " " . $::locale->text('To Date')   ],
84   );
85
86   my @filter_strings = grep { $_ }
87                        map  { $filter->{ $_->[0] } ? $_->[1] . ' ' . $filter->{ $_->[0] } : undef }
88                        @filters;
89
90   my %status = (
91     failed   => $::locale->text('failed'),
92     success  => $::locale->text('succeeded'),
93   );
94   push @filter_strings, $status{ $filter->{'status:eq_ignore_empty'} } if $filter->{'status:eq_ignore_empty'};
95
96   $self->filter_summary(join(', ', @filter_strings));
97 }
98
99 sub init_models {
100   my ($self) = @_;
101
102   SL::Controller::Helper::GetModels->new(
103     controller => $self,
104     sorted => {
105       package_name => t8('Package name'),
106       run_at       => t8('Run at'),
107       status       => t8('Execution status'),
108       result       => t8('Result'),
109       error        => t8('Error'),
110     },
111   );
112 }
113
114 1;