Bug 16849: Move IsDebarred to Koha::Patron->is_debarred
[koha.git] / opac / opac-addbybiblionumber.pl
1 #!/usr/bin/perl
2
3 #script to provide virtualshelf management
4 #
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 use strict;
23 use warnings;
24
25 use CGI qw ( -utf8 );
26 use C4::Biblio;
27 use C4::Output;
28 use C4::Auth;
29
30 use Koha::Virtualshelves;
31
32 our $query              = new CGI;
33 our @biblionumber       = $query->param('biblionumber');
34 our $selectedshelf      = $query->param('selectedshelf');
35 our $newshelf           = $query->param('newshelf');
36 our $shelfnumber        = $query->param('shelfnumber');
37 our $newvirtualshelf    = $query->param('newvirtualshelf');
38 our $category           = $query->param('category');
39 our $authorized          = 1;
40 our $errcode            = 0;
41 our @biblios = ();
42
43 # if virtualshelves is disabled, leave immediately
44 if ( ! C4::Context->preference('virtualshelves') ) {
45     print $query->redirect("/cgi-bin/koha/errors/404.pl");
46     exit;
47 }
48
49 if (scalar(@biblionumber) == 1) {
50     @biblionumber = (split /\//,$biblionumber[0]);
51 }
52
53 our ( $template, $loggedinuser, $cookie ) = get_template_and_user(
54     {
55         template_name   => "opac-addbybiblionumber.tt",
56         query           => $query,
57         type            => "opac",
58         authnotrequired => 0,
59     }
60 );
61
62 if( $newvirtualshelf) {
63     HandleNewVirtualShelf();
64     exit if $authorized;
65     ShowTemplate(); #error message
66 }
67 elsif($shelfnumber) {
68     HandleShelfNumber();
69     exit if $authorized;
70     ShowTemplate(); #error message
71 }
72 elsif($selectedshelf) {
73     HandleSelectedShelf();
74     LoadBib() if $authorized;
75     ShowTemplate();
76 }
77 else {
78     HandleSelect();
79     LoadBib() if $authorized;
80     ShowTemplate();
81 }
82 #end
83
84 sub HandleNewVirtualShelf {
85     if ( $loggedinuser > 0 and
86         (
87             $category == 1
88                 or $category == 2 and $loggedinuser>0 && C4::Context->preference('OpacAllowPublicListCreation')
89         )
90     ) {
91         my $shelf = eval {
92             Koha::Virtualshelf->new(
93                 {
94                     shelfname => $newvirtualshelf,
95                     category => $category,
96                     owner => $loggedinuser,
97                 }
98             )->store;
99         };
100         if ( $@ or not $shelf ) {
101             $authorized = 0;
102             $errcode = 1;
103             return;
104         }
105
106         for my $bib (@biblionumber) {
107             $shelf->add_biblio( $bib, $loggedinuser );
108         }
109
110         #Reload the page where you came from
111         print $query->header;
112         print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"window.opener.location.reload(true);self.close();\"></body></html>";
113     }
114 }
115
116 sub HandleShelfNumber {
117     my $shelfnumber = $query->param('shelfnumber');
118     my $shelf = Koha::Virtualshelves->find( $shelfnumber );
119     if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
120         for my $bib (@biblionumber) {
121             $shelf->add_biblio( $bib, $loggedinuser );
122         }
123         #Close this page and return
124         print $query->header;
125         print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"self.close();\"></body></html>";
126     } else {
127         # TODO
128     }
129 }
130
131 sub HandleSelectedShelf {
132     my $shelfnumber = $query->param('selectedshelf');
133     my $shelf = Koha::Virtualshelves->find( $shelfnumber );
134     if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
135         $template->param(
136             singleshelf               => 1,
137             shelfnumber               => $shelf->shelfnumber,
138             shelfname                 => $shelf->shelfname,
139         );
140     } else {
141         # TODO
142     }
143 }
144
145 sub HandleSelect {
146     return unless $authorized= $loggedinuser>0;
147     my $private_shelves = Koha::Virtualshelves->search(
148         {
149             category => 1,
150             owner => $loggedinuser,
151         },
152         { order_by => 'shelfname' }
153     );
154     my $shelves_shared_with_me = Koha::Virtualshelves->search(
155         {
156             category => 1,
157             'virtualshelfshares.borrowernumber' => $loggedinuser,
158             -or => {
159                 allow_add => 1,
160                 owner => $loggedinuser,
161             }
162         },
163         {
164             join => 'virtualshelfshares',
165         }
166     );
167     my $public_shelves= Koha::Virtualshelves->search(
168         {
169             category => 2,
170             -or => {
171                 allow_add => 1,
172                 owner => $loggedinuser,
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 sub LoadBib {
185     for my $bib (@biblionumber) {
186         my $data = GetBiblioData( $bib );
187         push(@biblios,
188             { biblionumber => $bib,
189               title        => $data->{'title'},
190               author       => $data->{'author'},
191         } );
192     }
193     $template->param(
194         multiple => (scalar(@biblios) > 1),
195     total    => scalar @biblios,
196     biblios  => \@biblios,
197     );
198 }
199
200 sub ShowTemplate {
201     $template->param (
202     newshelf => $newshelf||0,
203     authorized  => $authorized,
204     errcode             => $errcode,
205     OpacAllowPublicListCreation => C4::Context->preference('OpacAllowPublicListCreation'),
206     );
207     output_html_with_http_headers $query, $cookie, $template->output;
208 }