Bug #2709 Cannot add from Search to New list
[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 with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA  02111-1307 USA
24
25 use strict;
26 use C4::Biblio;
27 use CGI;
28 use C4::VirtualShelves qw/:DEFAULT GetRecentShelves RefreshShelvesSummary/;
29 use C4::Auth;
30 use C4::Output;
31 use C4::Auth qw/get_session/;
32 use C4::Debug;
33
34 #splits incoming biblionumber(s) to array and adds each to shelf.
35 sub AddBibliosToShelf {
36     my ($shelfnumber,@biblionumber)=@_;
37
38     # multiple bibs might come in as '/' delimited string (from where, i don't see), or as array.
39     if (scalar(@biblionumber) == 1) {
40         @biblionumber = (split /\//,$biblionumber[0]);
41     }
42     for my $bib (@biblionumber){
43         AddToShelfFromBiblio($bib, $shelfnumber);
44     }
45 }
46
47 my $query               = new CGI;
48 my @biblionumber        = $query->param('biblionumber');
49 my $selectedshelf       = $query->param('selectedshelf');
50 my $newshelf            = $query->param('newshelf');
51 my $shelfnumber         = $query->param('shelfnumber');
52 my $newvirtualshelf     = $query->param('newvirtualshelf');
53 my $category            = $query->param('category');
54
55 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
56     {
57         template_name   => "opac-addbybiblionumber.tmpl",
58         query           => $query,
59         type            => "opac",
60         authnotrequired => 1,
61     }
62 );
63
64 if ($newvirtualshelf) {
65         $shelfnumber = AddShelf(  $newvirtualshelf, $loggedinuser, $category );
66         AddBibliosToShelf($shelfnumber, @biblionumber);
67         RefreshShelvesSummary($query->cookie("CGISESSID"),$loggedinuser,($loggedinuser == -1 ? 20 : 10));
68         print $query->header;
69         print "<html><body onload=\"window.opener.location.reload(true);self.close();\"></body></html>";
70         exit;
71 }
72
73 # verify user is authorized to perform the action on the shelf...
74 my $authorized = 1;
75 if ($selectedshelf) {
76         $authorized = 0 unless ShelfPossibleAction( $loggedinuser, $selectedshelf );
77 }
78
79 if ($shelfnumber && ($shelfnumber != -1)) {
80         AddBibliosToShelf($shelfnumber,@biblionumber);
81         RefreshShelvesSummary($query->cookie("CGISESSID"),$loggedinuser,($loggedinuser == -1 ? 20 : 10));
82         print $query->header;
83         print "<html><body onload=\"window.opener.location.reload(true);self.close();\"></body></html>";
84         exit;
85 }
86 else {
87         if($selectedshelf){
88         # adding to specific shelf
89         my ( $singleshelf, $singleshelfname, $singlecategory ) = GetShelf( $query->param('selectedshelf') );
90                                 $template->param(
91                                 singleshelf             => 1,
92                                 shelfnumber         => $singleshelf,
93                                 shelfname           => $singleshelfname,
94                                 "category$singlecategory" => 1
95                         );
96         } else {
97         # offer choice of shelves
98         # first private shelves...
99         my $limit = 10;
100         my ($shelflist) = GetRecentShelves(1, $limit, $loggedinuser);
101     my @shelvesloop;
102     my %shelvesloop;
103     for my $shelf ( @{ $shelflist->[0] } ) {
104         push( @shelvesloop, $shelf->{shelfnumber} );
105                 $shelvesloop{$shelf->{shelfnumber}} = $shelf->{shelfname};
106         }
107         # then open shelves...
108         my ($shelflist) = GetRecentShelves(3, $limit, undef);
109     for my $shelf ( @{ $shelflist->[0] } ) {
110         push( @shelvesloop, $shelf->{shelfnumber} );
111                 $shelvesloop{$shelf->{shelfnumber}} = $shelf->{shelfname};
112         }
113     my $CGIvirtualshelves;
114     if ( @shelvesloop > 0 ) {
115         $CGIvirtualshelves = CGI::scrolling_list (
116             -name     => 'shelfnumber',
117             -id     => 'shelfnumber',
118             -values   => \@shelvesloop,
119             -labels   => \%shelvesloop,
120             -size     => 1,
121             -tabindex => '',
122             -multiple => 0
123         );
124
125         $template->param (
126                 CGIvirtualshelves       => $CGIvirtualshelves,
127         );
128     }
129         }
130
131         my @biblios;
132         for my $bib (@biblionumber) {
133                 my $data = GetBiblioData( $bib );
134                 push(@biblios, 
135                         { biblionumber => $bib,
136                           title        => $data->{'title'},
137                           author       => $data->{'author'},
138                         } );
139         }
140         $template->param (
141                 newshelf => $newshelf,
142                 multiple => (scalar(@biblios) > 1),
143                 total    => scalar @biblios,
144                 biblios  => \@biblios,
145                 authorized      => $authorized,
146         );
147
148         output_html_with_http_headers $query, $cookie, $template->output;
149 }