marei: headline/colspec keys for SimpleTabular
[kivitendo-erp.git] / SL / Controller / EmailJournal.pm
1 package SL::Controller::EmailJournal;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use SL::Controller::Helper::GetModels;
8 use SL::DB::Employee;
9 use SL::DB::EmailJournal;
10 use SL::DB::EmailJournalAttachment;
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(entry) ],
18   'scalar --get_set_init' => [ qw(models can_view_all filter_summary) ],
19 );
20
21 __PACKAGE__->run_before('add_stylesheet');
22
23 #
24 # actions
25 #
26
27 sub action_list {
28   my ($self) = @_;
29
30   $::auth->assert('email_journal');
31
32   if ( $::instance_conf->get_email_journal == 0 ) {
33     flash('info',  $::locale->text('Storing the emails in the journal is currently disabled in the client configuration.'));
34   }
35   $self->setup_list_action_bar;
36   $self->render('email_journal/list',
37                 title   => $::locale->text('Email journal'),
38                 ENTRIES => $self->models->get,
39                 MODELS  => $self->models);
40 }
41
42 sub action_show {
43   my ($self) = @_;
44
45   $::auth->assert('email_journal');
46
47   my $back_to = $::form->{back_to} || $self->url_for(action => 'list');
48
49   $self->entry(SL::DB::EmailJournal->new(id => $::form->{id})->load);
50
51   if (!$self->can_view_all && ($self->entry->sender_id != SL::DB::Manager::Employee->current->id)) {
52     $::form->error(t8('You do not have permission to access this entry.'));
53   }
54
55   $self->setup_show_action_bar;
56   $self->render('email_journal/show',
57                 title   => $::locale->text('View sent email'),
58                 back_to => $back_to);
59 }
60
61 sub action_download_attachment {
62   my ($self) = @_;
63
64   $::auth->assert('email_journal');
65
66   my $attachment = SL::DB::EmailJournalAttachment->new(id => $::form->{id})->load;
67
68   if (!$self->can_view_all && ($attachment->email_journal->sender_id != SL::DB::Manager::Employee->current->id)) {
69     $::form->error(t8('You do not have permission to access this entry.'));
70   }
71   my $ref = \$attachment->content;
72   if ( $attachment->file_id > 0 ) {
73     my $file = SL::File->get(id => $attachment->file_id );
74     $ref = $file->get_content if $file;
75   }
76   $self->send_file($ref, name => $attachment->name, type => $attachment->mime_type);
77 }
78
79 #
80 # filters
81 #
82
83 sub add_stylesheet {
84   $::request->{layout}->use_stylesheet('email_journal.css');
85 }
86
87 #
88 # helpers
89 #
90
91 sub init_can_view_all { $::auth->assert('email_employee_readall', 1) }
92
93 sub init_models {
94   my ($self) = @_;
95
96   my @where;
97   push @where, (sender_id => SL::DB::Manager::Employee->current->id) if !$self->can_view_all;
98
99   SL::Controller::Helper::GetModels->new(
100     controller        => $self,
101     query             => \@where,
102     with_objects      => [ 'sender' ],
103     sorted            => {
104       sender          => t8('Sender'),
105       from            => t8('From'),
106       recipients      => t8('Recipients'),
107       subject         => t8('Subject'),
108       sent_on         => t8('Sent on'),
109       status          => t8('Status'),
110       extended_status => t8('Extended status'),
111     },
112   );
113 }
114
115 sub init_filter_summary {
116   my ($self)  = @_;
117
118   my $filter  = $::form->{filter} || {};
119   my @filters = (
120     [ "from:substr::ilike",       $::locale->text('From')                                         ],
121     [ "recipients:substr::ilike", $::locale->text('Recipients')                                   ],
122     [ "sent_on:date::ge",         $::locale->text('Sent on') . " " . $::locale->text('From Date') ],
123     [ "sent_on:date::le",         $::locale->text('Sent on') . " " . $::locale->text('To Date')   ],
124   );
125
126   my @filter_strings = grep { $_ }
127                        map  { $filter->{ $_->[0] } ? $_->[1] . ' ' . $filter->{ $_->[0] } : undef }
128                        @filters;
129
130   my %status = (
131     failed  => $::locale->text('failed'),
132     ok      => $::locale->text('succeeded'),
133   );
134   push @filter_strings, $status{ $filter->{'status:eq_ignore_empty'} } if $filter->{'status:eq_ignore_empty'};
135
136   return join ', ', @filter_strings;
137 }
138
139 sub setup_list_action_bar {
140   my ($self) = @_;
141
142   for my $bar ($::request->layout->get('actionbar')) {
143     $bar->add(
144       action => [
145         t8('Filter'),
146         submit    => [ '#filter_form', { action => 'EmailJournal/list' } ],
147         accesskey => 'enter',
148       ],
149     );
150   }
151 }
152
153 sub setup_show_action_bar {
154   my ($self) = @_;
155
156   for my $bar ($::request->layout->get('actionbar')) {
157     $bar->add(
158       action => [
159         t8('Back'),
160         call => [ 'kivi.history_back' ],
161       ],
162     );
163   }
164 }
165
166 1;