merging new_acq fix: missing links
[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 =back
54
55 =cut
56
57 use strict;
58 use C4::Biblio;
59 use CGI;
60 use C4::Output;
61 use C4::VirtualShelves qw/:DEFAULT GetRecentShelves/;
62 use C4::Circulation;
63 use C4::Auth;
64
65 #use it only to debug !
66 use warnings;
67
68 sub AddBibliosToShelf {
69     my ($shelfnumber,@biblionumber)=@_;
70
71     # multiple bibs might come in as '/' delimited string (from where, i don't see), or as array.
72     if (scalar(@biblionumber) == 1) {
73         @biblionumber = (split /\//,$biblionumber[0]);
74     }
75     for my $bib (@biblionumber){
76         AddToShelfFromBiblio($bib, $shelfnumber);
77     }
78 }
79
80 my $query           = new CGI;
81
82 # If set, then single item case.
83 my @biblionumber    = $query->param('biblionumber');
84
85 # If set, then multiple item case.
86
87 my $biblionumbers   = $query->param('biblionumbers');
88 my $shelfnumber     = $query->param('shelfnumber');
89 my $newvirtualshelf = $query->param('newvirtualshelf');
90 my $category        = $query->param('category');
91 my $sortfield           = $query->param('sortfield');
92
93 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
94     {
95         template_name   => "virtualshelves/addbybiblionumber.tmpl",
96         query           => $query,
97         type            => "intranet",
98         authnotrequired => 0,
99         flagsrequired   => { catalogue => 1 },
100     }
101 );
102
103 my @biblionumbers;
104 if ($biblionumbers) {
105     @biblionumbers = split '/', $biblionumbers;
106 } else {
107     @biblionumbers = ($biblionumber);
108 }
109
110 $shelfnumber = AddShelf( $newvirtualshelf, $loggedinuser, $category, $sortfield )
111   if $newvirtualshelf;
112 if ( $shelfnumber || ( $shelfnumber == -1 ) ) {    # the shelf already exist.
113     if ($confirmed == 1) {
114         AddBibliosToShelf($shelfnumber,@biblionumber);
115         print
116     "Content-Type: text/html\n\n<html><body onload=\"window.opener.location.reload(true);window.close()\"></body></html>";
117         exit;
118     } else {
119         my ( $singleshelf, $singleshelfname, $singlecategory ) = GetShelf( $query->param('shelfnumber') );
120         my @biblios;
121         for my $bib (@biblionumber) {
122             my $data = GetBiblioData( $bib );
123             push(@biblios,
124                         { biblionumber => $bib,
125                           title        => $data->{'title'},
126                           author       => $data->{'author'},
127                         } );
128         }
129
130         $template->param
131         (
132          biblionumber => \@biblionumber,
133          biblios      => \@biblios,
134          multiple     => (scalar(@biblionumber) > 1),
135          singleshelf  => 1,
136          shelfname    => $singleshelfname,
137          shelfnumber  => $singleshelf,
138          total        => scalar(@biblionumber),
139          confirm      => 1,
140         );
141     }
142 }
143 else {    # this shelf doesn't already exist.
144     my $limit = 10;
145     my ($shelflist) = GetRecentShelves(1, $limit, $loggedinuser);
146     my @shelvesloop;
147     my %shelvesloop;
148     for my $shelf ( @{ $shelflist->[0] } ) {
149         push( @shelvesloop, $shelf->{shelfnumber} );
150         $shelvesloop{$shelf->{shelfnumber}} = $shelf->{shelfname};
151     }
152     # then open shelves...
153     ($shelflist) = GetRecentShelves(3, $limit, undef);
154     for my $shelf ( @{ $shelflist->[0] } ) {
155         push( @shelvesloop, $shelf->{shelfnumber} );
156         $shelvesloop{$shelf->{shelfnumber}} = $shelf->{shelfname};
157     }
158     if(@shelvesloop gt 0){
159         my $CGIvirtualshelves = CGI::scrolling_list
160           (
161            -name     => 'shelfnumber',
162            -values   => \@shelvesloop,
163            -labels   => \%shelvesloop,
164            -size     => 1,
165            -tabindex => '',
166            -multiple => 0
167           );
168         $template->param
169           (
170            CGIvirtualshelves => $CGIvirtualshelves,
171           );
172     }
173     
174     unless ($biblionumbers) {
175         my ( $bibliocount, @biblios ) = GetBiblio($biblionumber);
176     
177         $template->param
178           (
179            biblionumber      => $biblionumber,
180            title             => $biblios[0]->{'title'},
181            author            => $biblios[0]->{'author'},
182           );
183     } else {
184         my @biblioloop = ();
185         foreach my $biblionumber (@biblionumbers) {
186             my ( $bibliocount, @biblios ) = GetBiblio($biblionumber);
187             my %biblioiter = (
188                               title=>$biblios[0]->{'title'},
189                               author=>$biblios[0]->{'author'}
190                              );
191             push @biblioloop, \%biblioiter;
192         }
193         $template->param
194           (
195            biblioloop => \@biblioloop,
196            biblionumbers => $biblionumbers
197           );
198     }
199     
200 }
201 output_html_with_http_headers $query, $cookie, $template->output;