Bug 14930 - Leaving OpacFineNoRenewals blank blocks renewals, but should disable...
[koha.git] / opac / opac-addbybiblionumber.pl
1 #!/usr/bin/perl
2
3 #script to provide virtualshelf management
4 # WARNING: This file uses 4-character tabs!
5 #
6 # $Header$
7 #
8 # Copyright 2000-2002 Katipo Communications
9 #
10 # This file is part of Koha.
11 #
12 # Koha is free software; you can redistribute it and/or modify it
13 # under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 3 of the License, or
15 # (at your option) any later version.
16 #
17 # Koha is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24
25 use strict;
26 use warnings;
27
28 use CGI qw ( -utf8 );
29 use C4::Biblio;
30 use C4::VirtualShelves qw/:DEFAULT GetAllShelves/;
31 use C4::Output;
32 use C4::Auth;
33
34 our $query              = new CGI;
35 our @biblionumber       = $query->param('biblionumber');
36 our $selectedshelf      = $query->param('selectedshelf');
37 our $newshelf           = $query->param('newshelf');
38 our $shelfnumber        = $query->param('shelfnumber');
39 our $newvirtualshelf    = $query->param('newvirtualshelf');
40 our $category           = $query->param('category');
41 our $authorized          = 1;
42 our $errcode            = 0;
43 our @biblios;
44
45 # if virtualshelves is disabled, leave immediately
46 if ( ! C4::Context->preference('virtualshelves') ) {
47     print $query->redirect("/cgi-bin/koha/errors/404.pl");
48     exit;
49 }
50
51 if (scalar(@biblionumber) == 1) {
52     @biblionumber = (split /\//,$biblionumber[0]);
53 }
54
55 our ( $template, $loggedinuser, $cookie ) = get_template_and_user(
56     {
57         template_name   => "opac-addbybiblionumber.tt",
58         query           => $query,
59         type            => "opac",
60         authnotrequired => 0,
61     }
62 );
63
64 if( $newvirtualshelf) {
65     HandleNewVirtualShelf();
66     exit if $authorized;
67     ShowTemplate(); #error message
68 }
69 elsif($shelfnumber) {
70     HandleShelfNumber();
71     exit if $authorized;
72     ShowTemplate(); #error message
73 }
74 elsif($selectedshelf) {
75     HandleSelectedShelf();
76     LoadBib() if $authorized;
77     ShowTemplate();
78 }
79 else {
80     HandleSelect();
81     LoadBib() if $authorized;
82     ShowTemplate();
83 }
84 #end
85
86 sub AddBibliosToShelf {
87     #splits incoming biblionumber(s) to array and adds each to shelf.
88     my ($shelfnumber,@biblionumber)=@_;
89
90     #multiple bibs might come in as '/' delimited string (from where, i don't see), or as array.
91     if (scalar(@biblionumber) == 1) {
92         @biblionumber = (split /\//,$biblionumber[0]);
93     }
94     for my $bib (@biblionumber) {
95         AddToShelf($bib, $shelfnumber, $loggedinuser);
96     }
97 }
98
99 sub HandleNewVirtualShelf {
100     if($authorized= ShelfPossibleAction($loggedinuser, undef, $category==1? 'new_private': 'new_public')) {
101     $shelfnumber = AddShelf( {
102             shelfname => $newvirtualshelf,
103             category => $category }, $loggedinuser);
104     if($shelfnumber == -1) {
105         $authorized=0;
106         $errcode=1;
107         return;
108     }
109     AddBibliosToShelf($shelfnumber, @biblionumber);
110     #Reload the page where you came from
111     print $query->header;
112     print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"window.opener.location.reload(true);self.close();\"></body></html>";
113     }
114 }
115
116 sub HandleShelfNumber {
117     if($authorized= ShelfPossibleAction($loggedinuser, $shelfnumber, 'add')) {
118     AddBibliosToShelf($shelfnumber,@biblionumber);
119     #Close this page and return
120     print $query->header;
121     print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"self.close();\"></body></html>";
122     }
123 }
124
125 sub HandleSelectedShelf {
126     if($authorized= ShelfPossibleAction( $loggedinuser, $selectedshelf, 'add')){
127         #adding to specific shelf
128         my ($singleshelf, $singleshelfname)= GetShelf($query->param('selectedshelf'));
129         $template->param(
130         singleshelf               => 1,
131         shelfnumber               => $singleshelf,
132         shelfname                 => $singleshelfname,
133         );
134     }
135 }
136
137 sub HandleSelect {
138     return unless $authorized= $loggedinuser>0;
139     my $privateshelves = GetAllShelves(1,$loggedinuser,1);
140     if(@{$privateshelves}){
141         $template->param (
142         privatevirtualshelves          => $privateshelves,
143         existingshelves => 1
144     );
145     }
146     my $publicshelves = GetAllShelves(2,$loggedinuser,1);
147     if(@{$publicshelves}){
148         $template->param (
149         publicvirtualshelves          => $publicshelves,
150         existingshelves => 1
151     );
152     }
153 }
154
155 sub LoadBib {
156     #see comment in AddBibliosToShelf
157     if (scalar(@biblionumber) == 1) {
158         @biblionumber = (split /\//,$biblionumber[0]);
159     }
160     for my $bib (@biblionumber) {
161         my $data = GetBiblioData( $bib );
162     push(@biblios,
163         { biblionumber => $bib,
164           title        => $data->{'title'},
165           author       => $data->{'author'},
166     } );
167     }
168     $template->param(
169         multiple => (scalar(@biblios) > 1),
170     total    => scalar @biblios,
171     biblios  => \@biblios,
172     );
173 }
174
175 sub ShowTemplate {
176     $template->param (
177     newshelf => $newshelf||0,
178     authorized  => $authorized,
179     errcode             => $errcode,
180     OpacAllowPublicListCreation => C4::Context->preference('OpacAllowPublicListCreation'),
181     );
182     output_html_with_http_headers $query, $cookie, $template->output;
183 }