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