Bug 27945: Add limit article request feature
[koha.git] / opac / opac-request-article.pl
1 #!/usr/bin/perl
2
3 # Copyright ByWater Solutions 2015
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use CGI qw ( -utf8 );
23
24 use C4::Auth qw( get_template_and_user );
25 use C4::Output qw( output_html_with_http_headers );
26
27 use Koha::Biblios;
28 use Koha::Patrons;
29 use Try::Tiny;
30
31 my $cgi = CGI->new;
32
33 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
34     {
35         template_name   => "opac-request-article.tt",
36         query           => $cgi,
37         type            => "opac",
38     }
39 );
40
41 my $action = $cgi->param('action') || q{};
42 my $biblionumber = $cgi->param('biblionumber');
43 my $biblio = Koha::Biblios->find($biblionumber);
44 if( !$biblio ) {
45     print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
46     exit;
47 }
48
49 if ( $action eq 'create' ) {
50     my $branchcode = $cgi->param('branchcode');
51
52     my $itemnumber   = $cgi->param('itemnumber')   || undef;
53     my $title        = $cgi->param('title')        || undef;
54     my $author       = $cgi->param('author')       || undef;
55     my $volume       = $cgi->param('volume')       || undef;
56     my $issue        = $cgi->param('issue')        || undef;
57     my $date         = $cgi->param('date')         || undef;
58     my $pages        = $cgi->param('pages')        || undef;
59     my $chapters     = $cgi->param('chapters')     || undef;
60     my $patron_notes = $cgi->param('patron_notes') || undef;
61     my $format       = $cgi->param('format')       || undef;
62
63     try {
64         my $ar = Koha::ArticleRequest->new(
65             {
66                 borrowernumber => $borrowernumber,
67                 biblionumber   => $biblionumber,
68                 branchcode     => $branchcode,
69                 itemnumber     => $itemnumber,
70                 title          => $title,
71                 author         => $author,
72                 volume         => $volume,
73                 issue          => $issue,
74                 date           => $date,
75                 pages          => $pages,
76                 chapters       => $chapters,
77                 patron_notes   => $patron_notes,
78                 format         => $format,
79             }
80         )->store();
81
82         print $cgi->redirect("/cgi-bin/koha/opac-user.pl#opac-user-article-requests");
83         exit;
84     } catch {
85         exit unless $_->[0] && $_->[0] eq 'EXIT';
86         $template->param(
87             error_message => $_->{message}
88         );
89     };
90 # Should we redirect?
91 }
92 elsif ( !$action && C4::Context->preference('ArticleRequestsOpacHostRedirection') ) {
93   # Conditions: no items, host item entry (MARC21 773)
94   my ( $host, $pageinfo ) = $biblio->get_marc_host( { no_items => 1 } );
95   if ($host) {
96       $template->param(
97           pageinfo => $pageinfo,
98           title    => $biblio->title,
99           author   => $biblio->author
100       );
101       $biblio = $host;
102   }
103 }
104
105 my $patron = Koha::Patrons->find($borrowernumber);
106
107 if(!$patron->can_request_article) {
108     $template->param(
109         error_message => 'You cannot request more articles for today'
110     );
111 }
112
113 $template->param(
114     biblio => $biblio,
115     patron => $patron,
116     action => $action
117 );
118
119 output_html_with_http_headers $cgi, $cookie, $template->output, undef, { force_no_caching => 1 };