wr72054 fixing the z3950 search for acquisitions
[koha.git] / cataloguing / value_builder / unimarc_field_4XX.pl
blob7eadc5d60e9a5ecdd5d0160a6d690c041ec3bf20
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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.
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.
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.
21 use strict;
22 #use warnings; FIXME - Bug 2505
24 use CGI;
25 use C4::Output;
26 use C4::Context;
27 use C4::Search;
28 use C4::Auth;
29 use C4::Output;
31 use C4::Biblio;
32 use C4::Koha;
33 use MARC::Record;
34 use C4::Branch; # GetBranches
35 use C4::ItemType;
37 sub plugin_parameters {
38 my ( $dbh, $record, $tagslib, $i, $tabloop ) = @_;
39 return "";
42 sub plugin_javascript {
43 my ( $dbh, $record, $tagslib, $field_number, $tabloop ) = @_;
44 my $function_name = $field_number;
45 my $res = "
46 <script type='text/javascript'>
47 function Focus$function_name(subfield_managed) {
48 return 1;
51 function Blur$function_name(subfield_managed) {
52 return 1;
55 function Clic$function_name(i) {
56 defaultvalue=document.getElementById(\"$field_number\").value;
57 window.open(\"/cgi-bin/koha/cataloguing/plugin_launcher.pl?plugin_name=unimarc_field_4XX.pl&index=\" + i + \"&result=\"+defaultvalue,\"unimarc_field_4\"+i+\"\",'width=900,height=700,toolbar=false,scrollbars=yes');
60 </script>
63 return ( $function_name, $res );
66 # sub plugin
68 # input arg :
69 # -- op could be equals to
70 # * fillinput :
71 # * do_search :
74 sub plugin {
75 my ($input) = @_;
76 my $dbh = C4::Context->dbh;
77 my $query = new CGI;
78 my $op = $query->param('op');
79 my $type = $query->param('type');
80 my $startfrom = $query->param('startfrom');
81 $startfrom = 0 if ( !defined $startfrom );
82 my ( $template, $loggedinuser, $cookie );
83 my $resultsperpage;
84 my $searchdesc;
86 if ( $op eq "fillinput" ) {
87 my $biblionumber = $query->param('biblionumber');
88 my $index = $query->param('index');
89 my $marcrecord;
91 # open template
92 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
94 template_name =>
95 "cataloguing/value_builder/unimarc_field_4XX.tmpl",
96 query => $query,
97 type => "intranet",
98 authnotrequired => 0,
99 flagsrequired => { editcatalogue => '*' },
100 debug => 1,
104 #get marc record
105 $marcrecord = GetMarcBiblio($biblionumber);
107 my $subfield_value_9 = $biblionumber;
108 my $subfield_value_0;
109 $subfield_value_0 = $marcrecord->field('001')->data
110 if $marcrecord->field('001');
111 my $subfield_value_a;
112 if ( $marcrecord->field('700') ) {
113 $subfield_value_a = $marcrecord->field('700')->subfield("a");
114 $subfield_value_a .= ", " . $marcrecord->subfield( '700', "b" )
115 if $marcrecord->subfield( '700', 'b' );
116 $subfield_value_a .= " " . $marcrecord->subfield( '700', "d" )
117 if $marcrecord->subfield( '700', 'd' );
118 $subfield_value_a .=
119 " (" . $marcrecord->subfield( '700', 'c' ) . " - "
120 if $marcrecord->subfield( '700', 'c' );
121 $subfield_value_a .= " ("
122 if ( $marcrecord->subfield( '700', 'f' )
123 and not( $marcrecord->subfield( '700', 'c' ) ) );
124 $subfield_value_a .= $marcrecord->subfield( '700', 'f' )
125 if ( $marcrecord->subfield( '700', 'f' ) );
126 $subfield_value_a .= ")"
127 if ( $marcrecord->subfield( '701', 'f' )
128 or $marcrecord->subfield( '701', 'c' ) );
130 elsif ( $marcrecord->field('702') ) {
131 $subfield_value_a = $marcrecord->subfield( '702', 'a' );
132 $subfield_value_a .= ", " . $marcrecord->subfield( '702', 'b' )
133 if $marcrecord->subfield( '702', 'b' );
134 $subfield_value_a .= " " . $marcrecord->subfield( '702', 'd' )
135 if $marcrecord->subfield( '702', 'd' );
136 $subfield_value_a .=
137 " (" . $marcrecord->subfield( '702', 'c' ) . "; "
138 if $marcrecord->subfield( '702', 'c' );
139 $subfield_value_a .= " ("
140 if $marcrecord->subfield( '702', 'f' )
141 and not $marcrecord->subfield( '702', 'c' );
142 $subfield_value_a .= $marcrecord->subfield( '702', 'f' )
143 if $marcrecord->subfield( '702', 'f' );
144 $subfield_value_a .= ")"
145 if $marcrecord->subfield( '702', 'f' )
146 or $marcrecord->subfield( '702', 'c' );
148 elsif ( $marcrecord->field('710') ) {
149 $subfield_value_a = $marcrecord->subfield( '710', 'd' ) . " "
150 if $marcrecord->subfield( '710', 'd' );
151 $subfield_value_a .= $marcrecord->subfield( '710', 'a' )
152 if $marcrecord->subfield( '710', 'a' );
153 $subfield_value_a .= ", " . $marcrecord->subfield( '710', 'b' )
154 if $marcrecord->subfield('710');
155 $subfield_value_a .=
156 " (" . $marcrecord->subfield( '710', 'f' ) . " - "
157 if $marcrecord->subfield( '710', 'f' );
158 $subfield_value_a .= " ("
159 if $marcrecord->subfield( '710', 'e' )
160 and not $marcrecord->subfield( '710', 'f' );
161 $subfield_value_a .= $marcrecord->subfield( '710', 'e' )
162 if $marcrecord->subfield( '710', 'e' );
163 $subfield_value_a .= ")"
164 if $marcrecord->subfield( '710', 'e' )
165 or $marcrecord->subfield( '710', 'f' );
167 elsif ( $marcrecord->field('701') ) {
168 $subfield_value_a = $marcrecord->subfield( '701', 'a' );
169 $subfield_value_a .= ", " . $marcrecord->subfield( '701', 'b' )
170 if $marcrecord->subfield( '701', 'b' );
171 $subfield_value_a .= " " . $marcrecord->subfield( '701', 'd', )
172 if $marcrecord->subfield( '701', 'd' );
173 $subfield_value_a .=
174 " (" . $marcrecord->subfield( '701', 'c' ) . " - "
175 if $marcrecord->subfield( '701', 'c' );
176 $subfield_value_a .= " ("
177 if $marcrecord->subfield( '701', 'f' )
178 and not( $marcrecord->subfield( '701', 'c' ) );
179 $subfield_value_a .= $marcrecord->subfield( '701', 'f' )
180 if $marcrecord->subfield( '701', 'f' );
181 $subfield_value_a .= ")"
182 if $marcrecord->subfield( '701', 'f' )
183 or $marcrecord->subfield( '701', 'c' );
185 elsif ( $marcrecord->field('712') ) {
186 $subfield_value_a = $marcrecord->subfield( '712', 'd' ) . " "
187 if $marcrecord->subfield( '712', 'd' );
188 $subfield_value_a .= $marcrecord->subfield( '712', 'a' )
189 if $marcrecord->subfield( '712', 'a' );
190 $subfield_value_a .= ", " . $marcrecord->subfield( '712', 'b' )
191 if $marcrecord->subfield( '712', 'b' );
192 $subfield_value_a .=
193 " (" . $marcrecord->subfield( '712', 'f' ) . " - "
194 if $marcrecord->subfield( '712', 'f' );
195 $subfield_value_a .= " ("
196 if $marcrecord->field( '712', "e" )
197 and not $marcrecord->subfield( '712', 'f' );
198 $subfield_value_a .= $marcrecord->subfield( '712', 'e' )
199 if $marcrecord->subfield( '712', 'e' );
200 $subfield_value_a .= ")"
201 if $marcrecord->subfield( '712', 'e' )
202 or $marcrecord->subfield( '712', 'f' );
204 elsif ( $marcrecord->field('200') ) {
205 $subfield_value_a = $marcrecord->subfield( '200', 'f' );
207 my $subfield_value_c = $marcrecord->field('210')->subfield("a")
208 if ( $marcrecord->field('210') );
209 my $subfield_value_d = $marcrecord->field('210')->subfield("d")
210 if ( $marcrecord->field('210') );
212 my $subfield_value_e = $marcrecord->field('205')->subfield("a")
213 if ( $marcrecord->field('205') );
215 my $subfield_value_h;
216 if ( ( $marcrecord->field('200') )
217 && ( $marcrecord->field('200')->subfield("h") ) )
219 $subfield_value_h = $marcrecord->field('200')->subfield("h");
221 elsif (( $marcrecord->field('225') )
222 && ( $marcrecord->field('225')->subfield("h") ) )
224 $subfield_value_h = $marcrecord->field('225')->subfield("h");
226 elsif (( $marcrecord->field('500') )
227 && ( $marcrecord->field('500')->subfield("h") ) )
229 $subfield_value_h = $marcrecord->field('500')->subfield("h");
232 my $subfield_value_i;
233 if ( ( $marcrecord->field('200') )
234 && ( $marcrecord->field('200')->subfield("i") ) )
236 $subfield_value_i = $marcrecord->field('200')->subfield("i");
238 elsif (( $marcrecord->field('225') )
239 && ( $marcrecord->field('225')->subfield("i") ) )
241 $subfield_value_i = $marcrecord->field('225')->subfield("i");
243 elsif (( $marcrecord->field('500') )
244 && ( $marcrecord->field('500')->subfield("i") ) )
246 $subfield_value_i = $marcrecord->field('500')->subfield("i");
249 my $subfield_value_p = $marcrecord->field('215')->subfield("a")
250 if ( $marcrecord->field('215') );
252 my $subfield_value_t;
253 if ( ( $marcrecord->field('200') )
254 && ( $marcrecord->field('200')->subfield("a") ) )
256 $subfield_value_t = $marcrecord->field('200')->subfield("a");
258 elsif (( $marcrecord->field('225') )
259 && ( $marcrecord->field('225')->subfield("a") ) )
261 $subfield_value_t = $marcrecord->field('225')->subfield("a");
263 elsif (( $marcrecord->field('500') )
264 && ( $marcrecord->field('500')->subfield("a") ) )
266 $subfield_value_t = $marcrecord->field('500')->subfield("a");
269 my $subfield_value_u = $marcrecord->field('856')->subfield("u")
270 if ( $marcrecord->field('856') );
272 my $subfield_value_v;
273 if ( ( $marcrecord->field('225') )
274 && ( $marcrecord->field('225')->subfield("v") ) )
276 $subfield_value_v = $marcrecord->field('225')->subfield("v");
278 elsif (( $marcrecord->field('200') )
279 && ( $marcrecord->field('200')->subfield("h") ) )
281 $subfield_value_v = $marcrecord->field('200')->subfield("h");
283 my $subfield_value_x = $marcrecord->field('011')->subfield("a")
284 if (
285 $marcrecord->field('011')
286 and not( ( $marcrecord->field('011')->subfield("y") )
287 or ( $marcrecord->field('011')->subfield("z") ) )
289 my $subfield_value_y = $marcrecord->field('013')->subfield("a")
290 if ( $marcrecord->field('013') );
291 if ( $marcrecord->field('010') ) {
292 $subfield_value_y = $marcrecord->field('010')->subfield("a");
294 # escape the 's
295 $subfield_value_9 =~ s/'/\\'/g;
296 $subfield_value_0 =~ s/'/\\'/g;
297 $subfield_value_a =~ s/'/\\'/g;
298 $subfield_value_c =~ s/'/\\'/g;
299 $subfield_value_d =~ s/'/\\'/g;
300 $subfield_value_e =~ s/'/\\'/g;
301 $subfield_value_h =~ s/'/\\'/g;
302 $subfield_value_i =~ s/'/\\'/g;
303 $subfield_value_p =~ s/'/\\'/g;
304 $subfield_value_t =~ s/'/\\'/g;
305 $subfield_value_u =~ s/'/\\'/g;
306 $subfield_value_v =~ s/'/\\'/g;
307 $subfield_value_x =~ s/'/\\'/g;
308 $subfield_value_y =~ s/'/\\'/g;
309 $template->param(
310 fillinput => 1,
311 index => $query->param('index') . "",
312 biblionumber => $biblionumber ? $biblionumber : "",
313 subfield_value_9 => "$subfield_value_9",
314 subfield_value_0 => "$subfield_value_0",
315 subfield_value_a => "$subfield_value_a",
316 subfield_value_c => "$subfield_value_c",
317 subfield_value_d => "$subfield_value_d",
318 subfield_value_e => "$subfield_value_e",
319 subfield_value_h => "$subfield_value_h",
320 subfield_value_i => "$subfield_value_i",
321 subfield_value_p => "$subfield_value_p",
322 subfield_value_t => "$subfield_value_t",
323 subfield_value_u => "$subfield_value_u",
324 subfield_value_v => "$subfield_value_v",
325 subfield_value_x => "$subfield_value_x",
326 subfield_value_y => "$subfield_value_y",
328 ###############################################################
330 elsif ( $op eq "do_search" ) {
331 my $search = $query->param('search');
332 my $itype = $query->param('itype');
333 my $startfrom = $query->param('startfrom');
334 my $resultsperpage = $query->param('resultsperpage') || 20;
335 my $orderby;
336 $search = 'kw,wrdl='.$search.' and mc-itemtype='.$itype if $itype;
337 my ( $errors, $results, $total_hits ) = SimpleSearch($search, $startfrom * $resultsperpage, $resultsperpage );
338 my $total = scalar(@$results);
340 # warn " biblio count : ".$total;
342 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
344 template_name =>
345 "cataloguing/value_builder/unimarc_field_4XX.tmpl",
346 query => $query,
347 type => 'intranet',
348 authnotrequired => 1,
349 debug => 1,
353 # multi page display gestion
354 my $displaynext = 0;
355 my $displayprev = $startfrom;
357 if( ( $total_hits - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
358 $displaynext = 1;
360 my @arrayresults;
361 my @field_data = ($search);
362 for (
363 my $i = 0 ;
364 $i < $resultsperpage ;
365 $i++
368 my $record = MARC::Record::new_from_usmarc( $results->[$i] );
369 my $rechash = TransformMarcToKoha( $dbh, $record );
370 my $pos;
371 my $countitems = 1 if ( $rechash->{itemnumber} );
372 while ( index( $rechash->{itemnumber}, '|', $pos ) > 0 ) {
373 $countitems += 1;
374 $pos = index( $rechash->{itemnumber}, '|', $pos ) + 1;
376 $rechash->{totitem} = $countitems;
377 my @holdingbranches = split /\|/, $rechash->{holdingbranch};
378 my @itemcallnumbers = split /\|/, $rechash->{itemcallnumber};
379 my $CN;
380 for ( my $i = 0 ; $i < @holdingbranches ; $i++ ) {
381 $CN .=
382 $holdingbranches[$i] . " ( " . $itemcallnumbers[$i] . " ) |";
384 $CN =~ s/ \|$//;
385 $rechash->{CN} = $CN;
386 push @arrayresults, $rechash;
389 # for(my $i = 0 ; $i <= $#marclist ; $i++)
391 # push @field_data, { term => "marclist", val=>$marclist[$i] };
392 # push @field_data, { term => "and_or", val=>$and_or[$i] };
393 # push @field_data, { term => "excluding", val=>$excluding[$i] };
394 # push @field_data, { term => "operator", val=>$operator[$i] };
395 # push @field_data, { term => "value", val=>$value[$i] };
398 my @numbers = ();
400 if ( $total > $resultsperpage ) {
401 for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
402 if ( $i < 16 ) {
403 my $highlight = 0;
404 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
405 push @numbers,
407 number => $i,
408 highlight => $highlight,
409 searchdata => \@field_data,
410 startfrom => ( $i - 1 )
416 my $from = $startfrom * $resultsperpage + 1;
417 my $to;
419 if ( $total_hits < $from + $resultsperpage ) {
420 $to = $total_hits;
421 }else{
422 $to = $from + $resultsperpage ;
424 my $defaultview =
425 'BiblioDefaultView' . C4::Context->preference('BiblioDefaultView');
426 # my $link="/cgi-bin/koha/cataloguing/value_builder/unimarc4XX.pl?op=do_search&q=$search_desc&resultsperpage=$resultsperpage&startfrom=$startfrom&search=$search";
427 # foreach my $sort (@sort_by){
428 # $link.="&sort_by=".$sort."&";
430 # $template->param(
431 # pagination_bar => pagination_bar(
432 # $link,
433 # getnbpages($hits, $results_per_page),
434 # $page,
435 # 'page'
436 # ),
437 # );
438 $template->param(
439 result => \@arrayresults,
440 index => $query->param('index') . "",
441 startfrom => $startfrom,
442 displaynext => $displaynext,
443 displayprev => $displayprev,
444 resultsperpage => $resultsperpage,
445 orderby => $orderby,
446 startfromnext => $startfrom + 1,
447 startfromprev => $startfrom - 1,
448 searchdata => \@field_data,
449 total => $total_hits,
450 from => $from,
451 to => $to,
452 numbers => \@numbers,
453 search => $search,
454 $defaultview => 1,
455 Search => 0
459 else {
460 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
462 template_name =>
463 "cataloguing/value_builder/unimarc_field_4XX.tmpl",
464 query => $query,
465 type => "intranet",
466 authnotrequired => 1,
470 my $sth =
471 $dbh->prepare(
472 "Select itemtype,description from itemtypes order by description");
473 $sth->execute;
474 my @itemtype;
475 my %itemtypes;
476 push @itemtype, "";
477 $itemtypes{''} = "";
478 while ( my ( $value, $lib ) = $sth->fetchrow_array ) {
479 push @itemtype, $value;
480 $itemtypes{$value} = $lib;
483 my $CGIitemtype = CGI::scrolling_list(
484 -name => 'value',
485 -values => \@itemtype,
486 -labels => \%itemtypes,
487 -size => 1,
488 -multiple => 0
490 $sth->finish;
492 my @branchloop;
493 my @select_branch;
494 my %select_branches;
495 my $branches = GetBranches;
496 push @select_branch, "";
497 $select_branches{''} = "";
498 foreach my $thisbranch ( keys %$branches ) {
499 push @select_branch, $branches->{$thisbranch}->{'branchcode'};
500 $select_branches{ $branches->{$thisbranch}->{'branchcode'} } =
501 $branches->{$thisbranch}->{'branchname'};
503 my $CGIbranch = CGI::scrolling_list(
504 -name => 'value',
505 -values => \@select_branch,
506 -labels => \%select_branches,
507 -size => 1,
508 -multiple => 0
510 $sth->finish;
512 my $req =
513 $dbh->prepare(
514 "select distinctrow left(publishercode,45) from biblioitems order by publishercode"
516 $req->execute;
517 my @select;
518 push @select, "";
519 while ( my ($value) = $req->fetchrow ) {
520 push @select, $value;
522 my $CGIpublisher = CGI::scrolling_list(
523 -name => 'value',
524 -id => 'publisher',
525 -values => \@select,
526 -size => 1,
527 -multiple => 0
530 # my $sth=$dbh->prepare("select description,itemtype from itemtypes order by description");
531 # $sth->execute;
532 # while (my ($description,$itemtype) = $sth->fetchrow) {
533 # $classlist.="<option value=\"$itemtype\">$description</option>\n";
535 # $sth->finish;
537 my @itemtypes = C4::ItemType->all;
539 $template->param( #classlist => $classlist,
540 CGIitemtype => $CGIitemtype,
541 CGIbranch => $CGIbranch,
542 CGIPublisher => $CGIpublisher,
543 itypeloop => \@itemtypes,
544 index => $query->param('index'),
545 Search => 1,
548 output_html_with_http_headers $query, $cookie, $template->output;