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