Revert Revert "Anpassungen an neues SL::File::Object Interface"
[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->render('email_journal/list',
36                 title   => $::locale->text('Email journal'),
37                 ENTRIES => $self->models->get,
38                 MODELS  => $self->models);
39 }
40
41 sub action_show {
42   my ($self) = @_;
43
44   $::auth->assert('email_journal');
45
46   my $back_to = $::form->{back_to} || $self->url_for(action => 'list');
47
48   $self->entry(SL::DB::EmailJournal->new(id => $::form->{id})->load);
49
50   if (!$self->can_view_all && ($self->entry->sender_id != SL::DB::Manager::Employee->current->id)) {
51     $::form->error(t8('You do not have permission to access this entry.'));
52   }
53
54   $self->render('email_journal/show',
55                 title   => $::locale->text('View sent email'),
56                 back_to => $back_to);
57 }
58
59 sub action_download_attachment {
60   my ($self) = @_;
61
62   $::auth->assert('email_journal');
63
64   my $attachment = SL::DB::EmailJournalAttachment->new(id => $::form->{id})->load;
65
66   if (!$self->can_view_all && ($attachment->email_journal->sender_id != SL::DB::Manager::Employee->current->id)) {
67     $::form->error(t8('You do not have permission to access this entry.'));
68   }
69   my $ref = \$attachment->content;
70   if ( $attachment->file_id > 0 ) {
71     my $file = SL::File->get(id => $attachment->file_id );
72     $ref = $file->get_content if $file;
73   }
74   $self->send_file($ref, name => $attachment->name, type => $attachment->mime_type);
75 }
76
77 #
78 # filters
79 #
80
81 sub add_stylesheet {
82   $::request->{layout}->use_stylesheet('email_journal.css');
83 }
84
85 #
86 # helpers
87 #
88
89 sub init_can_view_all { $::auth->assert('email_employee_readall', 1) }
90
91 sub init_models {
92   my ($self) = @_;
93
94   my @where;
95   push @where, (sender_id => SL::DB::Manager::Employee->current->id) if !$self->can_view_all;
96
97   SL::Controller::Helper::GetModels->new(
98     controller        => $self,
99     query             => \@where,
100     with_objects      => [ 'sender' ],
101     sorted            => {
102       sender          => t8('Sender'),
103       from            => t8('From'),
104       recipients      => t8('Recipients'),
105       subject         => t8('Subject'),
106       sent_on         => t8('Sent on'),
107       status          => t8('Status'),
108       extended_status => t8('Extended status'),
109     },
110   );
111 }
112
113 sub init_filter_summary {
114   my ($self)  = @_;
115
116   my $filter  = $::form->{filter} || {};
117   my @filters = (
118     [ "from:substr::ilike",       $::locale->text('From')                                         ],
119     [ "recipients:substr::ilike", $::locale->text('Recipients')                                   ],
120     [ "sent_on:date::ge",         $::locale->text('Sent on') . " " . $::locale->text('From Date') ],
121     [ "sent_on:date::le",         $::locale->text('Sent on') . " " . $::locale->text('To Date')   ],
122   );
123
124   my @filter_strings = grep { $_ }
125                        map  { $filter->{ $_->[0] } ? $_->[1] . ' ' . $filter->{ $_->[0] } : undef }
126                        @filters;
127
128   my %status = (
129     failed  => $::locale->text('failed'),
130     ok      => $::locale->text('succeeded'),
131   );
132   push @filter_strings, $status{ $filter->{'status:eq_ignore_empty'} } if $filter->{'status:eq_ignore_empty'};
133
134   return join ', ', @filter_strings;
135 }
136
137 1;