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