Merge remote branch 'kc/new/enh/bug_3550' into kcmaster
[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 under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
15 # version.
16 #
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License along
22 # with Koha; if not, write to the Free Software Foundation, Inc.,
23 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
25 use strict;
26 use warnings;
27
28 use C4::Biblio;
29 use CGI;
30 use C4::VirtualShelves qw/:DEFAULT GetRecentShelves RefreshShelvesSummary/;
31 use C4::Auth;
32 use C4::Output;
33 use C4::Auth qw/get_session/;
34 use C4::Debug;
35
36 #splits incoming biblionumber(s) to array and adds each to shelf.
37 sub AddBibliosToShelf {
38     my ($shelfnumber,@biblionumber)=@_;
39
40     # multiple bibs might come in as '/' delimited string (from where, i don't see), or as array.
41     if (scalar(@biblionumber) == 1) {
42         @biblionumber = (split /\//,$biblionumber[0]);
43     }
44     for my $bib (@biblionumber){
45         AddToShelf($bib, $shelfnumber);
46     }
47 }
48
49 my $query               = new CGI;
50 my @biblionumber        = $query->param('biblionumber');
51 my $selectedshelf       = $query->param('selectedshelf');
52 my $newshelf            = $query->param('newshelf');
53 my $shelfnumber         = $query->param('shelfnumber');
54 my $newvirtualshelf     = $query->param('newvirtualshelf');
55 my $category            = $query->param('category');
56
57 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
58     {
59         template_name   => "opac-addbybiblionumber.tmpl",
60         query           => $query,
61         type            => "opac",
62         authnotrequired =>( C4::Context->preference("OpacPublic") ? 1 : 0 ),
63     }
64 );
65
66 if ($newvirtualshelf) {
67         $shelfnumber = AddShelf(  $newvirtualshelf, $loggedinuser, $category );
68         AddBibliosToShelf($shelfnumber, @biblionumber);
69         RefreshShelvesSummary($query->cookie("CGISESSID"),$loggedinuser,($loggedinuser == -1 ? 20 : 10));
70         print $query->header;
71         print "<html><meta http-equiv=\"refresh\" content=\"0;url=opac-shelves.pl?display=privateshelves\" /><body onload=\"window.opener.location.reload(true);self.close();\"></body></html>";
72         exit;
73 }
74
75 # verify user is authorized to perform the action on the shelf...
76 my $authorized = 1;
77 if ($selectedshelf) {
78         $authorized = 0 unless ShelfPossibleAction( $loggedinuser, $selectedshelf );
79 }
80
81 if ($shelfnumber && ($shelfnumber != -1)) {
82         AddBibliosToShelf($shelfnumber,@biblionumber);
83         RefreshShelvesSummary($query->cookie("CGISESSID"),$loggedinuser,($loggedinuser == -1 ? 20 : 10));
84         print $query->header;
85         print "<html><meta http-equiv=\"refresh\" content=\"0;url=opac-shelves.pl?display=privateshelves\" /><body onload=\"self.close();\"></body></html>";
86         exit;
87 }
88 else {
89         if($selectedshelf){
90         # adding to specific shelf
91         my ( $singleshelf, $singleshelfname, $singlecategory ) = GetShelf( $query->param('selectedshelf') );
92                                 $template->param(
93                                 singleshelf             => 1,
94                                 shelfnumber         => $singleshelf,
95                                 shelfname           => $singleshelfname,
96                                 "category$singlecategory" => 1
97                         );
98         } else {
99         # offer choice of shelves
100         my $limit = 10;
101     my @shelvesloop;
102     my %shelvesloop;
103     #grab each type of shelf, open (type 3) should not be limited by user.
104     foreach my $shelftype (1,2,3) {
105         my ($shelflist) = GetRecentShelves($shelftype, $limit, $shelftype == 3 ? undef : $loggedinuser);
106         for my $shelf (@{ $shelflist->[0] }) {
107             push(@shelvesloop, $shelf->{shelfnumber});
108             $shelvesloop{$shelf->{shelfnumber}} = $shelf->{shelfname};
109         }
110     }
111     my $CGIvirtualshelves;
112     if ( @shelvesloop > 0 ) {
113         $CGIvirtualshelves = CGI::scrolling_list (
114             -name     => 'shelfnumber',
115             -id     => 'shelfnumber',
116             -values   => \@shelvesloop,
117             -labels   => \%shelvesloop,
118             -size     => 1,
119             -tabindex => '',
120             -multiple => 0
121         );
122
123         $template->param (
124                 CGIvirtualshelves       => $CGIvirtualshelves,
125         );
126     }
127         }
128
129         my @biblios;
130         for my $bib (@biblionumber) {
131                 my $data = GetBiblioData( $bib );
132                 push(@biblios, 
133                         { biblionumber => $bib,
134                           title        => $data->{'title'},
135                           author       => $data->{'author'},
136                         } );
137         }
138         $template->param (
139                 newshelf => $newshelf,
140                 multiple => (scalar(@biblios) > 1),
141                 total    => scalar @biblios,
142                 biblios  => \@biblios,
143                 authorized      => $authorized,
144         );
145
146         output_html_with_http_headers $query, $cookie, $template->output;
147 }