Bug 14544: Get rid of ShelfPossibleAction
[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::VirtualShelves qw/:DEFAULT/;
28 use C4::Output;
29 use C4::Auth;
30
31 use Koha::Virtualshelves;
32
33 our $query              = new CGI;
34 our @biblionumber       = $query->param('biblionumber');
35 our $selectedshelf      = $query->param('selectedshelf');
36 our $newshelf           = $query->param('newshelf');
37 our $shelfnumber        = $query->param('shelfnumber');
38 our $newvirtualshelf    = $query->param('newvirtualshelf');
39 our $category           = $query->param('category');
40 our $authorized          = 1;
41 our $errcode            = 0;
42 our @biblios;
43
44 our ( $template, $loggedinuser, $cookie ) = get_template_and_user(
45     {
46         template_name   => "opac-addbybiblionumber.tt",
47         query           => $query,
48         type            => "opac",
49         authnotrequired => 0,
50     }
51 );
52
53 if( $newvirtualshelf) {
54     HandleNewVirtualShelf();
55     exit if $authorized;
56     ShowTemplate(); #error message
57 }
58 elsif($shelfnumber) {
59     HandleShelfNumber();
60     exit if $authorized;
61     ShowTemplate(); #error message
62 }
63 elsif($selectedshelf) {
64     HandleSelectedShelf();
65     LoadBib() if $authorized;
66     ShowTemplate();
67 }
68 else {
69     HandleSelect();
70     LoadBib() if $authorized;
71     ShowTemplate();
72 }
73 #end
74
75 sub AddBibliosToShelf {
76     #splits incoming biblionumber(s) to array and adds each to shelf.
77     my ($shelfnumber,@biblionumber)=@_;
78
79     #multiple bibs might come in as '/' delimited string (from where, i don't see), or as array.
80     if (scalar(@biblionumber) == 1) {
81         @biblionumber = (split /\//,$biblionumber[0]);
82     }
83     for my $bib (@biblionumber) {
84         my $shelf = Koha::Virtualshelves->find( $shelfnumber );
85         $shelf->add_biblio( $bib, $loggedinuser );
86     }
87 }
88
89 sub HandleNewVirtualShelf {
90     if ( $loggedinuser > 0 and
91         (
92             $category == 1
93                 or $category == 2 and $loggedinuser>0 && C4::Context->preference('OpacAllowPublicListCreation')
94         )
95     ) {
96         my $shelf = eval {
97             Koha::Virtualshelf->new(
98                 {
99                     shelfname => $newvirtualshelf,
100                     category => $category,
101                     owner => $loggedinuser,
102                 }
103             );
104         };
105         if ( $@ or not $shelf ) {
106             $authorized = 0;
107             $errcode = 1;
108             return;
109         }
110         AddBibliosToShelf($shelfnumber, @biblionumber);
111         #Reload the page where you came from
112         print $query->header;
113         print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"window.opener.location.reload(true);self.close();\"></body></html>";
114     }
115 }
116
117 sub HandleShelfNumber {
118     my $shelfnumber = $query->param('shelfnumber');
119     my $shelf = Koha::Virtualshelves->find( $shelfnumber );
120     if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
121         AddBibliosToShelf($shelfnumber,@biblionumber);
122         #Close this page and return
123         print $query->header;
124         print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"self.close();\"></body></html>";
125     } else {
126         # TODO
127     }
128 }
129
130 sub HandleSelectedShelf {
131     my $shelfnumber = $query->param('selectedshelf');
132     my $shelf = Koha::Virtualshelves->find( $shelfnumber );
133     if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
134         $template->param(
135             singleshelf               => 1,
136             shelfnumber               => $shelf->shelfnumber,
137             shelfname                 => $shelf->shelfname,
138         );
139     } else {
140         # TODO
141     }
142 }
143
144 sub HandleSelect {
145     return unless $authorized= $loggedinuser>0;
146     my $private_shelves = Koha::Virtualshelves->search(
147         {
148             category => 1,
149             owner => $loggedinuser,
150         },
151         { order_by => 'shelfname' }
152     );
153     my $shelves_shared_with_me = Koha::Virtualshelves->search(
154         {
155             category => 1,
156             'virtualshelfshares.borrowernumber' => $loggedinuser,
157             -or => {
158                 allow_add => 1,
159                 owner => $loggedinuser,
160             }
161         },
162         {
163             join => 'virtualshelfshares',
164         }
165     );
166     my $public_shelves= Koha::Virtualshelves->search(
167         {
168             category => 2,
169             -or => {
170                 allow_add => 1,
171                 owner => $loggedinuser,
172             }
173         },
174         { order_by => 'shelfname' }
175     );
176     $template->param (
177         private_shelves => $private_shelves,
178         private_shelves_shared_with_me => $shelves_shared_with_me,
179         public_shelves  => $public_shelves,
180     );
181 }
182
183 sub LoadBib {
184     #see comment in AddBibliosToShelf
185     if (scalar(@biblionumber) == 1) {
186         @biblionumber = (split /\//,$biblionumber[0]);
187     }
188     for my $bib (@biblionumber) {
189         my $data = GetBiblioData( $bib );
190     push(@biblios,
191         { biblionumber => $bib,
192           title        => $data->{'title'},
193           author       => $data->{'author'},
194     } );
195     }
196     $template->param(
197         multiple => (scalar(@biblios) > 1),
198     total    => scalar @biblios,
199     biblios  => \@biblios,
200     );
201 }
202
203 sub ShowTemplate {
204     $template->param (
205     newshelf => $newshelf||0,
206     authorized  => $authorized,
207     errcode             => $errcode,
208     OpacAllowPublicListCreation => C4::Context->preference('OpacAllowPublicListCreation'),
209     );
210     output_html_with_http_headers $query, $cookie, $template->output;
211 }