Merge remote branch 'kc/new/bug_4276' 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 GetAllShelves 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
100         my $privateshelves = GetAllShelves(1,$loggedinuser);
101         my @privateshelves = @{$privateshelves};
102         warn scalar($privateshelves);
103         if(@privateshelves){
104                         $template->param (
105                                 privatevirtualshelves          => @privateshelves,
106                                 existingshelves => 1
107                         );
108                 }
109         my $publicshelves = GetAllShelves(2,$loggedinuser);
110         my @publicshelves = @{$publicshelves};
111         if(@publicshelves){
112                         $template->param (
113                                 publicvirtualshelves          => @publicshelves,
114                                 existingshelves => 1
115                         );
116         }
117
118 }
119         my @biblios;
120         for my $bib (@biblionumber) {
121                 my $data = GetBiblioData( $bib );
122                 push(@biblios, 
123                         { biblionumber => $bib,
124                           title        => $data->{'title'},
125                           author       => $data->{'author'},
126                         } );
127         }
128         $template->param (
129                 newshelf => $newshelf,
130                 multiple => (scalar(@biblios) > 1),
131                 total    => scalar @biblios,
132                 biblios  => \@biblios,
133                 authorized      => $authorized,
134         );
135
136         output_html_with_http_headers $query, $cookie, $template->output;
137 }