Bug 30477: Add new UNIMARC installer translation files
[koha.git] / C4 / Breeding.pm
1 package C4::Breeding;
2
3 # Copyright 2000-2002 Katipo Communications
4 # Parts Copyright 2013 Prosentient Systems
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use strict;
22 use warnings;
23
24 use C4::Biblio;
25 use C4::Koha qw( GetVariationsOfISBN );
26 use C4::Charset qw( MarcToUTF8Record SetUTF8Flag );
27 use MARC::File::USMARC;
28 use MARC::Field;
29 use C4::ImportBatch qw( GetZ3950BatchId AddBiblioToBatch AddAuthToBatch );
30 use C4::AuthoritiesMarc qw( GuessAuthTypeCode );
31 use C4::Languages;
32 use Koha::Database;
33 use Koha::XSLT::Base;
34
35 our (@ISA, @EXPORT_OK);
36 BEGIN {
37     require Exporter;
38     @ISA       = qw(Exporter);
39     @EXPORT_OK = qw(BreedingSearch Z3950Search Z3950SearchAuth);
40 }
41
42 =head1 NAME
43
44 C4::Breeding : module to add biblios to import_records via
45                the breeding/reservoir API.
46
47 =head1 SYNOPSIS
48
49     Z3950Search($pars, $template);
50     ($count, @results) = &BreedingSearch($title,$isbn);
51
52 =head1 DESCRIPTION
53
54 This module contains routines related to Koha's Z39.50 search into
55 cataloguing reservoir features.
56
57 =head2 BreedingSearch
58
59 ($count, @results) = &BreedingSearch($term);
60 C<$term> contains the term to search, it will be searched as title,author, or isbn
61
62 C<$count> is the number of items in C<@results>. C<@results> is an
63 array of references-to-hash; the keys are the items from the C<import_records> and
64 C<import_biblios> tables of the Koha database.
65
66 =cut
67
68 sub BreedingSearch {
69     my ($term) = @_;
70     my $dbh   = C4::Context->dbh;
71     my $count = 0;
72     my ($query,@bind);
73     my $sth;
74     my @results;
75
76     my $authortitle = $term;
77     $authortitle =~ s/(\s+)/\%/g; #Replace spaces with wildcard
78     $authortitle = "%" . $authortitle . "%"; #Add wildcard to start and end of string
79     # normalise ISBN like at import
80     my @isbns = C4::Koha::GetVariationsOfISBN($term);
81
82     $query = "SELECT import_biblios.import_record_id,
83                 import_batches.file_name,
84                 import_biblios.isbn,
85                 import_biblios.title,
86                 import_biblios.author,
87                 import_batches.upload_timestamp
88               FROM  import_biblios
89               JOIN import_records USING (import_record_id)
90               JOIN import_batches USING (import_batch_id)
91               WHERE title LIKE ? OR author LIKE ? OR isbn IN (" . join(',',('?') x @isbns) . ")";
92     @bind=( $authortitle, $authortitle, @isbns );
93     $sth = $dbh->prepare($query);
94     $sth->execute(@bind);
95     while (my $data = $sth->fetchrow_hashref) {
96             $results[$count] = $data;
97             # FIXME - hack to reflect difference in name 
98             # of columns in old marc_breeding and import_records
99             # There needs to be more separation between column names and 
100             # field names used in the templates </soapbox>
101             $data->{'file'} = $data->{'file_name'};
102             $data->{'id'} = $data->{'import_record_id'};
103             $count++;
104     } # while
105
106     $sth->finish;
107     return($count, @results);
108 } # sub breedingsearch
109
110
111 =head2 Z3950Search
112
113 Z3950Search($pars, $template);
114
115 Parameters for Z3950 search are all passed via the $pars hash. It may contain isbn, title, author, dewey, subject, lccall, controlnumber, stdid, srchany.
116 Also it should contain an arrayref id that points to a list of id's of the z3950 targets to be queried (see z3950servers table).
117 This code is used in acqui/z3950_search and cataloging/z3950_search.
118 The second parameter $template is a Template object. The routine uses this parameter to store the found values into the template.
119
120 =cut
121
122 sub Z3950Search {
123     my ($pars, $template)= @_;
124
125     my @id= @{$pars->{id}};
126     my $page= $pars->{page};
127     my $biblionumber= $pars->{biblionumber};
128
129     my $show_next       = 0;
130     my $total_pages     = 0;
131     my @results;
132     my @breeding_loop = ();
133     my @oConnection;
134     my @oResult;
135     my @errconn;
136     my $s = 0;
137     my $imported=0;
138
139     my ( $zquery, $squery ) = _bib_build_query( $pars );
140
141     my $schema = Koha::Database->new()->schema();
142     my $rs = $schema->resultset('Z3950server')->search(
143         { id => [ @id ] },
144         { result_class => 'DBIx::Class::ResultClass::HashRefInflator' },
145     );
146     my @servers = $rs->all;
147     foreach my $server ( @servers ) {
148         my $server_zquery = $zquery;
149         if(my $attributes = $server->{attributes}){
150             $server_zquery = "$attributes $zquery";
151         }
152         $oConnection[$s] = _create_connection( $server );
153         $oResult[$s] =
154             $server->{servertype} eq 'zed'?
155                 $oConnection[$s]->search_pqf( $server_zquery ):
156                 $oConnection[$s]->search(ZOOM::Query::CQL->new(
157                     _translate_query( $server, $squery )));
158         $s++;
159     }
160     my $xslh = Koha::XSLT::Base->new;
161
162     my $nremaining = $s;
163     while ( $nremaining-- ) {
164         my $k;
165         my $event;
166         while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
167             $event = $oConnection[ $k - 1 ]->last_event();
168             last if $event == ZOOM::Event::ZEND;
169         }
170
171         if ( $k != 0 ) {
172             $k--;
173             my ($error)= $oConnection[$k]->error_x(); #ignores errmsg, addinfo, diagset
174             if ($error) {
175                 if ($error =~ m/^(10000|10007)$/ ) {
176                     push(@errconn, { server => $servers[$k]->{host}, error => $error } );
177                 }
178             }
179             else {
180                 my $numresults = $oResult[$k]->size();
181                 my $i;
182                 my $res;
183                 if ( $numresults > 0  and $numresults >= (($page-1)*20)) {
184                     $show_next = 1 if $numresults >= ($page*20);
185                     $total_pages = int($numresults/20)+1 if $total_pages < ($numresults/20);
186                     for ($i = ($page-1)*20; $i < (($numresults < ($page*20)) ? $numresults : ($page*20)); $i++) {
187                         if ( $oResult[$k]->record($i) ) {
188                             undef $error;
189                             ( $res, $error ) = _handle_one_result( $oResult[$k]->record($i), $servers[$k], ++$imported, $biblionumber, $xslh ); #ignores error in sequence numbering
190                             push @breeding_loop, $res if $res;
191                             push @errconn, { server => $servers[$k]->{servername}, error => $error, seq => $i+1 } if $error;
192                         }
193                         else {
194                             push @errconn, { 'server' => $servers[$k]->{servername}, error => ( ( $oConnection[$k]->error_x() )[0] ), seq => $i+1 };
195                         }
196                     }
197                 }    #if $numresults
198             }
199         }    # if $k !=0
200
201         $template->param(
202             numberpending => $nremaining,
203             current_page => $page,
204             total_pages => $total_pages,
205             show_nextbutton => $show_next?1:0,
206             show_prevbutton => $page!=1,
207         );
208     } # while nremaining
209
210     #close result sets and connections
211     foreach(0..$s-1) {
212         $oResult[$_]->destroy();
213         $oConnection[$_]->destroy();
214     }
215
216     $template->param(
217         breeding_loop => \@breeding_loop,
218         servers => \@servers,
219         errconn       => \@errconn
220     );
221 }
222
223 sub _auth_build_query {
224     my ( $pars ) = @_;
225
226     my $qry_build = {
227         nameany           => '@attr 1=1002 "#term" ',
228         authorany         => '@attr 1=1003 "#term" ',
229         authorcorp        => '@attr 1=2 "#term" ',
230         authorpersonal    => '@attr 1=1 "#term" ',
231         authormeetingcon  => '@attr 1=3 "#term" ',
232         subject           => '@attr 1=21 "#term" ',
233         subjectsubdiv     => '@attr 1=47 "#term" ',
234         title             => '@attr 1=4 "#term" ',
235         uniformtitle      => '@attr 1=6 "#term" ',
236         srchany           => '@attr 1=1016 "#term" ',
237         controlnumber     => '@attr 1=12 "#term" ',
238     };
239
240     return _build_query( $pars, $qry_build );
241 }
242
243 sub _bib_build_query {
244
245     my ( $pars ) = @_;
246
247     my $qry_build = {
248         isbn    => '@attr 1=7 @attr 5=1 "#term" ',
249         issn    => '@attr 1=8 @attr 5=1 "#term" ',
250         title   => '@attr 1=4 "#term" ',
251         author  => '@attr 1=1003 "#term" ',
252         dewey   => '@attr 1=16 "#term" ',
253         subject => '@attr 1=21 "#term" ',
254         lccall  => '@attr 1=16 @attr 2=3 @attr 3=1 @attr 4=1 @attr 5=1 '.
255                    '@attr 6=1 "#term" ',
256         controlnumber => '@attr 1=12 "#term" ',
257         srchany => '@attr 1=1016 "#term" ',
258         stdid   => '@attr 1=1007 "#term" ',
259         publicationyear => '@attr 1=31 "#term" '
260     };
261
262     return _build_query( $pars, $qry_build );
263 }
264
265 sub _build_query {
266
267     my ( $pars, $qry_build ) = @_;
268
269     my $zquery='';
270     my $squery='';
271     my $nterms=0;
272     foreach my $k ( sort keys %$pars ) {
273     #note that the sort keys forces an identical result under Perl 5.18
274     #one of the unit tests is based on that assumption
275         if( ( my $val=$pars->{$k} ) && $qry_build->{$k} ) {
276             $qry_build->{$k} =~ s/#term/$val/g;
277             $zquery .= $qry_build->{$k};
278             $squery .= "[$k]=\"$val\" and ";
279             $nterms++;
280         }
281     }
282     $zquery = "\@and " . $zquery for 2..$nterms;
283     $squery =~ s/ and $//;
284     return ( $zquery, $squery );
285 }
286
287 sub _handle_one_result {
288     my ( $zoomrec, $servhref, $seq, $bib, $xslh )= @_;
289
290     my $raw= $zoomrec->raw();
291     my $marcrecord;
292     if( $servhref->{servertype} eq 'sru' ) {
293         $marcrecord= MARC::Record->new_from_xml( $raw, 'UTF-8',
294             $servhref->{syntax} );
295         $marcrecord->encoding('UTF-8');
296     } else {
297         ($marcrecord) = MarcToUTF8Record($raw, C4::Context->preference('marcflavour'), $servhref->{encoding} // "iso-5426" ); #ignores charset return values
298     }
299     SetUTF8Flag($marcrecord);
300     my $error;
301     ( $marcrecord, $error ) = _do_xslt_proc($marcrecord, $servhref, $xslh);
302
303     my $batch_id = GetZ3950BatchId($servhref->{servername});
304     my $breedingid = AddBiblioToBatch($batch_id, $seq, $marcrecord, 'UTF-8', 0);
305         #Last zero indicates: no update for batch record counts
306
307
308     #call to TransformMarcToKoha replaced by next call
309     #we only need six fields from the marc record
310     my $row;
311     $row = _add_rowdata(
312         {
313             biblionumber => $bib,
314             server       => $servhref->{servername},
315             breedingid   => $breedingid,
316         }, $marcrecord) if $breedingid;
317     return ( $row, $error );
318 }
319
320 sub _do_xslt_proc {
321     my ( $marc, $server, $xslh ) = @_;
322     return $marc if !$server->{add_xslt};
323
324     my $htdocs = C4::Context->config('intrahtdocs');
325     my $theme = C4::Context->preference("template"); #staff
326     my $lang = C4::Languages::getlanguage() || 'en';
327
328     my @files= split ',', $server->{add_xslt};
329     my $xml = $marc->as_xml;
330     foreach my $f ( @files ) {
331         $f =~ s/^\s+//; $f =~ s/\s+$//; next if !$f;
332         $f = C4::XSLT::_get_best_default_xslt_filename(
333             $htdocs, $theme, $lang, $f ) unless $f =~ /^\//;
334         $xml = $xslh->transform( $xml, $f );
335         last if $xslh->err; #skip other files
336     }
337     if( !$xslh->err ) {
338         return MARC::Record->new_from_xml($xml, 'UTF-8');
339     } else {
340         return ( $marc, $xslh->err ); #original record in case of errors
341     }
342 }
343
344 sub _add_rowdata {
345     my ($row, $record)=@_;
346     my %fetch= (
347         title => 'biblio.title',
348         author => 'biblio.author',
349         isbn =>'biblioitems.isbn',
350         lccn =>'biblioitems.lccn', #LC control number (not call number)
351         edition =>'biblioitems.editionstatement'
352     );
353     $fetch{date} = C4::Context->preference('marcflavour') eq "MARC21" ? 'biblio.copyrightdate' : 'biblioitems.publicationyear';
354
355     foreach my $k (keys %fetch) {
356         $row->{$k} = C4::Biblio::TransformMarcToKohaOneField( $fetch{$k}, $record );
357     }
358     $row->{date}//= $row->{date2};
359     $row->{isbn}=_isbn_replace($row->{isbn});
360
361     $row = _add_custom_field_rowdata($row, $record);
362
363     return $row;
364 }
365
366 sub _add_custom_field_rowdata
367 {
368     my ( $row, $record ) = @_;
369     my $pref_newtags = C4::Context->preference('AdditionalFieldsInZ3950ResultSearch');
370     my $pref_flavour = C4::Context->preference('MarcFlavour');
371
372     $pref_newtags =~ s/^\s+|\s+$//g;
373     $pref_newtags =~ s/\h+/ /g;
374
375     my @addnumberfields;
376
377     foreach my $field (split /\,/, $pref_newtags) {
378         $field =~ s/^\s+|\s+$//g ;  # trim whitespace
379         my ($tag, $subtags) = split(/\$/, $field);
380
381         if ( $record->field($tag) ) {
382             my @content = ();
383
384             for my $marcfield ($record->field($tag)) {
385                 if ( $subtags ) {
386                     my $str = '';
387                     for my $code (split //, $subtags) {
388                         if ( $marcfield->subfield($code) ) {
389                             $str .= $marcfield->subfield($code) . ' ';
390                         }
391                     }
392                     if ( not $str eq '') {
393                         push @content, $str;
394                     }
395                 } elsif ( $tag == 10 ) {
396                     push @content, ( $pref_flavour eq "MARC21" ? $marcfield->data : $marcfield->as_string );
397                 } elsif ( $tag < 10 ) {
398                     push @content, $marcfield->data();
399                 } else {
400                     push @content, $marcfield->as_string();
401                 }
402             }
403
404             if ( @content ) {
405                 $row->{$field} = \@content;
406                 push( @addnumberfields, $field );
407             }
408         }
409     }
410
411     $row->{'addnumberfields'} = \@addnumberfields;
412
413     return $row;
414 }
415
416 sub _isbn_replace {
417     my ($isbn) = @_;
418     return unless defined $isbn;
419     $isbn =~ s/ |-|\.//g;
420     $isbn =~ s/\|/ \| /g;
421     $isbn =~ s/\(/ \(/g;
422     return $isbn;
423 }
424
425 sub _create_connection {
426     my ( $server ) = @_;
427     my $option1= ZOOM::Options->new();
428     $option1->option( 'async' => 1 );
429     $option1->option( 'elementSetName', 'F' );
430     $option1->option( 'preferredRecordSyntax', $server->{syntax} );
431     $option1->option( 'timeout', $server->{timeout} ) if $server->{timeout};
432
433     if( $server->{servertype} eq 'sru' ) {
434         foreach( split ',', $server->{sru_options}//'' ) {
435             #first remove surrounding spaces at comma and equals-sign
436             s/^\s+|\s+$//g;
437             my @temp= split '=', $_, 2;
438             @temp= map { my $c=$_; $c=~s/^\s+|\s+$//g; $c; } @temp;
439             $option1->option( $temp[0] => $temp[1] ) if @temp;
440         }
441     } elsif( $server->{servertype} eq 'zed' ) {
442         $option1->option( 'databaseName',   $server->{db} );
443         $option1->option( 'user', $server->{userid} ) if $server->{userid};
444         $option1->option( 'password', $server->{password} ) if $server->{password};
445     }
446     my $obj= ZOOM::Connection->create($option1);
447     if( $server->{servertype} eq 'sru' ) {
448         my $host= $server->{host};
449         if( $host !~ /^https?:\/\// ) {
450             #Normally, host will not be prefixed by protocol.
451             #In that case we can (safely) assume http.
452             #In case someone prefixed with https, give it a try..
453             $host = 'http://' . $host;
454         }
455         $obj->connect( $host.':'.$server->{port}.'/'.$server->{db} );
456     } else {
457         $obj->connect( $server->{host}, $server->{port} );
458     }
459     return $obj;
460 }
461
462 sub _translate_query { #SRU query adjusted per server cf. srufields column
463     my ($server, $query) = @_;
464
465     #sru_fields is in format title=field,isbn=field,...
466     #if a field doesn't exist, try anywhere or remove [field]=
467     my @parts= split(',', $server->{sru_fields} );
468     my %trans= map { if( /=/ ) { ( $`,$' ) } else { () } } @parts;
469     my $any= $trans{srchany}?$trans{srchany}.'=':'';
470
471     my $q=$query;
472     foreach my $key (keys %trans) {
473         my $f=$trans{$key};
474         if( $f ) {
475             $q=~s/\[$key\]/$f/g;
476         } else {
477             $q=~s/\[$key\]=/$any/g;
478         }
479     }
480     $q=~s/\[\w+\]=/$any/g; # remove remaining fields (not found in field list)
481     return $q;
482 }
483
484 =head2 ImportBreedingAuth
485
486 ImportBreedingAuth( $marcrecord, $filename, $encoding, $heading );
487
488     ImportBreedingAuth imports MARC records in the reservoir (import_records table) or returns their id if they already exist.
489
490 =cut
491
492 sub ImportBreedingAuth {
493     my ( $marcrecord, $filename, $encoding, $heading ) = @_;
494     my $dbh = C4::Context->dbh;
495
496     my $batch_id = GetZ3950BatchId($filename);
497     my $searchbreeding = $dbh->prepare("select import_record_id from import_auths where control_number=? and authorized_heading=?");
498
499     my $controlnumber = $marcrecord->field('001')->data;
500
501     # Normalize the record so it doesn't have separated diacritics
502     SetUTF8Flag($marcrecord);
503
504     $searchbreeding->execute($controlnumber,$heading);
505     my ($breedingid) = $searchbreeding->fetchrow;
506
507     return $breedingid if $breedingid;
508     $breedingid = AddAuthToBatch($batch_id, 0, $marcrecord, $encoding);
509     return $breedingid;
510 }
511
512 =head2 Z3950SearchAuth
513
514 Z3950SearchAuth($pars, $template);
515
516 Parameters for Z3950 search are all passed via the $pars hash. It may contain nameany, namepersonal, namecorp, namemeetingcon,
517 title, uniform title, subject, subjectsubdiv, srchany.
518 Also it should contain an arrayref id that points to a list of IDs of the z3950 targets to be queried (see z3950servers table).
519 This code is used in cataloging/z3950_auth_search.
520 The second parameter $template is a Template object. The routine uses this parameter to store the found values into the template.
521
522 =cut
523
524 sub Z3950SearchAuth {
525     my ($pars, $template)= @_;
526
527     my $dbh   = C4::Context->dbh;
528     my @id= @{$pars->{id}};
529     my $page= $pars->{page};
530
531
532     my $show_next       = 0;
533     my $total_pages     = 0;
534     my @encoding;
535     my @results;
536     my @serverhost;
537     my @breeding_loop = ();
538     my @oConnection;
539     my @oResult;
540     my @errconn;
541     my @servers;
542     my $s = 0;
543     my $query;
544     my $nterms=0;
545
546     my $marcflavour = C4::Context->preference('marcflavour');
547     my $marc_type = $marcflavour eq 'UNIMARC' ? 'UNIMARCAUTH' : $marcflavour;
548     my $authid= $pars->{authid};
549     my ( $zquery, $squery ) = _auth_build_query( $pars );
550     foreach my $servid (@id) {
551         my $sth = $dbh->prepare("select * from z3950servers where id=?");
552         $sth->execute($servid);
553         while ( my $server = $sth->fetchrow_hashref ) {
554             $oConnection[$s] = _create_connection( $server );
555
556             if ( $server->{servertype} eq 'zed' ) {
557                 my $server_zquery = $zquery;
558                 if ( my $attributes = $server->{attributes} ) {
559                     $server_zquery = "$attributes $zquery";
560                 }
561                 $oResult[$s] = $oConnection[$s]->search_pqf( $server_zquery );
562             }
563             else {
564                 $oResult[$s] = $oConnection[$s]->search(
565                     ZOOM::Query::CQL->new(_translate_query( $server, $squery ))
566                 );
567             }
568             $encoding[$s]   = ($server->{encoding}?$server->{encoding}:"iso-5426");
569             $servers[$s] = $server;
570             $s++;
571         }   ## while fetch
572     }    # foreach
573     my $nremaining  = $s;
574
575     while ( $nremaining-- ) {
576         my $k;
577         my $event;
578         while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
579             $event = $oConnection[ $k - 1 ]->last_event();
580             last if $event == ZOOM::Event::ZEND;
581         }
582
583         if ( $k != 0 ) {
584             $k--;
585             my ($error )= $oConnection[$k]->error_x(); #ignores errmsg, addinfo, diagset
586             if ($error) {
587                 if ($error =~ m/^(10000|10007)$/ ) {
588                     push(@errconn, {'server' => $serverhost[$k]});
589                 }
590             }
591             else {
592                 my $numresults = $oResult[$k]->size();
593                 my $i;
594                 my $result = '';
595                 if ( $numresults > 0  and $numresults >= (($page-1)*20)) {
596                     $show_next = 1 if $numresults >= ($page*20);
597                     $total_pages = int($numresults/20)+1 if $total_pages < ($numresults/20);
598                     for ($i = ($page-1)*20; $i < (($numresults < ($page*20)) ? $numresults : ($page*20)); $i++) {
599                         my $rec = $oResult[$k]->record($i);
600                         if ($rec) {
601                             my $marcrecord;
602                             my $marcdata;
603                             $marcdata   = $rec->raw();
604
605                             my ($charset_result, $charset_errors);
606                             if( $servers[$k]->{servertype} eq 'sru' ) {
607                                 $marcrecord = MARC::Record->new_from_xml( $marcdata, 'UTF-8', $servers[$k]->{syntax} );
608                                 $marcrecord->encoding('UTF-8');
609                             } else {
610                                 ( $marcrecord, $charset_result, $charset_errors ) = MarcToUTF8Record( $marcdata, $marc_type, $encoding[$k] );
611                             }
612                             my $heading;
613                             my $heading_authtype_code;
614                             $heading_authtype_code = GuessAuthTypeCode($marcrecord);
615                             next if ( not defined $heading_authtype_code ) ;
616
617                             $heading = C4::AuthoritiesMarc::GetAuthorizedHeading({ record => $marcrecord });
618
619                             my $breedingid = ImportBreedingAuth( $marcrecord, $serverhost[$k], $encoding[$k], $heading );
620                             my %row_data;
621                             $row_data{server}       = $servers[$k]->{'servername'};
622                             $row_data{breedingid}   = $breedingid;
623                             $row_data{heading}      = $heading;
624                             $row_data{authid}       = $authid;
625                             $row_data{heading_code}      = $heading_authtype_code;
626                             push( @breeding_loop, \%row_data );
627                         }
628                         else {
629                             push(@breeding_loop,{'server'=>$servers[$k]->{'servername'},'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'authid'=>-1});
630                         }
631                     }
632                 }    #if $numresults
633             }
634         }    # if $k !=0
635
636         $template->param(
637             numberpending => $nremaining,
638             current_page => $page,
639             total_pages => $total_pages,
640             show_nextbutton => $show_next?1:0,
641             show_prevbutton => $page!=1,
642         );
643     } # while nremaining
644
645     #close result sets and connections
646     foreach(0..$s-1) {
647         $oResult[$_]->destroy();
648         $oConnection[$_]->destroy();
649     }
650
651     @servers = ();
652     foreach my $id (@id) {
653         push @servers, {id => $id};
654     }
655     $template->param(
656         breeding_loop => \@breeding_loop,
657         servers => \@servers,
658         errconn       => \@errconn
659     );
660 }
661
662 1;
663 __END__
664