remove char_decode
[koha.git] / cataloguing / isbnsearch.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use CGI;
22 use C4::Auth;
23 use C4::Biblio;
24 # use C4::Search;
25 use C4::Breeding;
26 use C4::SearchMarc;
27 use C4::Output;
28 use C4::Interface::CGI::Output;
29 use HTML::Template;
30 use C4::Koha;
31
32 my $input      = new CGI;
33 my $offset     = $input->param('offset');
34 my $num        = $input->param('num');
35 # my $total;
36 # my $count;
37 # my @results;
38 my $marc_p = C4::Context->boolean_preference("marc");
39 my $dbh = C4::Context->dbh;
40
41     my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42         {
43             template_name   => "cataloguing/isbnsearch.tmpl",
44             query           => $input,
45             type            => "intranet",
46             authnotrequired => 0,
47             flagsrequired   => { editcatalogue => 1 },
48             debug           => 1,
49         }
50     );
51
52     # fill with books in ACTIVE DB (biblio)
53     if ( !$offset ) {
54         $offset     = 0;
55     }
56     if ( !$num ) { $num = 10 }
57         my @marclist = $input->param('marclist');
58         my @and_or = $input->param('and_or');
59         my @excluding = $input->param('excluding');
60         my @operator = $input->param('operator');
61         my @value = $input->param('value');
62         my $title= @value[0];
63         my $isbn = @value[1];
64         my $resultsperpage= $input->param('resultsperpage');
65         $resultsperpage = 10 if(!defined $resultsperpage);
66         my $startfrom=$input->param('startfrom');
67         $startfrom=0 if(!defined $startfrom);
68         my $orderby = $input->param('orderby');
69         my $desc_or_asc = $input->param('desc_or_asc');
70
71         # builds tag and subfield arrays
72         my @tags;
73
74         foreach my $marc (@marclist) {
75                 if ($marc) {
76                         my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,$marc,'');
77                         if ($tag) {
78                                 push @tags,$dbh->quote("$tag$subfield");
79                         } else {
80                                 push @tags, $dbh->quote(substr($marc,0,4));
81                         }
82                 } else {
83                         push @tags, "";
84                 }
85         }
86         findseealso($dbh,\@tags);
87         my ($results,$total) = catalogsearch($dbh, \@tags,\@and_or,
88                                                                                 \@excluding, \@operator, \@value,
89                                                                                 $startfrom*$resultsperpage, $resultsperpage,'biblio.title','ASC');
90 #       @results = @$resultsref;
91
92 #     my @loop_data = ();
93 #     my $toggle;
94 #     for ( my $i = $offset ; $i < $total ; $i++ ) {
95 #         if ( $i % 2 ) {
96 #             $toggle = 0;
97 #         } else {
98 #             $toggle = 1;
99 #         }
100 #         my %row_data;    # get a fresh hash for the row data
101 #         $row_data{toggle}        = $toggle;
102 #         $row_data{biblionumber}  = $results[$i]->{'biblionumber'};
103 #         $row_data{title}         = $results[$i]->{'title'};
104 #         $row_data{author}        = $results[$i]->{'author'};
105 #         $row_data{copyrightdate} = $results[$i]->{'copyrightdate'};
106 #               $row_data{classification} = $results[$i]->{'classification'};
107 #         $row_data{NOTMARC}       = !$marc_p;  
108 #         push ( @loop_data, \%row_data );
109 #     }
110         # multi page display gestion
111         my $displaynext=0;
112         my $displayprev=$startfrom;
113         if(($total - (($startfrom+1)*($resultsperpage))) > 0 ) {
114                 $displaynext = 1;
115         }
116
117         my @field_data = ();
118
119         for(my $i = 0 ; $i <= $#marclist ; $i++) {
120                 push @field_data, { term => "marclist", val=>$marclist[$i] };
121                 push @field_data, { term => "and_or", val=>$and_or[$i] };
122                 push @field_data, { term => "excluding", val=>$excluding[$i] };
123                 push @field_data, { term => "operator", val=>$operator[$i] };
124                 push @field_data, { term => "value", val=>$value[$i] };
125         }
126
127         my @numbers = ();
128
129         if ($total>$resultsperpage) {
130                 for (my $i=1; $i<$total/$resultsperpage+1; $i++) {
131                         if ($i<16) {
132                         my $highlight=0;
133                         ($startfrom==($i-1)) && ($highlight=1);
134                         push @numbers, { number => $i,
135                                         highlight => $highlight ,
136                                         searchdata=> \@field_data,
137                                         startfrom => ($i-1)};
138                         }
139         }
140         }
141         my $from = $startfrom*$resultsperpage+1;
142         my $to;
143
144         if($total < (($startfrom+1)*$resultsperpage))
145         {
146                 $to = $total;
147         } else {
148                 $to = (($startfrom+1)*$resultsperpage);
149         }
150
151     # fill with books in breeding farm
152         my $toggle=0;
153     my ( $countbr, @resultsbr ) = BreedingSearch( @value[0], @value[1] );
154     my @breeding_loop = ();
155     for ( my $i = 0 ; $i <= $#resultsbr ; $i++ ) {
156         my %row_data;
157         if ( $i % 2 ) {
158             $toggle = 0;
159         }
160         else {
161             $toggle = 1;
162         }
163         $row_data{toggle} = $toggle;
164         $row_data{id}     = $resultsbr[$i]->{'id'};
165         $row_data{isbn}   = $resultsbr[$i]->{'isbn'};
166         $row_data{file}   = $resultsbr[$i]->{'file'};
167         $row_data{title}  = $resultsbr[$i]->{'title'};
168         $row_data{author} = $resultsbr[$i]->{'author'};
169         $row_data{NOTMARC}= !$marc_p;   
170         push ( @breeding_loop, \%row_data );
171     }
172
173         # get framework list
174         my $frameworks = getframeworks;
175         my @frameworkcodeloop;
176         foreach my $thisframeworkcode (keys %$frameworks) {
177                 my %row =(value => $thisframeworkcode,
178                                         frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
179                                 );
180                 push @frameworkcodeloop, \%row;
181         }
182
183     $template->param(
184                 title             => $title,
185                 isbn              => $isbn,
186                                                         startfrom=> $startfrom,
187                                                         displaynext=> $displaynext,
188                                                         displayprev=> $displayprev,
189                                                         resultsperpage => $resultsperpage,
190                                                         startfromnext => $startfrom+1,
191                                                         startfromprev => $startfrom-1,
192                                                         searchdata=>\@field_data,
193                                                         numbers=>\@numbers,
194                                                         from => $from,
195                                                         to => $to,
196         total         => $total,
197 #         offset        => $offset,
198         loop          => $results,
199         breeding_loop => \@breeding_loop,
200         NOTMARC       => !$marc_p,
201                 frameworkcodeloop => \@frameworkcodeloop,
202     );
203
204     print $input->header(
205         -type   => guesstype( $template->output ),
206         -cookie => $cookie
207       ),
208       $template->output;