Bug 12443 - Initial re-factoring of buildQuery
[koha.git] / catalogue / labeledMARCdetail.pl
1 #!/usr/bin/perl
2
3 # Copyright 2008-2009 LibLime
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22 use CGI; 
23 use MARC::Record;
24 use C4::Auth;
25 use C4::Context;
26 use C4::Output;
27 use C4::Biblio;
28 use C4::Items;
29 use C4::Members; # to use GetMember
30 use C4::Search;         # enabled_staff_search_views
31 use C4::Acquisition qw(GetOrdersByBiblionumber);
32
33 my $query        = new CGI;
34 my $dbh          = C4::Context->dbh;
35 my $biblionumber = $query->param('biblionumber');
36 my $frameworkcode = $query->param('frameworkcode');
37 $frameworkcode = GetFrameworkCode( $biblionumber ) unless ($frameworkcode);
38 my $popup        =
39   $query->param('popup')
40   ;    # if set to 1, then don't insert links, it's just to show the biblio
41
42 # open template
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
44     {
45         template_name   => "catalogue/labeledMARCdetail.tmpl",
46         query           => $query,
47         type            => "intranet",
48         authnotrequired => 0,
49         flagsrequired   => { catalogue => 1 },
50         debug           => 1,
51     }
52 );
53
54 my $record = GetMarcBiblio($biblionumber);
55 if ( not defined $record ) {
56     # biblionumber invalid -> report and exit
57     $template->param( unknownbiblionumber => 1,
58                 biblionumber => $biblionumber
59     );
60     output_html_with_http_headers $query, $cookie, $template->output;
61     exit;
62 }
63
64 my $tagslib = GetMarcStructure(1,$frameworkcode);
65 my $biblio = GetBiblioData($biblionumber);
66
67 if($query->cookie("holdfor")){ 
68     my $holdfor_patron = GetMember('borrowernumber' => $query->cookie("holdfor"));
69     $template->param(
70         holdfor => $query->cookie("holdfor"),
71         holdfor_surname => $holdfor_patron->{'surname'},
72         holdfor_firstname => $holdfor_patron->{'firstname'},
73         holdfor_cardnumber => $holdfor_patron->{'cardnumber'},
74     );
75 }
76
77 #count of item linked
78 my $itemcount = GetItemsCount($biblionumber);
79 $template->param( count => $itemcount,
80                                         bibliotitle => $biblio->{title}, );
81
82 #Getting the list of all frameworks
83 my $queryfwk =
84   $dbh->prepare("select frameworktext, frameworkcode from biblio_framework");
85 $queryfwk->execute;
86 my %select_fwk;
87 my @select_fwk;
88 my $curfwk;
89 push @select_fwk, "Default";
90 $select_fwk{"Default"} = "Default";
91
92 while ( my ( $description, $fwk ) = $queryfwk->fetchrow ) {
93     push @select_fwk, $fwk;
94     $select_fwk{$fwk} = $description;
95 }
96 $curfwk=$frameworkcode;
97 my $framework=CGI::scrolling_list( -name     => 'Frameworks',
98             -id => 'Frameworks',
99             -default => $curfwk,
100             -OnChange => 'Changefwk(this);',
101             -values   => \@select_fwk,
102             -labels   => \%select_fwk,
103             -size     => 1,
104             -multiple => 0 );
105 $template->param(framework => $framework);
106
107 my @marc_data;
108 my $prevlabel = '';
109 for my $field ($record->fields)
110 {
111         my $tag = $field->tag;
112         next if ! exists $tagslib->{$tag}->{lib};
113         my $label = $tagslib->{$tag}->{lib};
114         if ($label eq $prevlabel)
115         {
116                 $label = '';
117         }
118         else
119         {
120                 $prevlabel = $label;
121         }
122         my $value = $tag < 10
123                 ? $field->data
124                 : join ' ', map { $_->[1] } $field->subfields;
125         push @marc_data, {
126                 label => $label,
127                 value => $value,
128         };
129 }
130
131 $template->param (
132         marc_data                               => \@marc_data,
133     biblionumber            => $biblionumber,
134     popup                   => $popup,
135         labeledmarcview => 1,
136         z3950_search_params             => C4::Search::z3950_search_args($biblio),
137         C4::Search::enabled_staff_search_views,
138     searchid            => $query->param('searchid'),
139 );
140
141 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
142 my @deletedorders_using_biblio;
143 my @orders_using_biblio;
144 my @baskets_orders;
145 my @baskets_deletedorders;
146
147 foreach my $myorder (@allorders_using_biblio) {
148     my $basket = $myorder->{'basketno'};
149     if ((defined $myorder->{'datecancellationprinted'}) and  ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
150         push @deletedorders_using_biblio, $myorder;
151         unless (grep(/^$basket$/, @baskets_deletedorders)){
152             push @baskets_deletedorders,$myorder->{'basketno'};
153         }
154     }
155     else {
156         push @orders_using_biblio, $myorder;
157         unless (grep(/^$basket$/, @baskets_orders)){
158             push @baskets_orders,$myorder->{'basketno'};
159             }
160     }
161 }
162
163 my $count_orders_using_biblio = scalar @orders_using_biblio ;
164 $template->param (countorders => $count_orders_using_biblio);
165
166 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
167 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
168
169 my $holds= C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
170 my $holdcount = scalar( @$holds );
171 $template->param( holdcount => scalar ( @$holds ) );
172
173 output_html_with_http_headers $query, $cookie, $template->output;