Adding Merge of two biblios from a virtualshelf
[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 CGI::Carp qw/fatalsToBrowser/;
67 use warnings;
68
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 my $query           = new CGI;
82
83 # If set, then single item case.
84 my $biblionumber    = $query->param('biblionumber');
85
86 # If set, then multiple item case.
87 my $biblionumbers   = $query->param('biblionumbers');
88
89 my $shelfnumber     = $query->param('shelfnumber');
90 my $newvirtualshelf = $query->param('newvirtualshelf');
91 my $category        = $query->param('category');
92 my $sortfield           = $query->param('sortfield');
93
94 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
95     {
96         template_name   => "virtualshelves/addbybiblionumber.tmpl",
97         query           => $query,
98         type            => "intranet",
99         authnotrequired => 0,
100         flagsrequired   => { catalogue => 1 },
101     }
102 );
103
104 my @biblionumbers;
105 if ($biblionumbers) {
106     @biblionumbers = split '/', $biblionumbers;
107 } else {
108     @biblionumbers = ($biblionumber);
109 }
110
111 $shelfnumber = AddShelf( $newvirtualshelf, $loggedinuser, $category, $sortfield )
112   if $newvirtualshelf;
113 if ( $shelfnumber || ( $shelfnumber == -1 ) ) {    # the shelf already exist.
114     if ($confirmed == 1) {
115         AddBibliosToShelf($shelfnumber,@biblionumber);
116         print
117     "Content-Type: text/html\n\n<html><body onload=\"window.opener.location.reload(true);window.close()\"></body></html>";
118         exit;
119     } else {
120         my ( $singleshelf, $singleshelfname, $singlecategory ) = GetShelf( $query->param('shelfnumber') );
121         my @biblios;
122         for my $bib (@biblionumber) {
123             my $data = GetBiblioData( $bib );
124             push(@biblios,
125                         { biblionumber => $bib,
126                           title        => $data->{'title'},
127                           author       => $data->{'author'},
128                         } );
129         }
130
131         $template->param
132         (
133          biblionumber => \@biblionumber,
134          biblios      => \@biblios,
135          multiple     => (scalar(@biblionumber) > 1),
136          singleshelf  => 1,
137          shelfname    => $singleshelfname,
138          shelfnumber  => $singleshelf,
139          total        => scalar(@biblionumber),
140          confirm      => 1,
141         );
142     }
143 }
144 else {    # this shelf doesn't already exist.
145     my $limit = 10;
146     my ($shelflist) = GetRecentShelves(1, $limit, $loggedinuser);
147     my @shelvesloop;
148     my %shelvesloop;
149     for my $shelf ( @{ $shelflist->[0] } ) {
150         push( @shelvesloop, $shelf->{shelfnumber} );
151         $shelvesloop{$shelf->{shelfnumber}} = $shelf->{shelfname};
152     }
153     # then open shelves...
154     my ($shelflist) = GetRecentShelves(3, $limit, undef);
155     for my $shelf ( @{ $shelflist->[0] } ) {
156         push( @shelvesloop, $shelf->{shelfnumber} );
157         $shelvesloop{$shelf->{shelfnumber}} = $shelf->{shelfname};
158     }
159     if(@shelvesloop gt 0){
160         my $CGIvirtualshelves = CGI::scrolling_list
161           (
162            -name     => 'shelfnumber',
163            -values   => \@shelvesloop,
164            -labels   => \%shelvesloop,
165            -size     => 1,
166            -tabindex => '',
167            -multiple => 0
168           );
169         $template->param
170           (
171            CGIvirtualshelves => $CGIvirtualshelves,
172           );
173     }
174     
175     unless ($biblionumbers) {
176         my ( $bibliocount, @biblios ) = GetBiblio($biblionumber);
177     
178         $template->param
179           (
180            biblionumber      => $biblionumber,
181            title             => $biblios[0]->{'title'},
182            author            => $biblios[0]->{'author'},
183           );
184     } else {
185         my @biblioloop = ();
186         foreach my $biblionumber (@biblionumbers) {
187             my ( $bibliocount, @biblios ) = GetBiblio($biblionumber);
188             my %biblioiter = (
189                               title=>$biblios[0]->{'title'},
190                               author=>$biblios[0]->{'author'}
191                              );
192             push @biblioloop, \%biblioiter;
193         }
194         $template->param
195           (
196            biblioloop => \@biblioloop,
197            biblionumbers => $biblionumbers
198           );
199     }
200     
201 }
202 output_html_with_http_headers $query, $cookie, $template->output;