Bug 30500: Allow patrons to change in transit holds pickup locations
[koha.git] / opac / tracklinks.pl
1 #!/usr/bin/perl
2
3 # script to log clicks on links to external urls
4
5 # Copyright 2012 Catalyst IT
6 # This file is part of Koha.
7 #
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.
12 #
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.
17 #
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>.
20
21 use Modern::Perl;
22 use C4::Context;
23 use C4::Auth qw( checkauth );
24 use C4::Biblio;
25 use C4::Output qw( output_error );
26 use Koha::Biblios;
27 use Koha::Items;
28 use Koha::Linktracker;
29 use CGI qw ( -utf8 );
30 use List::MoreUtils qw( any );
31
32 my $cgi = CGI->new;
33 my $uri = $cgi->param('uri') || '';
34 my $biblionumber = $cgi->param('biblionumber');;
35 my $itemnumber   = $cgi->param('itemnumber');
36
37 my $tracking_method = C4::Context->preference('TrackClicks');
38 unless ( $tracking_method ) {
39     output_error( $cgi, '404' );
40     exit;
41 }
42 my $tracker = Koha::Linktracker->new(
43     { trackingmethod => $tracking_method } );
44 if ($uri && ($biblionumber || $itemnumber) ) {
45     my $borrowernumber;
46
47     # we have a uri and we want to track
48     if ( $tracker->trackingmethod() eq 'track' ) {
49         my ( $user, $cookie, $sessionID, $flags ) =
50           checkauth( $cgi, 1, {}, 'opac' );
51         my $userenv = C4::Context->userenv;
52
53         if (   defined($userenv)
54             && ref($userenv) eq 'HASH'
55             && $userenv->{number} )
56         {
57             $borrowernumber = $userenv->{number};
58         }
59
60         # get borrower info
61     }
62
63     my $biblio = Koha::Biblios->find($biblionumber);
64     my $record = eval{ $biblio->metadata->record };
65     my $marc_urls = $record ? C4::Biblio::GetMarcUrls($record, C4::Context->preference('marcflavour')) : [];
66     my $search_crit = { uri => { -like => "%$uri%" } };
67     if( $itemnumber ) { # itemnumber is leading over biblionumber
68         $search_crit->{itemnumber} = $itemnumber;
69     } elsif( $biblionumber ) {
70         $search_crit->{biblionumber} = $biblionumber;
71     }
72     if ( ( any { $_ eq $uri } map { $_->{MARCURL} } @$marc_urls )
73         || Koha::Items->search( $search_crit )->count )
74     {
75         $tracker->trackclick(
76             {
77                 uri            => $uri,
78                 biblionumber   => $biblionumber,
79                 borrowernumber => $borrowernumber,
80                 itemnumber     => $itemnumber
81             }
82         ) if (   $tracker->trackingmethod() eq 'track' || $tracker->trackingmethod() eq 'anonymous' );
83         print $cgi->redirect($uri);
84         exit;
85     }
86 }
87
88 output_error( $cgi, '404' );
89 exit;