Bug 31963: Only show hold fee msg on OPAC if patron will be charged
[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 qw(TransformMarcToKoha);
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     my $row;
308     if( $breedingid ){
309         my @kohafields = ('biblio.title','biblio.author','biblioitems.isbn','biblioitems.lccn','biblioitems.editionstatement');
310         push @kohafields, C4::Context->preference('marcflavour') eq "MARC21" ? 'biblio.copyrightdate' : 'biblioitems.publicationyear';
311         $row = C4::Biblio::TransformMarcToKoha({ record => $marcrecord, kohafields => \@kohafields, limit_table => 'no_items' });
312         $row->{biblionumber} = $bib;
313         $row->{server}       = $servhref->{servername};
314         $row->{breedingid}   = $breedingid;
315         $row->{isbn}=_isbn_replace($row->{isbn});
316         $row = _add_custom_field_rowdata($row, $marcrecord);
317     }
318     return ( $row, $error );
319 }
320
321 sub _do_xslt_proc {
322     my ( $marc, $server, $xslh ) = @_;
323     return $marc if !$server->{add_xslt};
324
325     my $htdocs = C4::Context->config('intrahtdocs');
326     my $theme = C4::Context->preference("template"); #staff
327     my $lang = C4::Languages::getlanguage() || 'en';
328
329     my @files= split ',', $server->{add_xslt};
330     my $xml = $marc->as_xml;
331     foreach my $f ( @files ) {
332         $f =~ s/^\s+//; $f =~ s/\s+$//; next if !$f;
333         $f = C4::XSLT::_get_best_default_xslt_filename(
334             $htdocs, $theme, $lang, $f ) unless $f =~ /^\//;
335         $xml = $xslh->transform( $xml, $f );
336         last if $xslh->err; #skip other files
337     }
338     if( !$xslh->err ) {
339         return MARC::Record->new_from_xml($xml, 'UTF-8');
340     } else {
341         return ( $marc, $xslh->err ); #original record in case of errors
342     }
343 }
344
345 sub _add_custom_field_rowdata
346 {
347     my ( $row, $record ) = @_;
348     my $pref_newtags = C4::Context->preference('AdditionalFieldsInZ3950ResultSearch');
349     my $pref_flavour = C4::Context->preference('MarcFlavour');
350
351     $pref_newtags =~ s/^\s+|\s+$//g;
352     $pref_newtags =~ s/\h+/ /g;
353
354     my @addnumberfields;
355
356     foreach my $field (split /\,/, $pref_newtags) {
357         $field =~ s/^\s+|\s+$//g ;  # trim whitespace
358         my ($tag, $subtags) = split(/\$/, $field);
359
360         if ( $record->field($tag) ) {
361             my @content = ();
362
363             for my $marcfield ($record->field($tag)) {
364                 if ( $subtags ) {
365                     my $str = '';
366                     for my $code (split //, $subtags) {
367                         if ( $marcfield->subfield($code) ) {
368                             $str .= $marcfield->subfield($code) . ' ';
369                         }
370                     }
371                     if ( not $str eq '') {
372                         push @content, $str;
373                     }
374                 } elsif ( $tag == 10 ) {
375                     push @content, ( $pref_flavour eq "MARC21" ? $marcfield->data : $marcfield->as_string );
376                 } elsif ( $tag < 10 ) {
377                     push @content, $marcfield->data();
378                 } else {
379                     push @content, $marcfield->as_string();
380                 }
381             }
382
383             if ( @content ) {
384                 $row->{$field} = \@content;
385                 push( @addnumberfields, $field );
386             }
387         }
388     }
389
390     $row->{'addnumberfields'} = \@addnumberfields;
391
392     return $row;
393 }
394
395 sub _isbn_replace {
396     my ($isbn) = @_;
397     return unless defined $isbn;
398     $isbn =~ s/ |-|\.//g;
399     $isbn =~ s/\|/ \| /g;
400     $isbn =~ s/\(/ \(/g;
401     return $isbn;
402 }
403
404 sub _create_connection {
405     my ( $server ) = @_;
406     my $option1= ZOOM::Options->new();
407     $option1->option( 'async' => 1 );
408     $option1->option( 'elementSetName', 'F' );
409     $option1->option( 'preferredRecordSyntax', $server->{syntax} );
410     $option1->option( 'timeout', $server->{timeout} ) if $server->{timeout};
411
412     if( $server->{servertype} eq 'sru' ) {
413         foreach( split ',', $server->{sru_options}//'' ) {
414             #first remove surrounding spaces at comma and equals-sign
415             s/^\s+|\s+$//g;
416             my @temp= split '=', $_, 2;
417             @temp= map { my $c=$_; $c=~s/^\s+|\s+$//g; $c; } @temp;
418             $option1->option( $temp[0] => $temp[1] ) if @temp;
419         }
420     } elsif( $server->{servertype} eq 'zed' ) {
421         $option1->option( 'databaseName',   $server->{db} );
422         $option1->option( 'user', $server->{userid} ) if $server->{userid};
423         $option1->option( 'password', $server->{password} ) if $server->{password};
424     }
425     my $obj= ZOOM::Connection->create($option1);
426     if( $server->{servertype} eq 'sru' ) {
427         my $host= $server->{host};
428         if( $host !~ /^https?:\/\// ) {
429             #Normally, host will not be prefixed by protocol.
430             #In that case we can (safely) assume http.
431             #In case someone prefixed with https, give it a try..
432             $host = 'http://' . $host;
433         }
434         $obj->connect( $host.':'.$server->{port}.'/'.$server->{db} );
435     } else {
436         $obj->connect( $server->{host}, $server->{port} );
437     }
438     return $obj;
439 }
440
441 sub _translate_query { #SRU query adjusted per server cf. srufields column
442     my ($server, $query) = @_;
443
444     #sru_fields is in format title=field,isbn=field,...
445     #if a field doesn't exist, try anywhere or remove [field]=
446     my @parts= split(',', $server->{sru_fields} );
447     my %trans= map { if( /=/ ) { ( $`,$' ) } else { () } } @parts;
448     my $any= $trans{srchany}?$trans{srchany}.'=':'';
449
450     my $q=$query;
451     foreach my $key (keys %trans) {
452         my $f=$trans{$key};
453         if( $f ) {
454             $q=~s/\[$key\]/$f/g;
455         } else {
456             $q=~s/\[$key\]=/$any/g;
457         }
458     }
459     $q=~s/\[\w+\]=/$any/g; # remove remaining fields (not found in field list)
460     return $q;
461 }
462
463 =head2 ImportBreedingAuth
464
465 ImportBreedingAuth( $marcrecord, $filename, $encoding, $heading );
466
467     ImportBreedingAuth imports MARC records in the reservoir (import_records table) or returns their id if they already exist.
468
469 =cut
470
471 sub ImportBreedingAuth {
472     my ( $marcrecord, $filename, $encoding, $heading ) = @_;
473     my $dbh = C4::Context->dbh;
474
475     my $batch_id = GetZ3950BatchId($filename);
476     my $searchbreeding = $dbh->prepare("select import_record_id from import_auths where control_number=? and authorized_heading=?");
477
478     my $controlnumber = $marcrecord->field('001')->data;
479
480     # Normalize the record so it doesn't have separated diacritics
481     SetUTF8Flag($marcrecord);
482
483     $searchbreeding->execute($controlnumber,$heading);
484     my ($breedingid) = $searchbreeding->fetchrow;
485
486     return $breedingid if $breedingid;
487     $breedingid = AddAuthToBatch($batch_id, 0, $marcrecord, $encoding);
488     return $breedingid;
489 }
490
491 =head2 Z3950SearchAuth
492
493 Z3950SearchAuth($pars, $template);
494
495 Parameters for Z3950 search are all passed via the $pars hash. It may contain nameany, namepersonal, namecorp, namemeetingcon,
496 title, uniform title, subject, subjectsubdiv, srchany.
497 Also it should contain an arrayref id that points to a list of IDs of the z3950 targets to be queried (see z3950servers table).
498 This code is used in cataloging/z3950_auth_search.
499 The second parameter $template is a Template object. The routine uses this parameter to store the found values into the template.
500
501 =cut
502
503 sub Z3950SearchAuth {
504     my ($pars, $template)= @_;
505
506     my $dbh   = C4::Context->dbh;
507     my @id= @{$pars->{id}};
508     my $page= $pars->{page};
509
510
511     my $show_next       = 0;
512     my $total_pages     = 0;
513     my @encoding;
514     my @results;
515     my @serverhost;
516     my @breeding_loop = ();
517     my @oConnection;
518     my @oResult;
519     my @errconn;
520     my @servers;
521     my $s = 0;
522     my $query;
523     my $nterms=0;
524
525     my $marcflavour = C4::Context->preference('marcflavour');
526     my $marc_type = $marcflavour eq 'UNIMARC' ? 'UNIMARCAUTH' : $marcflavour;
527     my $authid= $pars->{authid};
528     my ( $zquery, $squery ) = _auth_build_query( $pars );
529     foreach my $servid (@id) {
530         my $sth = $dbh->prepare("select * from z3950servers where id=?");
531         $sth->execute($servid);
532         while ( my $server = $sth->fetchrow_hashref ) {
533             $oConnection[$s] = _create_connection( $server );
534
535             if ( $server->{servertype} eq 'zed' ) {
536                 my $server_zquery = $zquery;
537                 if ( my $attributes = $server->{attributes} ) {
538                     $server_zquery = "$attributes $zquery";
539                 }
540                 $oResult[$s] = $oConnection[$s]->search_pqf( $server_zquery );
541             }
542             else {
543                 $oResult[$s] = $oConnection[$s]->search(
544                     ZOOM::Query::CQL->new(_translate_query( $server, $squery ))
545                 );
546             }
547             $encoding[$s]   = ($server->{encoding}?$server->{encoding}:"iso-5426");
548             $servers[$s] = $server;
549             $s++;
550         }   ## while fetch
551     }    # foreach
552     my $nremaining  = $s;
553
554     while ( $nremaining-- ) {
555         my $k;
556         my $event;
557         while ( ( $k = ZOOM::event( \@oConnection ) ) != 0 ) {
558             $event = $oConnection[ $k - 1 ]->last_event();
559             last if $event == ZOOM::Event::ZEND;
560         }
561
562         if ( $k != 0 ) {
563             $k--;
564             my ($error )= $oConnection[$k]->error_x(); #ignores errmsg, addinfo, diagset
565             if ($error) {
566                 if ($error =~ m/^(10000|10007)$/ ) {
567                     push(@errconn, {'server' => $serverhost[$k]});
568                 }
569             }
570             else {
571                 my $numresults = $oResult[$k]->size();
572                 my $i;
573                 my $result = '';
574                 if ( $numresults > 0  and $numresults >= (($page-1)*20)) {
575                     $show_next = 1 if $numresults >= ($page*20);
576                     $total_pages = int($numresults/20)+1 if $total_pages < ($numresults/20);
577                     for ($i = ($page-1)*20; $i < (($numresults < ($page*20)) ? $numresults : ($page*20)); $i++) {
578                         my $rec = $oResult[$k]->record($i);
579                         if ($rec) {
580                             my $marcrecord;
581                             my $marcdata;
582                             $marcdata   = $rec->raw();
583
584                             my ($charset_result, $charset_errors);
585                             if( $servers[$k]->{servertype} eq 'sru' ) {
586                                 $marcrecord = MARC::Record->new_from_xml( $marcdata, 'UTF-8', $servers[$k]->{syntax} );
587                                 $marcrecord->encoding('UTF-8');
588                             } else {
589                                 ( $marcrecord, $charset_result, $charset_errors ) = MarcToUTF8Record( $marcdata, $marc_type, $encoding[$k] );
590                             }
591                             my $heading;
592                             my $heading_authtype_code;
593                             $heading_authtype_code = GuessAuthTypeCode($marcrecord);
594                             next if ( not defined $heading_authtype_code ) ;
595
596                             $heading = C4::AuthoritiesMarc::GetAuthorizedHeading({ record => $marcrecord });
597
598                             my $breedingid = ImportBreedingAuth( $marcrecord, $serverhost[$k], $encoding[$k], $heading );
599                             my %row_data;
600                             $row_data{server}       = $servers[$k]->{'servername'};
601                             $row_data{breedingid}   = $breedingid;
602                             $row_data{heading}      = $heading;
603                             $row_data{authid}       = $authid;
604                             $row_data{heading_code}      = $heading_authtype_code;
605                             push( @breeding_loop, \%row_data );
606                         }
607                         else {
608                             push(@breeding_loop,{'server'=>$servers[$k]->{'servername'},'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'authid'=>-1});
609                         }
610                     }
611                 }    #if $numresults
612             }
613         }    # if $k !=0
614
615         $template->param(
616             numberpending => $nremaining,
617             current_page => $page,
618             total_pages => $total_pages,
619             show_nextbutton => $show_next?1:0,
620             show_prevbutton => $page!=1,
621         );
622     } # while nremaining
623
624     #close result sets and connections
625     foreach(0..$s-1) {
626         $oResult[$_]->destroy();
627         $oConnection[$_]->destroy();
628     }
629
630     @servers = ();
631     foreach my $id (@id) {
632         push @servers, {id => $id};
633     }
634     $template->param(
635         breeding_loop => \@breeding_loop,
636         servers => \@servers,
637         errconn       => \@errconn
638     );
639 }
640
641 1;
642 __END__
643