MT 1110, Follow-up : Balance cart and lists : cart is now in the intranet
[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
69 # splits incoming biblionumber(s) to array and adds each to shelf.
70 sub AddBibliosToShelf {
71     my ($shelfnumber,@biblionumber)=@_;
72
73     # multiple bibs might come in as '/' delimited string (from where, i don't see), or as array.
74     # (Note : they come in as '/' when added from the cart)
75     if (scalar(@biblionumber) == 1) {
76         @biblionumber = (split /\//,$biblionumber[0]);
77     }
78     for my $bib (@biblionumber){
79         AddToShelfFromBiblio($bib, $shelfnumber);
80     }
81 }
82
83 my $query           = new CGI;
84
85 # If set, then single item case.
86 my $biblionumber    = $query->param('biblionumber');
87
88 # If set, then multiple item case.
89 my @biblionumber   = $query->param('biblionumber');
90 my $biblionumbers   = $query->param('biblionumbers');
91
92 my $shelfnumber     = $query->param('shelfnumber');
93 my $newvirtualshelf = $query->param('newvirtualshelf');
94 my $newshelf        = $query->param('newshelf');
95 my $category        = $query->param('category');
96 my $sortfield       = $query->param('sortfield');
97 my $confirmed       = $query->param('confirmed') || 0;
98
99
100 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
101     {
102         template_name   => "virtualshelves/addbybiblionumber.tmpl",
103         query           => $query,
104         type            => "intranet",
105         authnotrequired => 0,
106         flagsrequired   => { catalogue => 1 },
107     }
108 );
109
110 my @biblionumbers;
111 if ($biblionumbers) {
112     @biblionumbers = split '/', $biblionumbers;
113 } else {
114     @biblionumbers = (@biblionumber);
115 }
116 if (scalar(@biblionumber) == 1) {
117         @biblionumber = (split /\//,$biblionumber[0]);
118 }
119
120 $shelfnumber = AddShelf( $newvirtualshelf, $loggedinuser, $category, $sortfield ) if $newvirtualshelf;
121 if ( $shelfnumber || ( $shelfnumber == -1 ) ) {    # the shelf already exist.
122
123     if ($confirmed == 1) {
124         AddBibliosToShelf($shelfnumber,@biblionumber);
125         print
126     "Content-Type: text/html\n\n<html><body onload=\"window.opener.location.reload(true);window.close()\"></body></html>";
127         exit;
128     } else {
129         my ( $singleshelf, $singleshelfname, $singlecategory ) = GetShelf( $query->param('shelfnumber') );
130         my @biblios;
131         for my $bib (@biblionumber) {
132             my $data = GetBiblioData( $bib );
133             push(@biblios,
134                         { biblionumber => $bib,
135                           title        => $data->{'title'},
136                           author       => $data->{'author'},
137                         } );
138         }
139
140         $template->param
141         (
142          biblionumber => \@biblionumber,
143          biblios      => \@biblios,
144          multiple     => (scalar(@biblionumber) > 1),
145          singleshelf  => 1,
146          shelfname    => $singleshelfname,
147          shelfnumber  => $singleshelf,
148          total        => scalar(@biblionumber),
149          confirm      => 1,
150         );
151     }
152 }
153 else {    # this shelf doesn't already exist.
154     my $limit = 10;
155     my ($shelflist) = GetRecentShelves(1, $limit, $loggedinuser);
156     my @shelvesloop;
157     my %shelvesloop;
158     for my $shelf ( @{ $shelflist->[0] } ) {
159         push( @shelvesloop, $shelf->{shelfnumber} );
160         $shelvesloop{$shelf->{shelfnumber}} = $shelf->{shelfname};
161     }
162     # then open shelves...
163     ($shelflist) = GetRecentShelves(3, $limit, undef);
164     for my $shelf ( @{ $shelflist->[0] } ) {
165         push( @shelvesloop, $shelf->{shelfnumber} );
166         $shelvesloop{$shelf->{shelfnumber}} = $shelf->{shelfname};
167     }
168
169     if(@shelvesloop gt 0){
170         my $CGIvirtualshelves = CGI::scrolling_list
171           (
172            -name     => 'shelfnumber',
173            -values   => \@shelvesloop,
174            -labels   => \%shelvesloop,
175            -size     => 1,
176            -tabindex => '',
177            -multiple => 0
178           );
179         $template->param
180           (
181            CGIvirtualshelves => $CGIvirtualshelves,
182           );
183     }
184         my @biblios;
185         for my $bib (@biblionumber) {
186             my $data = GetBiblioData( $bib );
187             push(@biblios,
188                         { biblionumber => $bib,
189                           title        => $data->{'title'},
190                           author       => $data->{'author'},
191                         } );
192         }
193     $template->param(
194            newshelf     => $newshelf,
195            biblios=>\@biblios,
196            multiple     => (scalar(@biblionumber) > 1),
197            total        => scalar(@biblionumber),
198     );
199
200     unless (@biblionumbers) {
201         my ( $bibliocount, @biblios ) = GetBiblio($biblionumber);
202     
203         $template->param
204           (
205            biblionumber      => $biblionumber,
206            title             => $biblios[0]->{'title'},
207            author            => $biblios[0]->{'author'},
208           );
209     } else {
210         my @biblioloop = ();
211         foreach my $biblionumber (@biblionumbers) {
212             my ( $bibliocount, @biblios ) = GetBiblio($biblionumber);
213             my %biblioiter = (
214                               title=>$biblios[0]->{'title'},
215                               author=>$biblios[0]->{'author'}
216                              );
217             push @biblioloop, \%biblioiter;
218         }
219         $template->param
220           (
221            biblioloop => \@biblioloop,
222            biblionumbers => $biblionumbers
223           );
224     }
225     
226 }
227 output_html_with_http_headers $query, $cookie, $template->output;