Bug 24434: Add Unit Tests for relations
[koha.git] / basket / sendbasket.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use CGI qw ( -utf8 );
21 use Encode qw(encode);
22 use Carp;
23 use Try::Tiny;
24
25 use C4::Biblio;
26 use C4::Items;
27 use C4::Auth;
28 use C4::Output;
29 use C4::Templates ();
30 use Koha::Email;
31 use Koha::Token;
32
33 my $query = CGI->new;
34
35 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
36     {
37         template_name   => "basket/sendbasketform.tt",
38         query           => $query,
39         type            => "intranet",
40         flagsrequired   => { catalogue => 1 },
41     }
42 );
43
44 my $bib_list  = $query->param('bib_list') || '';
45 my $email_add = $query->param('email_add');
46
47 my $dbh = C4::Context->dbh;
48
49 if ( $email_add ) {
50     output_and_exit( $query, $cookie, $template, 'wrong_csrf_token' )
51         unless Koha::Token->new->check_csrf({
52             session_id => scalar $query->cookie('CGISESSID'),
53             token  => scalar $query->param('csrf_token'),
54         });
55     my $comment = $query->param('comment');
56
57     # Since we are already logged in, no need to check credentials again
58     # when loading a second template.
59     my $template2 = C4::Templates::gettemplate(
60         'basket/sendbasket.tt', 'intranet', $query,
61     );
62
63     my @bibs = split( /\//, $bib_list );
64     my @results;
65     my $iso2709;
66     my $marcflavour = C4::Context->preference('marcflavour');
67     foreach my $biblionumber (@bibs) {
68         $template2->param( biblionumber => $biblionumber );
69
70         my $dat              = GetBiblioData($biblionumber);
71         next unless $dat;
72         my $record           = GetMarcBiblio({
73             biblionumber => $biblionumber,
74             embed_items => 1 });
75         my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
76         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
77
78         my @items = GetItemsInfo( $biblionumber );
79
80         my $hasauthors = 0;
81         if($dat->{'author'} || @$marcauthorsarray) {
82           $hasauthors = 1;
83         }
84         
85
86         $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
87         $dat->{MARCAUTHORS}    = $marcauthorsarray;
88         $dat->{HASAUTHORS}     = $hasauthors;
89         $dat->{'biblionumber'} = $biblionumber;
90         $dat->{ITEM_RESULTS}   = \@items;
91
92         $iso2709 .= $record->as_usmarc();
93
94         push( @results, $dat );
95     }
96
97     my $resultsarray = \@results;
98     $template2->param(
99         BIBLIO_RESULTS => $resultsarray,
100         comment        => $comment
101     );
102
103     # Getting template result
104     my $template_res = $template2->output();
105     my $body;
106
107     my $subject;
108     # Analysing information and getting mail properties
109     if ( $template_res =~ /<SUBJECT>(?<subject>.*)<END_SUBJECT>/s ) {
110         $subject = $+{subject};
111         $subject =~ s|\n?(.*)\n?|$1|;
112     }
113     else {
114         $subject = "no subject";
115     }
116
117     my $email_header = "";
118     if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
119         $email_header = $1;
120         $email_header =~ s|\n?(.*)\n?|$1|;
121     }
122
123     if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
124         $body = $1;
125         $body =~ s|\n?(.*)\n?|$1|;
126     }
127
128     my $THE_body = <<END_OF_BODY;
129 $email_header
130 $body
131 END_OF_BODY
132
133     try {
134
135         my $email = Koha::Email->create(
136             {
137                 to      => $email_add,
138                 subject => $subject,
139             }
140         );
141
142         $email->text_body( $THE_body );
143         $email->attach(
144             Encode::encode( "UTF-8", $iso2709 ),
145             content_type => 'application/octet-stream',
146             name         => 'basket.iso2709',
147             disposition  => 'attachment',
148         );
149
150         my $library = Koha::Patrons->find( $borrowernumber )->library;
151         $email->send_or_die({ transport => $library->smtp_server->transport });
152         $template->param( SENT => "1" );
153     }
154     catch {
155         carp "Error sending mail: $_";
156         $template->param( error => 1 );
157     };
158
159     $template->param( email_add => $email_add );
160     output_html_with_http_headers $query, $cookie, $template->output;
161 }
162 else {
163     $template->param(
164         bib_list       => $bib_list,
165         url            => "/cgi-bin/koha/basket/sendbasket.pl",
166         suggestion     => C4::Context->preference("suggestion"),
167         virtualshelves => C4::Context->preference("virtualshelves"),
168         csrf_token     => Koha::Token->new->generate_csrf({ session_id => scalar $query->cookie('CGISESSID'), }),
169     );
170     output_html_with_http_headers $query, $cookie, $template->output;
171 }