Bug 14544: Get rid of GetShelfContent
[koha.git] / virtualshelves / addbybiblionumber.pl
1 #!/usr/bin/perl
2
3 #script to provide virtual shelf management
4 #
5 #
6 # Copyright 2000-2002 Katipo Communications
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22
23
24 =head1 NAME
25
26 addbybiblionumber.pl
27
28 =head1 DESCRIPTION
29
30     This script allow to add a virtual in a virtual shelf from a biblionumber.
31
32 =head1 CGI PARAMETERS
33
34 =over 4
35
36 =item biblionumber
37
38     The biblionumber
39
40 =item shelfnumber
41
42     the shelfnumber where to add the virtual.
43
44 =item newvirtualshelf
45
46     if this parameter exists, then it must be equals to the name of the shelf
47     to add.
48
49 =item category
50
51     if this script has to add a shelf, it add one with this category.
52
53 =item newshelf
54
55     if this parameter exists, then we create a new shelf
56
57 =back
58
59 =cut
60
61 use strict;
62 use warnings;
63
64 use CGI qw ( -utf8 );
65 use C4::Biblio;
66 use C4::Output;
67 use C4::VirtualShelves qw/:DEFAULT/;
68 use C4::Auth;
69
70 use Koha::Virtualshelves;
71
72 our $query           = new CGI;
73 our @biblionumber    = HandleBiblioPars();
74 our $shelfnumber     = $query->param('shelfnumber');
75 our $newvirtualshelf = $query->param('newvirtualshelf');
76 our $newshelf        = $query->param('newshelf');
77 our $category        = $query->param('category');
78 our $sortfield      = $query->param('sortfield');
79 my $confirmed       = $query->param('confirmed') || 0;
80 our $authorized      = 1;
81 our $errcode        = 0;
82
83 our ( $template, $loggedinuser, $cookie ) = get_template_and_user(
84     {
85         template_name   => "virtualshelves/addbybiblionumber.tt",
86         query           => $query,
87         type            => "intranet",
88         authnotrequired => 0,
89         flagsrequired   => { catalogue => 1 },
90     }
91 );
92
93 if( $newvirtualshelf) {
94     HandleNewVirtualShelf();
95     exit if $authorized;
96     ShowTemplate(); #error message
97 }
98 elsif($shelfnumber && $confirmed) {
99     HandleShelfNumber();
100     exit if $authorized;
101     ShowTemplate(); #error message
102 }
103 elsif($shelfnumber) { #still needs confirmation
104     HandleSelectedShelf();
105     LoadBib() if $authorized;
106     ShowTemplate();
107 }
108 else {
109     HandleSelect();
110     LoadBib();
111     ShowTemplate();
112 }
113 #end
114
115 sub HandleBiblioPars {
116     my @bib= $query->param('biblionumber');
117     if(@bib==0 && $query->param('biblionumbers')) {
118         my $str= $query->param('biblionumbers');
119         @bib= split '/', $str;
120     }
121     elsif(@bib==1 && $bib[0]=~/\//) {
122         @bib= split '/', $bib[0];
123     }
124     return @bib;
125 }
126
127 sub HandleNewVirtualShelf {
128     my $shelf = eval {
129         Koha::Virtualshelf->new(
130             {
131                 shelfname => $newvirtualshelf,
132                 category => $category,
133                 sortfield => $sortfield,
134                 owner => $loggedinuser,
135             }
136         );
137     }->store;
138     if ( $@ or not $shelf ) {
139         $authorized = 0;
140         $errcode    = 1;
141         return;
142     }
143
144     for my $bib (@biblionumber){
145         $shelf->add_biblio( $bib, $loggedinuser );
146     }
147     #Reload the page where you came from
148     print $query->header;
149     print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"window.opener.location.reload(true);self.close();\"></body></html>";
150 }
151
152 sub HandleShelfNumber {
153     my $shelf = Koha::Virtualshelves->find( $shelfnumber );
154     if($authorized = $shelf->can_biblios_be_added( $loggedinuser ) ) {
155         for my $bib (@biblionumber){
156             $shelf->add_biblio( $bib, $loggedinuser );
157         }
158         #Close this page and return
159         print $query->header;
160         print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"self.close();\"></body></html>";
161     }
162     else {
163         $errcode=2; #no perm
164     }
165 }
166
167 sub HandleSelectedShelf {
168     my $shelf = Koha::Virtualshelves->find( $shelfnumber );
169     if($authorized = $shelf->can_biblios_be_added( $loggedinuser ) ) {
170         #confirm adding to specific shelf
171         $template->param(
172         singleshelf               => 1,
173         shelfnumber               => $shelf->shelfnumber,
174         shelfname                 => $shelf->shelfname,
175         );
176     }
177     else {
178     $errcode=2; #no perm
179     }
180 }
181
182 sub HandleSelect {
183     my $private_shelves = Koha::Virtualshelves->search(
184         {
185             category => 1,
186             owner => $loggedinuser,
187         },
188         { order_by => 'shelfname' }
189     );
190     my $shelves_shared_with_me = Koha::Virtualshelves->search(
191         {
192             category => 1,
193             'virtualshelfshares.borrowernumber' => $loggedinuser,
194             -or => {
195                 allow_add => 1,
196                 owner => $loggedinuser,
197             }
198         },
199         {
200             join => 'virtualshelfshares',
201         }
202     );
203     my $public_shelves= Koha::Virtualshelves->search(
204         {
205             category => 2,
206             -or => {
207                 allow_add => 1,
208                 owner => $loggedinuser,
209             }
210         },
211         { order_by => 'shelfname' }
212     );
213     $template->param (
214         private_shelves => $private_shelves,
215         private_shelves_shared_with_me => $shelves_shared_with_me,
216         public_shelves  => $public_shelves,
217     );
218 }
219
220 sub LoadBib {
221     my @biblios;
222     for my $bib (@biblionumber) {
223         my $data = GetBiblioData($bib);
224     push(@biblios,
225         { biblionumber => $bib,
226           title        => $data->{'title'},
227           author       => $data->{'author'},
228     } );
229     }
230     $template->param(
231         multiple => (scalar(@biblios) > 1),
232     total    => scalar @biblios,
233     biblios  => \@biblios,
234     );
235 }
236
237 sub ShowTemplate {
238     $template->param (
239     newshelf => $newshelf||0,
240     authorized  => $authorized,
241     errcode             => $errcode,
242     );
243     output_html_with_http_headers $query, $cookie, $template->output;
244 }