7310 Indentation followup replacing leading tabs with spaces
[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 under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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;
65 use C4::Biblio;
66 use C4::Output;
67 use C4::VirtualShelves qw/:DEFAULT GetAllShelves/;
68 use C4::Auth;
69
70
71 my $query           = new CGI;
72 my @biblionumber    = HandleBiblioPars();
73 my $shelfnumber     = $query->param('shelfnumber');
74 my $newvirtualshelf = $query->param('newvirtualshelf');
75 my $newshelf        = $query->param('newshelf');
76 my $category        = $query->param('category');
77 my $sortfield       = $query->param('sortfield');
78 my $confirmed       = $query->param('confirmed') || 0;
79 my $authorized      = 1;
80 my $errcode         = 0;
81
82 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
83     {
84         template_name   => "virtualshelves/addbybiblionumber.tmpl",
85         query           => $query,
86         type            => "intranet",
87         authnotrequired => 0,
88         flagsrequired   => { catalogue => 1 },
89     }
90 );
91
92 if( $newvirtualshelf) {
93     HandleNewVirtualShelf();
94     exit if $authorized;
95     ShowTemplate(); #error message
96 }
97 elsif($shelfnumber && $confirmed) {
98     HandleShelfNumber();
99     exit if $authorized;
100     ShowTemplate(); #error message
101 }
102 elsif($shelfnumber) { #still needs confirmation
103     HandleSelectedShelf();
104     LoadBib() if $authorized;
105     ShowTemplate();
106 }
107 else {
108     HandleSelect();
109     LoadBib();
110     ShowTemplate();
111 }
112 #end
113
114 sub HandleBiblioPars {
115     my @bib= $query->param('biblionumber');
116     if(@bib==0 && $query->param('biblionumbers')) {
117         my $str= $query->param('biblionumbers');
118         @bib= split '/', $str;
119     }
120     elsif(@bib==1 && $bib[0]=~/\//) {
121         @bib= split '/', $bib[0];
122     }
123     return @bib;
124 }
125
126 sub AddBibliosToShelf {
127     my ($shelfnumber, @biblionumber)=@_;
128     for my $bib (@biblionumber){
129         AddToShelf($bib, $shelfnumber, $loggedinuser);
130     }
131 }
132
133 sub HandleNewVirtualShelf {
134     $shelfnumber = AddShelf( {
135         shelfname => $newvirtualshelf,
136         sortfield => $sortfield,
137         category => $category }, $loggedinuser);
138     if($shelfnumber == -1) {
139         $authorized=0;
140         $errcode=1; #add failed
141         return;
142     }
143     AddBibliosToShelf($shelfnumber, @biblionumber);
144     #Reload the page where you came from
145     print $query->header;
146     print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"window.opener.location.reload(true);self.close();\"></body></html>";
147 }
148
149 sub HandleShelfNumber {
150     if($authorized= ShelfPossibleAction($loggedinuser, $shelfnumber, 'add')) {
151     AddBibliosToShelf($shelfnumber, @biblionumber);
152     #Close this page and return
153     print $query->header;
154     print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"self.close();\"></body></html>";
155     }
156     else {
157     $errcode=2; #no perm
158     }
159 }
160
161 sub HandleSelectedShelf {
162     if($authorized= ShelfPossibleAction( $loggedinuser, $shelfnumber, 'add')){
163         #confirm adding to specific shelf
164         my ($singleshelf, $singleshelfname, $singlecategory)= GetShelf($shelfnumber);
165         $template->param(
166         singleshelf               => 1,
167         shelfnumber               => $singleshelf,
168         shelfname                 => $singleshelfname,
169         "category$singlecategory" => 1
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 }