Bug 32482: (follow-up) Add markup comments
[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;
22 use Carp qw( carp );
23 use Try::Tiny qw( catch try );
24
25 use C4::Biblio qw(
26     GetMarcSubjects
27 );
28 use C4::Auth qw( get_template_and_user );
29 use C4::Output qw( output_and_exit output_html_with_http_headers );
30 use C4::Templates;
31 use Koha::Biblios;
32 use Koha::Email;
33 use Koha::Token;
34
35 my $query = CGI->new;
36
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
38     {
39         template_name   => "basket/sendbasketform.tt",
40         query           => $query,
41         type            => "intranet",
42         flagsrequired   => { catalogue => 1 },
43     }
44 );
45
46 my $bib_list  = $query->param('bib_list') || '';
47 my $email_add = $query->param('email_add');
48
49 my $dbh = C4::Context->dbh;
50
51 if ( $email_add ) {
52     output_and_exit( $query, $cookie, $template, 'wrong_csrf_token' )
53         unless Koha::Token->new->check_csrf({
54             session_id => scalar $query->cookie('CGISESSID'),
55             token  => scalar $query->param('csrf_token'),
56         });
57     my $comment = $query->param('comment');
58
59     # Since we are already logged in, no need to check credentials again
60     # when loading a second template.
61     my $template2 = C4::Templates::gettemplate(
62         'basket/sendbasket.tt', 'intranet', $query,
63     );
64
65     my @bibs = split( /\//, $bib_list );
66     my @results;
67     my $iso2709;
68     my $marcflavour = C4::Context->preference('marcflavour');
69     foreach my $biblionumber (@bibs) {
70         $template2->param( biblionumber => $biblionumber );
71
72         my $biblio           = Koha::Biblios->find( $biblionumber ) or next;
73         my $dat              = $biblio->unblessed;
74         my $record           = $biblio->metadata->record({ embed_items => 1 });
75         my $marcauthorsarray = $biblio->get_marc_contributors;
76         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
77
78         my $hasauthors = 0;
79         if($dat->{'author'} || @$marcauthorsarray) {
80           $hasauthors = 1;
81         }
82         
83
84         $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
85         $dat->{MARCAUTHORS}    = $marcauthorsarray;
86         $dat->{HASAUTHORS}     = $hasauthors;
87         $dat->{'biblionumber'} = $biblionumber;
88         $dat->{ITEM_RESULTS}   = $biblio->items->search_ordered;
89         my ( $host, $relatedparts ) = $biblio->get_marc_host;
90         $dat->{HOSTITEMENTRIES} = $host;
91         $dat->{RELATEDPARTS} = $relatedparts;
92
93         $iso2709 .= $record->as_usmarc();
94
95         push( @results, $dat );
96     }
97
98     my $resultsarray = \@results;
99     $template2->param(
100         BIBLIO_RESULTS => $resultsarray,
101         comment        => $comment
102     );
103
104     # Getting template result
105     my $template_res = $template2->output();
106     my $body;
107
108     my $subject;
109     # Analysing information and getting mail properties
110     if ( $template_res =~ /<SUBJECT>(?<subject>.*)<END_SUBJECT>/s ) {
111         $subject = $+{subject};
112         $subject =~ s|\n?(.*)\n?|$1|;
113     }
114     else {
115         $subject = "no subject";
116     }
117
118     my $email_header = "";
119     if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
120         $email_header = $1;
121         $email_header =~ s|\n?(.*)\n?|$1|;
122     }
123
124     if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
125         $body = $1;
126         $body =~ s|\n?(.*)\n?|$1|;
127     }
128
129     my $THE_body = <<END_OF_BODY;
130 $email_header
131 $body
132 END_OF_BODY
133
134     try {
135
136         my $email = Koha::Email->create(
137             {
138                 to      => $email_add,
139                 subject => $subject,
140             }
141         );
142
143         $email->text_body( $THE_body );
144         $email->attach(
145             Encode::encode( "UTF-8", $iso2709 ),
146             content_type => 'application/octet-stream',
147             name         => 'basket.iso2709',
148             disposition  => 'attachment',
149         );
150
151         my $library = Koha::Patrons->find( $borrowernumber )->library;
152         $email->send_or_die({ transport => $library->smtp_server->transport });
153         $template->param( SENT => "1" );
154     }
155     catch {
156         carp "Error sending mail: $_";
157         $template->param( error => 1 );
158     };
159
160     $template->param( email_add => $email_add );
161     output_html_with_http_headers $query, $cookie, $template->output;
162 }
163 else {
164     $template->param(
165         bib_list       => $bib_list,
166         url            => "/cgi-bin/koha/basket/sendbasket.pl",
167         suggestion     => C4::Context->preference("suggestion"),
168         virtualshelves => C4::Context->preference("virtualshelves"),
169         csrf_token     => Koha::Token->new->generate_csrf({ session_id => scalar $query->cookie('CGISESSID'), }),
170     );
171     output_html_with_http_headers $query, $cookie, $template->output;
172 }