ffzg/recall_notices.pl: added --interval and --dedup
[koha.git] / ill / ill-requests.pl
index 26cb350..45f99c2 100755 (executable)
@@ -24,10 +24,14 @@ use CGI;
 use C4::Auth;
 use C4::Output;
 use Koha::AuthorisedValues;
+use Koha::Illcomment;
 use Koha::Illrequests;
 use Koha::Libraries;
+use Koha::Token;
 
-my $cgi = CGI->new;
+use Try::Tiny;
+
+our $cgi = CGI->new;
 my $illRequests = Koha::Illrequests->new;
 
 # Grab all passed data
@@ -35,6 +39,12 @@ my $illRequests = Koha::Illrequests->new;
 # of 'my'
 our $params = $cgi->Vars();
 
+# Leave immediately if ILLModule is disabled
+unless ( C4::Context->preference('ILLModule') ) {
+    print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
+    exit;
+}
+
 my $op = $params->{method} || 'illlist';
 
 my ( $template, $patronnumber, $cookie ) = get_template_and_user( {
@@ -44,203 +54,298 @@ my ( $template, $patronnumber, $cookie ) = get_template_and_user( {
     flagsrequired => { ill => '*' },
 } );
 
-if ( $op eq 'illview' ) {
-    # View the details of an ILL
-    my $request = Koha::Illrequests->find($params->{illrequest_id});
-
-    $template->param(
-        request => $request
-    );
-
-} elsif ( $op eq 'create' ) {
-    # We're in the process of creating a request
-    my $request = Koha::Illrequest->new
-        ->load_backend($params->{backend});
-    my $backend_result = $request->backend_create($params);
-    $template->param(
-        whole   => $backend_result,
-        request => $request
-    );
-    handle_commit_maybe($backend_result, $request);
-
-} elsif ( $op eq 'confirm' ) {
-    # Backend 'confirm' method
-    # confirm requires a specific request, so first, find it.
-    my $request = Koha::Illrequests->find($params->{illrequest_id});
-    my $backend_result = $request->backend_confirm($params);
-    $template->param(
-        whole   => $backend_result,
-        request => $request,
-    );
-
-    # handle special commit rules & update type
-    handle_commit_maybe($backend_result, $request);
-
-} elsif ( $op eq 'cancel' ) {
-    # Backend 'cancel' method
-    # cancel requires a specific request, so first, find it.
-    my $request = Koha::Illrequests->find($params->{illrequest_id});
-    my $backend_result = $request->backend_cancel($params);
-    $template->param(
-        whole   => $backend_result,
-        request => $request,
-    );
-
-    # handle special commit rules & update type
-    handle_commit_maybe($backend_result, $request);
-
-} elsif ( $op eq 'edit_action' ) {
-    # Handle edits to the Illrequest object.
-    # (not the Illrequestattributes)
-    # We simulate the API for backend requests for uniformity.
-    # So, init:
-    my $request = Koha::Illrequests->find($params->{illrequest_id});
-    if ( !$params->{stage} ) {
-        my $backend_result = {
-            error   => 0,
-            status  => '',
-            message => '',
-            method  => 'edit_action',
-            stage   => 'init',
-            next    => '',
-            value   => {}
-        };
+# Are we able to actually work?
+my $cfg = Koha::Illrequest::Config->new;
+my $backends = $cfg->available_backends;
+my $has_branch = $cfg->has_branch;
+my $backends_available = ( scalar @{$backends} > 0 );
+$template->param(
+    backends_available => $backends_available,
+    has_branch         => $has_branch
+);
+
+if ( $backends_available ) {
+    if ( $op eq 'illview' ) {
+        # View the details of an ILL
+        my $request = Koha::Illrequests->find($params->{illrequest_id});
+
+        $template->param(
+            request    => $request,
+            csrf_token => Koha::Token->new->generate_csrf({
+                session_id => scalar $cgi->cookie('CGISESSID'),
+            }),
+            ( $params->{error} ? ( error => $params->{error} ) : () ),
+        );
+
+    } elsif ( $op eq 'create' ) {
+        # We're in the process of creating a request
+        my $request = Koha::Illrequest->new->load_backend( $params->{backend} );
+        my $backend_result = $request->backend_create($params);
         $template->param(
             whole   => $backend_result,
             request => $request
         );
-    } else {
-        # Commit:
-        # Save the changes
-        $request->borrowernumber($params->{borrowernumber});
-        $request->biblio_id($params->{biblio_id});
-        $request->branchcode($params->{branchcode});
-        $request->notesopac($params->{notesopac});
-        $request->notesstaff($params->{notesstaff});
-        $request->store;
-        my $backend_result = {
-            error   => 0,
-            status  => '',
-            message => '',
-            method  => 'edit_action',
-            stage   => 'commit',
-            next    => 'illlist',
-            value   => {}
-        };
         handle_commit_maybe($backend_result, $request);
-    }
 
-} elsif ( $op eq 'moderate_action' ) {
-    # Moderate action is required for an ILL submodule / syspref.
-    # Currently still needs to be implemented.
-    redirect_to_list();
+    } elsif ( $op eq 'migrate' ) {
+        # We're in the process of migrating a request
+        my $request = Koha::Illrequests->find($params->{illrequest_id});
+        my $backend_result;
+        if ( $params->{backend} ) {
+            my $new_request = Koha::Illrequest->new->load_backend( $params->{backend} );
+            $backend_result = $new_request->backend_migrate($params);
+            if ($backend_result) {
+                $template->param(
+                    whole   => $backend_result,
+                    request => $new_request
+                );
+                $request = $new_request;
+            } else {
+                # Backend failure, redirect back to illview
+                print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
+                      . '?method=illview'
+                      . '&illrequest_id='
+                      . $request->id
+                      . '&error=migrate_target' );
+                exit;
+            }
+        }
+        else {
+            $backend_result = $request->backend_migrate($params);
+            $template->param(
+                whole   => $backend_result,
+                request => $request
+            );
+        }
+        handle_commit_maybe( $backend_result, $request );
 
-} elsif ( $op eq 'delete_confirm') {
-    my $request = Koha::Illrequests->find($params->{illrequest_id});
+    } elsif ( $op eq 'confirm' ) {
+        # Backend 'confirm' method
+        # confirm requires a specific request, so first, find it.
+        my $request = Koha::Illrequests->find($params->{illrequest_id});
+        my $backend_result = $request->backend_confirm($params);
+        $template->param(
+            whole   => $backend_result,
+            request => $request,
+        );
 
-    $template->param(
-        request => $request
-    );
+        # handle special commit rules & update type
+        handle_commit_maybe($backend_result, $request);
 
-} elsif ( $op eq 'delete' ) {
+    } elsif ( $op eq 'cancel' ) {
+        # Backend 'cancel' method
+        # cancel requires a specific request, so first, find it.
+        my $request = Koha::Illrequests->find($params->{illrequest_id});
+        my $backend_result = $request->backend_cancel($params);
+        $template->param(
+            whole   => $backend_result,
+            request => $request,
+        );
+
+        # handle special commit rules & update type
+        handle_commit_maybe($backend_result, $request);
+
+    } elsif ( $op eq 'edit_action' ) {
+        # Handle edits to the Illrequest object.
+        # (not the Illrequestattributes)
+        # We simulate the API for backend requests for uniformity.
+        # So, init:
+        my $request = Koha::Illrequests->find($params->{illrequest_id});
+        if ( !$params->{stage} ) {
+            my $backend_result = {
+                error   => 0,
+                status  => '',
+                message => '',
+                method  => 'edit_action',
+                stage   => 'init',
+                next    => '',
+                value   => {}
+            };
+            $template->param(
+                whole          => $backend_result,
+                request        => $request
+            );
+        } else {
+            # Commit:
+            # Save the changes
+            $request->borrowernumber($params->{borrowernumber});
+            $request->biblio_id($params->{biblio_id});
+            $request->branchcode($params->{branchcode});
+            $request->price_paid($params->{price_paid});
+            $request->notesopac($params->{notesopac});
+            $request->notesstaff($params->{notesstaff});
+            my $alias = (length $params->{status_alias} > 0) ?
+                $params->{status_alias} :
+                "-1";
+            $request->status_alias($alias);
+            $request->store;
+            my $backend_result = {
+                error   => 0,
+                status  => '',
+                message => '',
+                method  => 'edit_action',
+                stage   => 'commit',
+                next    => 'illlist',
+                value   => {}
+            };
+            handle_commit_maybe($backend_result, $request);
+        }
 
-    # Check if the request is confirmed, if not, redirect
-    # to the confirmation view
-    if ($params->{confirmed} == 1) {
-        # We simply delete the request...
-        my $request = Koha::Illrequests->find(
-            $params->{illrequest_id}
-        )->delete;
-        # ... then return to list view.
+    } elsif ( $op eq 'moderate_action' ) {
+        # Moderate action is required for an ILL submodule / syspref.
+        # Currently still needs to be implemented.
         redirect_to_list();
-    } else {
-        print $cgi->redirect(
-            "/cgi-bin/koha/ill/ill-requests.pl?" .
-            "method=delete_confirm&illrequest_id=" .
-            $params->{illrequest_id});
-    }
 
-} elsif ( $op eq 'mark_completed' ) {
-    my $request = Koha::Illrequests->find($params->{illrequest_id});
-    my $backend_result = $request->mark_completed($params);
-    $template->param(
-        whole => $backend_result,
-        request => $request,
-    );
-
-    # handle special commit rules & update type
-    handle_commit_maybe($backend_result, $request);
-
-} elsif ( $op eq 'generic_confirm' ) {
-    my $request = Koha::Illrequests->find($params->{illrequest_id});
-    $params->{current_branchcode} = C4::Context->mybranch;
-    my $backend_result = $request->generic_confirm($params);
-    $template->param(
-        whole => $backend_result,
-        request => $request,
-    );
-
-    # handle special commit rules & update type
-    handle_commit_maybe($backend_result, $request);
-
-} elsif ( $op eq 'illlist') {
-    # Display all current ILLs
-    my $requests = $illRequests->search();
-
-    $template->param(
-        requests => $requests
-    );
-
-    # If we receive a pre-filter, make it available to the template
-    my $possible_filters = ['borrowernumber'];
-    my $active_filters = [];
-    foreach my $filter(@{$possible_filters}) {
-        if ($params->{$filter}) {
-            push @{$active_filters},
-                { name => $filter, value => $params->{$filter}};
+    } elsif ( $op eq 'delete_confirm') {
+        my $request = Koha::Illrequests->find($params->{illrequest_id});
+
+        $template->param(
+            request => $request
+        );
+
+    } elsif ( $op eq 'delete' ) {
+
+        # Check if the request is confirmed, if not, redirect
+        # to the confirmation view
+        if ($params->{confirmed}) {
+            # We simply delete the request...
+            Koha::Illrequests->find( $params->{illrequest_id} )->delete;
+            # ... then return to list view.
+            redirect_to_list();
+        } else {
+            print $cgi->redirect(
+                "/cgi-bin/koha/ill/ill-requests.pl?" .
+                "method=delete_confirm&illrequest_id=" .
+                $params->{illrequest_id});
+            exit;
         }
-    }
-    if (scalar @{$active_filters} > 0) {
+
+    } elsif ( $op eq 'mark_completed' ) {
+        my $request = Koha::Illrequests->find($params->{illrequest_id});
+        my $backend_result = $request->mark_completed($params);
+        $template->param(
+            whole => $backend_result,
+            request => $request,
+        );
+
+        # handle special commit rules & update type
+        handle_commit_maybe($backend_result, $request);
+
+    } elsif ( $op eq 'generic_confirm' ) {
+        my $backend_result;
+        my $request;
+        try {
+            $request = Koha::Illrequests->find($params->{illrequest_id});
+            $params->{current_branchcode} = C4::Context->mybranch;
+            $backend_result = $request->generic_confirm($params);
+            $template->param(
+                whole => $backend_result,
+                request => $request,
+            );
+            $template->param( error => $params->{error} )
+                if $params->{error};
+        }
+        catch {
+            my $error;
+            if ( ref($_) eq 'Koha::Exceptions::Ill::NoTargetEmail' ) {
+                $error = 'no_target_email';
+            }
+            elsif ( ref($_) eq 'Koha::Exceptions::Ill::NoLibraryEmail' ) {
+                $error = 'no_library_email';
+            }
+            else {
+                $error = 'unknown_error';
+            }
+            print $cgi->redirect(
+                "/cgi-bin/koha/ill/ill-requests.pl?" .
+                "method=generic_confirm&illrequest_id=" .
+                $params->{illrequest_id} .
+                "&error=$error" );
+            exit;
+        };
+
+        # handle special commit rules & update type
+        handle_commit_maybe($backend_result, $request);
+    } elsif ( $op eq 'illlist') {
+
+        # If we receive a pre-filter, make it available to the template
+        my $possible_filters = ['borrowernumber'];
+        my $active_filters = [];
+        foreach my $filter(@{$possible_filters}) {
+            if ($params->{$filter}) {
+                push @{$active_filters}, "$filter=$params->{$filter}";
+            }
+        }
+        if (scalar @{$active_filters} > 0) {
+            $template->param(
+                prefilters => join(",", @{$active_filters})
+            );
+        }
+
+    } elsif ( $op eq "save_comment" ) {
+        die "Wrong CSRF token" unless Koha::Token->new->check_csrf({
+           session_id => scalar $cgi->cookie('CGISESSID'),
+           token      => scalar $cgi->param('csrf_token'),
+        });
+        my $comment = Koha::Illcomment->new({
+            illrequest_id  => scalar $params->{illrequest_id},
+            borrowernumber => $patronnumber,
+            comment        => scalar $params->{comment},
+        });
+        $comment->store();
+        # Redirect to view the whole request
+        print $cgi->redirect("/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
+            scalar $params->{illrequest_id}
+        );
+        exit;
+
+    } else {
+        my $request = Koha::Illrequests->find($params->{illrequest_id});
+        my $backend_result = $request->custom_capability($op, $params);
         $template->param(
-            prefilters => $active_filters
+            whole => $backend_result,
+            request => $request,
         );
+
+        # handle special commit rules & update type
+        handle_commit_maybe($backend_result, $request);
     }
-} else {
-    my $request = Koha::Illrequests->find($params->{illrequest_id});
-    my $backend_result = $request->custom_capability($op, $params);
-    $template->param(
-        whole => $backend_result,
-        request => $request,
-    );
-
-    # handle special commit rules & update type
-    handle_commit_maybe($backend_result, $request);
 }
 
-# Get a list of backends
-my $ir = Koha::Illrequest->new;
-
 $template->param(
-    backends    => $ir->available_backends,
-    media       => [ "Book", "Article", "Journal" ],
-    query_type  => $op,
-    branches    => Koha::Libraries->search->unblessed,
-    here_link   => "/cgi-bin/koha/ill/ill-requests.pl"
+    backends   => $backends,
+    types      => [ "Book", "Article", "Journal" ],
+    query_type => $op,
+    branches   => scalar Koha::Libraries->search,
 );
 
 output_html_with_http_headers( $cgi, $cookie, $template->output );
 
 sub handle_commit_maybe {
     my ( $backend_result, $request ) = @_;
+
     # We need to special case 'commit'
     if ( $backend_result->{stage} eq 'commit' ) {
         if ( $backend_result->{next} eq 'illview' ) {
+
             # Redirect to a view of the newly created request
-            print $cgi->redirect(
-                '/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id='.
-                $request->id
-            );
-        } else {
+            print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
+                  . '?method=illview'
+                  . '&illrequest_id='
+                  . $request->id );
+            exit;
+        }
+        elsif ( $backend_result->{next} eq 'emigrate' ) {
+
+            # Redirect to a view of the newly created request
+            print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
+                  . '?method=migrate'
+                  . '&stage=emigrate'
+                  . '&illrequest_id='
+                  . $request->id );
+            exit;
+        }
+        else {
             # Redirect to a requests list view
             redirect_to_list();
         }
@@ -249,4 +354,5 @@ sub handle_commit_maybe {
 
 sub redirect_to_list {
     print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');
+    exit;
 }