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