kohabug 2392 Changing array dereferencing syntax
[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::Koha;
30 use C4::Auth qw/get_session/;
31 use C4::Members;
32 use C4::Output;
33 use C4::Dates qw/format_date/;
34 use Exporter;
35 use Data::Dumper;
36
37 use vars qw($debug @EXPORT @ISA $VERSION);
38
39 BEGIN {
40         $VERSION = 1.01;
41         @ISA = qw(Exporter);
42         @EXPORT = qw(&shelfpage);
43     $debug = $ENV{DEBUG} || 0;
44 }
45
46 our %pages = (
47         intranet => {
48                 redirect=>'/cgi-bin/koha/virtualshelves/shelves.pl',
49         },
50         opac => {
51                 redirect=>'/cgi-bin/koha/opac-shelves.pl',
52         },
53 );
54
55 sub shelfpage ($$$$$) {
56         my ($type, $query, $template, $loggedinuser, $cookie ) = @_;
57         ($pages{$type}) or $type = 'opac';
58         $query or die "No query";
59         $template or die "No template";
60         $template->param( { loggedinuser => $loggedinuser } );
61         my @paramsloop;
62         my $totitems;
63         my $shelfoff = ($query->param('shelfoff') ? $query->param('shelfoff') : 1);
64         my $itemoff = ($query->param('itemoff') ? $query->param('itemoff') : 1);
65         my $displaymode = ($query->param('display') ? $query->param('display') : 'publicshelves');
66         my ($shelflimit, $shelfoffset, $shelveslimit, $shelvesoffset);
67         # FIXME: These limits should not be hardcoded...
68         $shelflimit = 20;       # Limits number of items returned for a given query
69         $shelfoffset = (($itemoff == 1) ? 0 : ($itemoff * 10));         # Sets the offset to begin retrieving items at
70         $shelveslimit = 20;     # Limits number of shelves returned for a given query (row_count)
71         $shelvesoffset = (($shelfoff == 1) ? 0 : ($shelfoff * 10));             # Sets the offset to begin retrieving shelves at (offset)
72         # getting the Shelves list
73         my $category = (($displaymode eq 'privateshelves') ? 1 : 2);
74         my ($shelflist, $totshelves) = GetShelves( $category, $shelveslimit, $shelvesoffset, $loggedinuser );
75         my $op = $query->param('op');
76 #    my $imgdir = getitemtypeimagesrc();
77 #    my $itemtypes = GetItemTypes();
78     
79 # the format of this is unindented for ease of diff comparison to the old script
80 # Note: do not mistake the assignment statements below for comparisons!
81
82 if ( $query->param('modifyshelfcontents') ) {
83         my ($shelfnumber,$barcode,$item,$biblio);
84     if ($shelfnumber = $query->param('viewshelf')) {
85         if (ShelfPossibleAction($loggedinuser, $shelfnumber, 'manage')) {
86                 if ($barcode = $query->param('addbarcode')) {
87                         if ($item = GetItem( 0, $barcode )) {
88                                 $biblio = GetBiblioFromItemNumber($item->{'itemnumber'});
89                                 AddToShelf($biblio->{'biblionumber'}, $shelfnumber) or 
90                                                 push @paramsloop, {duplicatebiblio=>$barcode};
91                                 } else { push @paramsloop, {failgetitem=>$barcode}; }
92                 } else { 
93                                 (grep {/REM-(\d+)/} $query->param) or push @paramsloop, {nobarcode=>1};
94                         foreach ($query->param) {
95                                         /REM-(\d+)/ or next;
96                                         $debug and warn 
97                                                 "SHELVES: user $loggedinuser removing item $1 from shelf $shelfnumber";
98                                         DelFromShelf($1, $shelfnumber);  # $1 is biblionumber
99                                 }
100                         }
101                 } else { push @paramsloop, {nopermission=>$shelfnumber}; }
102     } else { push @paramsloop, {noshelfnumber=>1}; }
103 }
104
105 my $showadd = 1;
106 # set the default tab, etc. (for OPAC)
107 my $shelf_type = ($query->param('display') ? $query->param('display') : 'publicshelves');
108 if (defined $shelf_type) {
109         if ($shelf_type eq 'privateshelves')  {
110                 $template->param(showprivateshelves => 1);
111         } elsif ($shelf_type eq 'publicshelves') {
112                 $template->param(showpublicshelves => 1);
113                 $showadd = 0;
114         } else {
115                 $debug and warn "Invalid 'display' param ($shelf_type)";
116         }
117 } elsif ($loggedinuser == -1) {
118         $template->param(showpublicshelves => 1);
119 } else {
120         $template->param(showprivateshelves => 1);
121 }
122
123 my($okmanage, $okview);
124 my $shelfnumber = $query->param('shelfnumber') || $query->param('viewshelf');
125 if ($shelfnumber) {
126         $okmanage = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
127         $okview   = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' );
128 }
129
130 my $delflag = 0;
131
132 SWITCH: {
133         if ( $op ) {
134                 unless ($okmanage) {
135                         push @paramsloop, {nopermission=>$shelfnumber};
136                         last SWITCH;
137                 }
138                 if ( $op eq 'modifsave' ) {
139                         my $shelf = {
140                         'shelfname'             => $query->param('shelfname'),
141                                 'category'              => $query->param('category'),
142                                 'sortfield'             => $query->param('sortfield'),
143                         };
144
145                         ModShelf( $shelfnumber, $shelf );
146
147                 } elsif ( $op eq 'modif' ) {
148                         my ( $shelfnumber2, $shelfname, $owner, $category, $sortfield ) =GetShelf( $shelfnumber );
149                         my $member = GetMember($owner,'borrowernumber');
150                         my $ownername = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
151                         $template->param(
152                                 edit                => 1,
153                                 shelfnumber         => $shelfnumber2,
154                                 shelfname           => $shelfname,
155                                 owner               => $owner,
156                                 ownername                       => $ownername,
157                                 "category$category"     => 1,
158                                 category                        => $category,
159                                 "sort_$sortfield"   => 1,
160                         );
161                 }
162                 last SWITCH;
163         }
164     if ($shelfnumber = $query->param('viewshelf') ) {
165         #check that the user can view the shelf
166                 if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' ) ) {
167                         my $items;
168                         ($items, $totitems) = GetShelfContents($shelfnumber, $shelflimit, $shelfoffset);
169                         for my $this_item (@$items) {
170                                 # the virtualshelfcontents table does not store these columns nor are they retrieved from the items
171                                 # and itemtypes tables, so I'm commenting them out for now to quiet the log -crn
172                                 #$this_item->{imageurl} = $imgdir."/".$itemtypes->{ $this_item->{itemtype}  }->{'imageurl'};
173                                 #$this_item->{'description'} = $itemtypes->{ $this_item->{itemtype} }->{'description'};
174                                 $this_item->{'dateadded'} = format_date($this_item->{'dateadded'});
175                         }
176                         $showadd = 1;
177                         my $i = 0;
178                         foreach (grep {$i++ % 2} @$items) {     # every other item
179                                 $_->{toggle} = 1;
180                         }
181                         my $manageshelf = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
182                         $template->param(
183                                 shelfname   => $shelflist->{$shelfnumber}->{'shelfname'},
184                                 shelfnumber => $shelfnumber,
185                                 viewshelf   => $shelfnumber,
186                                 manageshelf => $manageshelf,
187                                 itemsloop => $items,
188                         );
189                 } else { push @paramsloop, {nopermission=>$shelfnumber} };
190         last SWITCH;
191     }
192     if ( $query->param('shelves') ) {
193                 my $stay = 1;
194         if (my $newshelf = $query->param('addshelf')) {
195                         # note: a user can always add a new shelf
196             my $shelfnumber = AddShelf(
197                 $newshelf,
198                 $query->param('owner'),
199                 $query->param('category'),
200                 $query->param('sortfield')
201             );
202                         $stay = 1;
203             if ( $shelfnumber == -1 ) {    #shelf already exists.
204                                 $showadd = 1;
205                                 push @paramsloop, { already => $newshelf };
206                 $template->param(shelfnumber => $shelfnumber);
207             } else {
208                 print $query->redirect($pages{$type}->{redirect} . "?viewshelf=$shelfnumber");
209                 exit;
210                         }
211         }
212                 foreach ($query->param()) {
213                         /DEL-(\d+)/ or next;
214                         $delflag = 1;
215                         my $number = $1;
216                         unless (defined $shelflist->{$number}) {
217                                 push(@paramsloop, {unrecognized=>$number}); last;
218                         }
219                         unless (ShelfPossibleAction($loggedinuser, $number, 'manage')) {
220                                 push(@paramsloop, {nopermission=>$shelfnumber}); last;
221                         }
222                         my $contents;
223                         ($contents, $totshelves) = GetShelfContents($number, $shelveslimit, $shelvesoffset);
224                         if (my $count = scalar @$contents){
225                                 unless (scalar grep {/^CONFIRM-$number$/} $query->param()) {
226                                         push(@paramsloop, {need_confirm=>$shelflist->{$number}->{shelfname}, count=>$count});
227                                         $shelflist->{$number}->{confirm} = $number;
228                                         $stay = 0;
229                                         next;
230                                 }
231                         } 
232                         my $name = $shelflist->{$number}->{'shelfname'};
233                         unless (DelShelf($number)) {
234                                 push(@paramsloop, {delete_fail=>$name}); last;
235                         }
236                         delete $shelflist->{$number};
237                         push(@paramsloop, {delete_ok=>$name});
238                         # print $query->redirect($pages{$type}->{redirect}); exit;
239                         $stay = 0;
240                 }
241                 $showadd = 1;
242                 $stay and $template->param(shelves => 1);
243                 last SWITCH;
244         }
245 }
246
247 (@paramsloop) and $template->param(paramsloop => \@paramsloop);
248 $showadd and $template->param(showadd => 1);
249 my @shelvesloop;
250 my @shelveslooppriv;
251 my $numberCanManage = 0;
252
253 # rebuild shelflist in case a shelf has been added
254 ($shelflist, $totshelves) = GetShelves( $category, $shelveslimit, $shelvesoffset, $loggedinuser ) unless $delflag;
255 foreach my $element (sort { lc($shelflist->{$a}->{'shelfname'}) cmp lc($shelflist->{$b}->{'shelfname'}) } keys %$shelflist) {
256         my %line;
257         $shelflist->{$element}->{shelf} = $element;
258         my $category = $shelflist->{$element}->{'category'};
259         my $owner    = $shelflist->{$element}->{ 'owner'  };
260         my $canmanage = ShelfPossibleAction( $loggedinuser, $element, 'manage' );
261         $shelflist->{$element}->{"viewcategory$category"} = 1;
262         $shelflist->{$element}->{manageshelf} = $canmanage;
263         if ($owner eq $loggedinuser or $canmanage) {
264                 $shelflist->{$element}->{'mine'} = 1;
265         } 
266         my $member = GetMember($owner,'borrowernumber');
267         $shelflist->{$element}->{ownername} = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
268         $numberCanManage++ if $canmanage;       # possibly outmoded
269         if ($shelflist->{$element}->{'category'} eq '1') {
270                 (scalar(@shelveslooppriv) % 2) and $shelflist->{$element}->{toggle} = 1;
271                 push (@shelveslooppriv, $shelflist->{$element});
272         } else {
273                 (scalar(@shelvesloop)     % 2) and $shelflist->{$element}->{toggle} = 1;
274                 push (@shelvesloop, $shelflist->{$element});
275         }
276 }
277 my $url = $type eq 'opac' ? "/cgi-bin/koha/opac-shelves.pl" : "/cgi-bin/koha/virtualshelves/shelves.pl";
278 $url .= "?display=" . $query->param('display') if $query->param('display');
279 $url .= "?viewshelf=" . $query->param('viewshelf') if $query->param('viewshelf');
280 if ($query->param('viewshelf')) {
281         $template->param( {pagination_bar => pagination_bar($url, (int($totitems/$shelflimit)) + (($totitems % $shelflimit) > 0 ? 1 : 0), $itemoff, "itemoff")} );
282 } else {
283         $template->param( {pagination_bar => pagination_bar($url, (int($totshelves/$shelveslimit)) + (($totshelves % $shelveslimit) > 0 ? 1 : 0), $shelfoff, "shelfoff")} );
284 }
285 $template->param(
286     shelveslooppriv => \@shelveslooppriv,
287     shelvesloop     => \@shelvesloop,
288     shelvesloopall  => [(@shelvesloop, @shelveslooppriv)],
289     numberCanManage => $numberCanManage,
290         "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
291 );
292 if ($template->param('viewshelf') or
293         $template->param( 'shelves' ) or
294         $template->param(  'edit'   ) ) {
295         $template->param(vseflag => 1);
296 }
297 if ($template->param( 'shelves' ) or
298         $template->param(  'edit'   ) ) {
299         $template->param( seflag => 1);
300 }
301
302 #FIXME: This refresh really only needs to happen when there is a modification of some sort
303 #               to the shelves, but the above code is so convoluted in its handling of the various
304 #               options, it is easier to do this refresh every time C4::VirtualShelves::Page.pm is
305 #               called
306
307 my ($total, $pubshelves, $barshelves) = RefreshShelvesSummary($query->cookie("CGISESSID"),$loggedinuser,($loggedinuser == -1 ? 20 : 10));
308
309 if (defined $barshelves) {
310         $template->param(       barshelves      => scalar (@{$barshelves->[0]}),
311                                                 barshelvesloop  => $barshelves->[0],
312                                         );
313         $template->param(       bartotal                => $total->{'bartotal'}, ) if ($total->{'bartotal'} > scalar (@{$barshelves->[0]}));
314 }
315
316 if (defined $pubshelves) {
317         $template->param(       pubshelves      => scalar (@{$pubshelves->[0]}),
318                                                 pubshelvesloop  => $pubshelves->[0],
319                                         );
320         $template->param(       pubtotal                => $total->{'pubtotal'}, ) if ($total->{'pubtotal'} > scalar (@{$pubshelves->[0]}));
321 }
322
323 output_html_with_http_headers $query, $cookie, $template->output;
324 }       
325
326 1;
327 __END__
328
329 =head1 NAME
330
331     VirtualShelves/Page.pm
332
333 =head1 DESCRIPTION
334
335     Module used for both OPAC and intranet pages.
336
337 =head1 CGI PARAMETERS
338
339 =over 4
340
341 =item C<modifyshelfcontents>
342
343     If this script has to modify the shelf content.
344
345 =item C<shelfnumber>
346
347     To know on which shelf to work.
348
349 =item C<addbarcode>
350
351 =item C<op>
352
353     Op can be:
354         * modif: show the template allowing modification of the shelves;
355         * modifsave: save changes from modif mode.
356
357 =item C<viewshelf>
358
359     Load template with 'viewshelves param' displaying the shelf's information.
360
361 =item C<shelves>
362
363     If the param shelves == 1, then add or delete a shelf.
364
365 =item C<addshelf>
366
367     If the param shelves == 1, then addshelf is the name of the shelf to add.
368
369 =back
370
371 =cut