Bug 23329: Fix tests
[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;
26 use Koha::Items;
27 use Koha::Linktracker;
28 use CGI qw ( -utf8 );
29 use List::MoreUtils qw(any);
30
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;
35
36 my $tracker = Koha::Linktracker->new(
37     { trackingmethod => C4::Context->preference('TrackClicks') } );
38
39 if ($uri && ($biblionumber || $itemnumber) ) {
40     my $borrowernumber = 0;
41
42     # we have a uri and we want to track
43     if ( $tracker->trackingmethod() eq 'track' ) {
44         my ( $user, $cookie, $sessionID, $flags ) =
45           checkauth( $cgi, 1, {}, 'opac' );
46         my $userenv = C4::Context->userenv;
47
48         if (   defined($userenv)
49             && ref($userenv) eq 'HASH'
50             && $userenv->{number} )
51         {
52             $borrowernumber = $userenv->{number};
53         }
54
55         # get borrower info
56     }
57
58     my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
59     my $marc_urls = $record ? C4::Biblio::GetMarcUrls($record, C4::Context->preference('marcflavour')) : [];
60     my $search_crit = { uri => $uri };
61     if( $itemnumber ) { # itemnumber is leading over biblionumber
62         $search_crit->{itemnumber} = $itemnumber;
63     } elsif( $biblionumber ) {
64         $search_crit->{biblionumber} = $biblionumber;
65     }
66     if ( ( any { $_ eq $uri } map { $_->{MARCURL} } @$marc_urls )
67         || Koha::Items->search( $search_crit )->count )
68     {
69         $tracker->trackclick(
70             {
71                 uri            => $uri,
72                 biblionumber   => $biblionumber,
73                 borrowernumber => $borrowernumber,
74                 itemnumber     => $itemnumber
75             }
76         ) if (   $tracker->trackingmethod() eq 'track' || $tracker->trackingmethod() eq 'anonymous' );
77         print $cgi->redirect($uri);
78         exit;
79     }
80 }
81
82 output_error( $cgi, '404' );
83 exit;