Merge remote-tracking branch 'origin/new/bug_8130'
[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 our $query           = new CGI;
72 our @biblionumber    = HandleBiblioPars();
73 our $shelfnumber     = $query->param('shelfnumber');
74 our $newvirtualshelf = $query->param('newvirtualshelf');
75 our $newshelf        = $query->param('newshelf');
76 our $category        = $query->param('category');
77 our $sortfield      = $query->param('sortfield');
78 my $confirmed       = $query->param('confirmed') || 0;
79 our $authorized      = 1;
80 our $errcode        = 0;
81
82 our ( $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)= GetShelf($shelfnumber);
165         $template->param(
166         singleshelf               => 1,
167         shelfnumber               => $singleshelf,
168         shelfname                 => $singleshelfname,
169         );
170     }
171     else {
172     $errcode=2; #no perm
173     }
174 }
175
176 sub HandleSelect {
177     my $privateshelves = GetAllShelves(1,$loggedinuser,1);
178     my $publicshelves = GetAllShelves(2,$loggedinuser,1);
179     $template->param(
180     privatevirtualshelves => $privateshelves,
181     publicvirtualshelves  => $publicshelves,
182     );
183 }
184
185 sub LoadBib {
186     my @biblios;
187     for my $bib (@biblionumber) {
188         my $data = GetBiblioData($bib);
189     push(@biblios,
190         { biblionumber => $bib,
191           title        => $data->{'title'},
192           author       => $data->{'author'},
193     } );
194     }
195     $template->param(
196         multiple => (scalar(@biblios) > 1),
197     total    => scalar @biblios,
198     biblios  => \@biblios,
199     );
200 }
201
202 sub ShowTemplate {
203     $template->param (
204     newshelf => $newshelf||0,
205     authorized  => $authorized,
206     errcode             => $errcode,
207     );
208     output_html_with_http_headers $query, $cookie, $template->output;
209 }