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