Bug 22227: Make GET /cities staff only
[koha.git] / virtualshelves / addbybiblionumber.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2016 Koha Development Team
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
17 #
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>.
20
21
22 =head1 NAME
23
24 addbybiblionumber.pl
25
26 =head1 DESCRIPTION
27
28     This script allow to add a virtual in a virtual shelf from a biblionumber.
29
30 =head1 CGI PARAMETERS
31
32 =over 4
33
34 =item biblionumber
35
36     The biblionumber
37
38 =item shelfnumber
39
40     the shelfnumber where to add the virtual.
41
42 =item newvirtualshelf
43
44     if this parameter exists, then it must be equals to the name of the shelf
45     to add.
46
47 =item category
48
49     if this script has to add a shelf, it add one with this category.
50
51 =item newshelf
52
53     if this parameter exists, then we create a new shelf
54
55 =back
56
57 =cut
58
59 use Modern::Perl;
60
61 use CGI qw ( -utf8 );
62 use C4::Biblio;
63 use C4::Output;
64 use C4::Auth;
65
66 use Koha::Biblios;
67 use Koha::Virtualshelves;
68
69 my $query           = new CGI;
70 my $shelfnumber     = $query->param('shelfnumber');
71 my $newvirtualshelf = $query->param('newvirtualshelf');
72 my $newshelf        = $query->param('newshelf');
73 my $category        = $query->param('category');
74 my $sortfield       = $query->param('sortfield');
75 my $confirmed       = $query->param('confirmed') || 0;
76 my ( $errcode, $authorized ) = ( 0, 1 );
77 my @biblionumbers = $query->multi_param('biblionumber');
78
79 if ( @biblionumbers == 0 && $query->param('biblionumbers') ) {
80     my $str = $query->param('biblionumbers');
81     @biblionumbers = split '/', $str;
82 } elsif ( @biblionumbers == 1 && $biblionumbers[0] =~ /\// ) {
83     @biblionumbers = split '/', $biblionumbers[0];
84 }
85
86 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
87     {   template_name   => "virtualshelves/addbybiblionumber.tt",
88         query           => $query,
89         type            => "intranet",
90         authnotrequired => 0,
91         flagsrequired   => { catalogue => 1 },
92     }
93 );
94
95 if ($newvirtualshelf) {
96     my $shelf = eval {
97         Koha::Virtualshelf->new(
98             {
99                 shelfname => $newvirtualshelf,
100                 category  => $category,
101                 sortfield => $sortfield,
102                 owner     => $loggedinuser,
103             }
104         )->store;
105     };
106     if ( $@ or not $shelf ) {
107         $errcode    = 1;
108         $authorized = 0;
109     } else {
110
111         for my $biblionumber (@biblionumbers) {
112             $shelf->add_biblio( $biblionumber, $loggedinuser );
113         }
114
115         #Reload the page where you came from
116         print $query->header;
117         print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"window.opener.location.reload(true);self.close();\"></body></html>";
118         exit;
119     }
120
121 } elsif ( $shelfnumber && $confirmed ) {
122     my $shelf = Koha::Virtualshelves->find($shelfnumber);
123     if ( $shelf->can_biblios_be_added($loggedinuser) ) {
124         for my $biblionumber (@biblionumbers) {
125             $shelf->add_biblio( $biblionumber, $loggedinuser );
126         }
127
128         #Close this page and return
129         print $query->header;
130         print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"self.close();\"></body></html>";
131         exit;
132     } else {
133         $errcode    = 2;    #no perm
134         $authorized = 0;
135     }
136
137 } elsif ($shelfnumber) {    #still needs confirmation
138     my $shelf = Koha::Virtualshelves->find($shelfnumber);
139     if ( $shelf->can_biblios_be_added($loggedinuser) ) {
140
141         #confirm adding to specific shelf
142         $template->param(
143             singleshelf => 1,
144             shelfnumber => $shelf->shelfnumber,
145             shelfname   => $shelf->shelfname,
146         );
147     } else {
148         $authorized = 0;
149         $errcode    = 2;    #no perm
150     }
151
152 } else {
153     my $private_shelves = Koha::Virtualshelves->search(
154         {   category => 1,
155             owner    => $loggedinuser,
156             allow_change_from_owner => 1,
157         },
158         { order_by => 'shelfname' }
159     );
160     my $shelves_shared_with_me = Koha::Virtualshelves->search(
161         {   category                            => 1,
162             'virtualshelfshares.borrowernumber' => $loggedinuser,
163             allow_change_from_others            => 1,
164         },
165         { join => 'virtualshelfshares', }
166     );
167     my $public_shelves = Koha::Virtualshelves->search(
168         {   category => 2,
169             -or      => [
170                 -and => {
171                     allow_change_from_owner => 1,
172                     owner     => $loggedinuser,
173                 },
174                 allow_change_from_others => 1,
175             ],
176         },
177         { order_by => 'shelfname' }
178     );
179     $template->param(
180         private_shelves                => $private_shelves,
181         private_shelves_shared_with_me => $shelves_shared_with_me,
182         public_shelves                 => $public_shelves,
183     );
184
185 }
186
187 my @biblios;
188 for my $biblionumber (@biblionumbers) {
189     my $biblio = Koha::Biblios->find( $biblionumber );
190     push(
191         @biblios,
192         {   biblionumber => $biblionumber,
193             title        => $biblio->title,
194             author       => $biblio->author,
195         }
196     );
197 }
198 $template->param(
199     multiple => ( scalar(@biblios) > 1 ),
200     total    => scalar @biblios,
201     biblios  => \@biblios,
202 );
203
204 $template->param(
205     newshelf => $newshelf || 0,
206     authorized => $authorized,
207     errcode    => $errcode,
208 );
209 output_html_with_http_headers $query, $cookie, $template->output;