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