Merge remote-tracking branch 'origin/new/bug_7310'
[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 CGI;
29 use C4::Biblio;
30 use C4::VirtualShelves qw/:DEFAULT GetAllShelves/;
31 use C4::Output;
32 use C4::Auth;
33
34 our $query              = new CGI;
35 our @biblionumber       = $query->param('biblionumber');
36 our $selectedshelf      = $query->param('selectedshelf');
37 our $newshelf           = $query->param('newshelf');
38 our $shelfnumber        = $query->param('shelfnumber');
39 our $newvirtualshelf    = $query->param('newvirtualshelf');
40 our $category           = $query->param('category');
41 our $authorized          = 1;
42 our $errcode            = 0;
43 our @biblios;
44
45 our ( $template, $loggedinuser, $cookie ) = get_template_and_user(
46     {
47         template_name   => "opac-addbybiblionumber.tmpl",
48         query           => $query,
49         type            => "opac",
50         authnotrequired => 0,
51     }
52 );
53
54 if( $newvirtualshelf) {
55     HandleNewVirtualShelf();
56     exit if $authorized;
57     ShowTemplate(); #error message
58 }
59 elsif($shelfnumber) {
60     HandleShelfNumber();
61     exit if $authorized;
62     ShowTemplate(); #error message
63 }
64 elsif($selectedshelf) {
65     HandleSelectedShelf();
66     LoadBib() if $authorized;
67     ShowTemplate();
68 }
69 else {
70     HandleSelect();
71     LoadBib() if $authorized;
72     ShowTemplate();
73 }
74 #end
75
76 sub AddBibliosToShelf {
77     #splits incoming biblionumber(s) to array and adds each to shelf.
78     my ($shelfnumber,@biblionumber)=@_;
79
80     #multiple bibs might come in as '/' delimited string (from where, i don't see), or as array.
81     if (scalar(@biblionumber) == 1) {
82         @biblionumber = (split /\//,$biblionumber[0]);
83     }
84     for my $bib (@biblionumber) {
85         AddToShelf($bib, $shelfnumber, $loggedinuser);
86     }
87 }
88
89 sub HandleNewVirtualShelf {
90     if($authorized= ShelfPossibleAction($loggedinuser, undef, $category==1? 'new_private': 'new_public')) {
91     $shelfnumber = AddShelf( {
92             shelfname => $newvirtualshelf,
93             category => $category }, $loggedinuser);
94     if($shelfnumber == -1) {
95         $authorized=0;
96         $errcode=1;
97         return;
98     }
99     AddBibliosToShelf($shelfnumber, @biblionumber);
100     #Reload the page where you came from
101     print $query->header;
102     print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"window.opener.location.reload(true);self.close();\"></body></html>";
103     }
104 }
105
106 sub HandleShelfNumber {
107     if($authorized= ShelfPossibleAction($loggedinuser, $shelfnumber, 'add')) {
108     AddBibliosToShelf($shelfnumber,@biblionumber);
109     #Close this page and return
110     print $query->header;
111     print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"self.close();\"></body></html>";
112     }
113 }
114
115 sub HandleSelectedShelf {
116     if($authorized= ShelfPossibleAction( $loggedinuser, $selectedshelf, 'add')){
117         #adding to specific shelf
118         my ($singleshelf, $singleshelfname, $singlecategory)= GetShelf($query->param('selectedshelf'));
119         $template->param(
120         singleshelf               => 1,
121         shelfnumber               => $singleshelf,
122         shelfname                 => $singleshelfname,
123         "category$singlecategory" => 1
124         );
125     }
126 }
127
128 sub HandleSelect {
129     return unless $authorized= $loggedinuser>0;
130     my $privateshelves = GetAllShelves(1,$loggedinuser,1);
131     if(@{$privateshelves}){
132         $template->param (
133         privatevirtualshelves          => $privateshelves,
134         existingshelves => 1
135     );
136     }
137     my $publicshelves = GetAllShelves(2,$loggedinuser,1);
138     if(@{$publicshelves}){
139         $template->param (
140         publicvirtualshelves          => $publicshelves,
141         existingshelves => 1
142     );
143     }
144 }
145
146 sub LoadBib {
147     for my $bib (@biblionumber) {
148         my $data = GetBiblioData( $bib );
149     push(@biblios,
150         { biblionumber => $bib,
151           title        => $data->{'title'},
152           author       => $data->{'author'},
153     } );
154     }
155     $template->param(
156         multiple => (scalar(@biblios) > 1),
157     total    => scalar @biblios,
158     biblios  => \@biblios,
159     );
160 }
161
162 sub ShowTemplate {
163     $template->param (
164     newshelf => $newshelf||0,
165     authorized  => $authorized,
166     errcode             => $errcode,
167     OpacAllowPublicListCreation => C4::Context->preference('OpacAllowPublicListCreation'),
168     );
169     output_html_with_http_headers $query, $cookie, $template->output;
170 }