Bug 15006: [QA Follow-up] Satisfy qa tools with one tab less
[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::Auth;
68
69 use Koha::Virtualshelves;
70
71 our $query           = new CGI;
72 our @biblionumber    = HandleBiblioPars();
73 our $shelfnumber     = $query->param('shelfnumber');
74 our $newvirtualshelf = $query->param('newvirtualshelf');
75 our $newshelf        = $query->param('newshelf');
76 our $category        = $query->param('category');
77 our $sortfield      = $query->param('sortfield');
78 my $confirmed       = $query->param('confirmed') || 0;
79 our $authorized      = 1;
80 our $errcode        = 0;
81
82 our ( $template, $loggedinuser, $cookie ) = get_template_and_user(
83     {
84         template_name   => "virtualshelves/addbybiblionumber.tt",
85         query           => $query,
86         type            => "intranet",
87         authnotrequired => 0,
88         flagsrequired   => { catalogue => 1 },
89     }
90 );
91
92 if( $newvirtualshelf) {
93     HandleNewVirtualShelf();
94     exit if $authorized;
95     ShowTemplate(); #error message
96 }
97 elsif($shelfnumber && $confirmed) {
98     HandleShelfNumber();
99     exit if $authorized;
100     ShowTemplate(); #error message
101 }
102 elsif($shelfnumber) { #still needs confirmation
103     HandleSelectedShelf();
104     LoadBib() if $authorized;
105     ShowTemplate();
106 }
107 else {
108     HandleSelect();
109     LoadBib();
110     ShowTemplate();
111 }
112 #end
113
114 sub HandleBiblioPars {
115     my @bib= $query->multi_param('biblionumber');
116     if(@bib==0 && $query->param('biblionumbers')) {
117         my $str= $query->param('biblionumbers');
118         @bib= split '/', $str;
119     }
120     elsif(@bib==1 && $bib[0]=~/\//) {
121         @bib= split '/', $bib[0];
122     }
123     return @bib;
124 }
125
126 sub HandleNewVirtualShelf {
127     my $shelf = eval {
128         Koha::Virtualshelf->new(
129             {
130                 shelfname => $newvirtualshelf,
131                 category => $category,
132                 sortfield => $sortfield,
133                 owner => $loggedinuser,
134             }
135         )->store;
136     };
137     if ( $@ or not $shelf ) {
138         $authorized = 0;
139         $errcode    = 1;
140         return;
141     }
142
143     for my $bib (@biblionumber){
144         $shelf->add_biblio( $bib, $loggedinuser );
145     }
146     #Reload the page where you came from
147     print $query->header;
148     print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"window.opener.location.reload(true);self.close();\"></body></html>";
149 }
150
151 sub HandleShelfNumber {
152     my $shelf = Koha::Virtualshelves->find( $shelfnumber );
153     if($authorized = $shelf->can_biblios_be_added( $loggedinuser ) ) {
154         for my $bib (@biblionumber){
155             $shelf->add_biblio( $bib, $loggedinuser );
156         }
157         #Close this page and return
158         print $query->header;
159         print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"self.close();\"></body></html>";
160     }
161     else {
162         $errcode=2; #no perm
163     }
164 }
165
166 sub HandleSelectedShelf {
167     my $shelf = Koha::Virtualshelves->find( $shelfnumber );
168     if($authorized = $shelf->can_biblios_be_added( $loggedinuser ) ) {
169         #confirm adding to specific shelf
170         $template->param(
171         singleshelf               => 1,
172         shelfnumber               => $shelf->shelfnumber,
173         shelfname                 => $shelf->shelfname,
174         );
175     }
176     else {
177     $errcode=2; #no perm
178     }
179 }
180
181 sub HandleSelect {
182     my $private_shelves = Koha::Virtualshelves->search(
183         {
184             category => 1,
185             owner => $loggedinuser,
186         },
187         { order_by => 'shelfname' }
188     );
189     my $shelves_shared_with_me = Koha::Virtualshelves->search(
190         {
191             category => 1,
192             'virtualshelfshares.borrowernumber' => $loggedinuser,
193             -or => {
194                 allow_add => 1,
195                 owner => $loggedinuser,
196             }
197         },
198         {
199             join => 'virtualshelfshares',
200         }
201     );
202     my $public_shelves= Koha::Virtualshelves->search(
203         {
204             category => 2,
205             -or => {
206                 allow_add => 1,
207                 owner => $loggedinuser,
208             }
209         },
210         { order_by => 'shelfname' }
211     );
212     $template->param (
213         private_shelves => $private_shelves,
214         private_shelves_shared_with_me => $shelves_shared_with_me,
215         public_shelves  => $public_shelves,
216     );
217 }
218
219 sub LoadBib {
220     my @biblios;
221     for my $bib (@biblionumber) {
222         my $data = GetBiblioData($bib);
223     push(@biblios,
224         { biblionumber => $bib,
225           title        => $data->{'title'},
226           author       => $data->{'author'},
227     } );
228     }
229     $template->param(
230         multiple => (scalar(@biblios) > 1),
231     total    => scalar @biblios,
232     biblios  => \@biblios,
233     );
234 }
235
236 sub ShowTemplate {
237     $template->param (
238     newshelf => $newshelf||0,
239     authorized  => $authorized,
240     errcode             => $errcode,
241     );
242     output_html_with_http_headers $query, $cookie, $template->output;
243 }