Bug 22696: Simplify visibility logic in opac-ISBDdetail.pl
[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::Biblio;
25 use C4::Output;
26 use C4::Auth;
27
28 use Koha::Biblios;
29 use Koha::Virtualshelves;
30
31 my $query           = new CGI;
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 $category        = $query->param('category');
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         authnotrequired => 0,
56     }
57 );
58
59 if ($newvirtualshelf) {
60     if ($loggedinuser > 0
61         and (  $category == 1
62             or $category == 2 and $loggedinuser > 0 && C4::Context->preference('OpacAllowPublicListCreation') )
63       ) {
64         my $shelf = eval { Koha::Virtualshelf->new( { shelfname => $newvirtualshelf, category => $category, owner => $loggedinuser, } )->store; };
65         if ( $@ or not $shelf ) {
66             $errcode    = 1;
67             $authorized = 0;
68         } else {
69             for my $biblionumber (@biblionumbers) {
70                 $shelf->add_biblio( $biblionumber, $loggedinuser );
71             }
72
73             #Reload the page where you came from
74             print $query->header;
75             print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"window.opener.location.reload(true);self.close();\"></body></html>";
76             exit;
77         }
78     }
79 } elsif ($shelfnumber) {
80     my $shelfnumber = $query->param('shelfnumber');
81     my $shelf       = Koha::Virtualshelves->find($shelfnumber);
82     if ( $shelf->can_biblios_be_added($loggedinuser) ) {
83         for my $biblionumber (@biblionumbers) {
84             $shelf->add_biblio( $biblionumber, $loggedinuser );
85         }
86
87         #Close this page and return
88         print $query->header;
89         print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"self.close();\"></body></html>";
90         exit;
91     } else {
92         $authorized = 0;
93     }
94 } elsif ($selectedshelf) {
95     my $shelfnumber = $query->param('selectedshelf');
96     my $shelf       = Koha::Virtualshelves->find($shelfnumber);
97     if ( $shelf->can_biblios_be_added($loggedinuser) ) {
98         $template->param(
99             singleshelf => 1,
100             shelfnumber => $shelf->shelfnumber,
101             shelfname   => $shelf->shelfname,
102         );
103     } else {
104         $authorized = 0;
105     }
106 } else {
107     if ( $loggedinuser > 0 ) {
108         my $private_shelves = Koha::Virtualshelves->search(
109             {   category => 1,
110                 owner    => $loggedinuser,
111                 allow_change_from_owner => 1,
112             },
113             { order_by => 'shelfname' }
114         );
115         my $shelves_shared_with_me = Koha::Virtualshelves->search(
116             {   category                            => 1,
117                 'virtualshelfshares.borrowernumber' => $loggedinuser,
118                 allow_change_from_others            => 1,
119             },
120             { join => 'virtualshelfshares', }
121         );
122         my $public_shelves = Koha::Virtualshelves->search(
123             {   category => 2,
124                 -or      => [
125                     -and => {
126                         allow_change_from_owner => 1,
127                         owner     => $loggedinuser,
128                     },
129                     allow_change_from_others => 1,
130                 ],
131             },
132             { order_by => 'shelfname' }
133         );
134         $template->param(
135             private_shelves                => $private_shelves,
136             private_shelves_shared_with_me => $shelves_shared_with_me,
137             public_shelves                 => $public_shelves,
138         );
139     } else {
140         $authorized = 0;
141     }
142 }
143
144 if ($authorized) {
145     for my $biblionumber (@biblionumbers) {
146         my $biblio = Koha::Biblios->find( $biblionumber );
147         push(
148             @biblios,
149             {   biblionumber => $biblionumber,
150                 title        => $biblio->title,
151                 author       => $biblio->author,
152             }
153         );
154     }
155     $template->param(
156         multiple => ( scalar(@biblios) > 1 ),
157         total    => scalar @biblios,
158         biblios  => \@biblios,
159     );
160
161     $template->param(
162         newshelf => $newshelf || 0,
163         OpacAllowPublicListCreation => C4::Context->preference('OpacAllowPublicListCreation'),
164     );
165 }
166 $template->param( authorized => $authorized, errcode => $errcode, );
167 output_html_with_http_headers $query, $cookie, $template->output;