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