Bug 22417: Add Koha::BackgroundJob::BatchUpdateAuthority
[koha.git] / opac / tracklinks.pl
blob2c0d2136174cd87bd871f7b2dd3092c3068a4262
1 #!/usr/bin/perl
3 # script to log clicks on links to external urls
5 # Copyright 2012 Catalyst IT
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use Modern::Perl;
22 use C4::Context;
23 use C4::Auth qw(checkauth);
24 use C4::Biblio;
25 use C4::Output;
26 use Koha::Items;
27 use Koha::Linktracker;
28 use CGI qw ( -utf8 );
29 use List::MoreUtils qw(any);
31 my $cgi = new CGI;
32 my $uri = $cgi->param('uri') || '';
33 my $biblionumber = $cgi->param('biblionumber') || 0;
34 my $itemnumber = $cgi->param('itemnumber') || 0;
36 my $tracking_method = C4::Context->preference('TrackClicks');
37 unless ( $tracking_method ) {
38 output_error( $cgi, '404' );
39 exit;
41 my $tracker = Koha::Linktracker->new(
42 { trackingmethod => $tracking_method } );
43 if ($uri && ($biblionumber || $itemnumber) ) {
44 my $borrowernumber = 0;
46 # we have a uri and we want to track
47 if ( $tracker->trackingmethod() eq 'track' ) {
48 my ( $user, $cookie, $sessionID, $flags ) =
49 checkauth( $cgi, 1, {}, 'opac' );
50 my $userenv = C4::Context->userenv;
52 if ( defined($userenv)
53 && ref($userenv) eq 'HASH'
54 && $userenv->{number} )
56 $borrowernumber = $userenv->{number};
59 # get borrower info
62 my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
63 my $marc_urls = $record ? C4::Biblio::GetMarcUrls($record, C4::Context->preference('marcflavour')) : [];
64 my $search_crit = { uri => $uri };
65 if( $itemnumber ) { # itemnumber is leading over biblionumber
66 $search_crit->{itemnumber} = $itemnumber;
67 } elsif( $biblionumber ) {
68 $search_crit->{biblionumber} = $biblionumber;
70 if ( ( any { $_ eq $uri } map { $_->{MARCURL} } @$marc_urls )
71 || Koha::Items->search( $search_crit )->count )
73 $tracker->trackclick(
75 uri => $uri,
76 biblionumber => $biblionumber,
77 borrowernumber => $borrowernumber,
78 itemnumber => $itemnumber
80 ) if ( $tracker->trackingmethod() eq 'track' || $tracker->trackingmethod() eq 'anonymous' );
81 print $cgi->redirect($uri);
82 exit;
86 output_error( $cgi, '404' );
87 exit;