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