Bug 17600: Standardize our EXPORT_OK
[srvgit] / members / merge-patrons.pl
index 8ad2cfb..5318ff6 100755 (executable)
 use Modern::Perl;
 
 use CGI qw ( -utf8 );
+use Try::Tiny qw( catch try );
 
-use C4::Auth;
-use C4::Output;
+use C4::Auth qw( get_template_and_user );
+use C4::Output qw( output_html_with_http_headers );
 use C4::Context;
 use Koha::Patrons;
 
-my $cgi = new CGI;
+my $cgi = CGI->new;
 
 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
     {
         template_name   => "members/merge-patrons.tt",
         query           => $cgi,
         type            => "intranet",
-        authnotrequired => 0,
-        flagsrequired   => { borrowers => 1 },
-        debug           => 1,
+        flagsrequired   => { borrowers => 'edit_borrowers' },
     }
 );
 
 my $action = $cgi->param('action') || 'show';
-my @ids = $cgi->multi_param('id');
+my @ids    = $cgi->multi_param('id');
 
 if ( $action eq 'show' ) {
-    my $patrons =
-      Koha::Patrons->search( { borrowernumber => { -in => \@ids } } );
+    my $patrons = Koha::Patrons->search({ borrowernumber => { -in => \@ids } });
     $template->param( patrons => $patrons );
 } elsif ( $action eq 'merge' ) {
-    my $keeper = $cgi->param('keeper');
-    my $results = Koha::Patrons->merge( { keeper => $keeper, patrons => \@ids } );
-    $template->param( results => $results );
+    my $keeper_id = $cgi->param('keeper');
+    my $results;
+
+    my $keeper = Koha::Patrons->find( $keeper_id );
+
+    if ( $keeper ) {
+        try {
+            $results = $keeper->merge_with( \@ids );
+            $template->param(
+                keeper  => $keeper,
+                results => $results
+            );
+        }
+        catch {
+            $template->param( error => $_ );
+        }
+    } else {
+        $template->param( error => 'INVALID_KEEPER' );
+    }
 }
 
 $template->param( action => $action );