Shelves wrap up - bugfix 1405, opac checkbox js fixed
[koha.git] / C4 / VirtualShelves / Page.pm
1 package C4::VirtualShelves::Page;
2
3 #
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 # perldoc at the end of the file, per convention.
22
23 use strict;
24 use warnings;
25 use CGI;
26 use C4::VirtualShelves;
27 use C4::Biblio;
28 use C4::Items;
29 use C4::Auth;
30 use C4::Output;
31 use Exporter;
32
33 use vars qw($debug @EXPORT @ISA $VERSION);
34
35 BEGIN {
36         $VERSION = 1.01;
37         @ISA = qw(Exporter);
38         @EXPORT = qw(&shelfpage);
39     $debug = $ENV{DEBUG} || 0;
40 }
41
42 our %pages = (
43         intranet => {
44                 redirect=>'/cgi-bin/koha/virtualshelves/shelves.pl',
45         },
46         opac => {
47                 redirect=>'/cgi-bin/koha/opac-shelves.pl',
48         },
49 );
50
51 sub shelfpage ($$$$$) {
52         my ($type, $query, $template, $loggedinuser, $cookie ) = @_;
53         ($pages{$type}) or $type = 'opac';
54         $query or die "No query";
55         $template or die "No template";
56         $template->param( { loggedinuser => $loggedinuser } );
57         my @paramsloop;
58         # getting the Shelves list
59         my $shelflist = GetShelves( $loggedinuser, 2 );
60         my $op = $query->param('op');
61
62 # the format of this is unindented for ease of diff comparison to the old script
63 # Note: do not mistake the assignment statements below for comparisons!
64
65 if ( $query->param('modifyshelfcontents') ) {
66         my ($shelfnumber,$barcode,$item,$biblio);
67     if ($shelfnumber = $query->param('viewshelf')) {
68         if (ShelfPossibleAction($loggedinuser, $shelfnumber, 'manage')) {
69                 if ($barcode = $query->param('addbarcode')) {
70                         if ($item = GetItem( 0, $barcode )) {
71                                 $biblio = GetBiblioFromItemNumber($item->{'itemnumber'});
72                                 AddToShelf($biblio->{'biblionumber'}, $shelfnumber) or 
73                                                 push @paramsloop, {duplicatebiblio=>$barcode};
74                                 } else { push @paramsloop, {failgetitem=>$barcode}; }
75                 } else { 
76                                 (grep {/REM-(\d+)/} $query->param) or push @paramsloop, {nobarcode=>1};
77                         foreach ($query->param) {
78                                         /REM-(\d+)/ or next;
79                                         $debug and warn 
80                                                 "SHELVES: user $loggedinuser removing item $1 from shelf $shelfnumber";
81                                         DelFromShelf($1, $shelfnumber);  # $1 is biblionumber
82                                 }
83                         }
84                 } else { push @paramsloop, {nopermission=>$shelfnumber}; }
85     } else { push @paramsloop, {noshelfnumber=>1}; }
86 }
87
88 my $showadd = 1;
89 # set the default tab, etc. (for OPAC)
90 my $shelf_type = $query->param('display');
91 if (defined $shelf_type) {
92         if ($shelf_type eq 'privateshelves')  {
93                 $template->param(showprivateshelves => 1);
94         } elsif ($shelf_type eq 'publicshelves') {
95                 $template->param(showpublicshelves => 1);
96                 $showadd = 0;
97         } else {
98                 $debug and warn "Invalid 'display' param ($shelf_type)";
99         }
100 } else {
101         $template->param(showprivateshelves => 1);
102 }
103
104 my($okmanage, $okview);
105 my $shelfnumber = $query->param('shelfnumber') || $query->param('viewshelf');
106 if ($shelfnumber) {
107         $okmanage = &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
108         $okview   = &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' );
109 }
110
111 SWITCH: {
112         if ( $op ) {
113                 unless ($okmanage) {
114                         push @paramsloop, {nopermission=>$shelfnumber};
115                         last SWITCH;
116                 }
117                 if ( $op eq 'modifsave' ) {
118                         ModShelf(
119                                 $shelfnumber, $query->param('shelfname'), $loggedinuser,
120                                 $query->param('category'), $query->param('sortfield')
121                         );
122                         $shelflist = GetShelves( $loggedinuser, 2 );    # refresh after mods
123                 } elsif ( $op eq 'modif' ) {
124                         my ( $shelfnumber2, $shelfname, $owner, $category, $sortfield ) =GetShelf( $query->param('shelf') );
125                         $template->param(
126                                 edit                => 1,
127                                 shelfnumber         => $shelfnumber2,
128                                 shelfname           => $shelfname,
129                                 owner               => $owner,
130                                 "category$category" => 1,
131                                 "sort_$sortfield"   => 1,
132                         );
133                 }
134                 last SWITCH;
135         }
136     if ($shelfnumber = $query->param('viewshelf') ) {
137         #check that the user can view the shelf
138         if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' ) ) {
139             my $items = GetShelfContents($shelfnumber);
140                         $showadd = 1;
141                         my $i = 0;
142                         foreach (grep {$i++ % 2} @$items) {     # every other item
143                                 $_->{toggle} = 1;
144                         }
145                         # my $manageshelf = &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
146                         # ($manageshelf) and $showadd = 1;
147             $template->param(
148                 shelfname   => $shelflist->{$shelfnumber}->{'shelfname'},
149                 shelfnumber => $shelfnumber,
150                 viewshelf   => $shelfnumber,
151                 manageshelf => &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' ),
152                 itemsloop => $items,
153             );
154         } else { push @paramsloop, {nopermission=>$shelfnumber}; }
155         last SWITCH;
156     }
157     if ( $query->param('shelves') ) {
158                 my $stay = 0;
159         if (my $newshelf = $query->param('addshelf')) {
160                         # note: a user can always add a new shelf
161             my $shelfnumber = AddShelf(
162                 $newshelf,
163                 $query->param('owner'),
164                 $query->param('category')
165             );
166                         $stay = 1;
167             if ( $shelfnumber == -1 ) {    #shelf already exists.
168                                 $showadd = 1;
169                                 push @paramsloop, { already => $newshelf };
170                 $template->param(shelfnumber => $shelfnumber);
171             } else {
172                 print $query->redirect($pages{$type}->{redirect} . "?viewshelf=$shelfnumber");
173                 exit;
174                         }
175         }
176                 foreach ($query->param()) {
177                         /DEL-(\d+)/ or next;
178                         my $number = $1;
179                         unless (defined $shelflist->{$number}) {
180                                 push(@paramsloop, {unrecognized=>$number}); last;
181                         }
182                         unless (ShelfPossibleAction($loggedinuser, $number, 'manage')) {
183                                 push(@paramsloop, {nopermission=>$shelfnumber}); last;
184                         }
185                         my $contents = GetShelfContents($number);
186                         if (my $count = scalar @$contents){
187                                 unless (scalar grep {/^CONFIRM-$number$/} $query->param()) {
188                                         push(@paramsloop, {need_confirm=>$shelflist->{$number}->{shelfname}, count=>$count});
189                                         $shelflist->{$number}->{confirm} = $number;
190                                         next;
191                                 }
192                         } 
193                         my $name = $shelflist->{$number}->{'shelfname'};
194                         unless (DelShelf($number)) {
195                                 push(@paramsloop, {delete_fail=>$name}); last;
196                         }
197                         delete $shelflist->{$number};
198                         push(@paramsloop, {delete_ok=>$name});
199                         # print $query->redirect($pages{$type}->{redirect}); exit;
200                 }
201                 $showadd = 1;
202                 $stay and $template->param(shelves => 1);
203                 last SWITCH;
204         }
205 }
206
207 (@paramsloop) and $template->param(paramsloop => \@paramsloop);
208 # rebuild shelflist in case a shelf has been added
209 # $shelflist = GetShelves( $loggedinuser, 2 );
210 $showadd and $template->param(showadd => 1);
211 my $i = 0;
212 my @shelvesloop;
213 my @shelveslooppriv;
214 my $numberCanManage = 0;
215
216 foreach my $element (sort { lc($shelflist->{$a}->{'shelfname'}) cmp lc($shelflist->{$b}->{'shelfname'}) } keys %$shelflist) {
217         my %line;
218         (++$i % 2) and $shelflist->{$element}->{toggle} = 1; # $line{'toggle'} = $i;
219         $shelflist->{$element}->{shelf} = $element;
220         my $category = $shelflist->{$element}->{'category'};
221         my $canmanage = ShelfPossibleAction( $loggedinuser, $element, 'manage' );
222         $shelflist->{$element}->{"viewcategory$category"} = 1;
223         $shelflist->{$element}->{canmanage} = $canmanage;
224         if ($shelflist->{$element}->{'owner'} eq $loggedinuser) {
225                 $shelflist->{$element}->{'mine'} = 1;
226         }
227         $numberCanManage++ if $canmanage;
228         if ($shelflist->{$element}->{'category'} eq '1') {
229                 push (@shelveslooppriv, $shelflist->{$element});
230         } else {
231                 push (@shelvesloop, $shelflist->{$element});
232         }
233 }
234
235 $template->param(
236     shelveslooppriv => \@shelveslooppriv,
237     shelvesloop     => \@shelvesloop,
238     shelvesloopall  => [(@shelvesloop, @shelveslooppriv)],
239     numberCanManage => $numberCanManage,
240         "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
241 );
242 if ($template->param('viewshelf') or
243         $template->param( 'shelves' ) or
244         $template->param(  'edit'   ) ) {
245         $template->param(vseflag => 1);
246 }
247 if ($template->param( 'shelves' ) or
248         $template->param(  'edit'   ) ) {
249         $template->param( seflag => 1);
250 }
251
252 output_html_with_http_headers $query, $cookie, $template->output;
253 }       
254
255 1;
256 __END__
257
258 =head1 NAME
259
260     VirtualShelves/Page.pm
261
262 =head1 DESCRIPTION
263
264     Module used for both OPAC and intranet pages.
265
266 =head1 CGI PARAMETERS
267
268 =over 4
269
270 =item C<modifyshelfcontents>
271
272     If this script has to modify the shelf content.
273
274 =item C<shelfnumber>
275
276     To know on which shelf to work.
277
278 =item C<addbarcode>
279
280 =item C<op>
281
282     Op can be:
283         * modif: show the template allowing modification of the shelves;
284         * modifsave: save changes from modif mode.
285
286 =item C<viewshelf>
287
288     Load template with 'viewshelves param' displaying the shelf's information.
289
290 =item C<shelves>
291
292     If the param shelves == 1, then add or delete a shelf.
293
294 =item C<addshelf>
295
296     If the param shelves == 1, then addshelf is the name of the shelf to add.
297
298 =back
299
300 =cut