Followup : fb4366cdad0ad96d0427810581763dc2fc189af1
[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 under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along with
20 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
21 # Suite 330, Boston, MA  02111-1307 USA
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 C4::Biblio;
63 use CGI;
64 use C4::Output;
65 use C4::VirtualShelves qw/:DEFAULT GetRecentShelves/;
66 use C4::Circulation;
67 use C4::Auth;
68 #splits incoming biblionumber(s) to array and adds each to shelf.
69 sub AddBibliosToShelf {
70     my ($shelfnumber,@biblionumber)=@_;
71
72     # multiple bibs might come in as '/' delimited string (from where, i don't see), or as array.
73     if (scalar(@biblionumber) == 1) {
74         @biblionumber = (split /\//,$biblionumber[0]);
75     }
76     for my $bib (@biblionumber){
77         AddToShelfFromBiblio($bib, $shelfnumber);
78     }
79 }
80
81
82
83 #use it only to debug !
84 use warnings;
85
86 sub AddBibliosToShelf {
87     my ($shelfnumber,@biblionumber)=@_;
88
89     # multiple bibs might come in as '/' delimited string (from where, i don't see), or as array.
90     if (scalar(@biblionumber) == 1) {
91         @biblionumber = (split /\//,$biblionumber[0]);
92     }
93     for my $bib (@biblionumber){
94         AddToShelfFromBiblio($bib, $shelfnumber);
95     }
96 }
97
98 my $query           = new CGI;
99
100 # If set, then single item case.
101 my $biblionumber    = $query->param('biblionumber');
102
103 # If set, then multiple item case.
104 my @biblionumber   = $query->param('biblionumber');
105 my $biblionumbers   = $query->param('biblionumbers');
106
107 my $shelfnumber     = $query->param('shelfnumber');
108 my $newvirtualshelf = $query->param('newvirtualshelf');
109 my $newshelf        = $query->param('newshelf');
110 my $category        = $query->param('category');
111 my $sortfield       = $query->param('sortfield');
112 my $confirmed       = $query->param('confirmed') || 0;
113
114
115 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
116     {
117         template_name   => "virtualshelves/addbybiblionumber.tmpl",
118         query           => $query,
119         type            => "intranet",
120         authnotrequired => 0,
121         flagsrequired   => { catalogue => 1 },
122     }
123 );
124
125 my @biblionumbers;
126 if ($biblionumbers) {
127     @biblionumbers = split '/', $biblionumbers;
128 } else {
129     @biblionumbers = (@biblionumber);
130 }
131
132 $shelfnumber = AddShelf( $newvirtualshelf, $loggedinuser, $category, $sortfield ) if $newvirtualshelf;
133 if ( $shelfnumber || ( $shelfnumber == -1 ) ) {    # the shelf already exist.
134
135     if ($confirmed == 1) {
136         AddBibliosToShelf($shelfnumber,@biblionumber);
137         print
138     "Content-Type: text/html\n\n<html><body onload=\"window.opener.location.reload(true);window.close()\"></body></html>";
139         exit;
140     } else {
141         my ( $singleshelf, $singleshelfname, $singlecategory ) = GetShelf( $query->param('shelfnumber') );
142         my @biblios;
143         for my $bib (@biblionumber) {
144             my $data = GetBiblioData( $bib );
145             push(@biblios,
146                         { biblionumber => $bib,
147                           title        => $data->{'title'},
148                           author       => $data->{'author'},
149                         } );
150         }
151
152         $template->param
153         (
154          biblionumber => \@biblionumber,
155          biblios      => \@biblios,
156          multiple     => (scalar(@biblionumber) > 1),
157          singleshelf  => 1,
158          shelfname    => $singleshelfname,
159          shelfnumber  => $singleshelf,
160          total        => scalar(@biblionumber),
161          confirm      => 1,
162         );
163     }
164 }
165 else {    # this shelf doesn't already exist.
166     my $limit = 10;
167     my ($shelflist) = GetRecentShelves(1, $limit, $loggedinuser);
168     my @shelvesloop;
169     my %shelvesloop;
170     for my $shelf ( @{ $shelflist->[0] } ) {
171         push( @shelvesloop, $shelf->{shelfnumber} );
172         $shelvesloop{$shelf->{shelfnumber}} = $shelf->{shelfname};
173     }
174     # then open shelves...
175     ($shelflist) = GetRecentShelves(3, $limit, undef);
176     for my $shelf ( @{ $shelflist->[0] } ) {
177         push( @shelvesloop, $shelf->{shelfnumber} );
178         $shelvesloop{$shelf->{shelfnumber}} = $shelf->{shelfname};
179     }
180
181     if(@shelvesloop gt 0){
182         my $CGIvirtualshelves = CGI::scrolling_list
183           (
184            -name     => 'shelfnumber',
185            -values   => \@shelvesloop,
186            -labels   => \%shelvesloop,
187            -size     => 1,
188            -tabindex => '',
189            -multiple => 0
190           );
191         $template->param
192           (
193            CGIvirtualshelves => $CGIvirtualshelves,
194           );
195     }
196         my @biblios;
197         for my $bib (@biblionumber) {
198             my $data = GetBiblioData( $bib );
199             push(@biblios,
200                         { biblionumber => $bib,
201                           title        => $data->{'title'},
202                           author       => $data->{'author'},
203                         } );
204         }
205     $template->param(
206            newshelf     => $newshelf,
207            biblios=>\@biblios,
208            multiple     => (scalar(@biblionumber) > 1),
209            total        => scalar(@biblionumber),
210     );
211
212     unless (@biblionumbers) {
213         my ( $bibliocount, @biblios ) = GetBiblio($biblionumber);
214     
215         $template->param
216           (
217            biblionumber      => $biblionumber,
218            title             => $biblios[0]->{'title'},
219            author            => $biblios[0]->{'author'},
220           );
221     } else {
222         my @biblioloop = ();
223         foreach my $biblionumber (@biblionumbers) {
224             my ( $bibliocount, @biblios ) = GetBiblio($biblionumber);
225             my %biblioiter = (
226                               title=>$biblios[0]->{'title'},
227                               author=>$biblios[0]->{'author'}
228                              );
229             push @biblioloop, \%biblioiter;
230         }
231         $template->param
232           (
233            biblioloop => \@biblioloop,
234            biblionumbers => $biblionumbers
235           );
236     }
237     
238 }
239 output_html_with_http_headers $query, $cookie, $template->output;