Bug 5670: [QA Followup] Create HouseboundRole objects.
[koha.git] / virtualshelves / addbybiblionumber.pl
blob6e058e9e490e9bf7671c86efa3093d08192024bb
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2016 Koha Development Team
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 =head1 NAME
24 addbybiblionumber.pl
26 =head1 DESCRIPTION
28 This script allow to add a virtual in a virtual shelf from a biblionumber.
30 =head1 CGI PARAMETERS
32 =over 4
34 =item biblionumber
36 The biblionumber
38 =item shelfnumber
40 the shelfnumber where to add the virtual.
42 =item newvirtualshelf
44 if this parameter exists, then it must be equals to the name of the shelf
45 to add.
47 =item category
49 if this script has to add a shelf, it add one with this category.
51 =item newshelf
53 if this parameter exists, then we create a new shelf
55 =back
57 =cut
59 use Modern::Perl;
61 use CGI qw ( -utf8 );
62 use C4::Biblio;
63 use C4::Output;
64 use C4::Auth;
66 use Koha::Virtualshelves;
68 my $query = new CGI;
69 my $shelfnumber = $query->param('shelfnumber');
70 my $newvirtualshelf = $query->param('newvirtualshelf');
71 my $newshelf = $query->param('newshelf');
72 my $category = $query->param('category');
73 my $sortfield = $query->param('sortfield');
74 my $confirmed = $query->param('confirmed') || 0;
75 my ( $errcode, $authorized ) = ( 0, 1 );
76 my @biblionumbers = $query->multi_param('biblionumber');
78 if ( @biblionumbers == 0 && $query->param('biblionumbers') ) {
79 my $str = $query->param('biblionumbers');
80 @biblionumbers = split '/', $str;
81 } elsif ( @biblionumbers == 1 && $biblionumbers[0] =~ /\// ) {
82 @biblionumbers = split '/', $biblionumbers[0];
85 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
86 { template_name => "virtualshelves/addbybiblionumber.tt",
87 query => $query,
88 type => "intranet",
89 authnotrequired => 0,
90 flagsrequired => { catalogue => 1 },
94 if ($newvirtualshelf) {
95 my $shelf = eval {
96 Koha::Virtualshelf->new(
98 shelfname => $newvirtualshelf,
99 category => $category,
100 sortfield => $sortfield,
101 owner => $loggedinuser,
103 )->store;
105 if ( $@ or not $shelf ) {
106 $errcode = 1;
107 $authorized = 0;
108 } else {
110 for my $biblionumber (@biblionumbers) {
111 $shelf->add_biblio( $biblionumber, $loggedinuser );
114 #Reload the page where you came from
115 print $query->header;
116 print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"window.opener.location.reload(true);self.close();\"></body></html>";
117 exit;
120 } elsif ( $shelfnumber && $confirmed ) {
121 my $shelf = Koha::Virtualshelves->find($shelfnumber);
122 if ( $shelf->can_biblios_be_added($loggedinuser) ) {
123 for my $biblionumber (@biblionumbers) {
124 $shelf->add_biblio( $biblionumber, $loggedinuser );
127 #Close this page and return
128 print $query->header;
129 print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"self.close();\"></body></html>";
130 exit;
131 } else {
132 $errcode = 2; #no perm
133 $authorized = 0;
136 } elsif ($shelfnumber) { #still needs confirmation
137 my $shelf = Koha::Virtualshelves->find($shelfnumber);
138 if ( $shelf->can_biblios_be_added($loggedinuser) ) {
140 #confirm adding to specific shelf
141 $template->param(
142 singleshelf => 1,
143 shelfnumber => $shelf->shelfnumber,
144 shelfname => $shelf->shelfname,
146 } else {
147 $authorized = 0;
148 $errcode = 2; #no perm
151 } else {
152 my $private_shelves = Koha::Virtualshelves->search(
153 { category => 1,
154 owner => $loggedinuser,
156 { order_by => 'shelfname' }
158 my $shelves_shared_with_me = Koha::Virtualshelves->search(
159 { category => 1,
160 'virtualshelfshares.borrowernumber' => $loggedinuser,
161 -or => {
162 allow_add => 1,
163 owner => $loggedinuser,
166 { join => 'virtualshelfshares', }
168 my $public_shelves = Koha::Virtualshelves->search(
169 { category => 2,
170 -or => {
171 allow_add => 1,
172 owner => $loggedinuser,
175 { order_by => 'shelfname' }
177 $template->param(
178 private_shelves => $private_shelves,
179 private_shelves_shared_with_me => $shelves_shared_with_me,
180 public_shelves => $public_shelves,
185 my @biblios;
186 for my $biblionumber (@biblionumbers) {
187 my $data = GetBiblioData($biblionumber);
188 push(
189 @biblios,
190 { biblionumber => $biblionumber,
191 title => $data->{'title'},
192 author => $data->{'author'},
196 $template->param(
197 multiple => ( scalar(@biblios) > 1 ),
198 total => scalar @biblios,
199 biblios => \@biblios,
202 $template->param(
203 newshelf => $newshelf || 0,
204 authorized => $authorized,
205 errcode => $errcode,
207 output_html_with_http_headers $query, $cookie, $template->output;