Bug 14659: (QA followup) show cardnumber for existing borrowers
[koha.git] / cataloguing / value_builder / unimarc_field_4XX.pl
blob092b0f1d90d1a0f97415766e9617ba5b593fd00a
1 #!/usr/bin/perl
3 # Copyright Biblibre 2007
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use strict;
22 #use warnings; FIXME - Bug 2505
24 use CGI qw ( -utf8 );
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
36 use Koha::ItemTypes;
38 sub plugin_javascript {
39 my ( $dbh, $record, $tagslib, $field_number, $tabloop ) = @_;
40 my $function_name = $field_number;
41 my $res = "
42 <script type='text/javascript'>
43 function Clic$function_name(i) {
44 defaultvalue=document.getElementById(\"$field_number\").value;
45 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');
48 </script>
51 return ( $function_name, $res );
54 # sub plugin
56 # input arg :
57 # -- op could be equals to
58 # * fillinput :
59 # * do_search :
62 sub plugin {
63 my ($input) = @_;
64 my $dbh = C4::Context->dbh;
65 my $query = new CGI;
66 my $op = $query->param('op');
67 my $type = $query->param('type');
68 my $startfrom = $query->param('startfrom');
69 $startfrom = 0 if ( !defined $startfrom );
70 my ( $template, $loggedinuser, $cookie );
71 my $resultsperpage;
73 if ( $op eq "fillinput" ) {
74 my $biblionumber = $query->param('biblionumber');
75 my $index = $query->param('index');
76 my $marcrecord;
78 # open template
79 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
81 template_name =>
82 "cataloguing/value_builder/unimarc_field_4XX.tt",
83 query => $query,
84 type => "intranet",
85 authnotrequired => 0,
86 flagsrequired => { editcatalogue => '*' },
87 debug => 1,
91 #get marc record
92 $marcrecord = GetMarcBiblio($biblionumber);
94 my $subfield_value_9 = $biblionumber;
95 my $subfield_value_0;
96 $subfield_value_0 = $marcrecord->field('001')->data
97 if $marcrecord->field('001');
98 my $subfield_value_a;
99 if ( $marcrecord->field('700') ) {
100 $subfield_value_a = $marcrecord->field('700')->subfield("a");
101 $subfield_value_a .= ", " . $marcrecord->subfield( '700', "b" )
102 if $marcrecord->subfield( '700', 'b' );
103 $subfield_value_a .= " " . $marcrecord->subfield( '700', "d" )
104 if $marcrecord->subfield( '700', 'd' );
105 $subfield_value_a .=
106 " (" . $marcrecord->subfield( '700', 'c' )
107 if $marcrecord->subfield( '700', 'c' );
108 $subfield_value_a .= " ; "
109 if $marcrecord->subfield( '700', 'c' )
110 and $marcrecord->subfield( '700', 'f' );
111 $subfield_value_a .= " ("
112 if ( $marcrecord->subfield( '700', 'f' )
113 and not( $marcrecord->subfield( '700', 'c' ) ) );
114 $subfield_value_a .= $marcrecord->subfield( '700', 'f' )
115 if ( $marcrecord->subfield( '700', 'f' ) );
116 $subfield_value_a .= ")"
117 if ( $marcrecord->subfield( '700', 'f' )
118 or $marcrecord->subfield( '700', 'c' ) );
120 elsif ( $marcrecord->field('702') ) {
121 $subfield_value_a = $marcrecord->subfield( '702', 'a' );
122 $subfield_value_a .= ", " . $marcrecord->subfield( '702', 'b' )
123 if $marcrecord->subfield( '702', 'b' );
124 $subfield_value_a .= " " . $marcrecord->subfield( '702', 'd' )
125 if $marcrecord->subfield( '702', 'd' );
126 $subfield_value_a .=
127 " (" . $marcrecord->subfield( '702', 'c' )
128 if $marcrecord->subfield( '702', 'c' );
129 $subfield_value_a .= " ; "
130 if $marcrecord->subfield( '702', 'c' )
131 and $marcrecord->subfield( '702', 'f' );
132 $subfield_value_a .= " ("
133 if $marcrecord->subfield( '702', 'f' )
134 and not $marcrecord->subfield( '702', 'c' );
135 $subfield_value_a .= $marcrecord->subfield( '702', 'f' )
136 if $marcrecord->subfield( '702', 'f' );
137 $subfield_value_a .= ")"
138 if $marcrecord->subfield( '702', 'f' )
139 or $marcrecord->subfield( '702', 'c' );
141 elsif ( $marcrecord->field('710') ) {
142 $subfield_value_a = $marcrecord->subfield( '710', 'd' ) . " "
143 if $marcrecord->subfield( '710', 'd' );
144 $subfield_value_a .= $marcrecord->subfield( '710', 'a' )
145 if $marcrecord->subfield( '710', 'a' );
146 $subfield_value_a .= ", " . $marcrecord->subfield( '710', 'b' )
147 if $marcrecord->subfield('710');
148 $subfield_value_a .=
149 " (" . $marcrecord->subfield( '710', 'f' ) . " - "
150 if $marcrecord->subfield( '710', 'f' );
151 $subfield_value_a .= " ("
152 if $marcrecord->subfield( '710', 'e' )
153 and not $marcrecord->subfield( '710', 'f' );
154 $subfield_value_a .= $marcrecord->subfield( '710', 'e' )
155 if $marcrecord->subfield( '710', 'e' );
156 $subfield_value_a .= ")"
157 if $marcrecord->subfield( '710', 'e' )
158 or $marcrecord->subfield( '710', 'f' );
160 elsif ( $marcrecord->field('701') ) {
161 $subfield_value_a = $marcrecord->subfield( '701', 'a' );
162 $subfield_value_a .= ", " . $marcrecord->subfield( '701', 'b' )
163 if $marcrecord->subfield( '701', 'b' );
164 $subfield_value_a .= " " . $marcrecord->subfield( '701', 'd', )
165 if $marcrecord->subfield( '701', 'd' );
166 $subfield_value_a .=
167 " (" . $marcrecord->subfield( '701', 'c' ) . " - "
168 if $marcrecord->subfield( '701', 'c' );
169 $subfield_value_a .= " ("
170 if $marcrecord->subfield( '701', 'f' )
171 and not( $marcrecord->subfield( '701', 'c' ) );
172 $subfield_value_a .= $marcrecord->subfield( '701', 'f' )
173 if $marcrecord->subfield( '701', 'f' );
174 $subfield_value_a .= ")"
175 if $marcrecord->subfield( '701', 'f' )
176 or $marcrecord->subfield( '701', 'c' );
178 elsif ( $marcrecord->field('712') ) {
179 $subfield_value_a = $marcrecord->subfield( '712', 'd' ) . " "
180 if $marcrecord->subfield( '712', 'd' );
181 $subfield_value_a .= $marcrecord->subfield( '712', 'a' )
182 if $marcrecord->subfield( '712', 'a' );
183 $subfield_value_a .= ", " . $marcrecord->subfield( '712', 'b' )
184 if $marcrecord->subfield( '712', 'b' );
185 $subfield_value_a .=
186 " (" . $marcrecord->subfield( '712', 'f' ) . " - "
187 if $marcrecord->subfield( '712', 'f' );
188 $subfield_value_a .= " ("
189 if $marcrecord->subfield( '712', "e" )
190 and not $marcrecord->subfield( '712', 'f' );
191 $subfield_value_a .= $marcrecord->subfield( '712', 'e' )
192 if $marcrecord->subfield( '712', 'e' );
193 $subfield_value_a .= ")"
194 if $marcrecord->subfield( '712', 'e' )
195 or $marcrecord->subfield( '712', 'f' );
197 elsif ( $marcrecord->field('200') ) {
198 $subfield_value_a = $marcrecord->subfield( '200', 'f' );
200 my $subfield_value_c;
201 my $subfield_value_d;
202 my $subfield_value_e;
203 $subfield_value_c = $marcrecord->field('210')->subfield("a")
204 if ( $marcrecord->field('210') );
205 $subfield_value_d = $marcrecord->field('210')->subfield("d")
206 if ( $marcrecord->field('210') );
207 $subfield_value_e = $marcrecord->field('205')->subfield("a")
208 if ( $marcrecord->field('205') );
210 my $subfield_value_h;
211 if ( ( $marcrecord->field('200') )
212 && ( $marcrecord->field('200')->subfield("h") ) )
214 $subfield_value_h = $marcrecord->field('200')->subfield("h");
216 elsif (( $marcrecord->field('225') )
217 && ( $marcrecord->field('225')->subfield("h") ) )
219 $subfield_value_h = $marcrecord->field('225')->subfield("h");
221 elsif (( $marcrecord->field('500') )
222 && ( $marcrecord->field('500')->subfield("h") ) )
224 $subfield_value_h = $marcrecord->field('500')->subfield("h");
227 my $subfield_value_i;
228 if ( ( $marcrecord->field('200') )
229 && ( $marcrecord->field('200')->subfield("i") ) )
231 $subfield_value_i = $marcrecord->field('200')->subfield("i");
233 elsif (( $marcrecord->field('225') )
234 && ( $marcrecord->field('225')->subfield("i") ) )
236 $subfield_value_i = $marcrecord->field('225')->subfield("i");
238 elsif (( $marcrecord->field('500') )
239 && ( $marcrecord->field('500')->subfield("i") ) )
241 $subfield_value_i = $marcrecord->field('500')->subfield("i");
244 my $subfield_value_l;
245 $subfield_value_l = $marcrecord->field('200')->subfield("d")
246 if ( $marcrecord->field('200') );
248 my $subfield_value_n;
249 $subfield_value_n = $marcrecord->field('210')->subfield("c")
250 if ( $marcrecord->field('210') );
252 my $subfield_value_o;
253 $subfield_value_o = $marcrecord->field('200')->subfield("e")
254 if ( $marcrecord->field('200') );
256 my $subfield_value_p;
257 $subfield_value_p = $marcrecord->field('215')->subfield("a")
258 if ( $marcrecord->field('215') );
260 my $subfield_value_t;
261 if ( ( $marcrecord->field('200') )
262 && ( $marcrecord->field('200')->subfield("a") ) )
264 $subfield_value_t = $marcrecord->field('200')->subfield("a");
266 elsif (( $marcrecord->field('225') )
267 && ( $marcrecord->field('225')->subfield("a") ) )
269 $subfield_value_t = $marcrecord->field('225')->subfield("a");
271 elsif (( $marcrecord->field('500') )
272 && ( $marcrecord->field('500')->subfield("a") ) )
274 $subfield_value_t = $marcrecord->field('500')->subfield("a");
277 my $subfield_value_u;
278 if ( $marcrecord->field('856') ) {
279 $subfield_value_u = $marcrecord->field('856')->subfield("u");
282 my $subfield_value_v;
283 if ( ( $marcrecord->field('225') )
284 && ( $marcrecord->field('225')->subfield("v") ) )
286 $subfield_value_v = $marcrecord->field('225')->subfield("v");
288 elsif (( $marcrecord->field('200') )
289 && ( $marcrecord->field('200')->subfield("h") ) )
291 $subfield_value_v = $marcrecord->field('200')->subfield("h");
293 my $subfield_value_x;
294 if (
295 $marcrecord->field('011')
296 and not( ( $marcrecord->field('011')->subfield("y") )
297 or ( $marcrecord->field('011')->subfield("z") ) ) ) {
298 $subfield_value_x = $marcrecord->field('011')->subfield("a");
300 my $subfield_value_y;
301 if ( $marcrecord->field('013') ) {
302 $subfield_value_y = $marcrecord->field('013')->subfield("a");
304 if ( $marcrecord->field('010') ) {
305 $subfield_value_y = $marcrecord->field('010')->subfield("a");
307 # escape the 's
308 $subfield_value_9 =~ s/'/\\'/g;
309 $subfield_value_0 =~ s/'/\\'/g;
310 $subfield_value_a =~ s/'/\\'/g;
311 $subfield_value_c =~ s/'/\\'/g;
312 $subfield_value_d =~ s/'/\\'/g;
313 $subfield_value_e =~ s/'/\\'/g;
314 $subfield_value_h =~ s/'/\\'/g;
315 $subfield_value_i =~ s/'/\\'/g;
316 $subfield_value_l =~ s/'/\\'/g;
317 $subfield_value_n =~ s/'/\\'/g;
318 $subfield_value_o =~ s/'/\\'/g;
319 $subfield_value_p =~ s/'/\\'/g;
320 $subfield_value_t =~ s/'/\\'/g;
321 $subfield_value_u =~ s/'/\\'/g;
322 $subfield_value_v =~ s/'/\\'/g;
323 $subfield_value_x =~ s/'/\\'/g;
324 $subfield_value_y =~ s/'/\\'/g;
325 $template->param(
326 fillinput => 1,
327 index => $query->param('index') . "",
328 biblionumber => $biblionumber ? $biblionumber : "",
329 subfield_value_9 => "$subfield_value_9",
330 subfield_value_0 => "$subfield_value_0",
331 subfield_value_a => "$subfield_value_a",
332 subfield_value_c => "$subfield_value_c",
333 subfield_value_d => "$subfield_value_d",
334 subfield_value_e => "$subfield_value_e",
335 subfield_value_h => "$subfield_value_h",
336 subfield_value_i => "$subfield_value_i",
337 subfield_value_l => "$subfield_value_l",
338 subfield_value_n => "$subfield_value_n",
339 subfield_value_o => "$subfield_value_o",
340 subfield_value_p => "$subfield_value_p",
341 subfield_value_t => "$subfield_value_t",
342 subfield_value_u => "$subfield_value_u",
343 subfield_value_v => "$subfield_value_v",
344 subfield_value_x => "$subfield_value_x",
345 subfield_value_y => "$subfield_value_y",
347 ###############################################################
349 elsif ( $op eq "do_search" ) {
350 my $search = $query->param('search');
351 my $itype = $query->param('itype');
352 my $startfrom = $query->param('startfrom');
353 my $resultsperpage = $query->param('resultsperpage') || 20;
354 my $orderby;
355 my $QParser;
356 $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser'));
357 my $op;
358 if ($QParser) {
359 $op = '&&';
360 } else {
361 $op = 'and';
363 $search = 'kw:'.$search." $op mc-itemtype:".$itype if $itype;
364 my ( $errors, $results, $total_hits ) = SimpleSearch($search, $startfrom * $resultsperpage, $resultsperpage );
365 if (defined $errors ) {
366 $results = [];
368 my $total = @{$results};
370 # warn " biblio count : ".$total;
372 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
374 template_name =>
375 "cataloguing/value_builder/unimarc_field_4XX.tt",
376 query => $query,
377 type => 'intranet',
378 authnotrequired => 0,
379 debug => 1,
383 # multi page display gestion
384 my $displaynext = 0;
385 my $displayprev = $startfrom;
387 if( ( $total_hits - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
388 $displaynext = 1;
390 my @arrayresults;
391 my @field_data = ($search);
392 for (
393 my $i = 0 ;
394 $i < $resultsperpage ;
395 $i++
398 my $record = C4::Search::new_record_from_zebra( 'biblioserver', $results->[$i] );
399 next unless $record;
400 my $rechash = TransformMarcToKoha( $dbh, $record );
401 if ( my $f = $record->field('200') ) {
402 $rechash->{fulltitle} =
403 join(', ', map { $_->[1] } grep { $_->[0] =~ /[aehi]/ } $f->subfields() );
405 else {
406 $rechash->{fulltitle} = $rechash->{title};
408 my $pos;
409 my $countitems;
410 if ( $rechash->{itemnumber} ) {
411 $countitems=1;
413 while ( index( $rechash->{itemnumber}, '|', $pos ) > 0 ) {
414 $countitems += 1;
415 $pos = index( $rechash->{itemnumber}, '|', $pos ) + 1;
417 $rechash->{totitem} = $countitems;
418 my @holdingbranches = split /\|/, $rechash->{holdingbranch};
419 my @itemcallnumbers = split /\|/, $rechash->{itemcallnumber};
420 my $CN;
421 for ( my $i = 0 ; $i < @holdingbranches ; $i++ ) {
422 $CN .=
423 $holdingbranches[$i] . " ( " . $itemcallnumbers[$i] . " ) |";
425 $CN =~ s/ \|$//;
426 $rechash->{CN} = $CN;
427 push @arrayresults, $rechash;
430 # for(my $i = 0 ; $i <= $#marclist ; $i++)
432 # push @field_data, { term => "marclist", val=>$marclist[$i] };
433 # push @field_data, { term => "and_or", val=>$and_or[$i] };
434 # push @field_data, { term => "excluding", val=>$excluding[$i] };
435 # push @field_data, { term => "operator", val=>$operator[$i] };
436 # push @field_data, { term => "value", val=>$value[$i] };
439 my @numbers = ();
441 if ( $total > $resultsperpage ) {
442 for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
443 if ( $i < 16 ) {
444 my $highlight = 0;
445 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
446 push @numbers,
448 number => $i,
449 highlight => $highlight,
450 searchdata => \@field_data,
451 startfrom => ( $i - 1 )
457 my $from = $startfrom * $resultsperpage + 1;
458 my $to;
460 if ( $total_hits < $from + $resultsperpage ) {
461 $to = $total_hits;
462 }else{
463 $to = $from + $resultsperpage ;
465 my $defaultview =
466 'BiblioDefaultView' . C4::Context->preference('BiblioDefaultView');
467 # my $link="/cgi-bin/koha/cataloguing/value_builder/unimarc4XX.pl?op=do_search&q=$search_desc&resultsperpage=$resultsperpage&startfrom=$startfrom&search=$search";
468 # foreach my $sort (@sort_by){
469 # $link.="&sort_by=".$sort."&";
471 # $template->param(
472 # pagination_bar => pagination_bar(
473 # $link,
474 # getnbpages($hits, $results_per_page),
475 # $page,
476 # 'page'
477 # ),
478 # );
479 $template->param(
480 result => \@arrayresults,
481 index => $query->param('index') . "",
482 startfrom => $startfrom,
483 displaynext => $displaynext,
484 displayprev => $displayprev,
485 resultsperpage => $resultsperpage,
486 orderby => $orderby,
487 startfromnext => $startfrom + 1,
488 startfromprev => $startfrom - 1,
489 searchdata => \@field_data,
490 total => $total_hits,
491 from => $from,
492 to => $to,
493 numbers => \@numbers,
494 search => $search,
495 $defaultview => 1,
496 Search => 0
500 else {
501 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
503 template_name =>
504 "cataloguing/value_builder/unimarc_field_4XX.tt",
505 query => $query,
506 type => "intranet",
507 authnotrequired => 0,
511 my @itemtypes = Koha::ItemTypes->search;
513 $template->param( #classlist => $classlist,
514 itypeloop => \@itemtypes,
515 index => $query->param('index'),
516 Search => 1,
519 output_html_with_http_headers $query, $cookie, $template->output;