script to edit the item subcategory table.
[koha.git] / acqui.simple / 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   => "acqui.simple/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 = 5 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,'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     if ( $count < ( $offset + $num ) ) {
128         $total = $count;
129     }
130     else {
131         $total = $offset + $num;
132     }    # else
133
134     my @loop_data = ();
135     my $toggle;
136     for ( my $i = $offset ; $i < $total ; $i++ ) {
137         if ( $i % 2 ) {
138             $toggle = 0;
139         } else {
140             $toggle = 1;
141         }
142         my %row_data;    # get a fresh hash for the row data
143         $row_data{toggle}        = $toggle;
144         $row_data{biblionumber}  = $results[$i]->{'biblionumber'};
145         $row_data{title}         = $results[$i]->{'title'};
146         $row_data{author}        = $results[$i]->{'author'};
147         $row_data{copyrightdate} = $results[$i]->{'copyrightdate'};
148                 $row_data{classification} = $results[$i]->{'classification'};
149         $row_data{NOTMARC}       = !$marc_p;    
150         push ( @loop_data, \%row_data );
151     }
152     $template->param( startfrom => $offset + 1 );
153     ( $offset + $num <= $count )
154       ? ( $template->param( endat => $offset + $num ) )
155       : ( $template->param( endat => $count ) );
156     $template->param( numrecords => $count );
157     my $nextstartfrom = ( $offset + $num < $count ) ? ( $offset + $num ) : (-1);
158     my $prevstartfrom = ( $offset - $num >= 0 ) ? ( $offset - $num ) : (-1);
159     $template->param( nextstartfrom => $nextstartfrom );
160     my $displaynext = 1;
161     my $displayprev = 0;
162     ( $nextstartfrom == -1 ) ? ( $displaynext = 0 ) : ( $displaynext = 1 );
163     ( $prevstartfrom == -1 ) ? ( $displayprev = 0 ) : ( $displayprev = 1 );
164     $template->param( displaynext => $displaynext );
165     $template->param( displayprev => $displayprev );
166     my @numbers = ();
167     my $term;
168     my $value;
169
170     if ($isbn) {
171         $term  = "isbn";
172         $value = $isbn;
173     }
174     else {
175         $term  = "title";
176         $value = $title;
177     }
178     if ( $count > 10 ) {
179         for ( my $i = 1 ; $i < $count / 10 + 1 ; $i++ ) {
180             if ( $i < 16 ) {
181                 my $highlight = 0;
182                 ( $offset == ( $i - 1 ) * 10 ) && ( $highlight = 1 );
183                 push @numbers,
184                   {
185                     number    => $i,
186                     highlight => $highlight,
187                     term      => $term,
188                     value     => $value,
189                     startfrom => ( $i - 1 ) * 10
190                 };
191             }
192         }
193     }
194
195     # fill with books in breeding farm
196         my $toggle=0;
197     my ( $countbr, @resultsbr ) = BreedingSearch( @value[0], @value[1] );
198     my @breeding_loop = ();
199     for ( my $i = 0 ; $i <= $#resultsbr ; $i++ ) {
200         my %row_data;
201         if ( $i % 2 ) {
202             $toggle = 0;
203         }
204         else {
205             $toggle = 1;
206         }
207         $row_data{toggle} = $toggle;
208         $row_data{id}     = $resultsbr[$i]->{'id'};
209         $row_data{isbn}   = $resultsbr[$i]->{'isbn'};
210         $row_data{file}   = $resultsbr[$i]->{'file'};
211         $row_data{title}  = $resultsbr[$i]->{'title'};
212         $row_data{author} = $resultsbr[$i]->{'author'};
213         $row_data{NOTMARC}= !$marc_p;   
214         push ( @breeding_loop, \%row_data );
215     }
216
217         # get framework list
218         my $frameworks = getframeworks;
219         my @frameworkcodeloop;
220         foreach my $thisframeworkcode (keys %$frameworks) {
221                 my %row =(value => $thisframeworkcode,
222                                         frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
223                                 );
224                 push @frameworkcodeloop, \%row;
225         }
226
227     $template->param(
228                 title             => $title,
229                 isbn              => $isbn,
230                                                         startfrom=> $startfrom,
231                                                         displaynext=> $displaynext,
232                                                         displayprev=> $displayprev,
233                                                         resultsperpage => $resultsperpage,
234                                                         startfromnext => $startfrom+1,
235                                                         startfromprev => $startfrom-1,
236                                                         searchdata=>\@field_data,
237                                                         numbers=>\@numbers,
238                                                         from => $from,
239                                                         to => $to,
240         total         => $total,
241 #         offset        => $offset,
242         loop          => $results,
243         breeding_loop => \@breeding_loop,
244         NOTMARC       => !$marc_p,
245                 frameworkcodeloop => \@frameworkcodeloop,
246     );
247
248     print $input->header(
249         -type   => guesstype( $template->output ),
250         -cookie => $cookie
251       ),
252       $template->output;