Bug 34597: Implementation
[koha.git] / opac / opac-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 use Modern::Perl;
22
23 use CGI qw ( -utf8 );
24 use C4::Output qw( output_html_with_http_headers );
25 use C4::Auth qw( get_template_and_user );
26
27 use Koha::Biblios;
28 use Koha::Virtualshelves;
29
30 my $query           = CGI->new;
31 my $op              = $query->param('op') // q{};
32 my @biblionumbers   = $query->multi_param('biblionumber');
33 my $selectedshelf   = $query->param('selectedshelf');
34 my $newshelf        = $query->param('newshelf');
35 my $shelfnumber     = $query->param('shelfnumber');
36 my $newvirtualshelf = $query->param('newvirtualshelf');
37 my $public          = $query->param('public');
38 my ( $errcode, $authorized ) = ( 0, 1 );
39 my @biblios;
40
41 # if virtualshelves is disabled, leave immediately
42 if ( !C4::Context->preference('virtualshelves') ) {
43     print $query->redirect("/cgi-bin/koha/errors/404.pl");
44     exit;
45 }
46
47 if ( scalar(@biblionumbers) == 1 ) {
48     @biblionumbers = ( split /\//, $biblionumbers[0] );
49 }
50
51 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
52     {   template_name   => "opac-addbybiblionumber.tt",
53         query           => $query,
54         type            => "opac",
55     }
56 );
57
58 if( $op && $op !~ /^cud-/ ) {
59     $authorized = 0;
60 } elsif ($newvirtualshelf) {
61     if ($loggedinuser > 0
62         and (  !$public
63             or $public and $loggedinuser > 0 && C4::Context->preference('OpacAllowPublicListCreation') )
64       ) {
65         my $shelf = eval { Koha::Virtualshelf->new( { shelfname => $newvirtualshelf, public => $public, owner => $loggedinuser, } )->store; };
66         if ( $@ or not $shelf ) {
67             $errcode    = 1;
68             $authorized = 0;
69         } else {
70             for my $biblionumber (@biblionumbers) {
71                 $shelf->add_biblio( $biblionumber, $loggedinuser );
72             }
73
74             #Reload the page where you came from
75             print $query->header;
76             print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"window.opener.location.reload(true);self.close();\"></body></html>";
77             exit;
78         }
79     }
80 } elsif ($shelfnumber) {
81     my $shelfnumber = $query->param('shelfnumber');
82     my $shelf       = Koha::Virtualshelves->find($shelfnumber);
83     if ( $shelf->can_biblios_be_added($loggedinuser) ) {
84         for my $biblionumber (@biblionumbers) {
85             $shelf->add_biblio( $biblionumber, $loggedinuser );
86         }
87
88         #Close this page and return
89         print $query->header;
90         print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"self.close();\"></body></html>";
91         exit;
92     } else {
93         $authorized = 0;
94     }
95 } elsif ($selectedshelf) {
96     my $shelfnumber = $query->param('selectedshelf');
97     my $shelf       = Koha::Virtualshelves->find($shelfnumber);
98     if ( $shelf->can_biblios_be_added($loggedinuser) ) {
99         $template->param(
100             singleshelf => 1,
101             shelfnumber => $shelf->shelfnumber,
102             shelfname   => $shelf->shelfname,
103         );
104     } else {
105         $authorized = 0;
106     }
107 } else {
108     if ( $loggedinuser > 0 ) {
109         my $private_shelves = Koha::Virtualshelves->search(
110             {   public   => 0,
111                 owner    => $loggedinuser,
112                 allow_change_from_owner => 1,
113             },
114             { order_by => 'shelfname' }
115         );
116         my $shelves_shared_with_me = Koha::Virtualshelves->search(
117             {   public                              => 0,
118                 'virtualshelfshares.borrowernumber' => $loggedinuser,
119                 allow_change_from_others            => 1,
120             },
121             { join => 'virtualshelfshares', }
122         );
123         my $public_shelves;
124         if ( $loggedinuser ) {
125             if ( Koha::Patrons->find( $loggedinuser )->can_patron_change_permitted_staff_lists ) {
126                 $public_shelves = Koha::Virtualshelves->search(
127                     {   public   => 1,
128                         -or      => [
129                             -and => {
130                                 allow_change_from_owner => 1,
131                                 owner     => $loggedinuser,
132                             },
133                             allow_change_from_others          => 1,
134                             allow_change_from_staff           => 1,
135                             allow_change_from_permitted_staff => 1
136                         ],
137                     },
138                     { order_by => 'shelfname' }
139                 );
140             } elsif ( Koha::Patrons->find( $loggedinuser )->can_patron_change_staff_only_lists ) {
141                 $public_shelves = Koha::Virtualshelves->search(
142                     {   public   => 1,
143                         -or      => [
144                             -and => {
145                                 allow_change_from_owner => 1,
146                                 owner     => $loggedinuser,
147                             },
148                             allow_change_from_others          => 1,
149                             allow_change_from_staff           => 1
150                         ],
151                     },
152                     { order_by => 'shelfname' }
153                 );
154             } else {
155                 $public_shelves = Koha::Virtualshelves->search(
156                     {   public   => 1,
157                         -or      => [
158                             -and => {
159                                 allow_change_from_owner => 1,
160                                 owner => $loggedinuser,
161                             },
162                             allow_change_from_others => 1,
163                         ],
164                     },
165                     {order_by => 'shelfname' }
166                 );
167             }
168         } else {
169             $public_shelves = Koha::Virtualshelves->search(
170                 {   public   => 1,
171                     -or      => [
172                         -and => {
173                             allow_change_from_owner => 1,
174                             owner => $loggedinuser,
175                         },
176                         allow_change_from_others => 1,
177                     ],
178                 },
179                 {order_by => 'shelfname' }
180             );
181         }
182
183         $template->param(
184             private_shelves                => $private_shelves,
185             private_shelves_shared_with_me => $shelves_shared_with_me,
186             public_shelves                 => $public_shelves,
187         );
188     } else {
189         $authorized = 0;
190     }
191 }
192
193 if ($authorized) {
194     for my $biblionumber (@biblionumbers) {
195         my $biblio = Koha::Biblios->find( $biblionumber );
196         push(
197             @biblios,
198             {   biblionumber => $biblionumber,
199                 title        => $biblio->title,
200                 subtitle     => $biblio->subtitle,
201                 medium       => $biblio->medium,
202                 part_number  => $biblio->part_number,
203                 part_name    => $biblio->part_name,
204                 author       => $biblio->author,
205             }
206         );
207     }
208     $template->param(
209         multiple => ( scalar(@biblios) > 1 ),
210         total    => scalar @biblios,
211         biblios  => \@biblios,
212     );
213
214     $template->param(
215         newshelf => $newshelf || 0,
216         OpacAllowPublicListCreation => C4::Context->preference('OpacAllowPublicListCreation'),
217     );
218 }
219 $template->param( authorized => $authorized, errcode => $errcode, );
220 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };