Bug 14544: Get rid of GetShelf
[koha.git] / virtualshelves / addbybiblionumber.pl
1 #!/usr/bin/perl
2
3 #script to provide virtual shelf management
4 #
5 #
6 # Copyright 2000-2002 Katipo Communications
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22
23
24 =head1 NAME
25
26 addbybiblionumber.pl
27
28 =head1 DESCRIPTION
29
30     This script allow to add a virtual in a virtual shelf from a biblionumber.
31
32 =head1 CGI PARAMETERS
33
34 =over 4
35
36 =item biblionumber
37
38     The biblionumber
39
40 =item shelfnumber
41
42     the shelfnumber where to add the virtual.
43
44 =item newvirtualshelf
45
46     if this parameter exists, then it must be equals to the name of the shelf
47     to add.
48
49 =item category
50
51     if this script has to add a shelf, it add one with this category.
52
53 =item newshelf
54
55     if this parameter exists, then we create a new shelf
56
57 =back
58
59 =cut
60
61 use strict;
62 use warnings;
63
64 use CGI qw ( -utf8 );
65 use C4::Biblio;
66 use C4::Output;
67 use C4::VirtualShelves qw/:DEFAULT GetAllShelves/;
68 use C4::Auth;
69
70 use Koha::Virtualshelves;
71
72 our $query           = new CGI;
73 our @biblionumber    = HandleBiblioPars();
74 our $shelfnumber     = $query->param('shelfnumber');
75 our $newvirtualshelf = $query->param('newvirtualshelf');
76 our $newshelf        = $query->param('newshelf');
77 our $category        = $query->param('category');
78 our $sortfield      = $query->param('sortfield');
79 my $confirmed       = $query->param('confirmed') || 0;
80 our $authorized      = 1;
81 our $errcode        = 0;
82
83 our ( $template, $loggedinuser, $cookie ) = get_template_and_user(
84     {
85         template_name   => "virtualshelves/addbybiblionumber.tt",
86         query           => $query,
87         type            => "intranet",
88         authnotrequired => 0,
89         flagsrequired   => { catalogue => 1 },
90     }
91 );
92
93 if( $newvirtualshelf) {
94     HandleNewVirtualShelf();
95     exit if $authorized;
96     ShowTemplate(); #error message
97 }
98 elsif($shelfnumber && $confirmed) {
99     HandleShelfNumber();
100     exit if $authorized;
101     ShowTemplate(); #error message
102 }
103 elsif($shelfnumber) { #still needs confirmation
104     HandleSelectedShelf();
105     LoadBib() if $authorized;
106     ShowTemplate();
107 }
108 else {
109     HandleSelect();
110     LoadBib();
111     ShowTemplate();
112 }
113 #end
114
115 sub HandleBiblioPars {
116     my @bib= $query->param('biblionumber');
117     if(@bib==0 && $query->param('biblionumbers')) {
118         my $str= $query->param('biblionumbers');
119         @bib= split '/', $str;
120     }
121     elsif(@bib==1 && $bib[0]=~/\//) {
122         @bib= split '/', $bib[0];
123     }
124     return @bib;
125 }
126
127 sub AddBibliosToShelf {
128     my ($shelfnumber, @biblionumber)=@_;
129     for my $bib (@biblionumber){
130         AddToShelf($bib, $shelfnumber, $loggedinuser);
131     }
132 }
133
134 sub HandleNewVirtualShelf {
135     $shelfnumber = AddShelf( {
136         shelfname => $newvirtualshelf,
137         sortfield => $sortfield,
138         category => $category }, $loggedinuser);
139     if($shelfnumber == -1) {
140         $authorized=0;
141         $errcode=1; #add failed
142         return;
143     }
144     AddBibliosToShelf($shelfnumber, @biblionumber);
145     #Reload the page where you came from
146     print $query->header;
147     print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"window.opener.location.reload(true);self.close();\"></body></html>";
148 }
149
150 sub HandleShelfNumber {
151     if($authorized= ShelfPossibleAction($loggedinuser, $shelfnumber, 'add')) {
152     AddBibliosToShelf($shelfnumber, @biblionumber);
153     #Close this page and return
154     print $query->header;
155     print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"self.close();\"></body></html>";
156     }
157     else {
158     $errcode=2; #no perm
159     }
160 }
161
162 sub HandleSelectedShelf {
163     if($authorized= ShelfPossibleAction( $loggedinuser, $shelfnumber, 'add')){
164         #confirm adding to specific shelf
165         my $shelf = Koha::Virtualshelves->find( $shelfnumber );
166         $template->param(
167         singleshelf               => 1,
168         shelfnumber               => $shelf->shelfnumber,
169         shelfname                 => $shelf->shelfname,
170         );
171     }
172     else {
173     $errcode=2; #no perm
174     }
175 }
176
177 sub HandleSelect {
178     my $privateshelves = GetAllShelves(1,$loggedinuser,1);
179     my $publicshelves = GetAllShelves(2,$loggedinuser,1);
180     $template->param(
181     privatevirtualshelves => $privateshelves,
182     publicvirtualshelves  => $publicshelves,
183     );
184 }
185
186 sub LoadBib {
187     my @biblios;
188     for my $bib (@biblionumber) {
189         my $data = GetBiblioData($bib);
190     push(@biblios,
191         { biblionumber => $bib,
192           title        => $data->{'title'},
193           author       => $data->{'author'},
194     } );
195     }
196     $template->param(
197         multiple => (scalar(@biblios) > 1),
198     total    => scalar @biblios,
199     biblios  => \@biblios,
200     );
201 }
202
203 sub ShowTemplate {
204     $template->param (
205     newshelf => $newshelf||0,
206     authorized  => $authorized,
207     errcode             => $errcode,
208     );
209     output_html_with_http_headers $query, $cookie, $template->output;
210 }