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