Bug 29857: (QA follow-up) Fix unit test Object.t
[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 public
48
49     if this script has to add a shelf, it adds one with this 'public' setting.
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::Output qw( output_html_with_http_headers );
63 use C4::Auth qw( get_template_and_user );
64
65 use Koha::Biblios;
66 use Koha::Virtualshelves;
67
68 my $query           = CGI->new;
69 my $shelfnumber     = $query->param('shelfnumber');
70 my $newvirtualshelf = $query->param('newvirtualshelf');
71 my $newshelf        = $query->param('newshelf');
72 my $public          = $query->param('public');
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');
77
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];
83 }
84
85 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
86     {   template_name   => "virtualshelves/addbybiblionumber.tt",
87         query           => $query,
88         type            => "intranet",
89         flagsrequired   => { catalogue => 1 },
90     }
91 );
92
93 if ($newvirtualshelf) {
94     my $shelf = eval {
95         Koha::Virtualshelf->new(
96             {
97                 shelfname => $newvirtualshelf,
98                 public    => $public,
99                 sortfield => $sortfield,
100                 owner     => $loggedinuser,
101             }
102         )->store;
103     };
104     if ( $@ or not $shelf ) {
105         $errcode    = 1;
106         $authorized = 0;
107     } else {
108
109         for my $biblionumber (@biblionumbers) {
110             $shelf->add_biblio( $biblionumber, $loggedinuser );
111         }
112
113         #Reload the page where you came from
114         print $query->header;
115         print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"window.opener.location.reload(true);self.close();\"></body></html>";
116         exit;
117     }
118
119 } elsif ( $shelfnumber && $confirmed ) {
120     my $shelf = Koha::Virtualshelves->find($shelfnumber);
121     if ( $shelf->can_biblios_be_added($loggedinuser) ) {
122         for my $biblionumber (@biblionumbers) {
123             $shelf->add_biblio( $biblionumber, $loggedinuser );
124         }
125
126         #Close this page and return
127         print $query->header;
128         print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"self.close();\"></body></html>";
129         exit;
130     } else {
131         $errcode    = 2;    #no perm
132         $authorized = 0;
133     }
134
135 } elsif ($shelfnumber) {    #still needs confirmation
136     my $shelf = Koha::Virtualshelves->find($shelfnumber);
137     if ( $shelf->can_biblios_be_added($loggedinuser) ) {
138
139         #confirm adding to specific shelf
140         $template->param(
141             singleshelf => 1,
142             shelfnumber => $shelf->shelfnumber,
143             shelfname   => $shelf->shelfname,
144         );
145     } else {
146         $authorized = 0;
147         $errcode    = 2;    #no perm
148     }
149
150 } else {
151     my $private_shelves = Koha::Virtualshelves->search(
152         {   public                  => 0,
153             owner                   => $loggedinuser,
154             allow_change_from_owner => 1,
155         },
156         { order_by => 'shelfname' }
157     );
158     my $shelves_shared_with_me = Koha::Virtualshelves->search(
159         {   public                              => 0,
160             'virtualshelfshares.borrowernumber' => $loggedinuser,
161             allow_change_from_others            => 1,
162         },
163         { join => 'virtualshelfshares', }
164     );
165     my $public_shelves = Koha::Virtualshelves->search(
166         {   public   => 1,
167             -or      => [
168                 -and => {
169                     allow_change_from_owner => 1,
170                     owner     => $loggedinuser,
171                 },
172                 allow_change_from_others => 1,
173             ],
174         },
175         { order_by => 'shelfname' }
176     );
177     $template->param(
178         private_shelves                => $private_shelves,
179         private_shelves_shared_with_me => $shelves_shared_with_me,
180         public_shelves                 => $public_shelves,
181     );
182
183 }
184
185 my @biblios;
186 for my $biblionumber (@biblionumbers) {
187     my $biblio = Koha::Biblios->find( $biblionumber );
188     push(
189         @biblios,
190         {   biblionumber => $biblionumber,
191             title        => $biblio->title,
192             author       => $biblio->author,
193         }
194     );
195 }
196 $template->param(
197     multiple => ( scalar(@biblios) > 1 ),
198     total    => scalar @biblios,
199     biblios  => \@biblios,
200 );
201
202 $template->param(
203     newshelf => $newshelf || 0,
204     authorized => $authorized,
205     errcode    => $errcode,
206 );
207 output_html_with_http_headers $query, $cookie, $template->output;