OPAC VirtualShelves overhaul -- bugfix 1413 (among others)
[koha.git] / opac / opac-shelves.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20
21 =head1 NAME
22
23     opac-shelves.pl
24
25 =head1 DESCRIPTION
26
27     this script is used to script to provide virtualshelf management
28
29 =head1 CGI PARAMETERS
30
31 =over 4
32
33 =item C<modifyshelfcontents>
34
35     if this script has to modify the shelf content.
36
37 =item C<shelfnumber>
38
39     to know on which shelf this script has to work.
40
41 =item C<addbarcode>
42
43 =item C<op>
44
45     op can equal the following values:
46         * 'modifsave' to save changes on the shelves
47         * 'modif' to change the template to allow modification of the shelves.
48
49 =item C<viewshelf>
50
51     to load the template with 'viewshelves param' which allows reading the shelves information.
52
53 =item C<shelves>
54
55     if == 1, then call the function shelves to add or delete a shelf.
56
57 =item C<addshelf>
58
59     if the param shelves == 1, then addshelf must be equals to the name of the shelf to add.
60
61 =back
62
63 =cut
64
65 use strict;
66 use warnings;
67 use CGI;
68 use C4::Output;
69 use C4::VirtualShelves;
70 use C4::Circulation;
71 use C4::Auth;
72 use C4::Output;
73 use C4::Biblio;
74
75 use vars qw($debug);
76
77 BEGIN { 
78         $debug = $ENV{DEBUG} || 0;
79 }
80
81 my $query = new CGI;
82
83 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
84     {
85         template_name   => "opac-shelves.tmpl",
86         query           => $query,
87         type            => "opac",
88         authnotrequired => 1,
89     }
90 );
91
92 if ( $query->param('modifyshelfcontents') ) {
93     my $shelfnumber = $query->param('viewshelf');
94     my $barcode     = $query->param('addbarcode');
95     my ($item) = GetItemnumberFromBarcode($barcode);
96     my ($biblio) = GetBiblioFromItemNumber($item->{'itemnumber'});
97     if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' ) ) {
98         AddToShelf( $biblio->{'biblionumber'}, $shelfnumber );
99         foreach ( $query->param ) {
100             /REM-(\d+)/ or next;
101             DelFromShelf( $1, $shelfnumber );   # $1 is biblionumber
102         }
103     }
104 }
105
106 my $showadd = 1;
107 # set the default tab, etc.
108 my $shelf_type = $query->param('display');
109 if (defined $shelf_type) {
110         if ($shelf_type eq 'privateshelves')  {
111                 $template->param(showprivateshelves => 1);
112         } elsif ($shelf_type eq 'publicshelves') {
113                 $template->param(showpublicshelves => 1);
114                 $showadd = 0;
115         } else {
116                 $debug and warn "Invalid 'display' param ($shelf_type)";
117         }
118 } else {
119     $template->param(showprivateshelves => 1);
120 }
121
122 # getting the Shelves list
123 my $shelflist = GetShelves( $loggedinuser, 2 );
124 $template->param( { loggedinuser => $loggedinuser } );
125 my $op = $query->param('op');
126
127 SWITCH: {
128         if ( $op ) {
129                 if ( $op eq 'modifsave' ) {
130                         ModShelf(
131                                 $query->param('shelfnumber'), $query->param('shelfname'),
132                                 $loggedinuser,                $query->param('category'), $query->param('sortfield')
133                         );
134                         $shelflist = GetShelves( $loggedinuser, 2 );    # refresh after mods
135                 } elsif ( $op eq 'modif' ) {
136                         my ( $shelfnumber, $shelfname, $owner, $category, $sortfield ) =GetShelf( $query->param('shelf') );
137                         $template->param(
138                                 edit                => 1,
139                                 shelfnumber         => $shelfnumber,
140                                 shelfname           => $shelfname,
141                                 "category$category" => 1,
142                                 "sort_$sortfield"   => 1,
143                         );
144                 }
145                 last SWITCH;
146         }
147         if ( $query->param('viewshelf') ) {
148         #check that the user can view the shelf
149         my $shelfnumber = $query->param('viewshelf');
150         if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' ) ) {
151             my $items = GetShelfContents($shelfnumber);
152                         $showadd = 1;
153             $template->param(
154                 shelfname   => $shelflist->{$shelfnumber}->{'shelfname'},
155                 shelfnumber => $shelfnumber,
156                 viewshelf   => $query->param('viewshelf'),
157                 manageshelf => &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' ),
158                 itemsloop   => $items,
159             );
160         } # else {;}  # FIXME - some kind of warning *may* be in order
161         last SWITCH;
162     }
163     if ( $query->param('shelves') ) {
164         if ( my $newshelf = $query->param('addshelf') ) {
165             my $shelfnumber = AddShelf(
166                 $newshelf,
167                 $query->param('owner'),
168                 $query->param('category')
169             );
170
171             if ( $shelfnumber == -1 ) {    #shelf already exists.
172                                 $showadd = 1;
173                 $template->param(
174                         shelfnumber => $shelfnumber,
175                         already     => $newshelf,
176                 );
177             } else {
178                 print $query->redirect("/cgi-bin/koha/opac-shelves.pl?viewshelf=$shelfnumber");
179                                 exit;           # can't redirect AND expect %line to DO anything!
180                         }
181         }
182         my @paramsloop;
183         foreach ( $query->param() ) {
184                         /^DEL-(\d+)/ or next;
185                         my %line;
186                         ( $line{status}, $line{count} ) = DelShelf($1);
187                         (defined $shelflist->{$1}) and delete $shelflist->{$1};
188                         # print $query->redirect("/cgi-bin/koha/opac-shelves.pl"); exit;
189                         # can't redirect and expect %line to DO anything!
190                         push( @paramsloop, \%line );
191         }
192                 $showadd = 1;
193         $template->param( 
194                         paramsloop => \@paramsloop,
195             shelves    => 1,
196         );
197         last SWITCH;
198     }
199 }
200
201 # rebuilding shelflist (in case a shelf has been added) is not necessary since add redirects!
202
203 $showadd and $template->param(showadd => 1);
204 my $color = 0;
205 my @shelvesloop;
206 my @shelveslooppriv;
207
208 foreach my $element (sort { lc($shelflist->{$a}->{'shelfname'}) cmp lc($shelflist->{$b}->{'shelfname'}) } keys %$shelflist) {
209         my %line;
210         $color = ($color) ? 0 : 1;
211         $line{'toggle'} = $color;
212         $line{'shelf'} = $element;
213         $line{'shelfname'} = $shelflist->{$element}->{'shelfname'};
214         $line{'sortfield'} = $shelflist->{$element}->{'sortfield'};
215         $line{"category".$shelflist->{$element}->{'category'}} = 1;
216         $line{'shelfvirtualcount'} = $shelflist->{$element}->{'count'};
217         $line{'canmanage'} = ShelfPossibleAction($loggedinuser,$element,'manage');
218         if ($shelflist->{$element}->{'owner'} eq $loggedinuser) {
219                 $line{'mine'} = 1;
220         } else {
221                 $line{'firstname'} = $shelflist->{$element}->{'firstname'};
222                 $line{ 'surname' } = $shelflist->{$element}->{ 'surname' };
223         }
224         if ($shelflist->{$element}->{'category'} eq 2) {
225                 push (@shelvesloop,     \%line);
226         } elsif ($shelflist->{$element}->{'category'} eq 1) {
227         push (@shelveslooppriv, \%line);
228     }
229 }
230
231 $template->param(
232     shelveslooppriv => \@shelveslooppriv,
233     shelvesloop     => \@shelvesloop,
234     "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
235 );
236
237 output_html_with_http_headers $query, $cookie, $template->output;