|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# Copyright Doxulting 2004 |
| 4 | +# |
| 5 | +# This file is part of Koha. |
| 6 | +# |
| 7 | +# Koha is free software; you can redistribute it and/or modify it |
| 8 | +# under the terms of the GNU General Public License as published by |
| 9 | +# the Free Software Foundation; either version 3 of the License, or |
| 10 | +# (at your option) any later version. |
| 11 | +# |
| 12 | +# Koha is distributed in the hope that it will be useful, but |
| 13 | +# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# GNU General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU General Public License |
| 18 | +# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 | + |
| 20 | +use Modern::Perl; |
| 21 | + |
| 22 | +use CGI qw ( -utf8 ); |
| 23 | +use Encode; |
| 24 | + |
| 25 | +use C4::Auth qw( get_template_and_user ); |
| 26 | +use C4::Biblio qw(GetMarcSubjects); |
| 27 | +use C4::Output qw( output_html_with_http_headers ); |
| 28 | +use C4::Templates; |
| 29 | +use Koha::Biblios; |
| 30 | +use Koha::Email; |
| 31 | +use Koha::Patrons; |
| 32 | +use Koha::Token; |
| 33 | + |
| 34 | +my $query = CGI->new; |
| 35 | + |
| 36 | +my $pluginsdir = C4::Context->config("pluginsdir"); |
| 37 | +my @pluginsdir = ref($pluginsdir) eq 'ARRAY' ? @$pluginsdir : $pluginsdir; |
| 38 | +my ($PluginDir) = grep { -f $_ . "/Koha/Plugin/EDS.pm" } @pluginsdir; |
| 39 | +$PluginDir = $PluginDir.'/Koha/Plugin/EDS'; |
| 40 | + |
| 41 | +do '../eds-methods.pl'; |
| 42 | +my $eds_data = $query->param('eds_data'); #EDS Patch |
| 43 | + |
| 44 | +my ( $template, $borrowernumber, $cookie ) = get_template_and_user( |
| 45 | + { |
| 46 | + template_name => "opac-sendbasketform.tt", |
| 47 | + query => $query, |
| 48 | + type => "opac", |
| 49 | + } |
| 50 | +); |
| 51 | +my $patron = Koha::Patrons->find($borrowernumber); |
| 52 | +my $user_email = $patron ? $patron->notice_email_address : undef; |
| 53 | + |
| 54 | +my $op = $query->param('op') || q{}; |
| 55 | +my $bib_list = $query->param('bib_list') || ''; |
| 56 | +#convert _dot_ to . to properly search for items |
| 57 | +$bib_list =~s/\_dot\_/\./g; |
| 58 | + |
| 59 | +my $email_add = $query->param('email_add'); |
| 60 | + |
| 61 | +if ( $op eq "cud-send" && $email_add && $user_email ) { |
| 62 | + die "Wrong CSRF token" |
| 63 | + unless Koha::Token->new->check_csrf( |
| 64 | + { |
| 65 | + session_id => scalar $query->cookie('CGISESSID'), |
| 66 | + token => scalar $query->param('csrf_token'), |
| 67 | + } |
| 68 | + ); |
| 69 | + |
| 70 | + my $comment = $query->param('comment'); |
| 71 | + |
| 72 | + my @bibs = split( /\//, $bib_list ); |
| 73 | + my $iso2709; |
| 74 | + my $catbibs; |
| 75 | + my $edscounter = 0; |
| 76 | + foreach my $biblionumber (@bibs) { |
| 77 | + my $biblio = ''; |
| 78 | + my $record = ''; |
| 79 | + if($biblionumber =~m/\_\_/){ |
| 80 | + #if biblionumber matches on __, split into an and db |
| 81 | + my ($biblio_an, $biblio_db) = split(/__/, $biblionumber); |
| 82 | + #add header if EDS items are sent in basket |
| 83 | + if ($edscounter == 0){ |
| 84 | + $comment .= "\n\n________________________________"; |
| 85 | + $comment .= "\n\nEBSCO Discovery Service items are listed first, followed by your library catalog items.\n"; |
| 86 | + } |
| 87 | + my $dat = ''; |
| 88 | + ($record,$dat)= ProcessEDSCartItems($biblionumber,$eds_data,$record,$dat); |
| 89 | + $iso2709 .= $record->as_usmarc() // q{}; |
| 90 | + $edscounter++; |
| 91 | + #EDS data added to comment |
| 92 | + $comment .= "\n".$edscounter.". ".$record->title."\n"; |
| 93 | + $comment .= "Author(s): ".$record->author."\n"; |
| 94 | + #form URL with OPACBaseURL, plugin path for Koha eds-detail.pl, and add biblio_an, biblio_db to form link back to Koha catalog for EDS item |
| 95 | + $comment .= "URL: ".C4::Context->preference('OPACBaseURL').'/plugin/Koha/Plugin/EDS/opac/eds-detail.pl?q=Retrieve?an='.$biblio_an.'|dbid='.$biblio_db."\n"; |
| 96 | + } #EDS Patch |
| 97 | + else { |
| 98 | + $catbibs .= $biblionumber."|"; |
| 99 | + $biblio = Koha::Biblios->find($biblionumber) or next; |
| 100 | + $iso2709 .= $biblio->metadata->record->as_usmarc(); |
| 101 | + } |
| 102 | + } |
| 103 | + if ( !defined $iso2709 ) { |
| 104 | + $template->param( error => 'NO_BODY' ); |
| 105 | + } |
| 106 | + else { |
| 107 | + my %loops; |
| 108 | + if ($catbibs){ |
| 109 | + my @catbibsloops = split( /\|/, $catbibs ); |
| 110 | + %loops = ( biblio => \@catbibsloops, ); |
| 111 | + } |
| 112 | + my %substitute = ( comment => $comment, ); |
| 113 | + |
| 114 | + my $letter = C4::Letters::GetPreparedLetter( |
| 115 | + module => 'catalogue', |
| 116 | + letter_code => 'CART', |
| 117 | + lang => $patron->lang, |
| 118 | + tables => { |
| 119 | + borrowers => $borrowernumber, |
| 120 | + }, |
| 121 | + message_transport_type => 'email', |
| 122 | + loops => \%loops, |
| 123 | + substitute => \%substitute, |
| 124 | + ); |
| 125 | + |
| 126 | + my $attachment = { |
| 127 | + filename => 'basket.iso2709', |
| 128 | + type => 'application/octet-stream', |
| 129 | + content => $iso2709, |
| 130 | + }; |
| 131 | + |
| 132 | + my $message_id = C4::Letters::EnqueueLetter( |
| 133 | + { |
| 134 | + letter => $letter, |
| 135 | + message_transport_type => 'email', |
| 136 | + to_address => $email_add, |
| 137 | + reply_address => $user_email, |
| 138 | + attachments => [$attachment], |
| 139 | + } |
| 140 | + ); |
| 141 | + |
| 142 | + C4::Letters::SendQueuedMessages( { message_id => $message_id } ) if $message_id; |
| 143 | + |
| 144 | + $template->param( SENT => 1 ); |
| 145 | + } |
| 146 | + |
| 147 | + $template->param( email_add => $email_add ); |
| 148 | + output_html_with_http_headers $query, $cookie, $template->output, undef, |
| 149 | + { force_no_caching => 1 }; |
| 150 | + |
| 151 | +} elsif ( !$user_email ) { |
| 152 | + $template->param( email_add => 1, error => 'NO_REPLY_ADDRESS' ); |
| 153 | + output_html_with_http_headers $query, $cookie, $template->output; |
| 154 | + |
| 155 | +} else { |
| 156 | + my $new_session_id = $query->cookie('CGISESSID'); |
| 157 | + $template->param( |
| 158 | + bib_list => $bib_list, |
| 159 | + url => "/cgi-bin/koha/opac-sendbasket.pl", |
| 160 | + suggestion => C4::Context->preference("suggestion"), |
| 161 | + virtualshelves => C4::Context->preference("virtualshelves"), |
| 162 | + csrf_token => |
| 163 | + Koha::Token->new->generate_csrf( { session_id => $new_session_id, } ), |
| 164 | + ); |
| 165 | + output_html_with_http_headers $query, $cookie, $template->output, undef, |
| 166 | + { force_no_caching => 1 }; |
| 167 | +} |
0 commit comments