Merge remote branch 'kc/master' into new/bug_3013
[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
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 qw/:DEFAULT RefreshShelvesSummary/;
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 C4::Tags qw(get_tags);
35 use Exporter;
36 use Data::Dumper;
37 use C4::Csv;
38
39 use vars qw($debug @EXPORT @ISA $VERSION);
40
41 BEGIN {
42     $VERSION = 1.01;
43     @ISA     = qw(Exporter);
44     @EXPORT  = qw(&shelfpage);
45     $debug   = $ENV{DEBUG} || 0;
46 }
47
48 our %pages = (
49     intranet => { redirect => '/cgi-bin/koha/virtualshelves/shelves.pl', },
50     opac     => { redirect => '/cgi-bin/koha/opac-shelves.pl', },
51 );
52
53 sub shelfpage ($$$$$) {
54     my ( $type, $query, $template, $loggedinuser, $cookie ) = @_;
55     ( $pages{$type} ) or $type = 'opac';
56     $query            or die "No query";
57     $template         or die "No template";
58     $template->param( { loggedinuser => $loggedinuser } );
59     my @paramsloop;
60     my $totitems;
61     my $shelfoff    = ( $query->param('shelfoff') ? $query->param('shelfoff') : 1 );
62     my $itemoff     = ( $query->param('itemoff')  ? $query->param('itemoff')  : 1 );
63     my $displaymode = ( $query->param('display')  ? $query->param('display')  : 'publicshelves' );
64     my ( $shelflimit, $shelfoffset, $shelveslimit, $shelvesoffset );
65     my $marcflavour = C4::Context->preference("marcflavour");
66
67     $shelflimit = ( $type eq 'opac' ? C4::Context->preference('OPACnumSearchResults') : C4::Context->preference('numSearchResults') );
68     $shelflimit = $shelflimit || 20;
69     $shelfoffset   = ( $itemoff - 1 ) * $shelflimit;     # Sets the offset to begin retrieving items at
70     $shelveslimit  = $shelflimit;                        # Limits number of shelves returned for a given query (row_count)
71     $shelvesoffset = ( $shelfoff - 1 ) * $shelflimit;    # 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
76     #Get a list of private shelves for possible deletion. Only do this when we've defaulted to public shelves
77     my ( $privshelflist, $privtotshelves );
78     if ( $category == 2 ) {
79         ( $privshelflist, $privtotshelves ) = GetShelves( 1, $shelveslimit, $shelvesoffset, $loggedinuser );
80     }
81     my $op = $query->param('op');
82
83     #    my $imgdir = getitemtypeimagesrc();
84     #    my $itemtypes = GetItemTypes();
85
86     # the format of this is unindented for ease of diff comparison to the old script
87     # Note: do not mistake the assignment statements below for comparisons!
88
89     if ( $query->param('modifyshelfcontents') ) {
90         my ( $shelfnumber, $barcode, $item, $biblio );
91         if ( $shelfnumber = $query->param('viewshelf') ) {
92             if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' ) ) {
93                 if ( $barcode = $query->param('addbarcode') ) {
94                     $item = GetItem( 0, $barcode );
95                     if (defined $item && $item->{'itemnumber'}){
96                         $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'} );
97                         AddToShelf( $biblio->{'biblionumber'}, $shelfnumber )
98                           or push @paramsloop, { duplicatebiblio => $barcode };
99                     } else {
100                         push @paramsloop, { failgetitem => $barcode };
101                     }
102                 } else {
103                     ( grep { /REM-(\d+)/ } $query->param ) or push @paramsloop, { nobarcode => 1 };
104                     foreach ( $query->param ) {
105                         /REM-(\d+)/ or next;
106                         $debug and warn "SHELVES: user $loggedinuser removing item $1 from shelf $shelfnumber";
107                         DelFromShelf( $1, $shelfnumber );    # $1 is biblionumber
108                     }
109                 }
110             } else {
111                 push @paramsloop, { nopermission => $shelfnumber };
112             }
113         } else {
114             push @paramsloop, { noshelfnumber => 1 };
115         }
116     }
117
118     my $showadd = 1;
119
120     # set the default tab, etc. (for OPAC)
121     my $shelf_type = ( $query->param('display') ? $query->param('display') : 'publicshelves' );
122     if ( defined $shelf_type ) {
123         if ( $shelf_type eq 'privateshelves' ) {
124             $template->param( showprivateshelves => 1 );
125         } elsif ( $shelf_type eq 'publicshelves' ) {
126             $template->param( showpublicshelves => 1 );
127             $showadd = 0;
128         } else {
129             $debug and warn "Invalid 'display' param ($shelf_type)";
130         }
131     } elsif ( $loggedinuser == -1 ) {
132         $template->param( showpublicshelves => 1 );
133     } else {
134         $template->param( showprivateshelves => 1 );
135     }
136
137     my ( $okmanage, $okview );
138     my $shelfnumber = $query->param('shelfnumber') || $query->param('viewshelf');
139     if ($shelfnumber) {
140         $okmanage = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
141         $okview   = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' );
142     }
143
144     my $delflag = 0;
145
146   SWITCH: {
147         if ($op) {
148             unless ($okmanage) {
149                 push @paramsloop, { nopermission => $shelfnumber };
150                 last SWITCH;
151             }
152             if ( $op eq 'modifsave' ) {
153                 my $shelf = {
154                     'shelfname' => $query->param('shelfname'),
155                     'category'  => $query->param('category'),
156                     'sortfield' => $query->param('sortfield'),
157                 };
158
159                 ModShelf( $shelfnumber, $shelf );
160
161             } elsif ( $op eq 'modif' ) {
162                 my ( $shelfnumber2, $shelfname, $owner, $category, $sortfield ) = GetShelf($shelfnumber);
163                 my $member = GetMember( 'borrowernumber' => $owner );
164                 my $ownername = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
165                 $template->param(
166                     edit                => 1,
167                     shelfnumber         => $shelfnumber2,
168                     shelfname           => $shelfname,
169                     owner               => $owner,
170                     ownername           => $ownername,
171                     "category$category" => 1,
172                     category            => $category,
173                     "sort_$sortfield"   => 1,
174                 );
175             }
176             last SWITCH;
177         }
178         if ( $shelfnumber = $query->param('viewshelf') ) {
179             # explicitly fetch this shelf
180             my ($shelfnumber2,$shelfname,$owner,$category,$sorton) = GetShelf($shelfnumber);
181
182             $template->param( 'AllowOnShelfHolds' => C4::Context->preference('AllowOnShelfHolds') );
183             if (C4::Context->preference('TagsEnabled')) {
184                 $template->param(TagsEnabled => 1);
185                     foreach (qw(TagsShowOnList TagsInputOnList)) {
186                     C4::Context->preference($_) and $template->param($_ => 1);
187                 }
188             }
189             #check that the user can view the shelf
190             if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' ) ) {
191                 my $items;
192                 my $authorsort;
193                 my $yearsort;
194                 my $tag_quantity;
195                 my $sortfield = ( $query->param('sortfield') ? $query->param('sortfield') : 'title' );
196                 if ( $sortfield eq 'author' ) {
197                     $authorsort = 'author';
198                 }
199                 if ( $sortfield eq 'year' ) {
200                     $yearsort = 'year';
201                 }
202                 ( $items, $totitems ) = GetShelfContents( $shelfnumber, $shelflimit, $shelfoffset );
203                 for my $this_item (@$items) {
204                     my $record = GetMarcBiblio( $this_item->{'biblionumber'} );
205
206                     # the virtualshelfcontents table does not store these columns nor are they retrieved from the items
207                     # and itemtypes tables, so I'm commenting them out for now to quiet the log -crn
208                     #$this_item->{imageurl} = $imgdir."/".$itemtypes->{ $this_item->{itemtype}  }->{'imageurl'};
209                     #$this_item->{'description'} = $itemtypes->{ $this_item->{itemtype} }->{'description'};
210                     $this_item->{'dateadded'} = format_date( $this_item->{'dateadded'} );
211                     $this_item->{'imageurl'}  = getitemtypeinfo( $this_item->{'itemtype'} )->{'imageurl'};
212                     $this_item->{'coins'}     = GetCOinSBiblio( $this_item->{'biblionumber'} );
213                     $this_item->{'subtitle'} = GetRecordValue('subtitle', $record, GetFrameworkCode($this_item->{'biblionumber'}));
214                     $this_item->{'normalized_upc'}  = GetNormalizedUPC(       $record,$marcflavour);
215                     $this_item->{'normalized_ean'}  = GetNormalizedEAN(       $record,$marcflavour);
216                     $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber($record,$marcflavour);
217                     $this_item->{'normalized_isbn'} = GetNormalizedISBN(undef,$record,$marcflavour);
218                     # Getting items infos for location display
219                     my @items_infos = &GetItemsInfo( $this_item->{'biblionumber'}, $type );
220                     $this_item->{'itemsissued'} = CountItemsIssued( $this_item->{'biblionumber'} );
221                     $this_item->{'ITEM_RESULTS'} = \@items_infos;
222
223                     if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnList')) {
224                         $this_item->{'TagLoop'} = get_tags({
225                             biblionumber=>$this_item->{'biblionumber'}, approved=>1, 'sort'=>'-weight',
226                             limit=>$tag_quantity
227                             });
228                     }
229
230                 }
231                 push @paramsloop, { display => 'privateshelves' } if $category == 1;
232                 $showadd = 1;
233                 my $i = 0;
234                 my $manageshelf = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
235                 $template->param(
236                     shelfname   => $shelfname,
237                     shelfnumber => $shelfnumber,
238                     viewshelf   => $shelfnumber,
239                     authorsort  => $authorsort,
240                     yearsort    => $yearsort,
241                     manageshelf => $manageshelf,
242                     itemsloop   => $items,
243                 );
244             } else {
245                 push @paramsloop, { nopermission => $shelfnumber };
246             }
247             last SWITCH;
248         }
249         if ( $query->param('shelves') ) {
250             my $stay = 1;
251             if ( my $newshelf = $query->param('addshelf') ) {
252
253                 # note: a user can always add a new shelf
254                 my $shelfnumber = AddShelf( $newshelf, $query->param('owner'), $query->param('category'), $query->param('sortfield') );
255                 $stay = 1;
256                 if ( $shelfnumber == -1 ) {    #shelf already exists.
257                     $showadd = 1;
258                     push @paramsloop, { already => $newshelf };
259                     $template->param( shelfnumber => $shelfnumber );
260                 } else {
261                     print $query->redirect( $pages{$type}->{redirect} . "?viewshelf=$shelfnumber" );
262                     exit;
263                 }
264             }
265             foreach ( $query->param() ) {
266                 /DEL-(\d+)/ or next;
267                 $delflag = 1;
268                 my $number = $1;
269                 unless ( defined $shelflist->{$number} || defined $privshelflist->{$number} ) {
270                     push( @paramsloop, { unrecognized => $number } );
271                     last;
272                 }
273                 unless ( ShelfPossibleAction( $loggedinuser, $number, 'manage' ) ) {
274                     push( @paramsloop, { nopermission => $shelfnumber } );
275                     last;
276                 }
277                 my $contents;
278                 ( $contents, $totshelves ) = GetShelfContents( $number, $shelveslimit, $shelvesoffset );
279                 if ( my $count = scalar @$contents ) {
280                     unless ( scalar grep { /^CONFIRM-$number$/ } $query->param() ) {
281                         if ( defined $shelflist->{$number} ) {
282                             push( @paramsloop, { need_confirm => $shelflist->{$number}->{shelfname}, count => $count, single => ($count eq 1 ? 1:0) } );
283                             $shelflist->{$number}->{confirm} = $number;
284                         } else {
285                             push( @paramsloop, { need_confirm => $privshelflist->{$number}->{shelfname}, count => $count } );
286                             $privshelflist->{$number}->{confirm} = $number;
287                         }
288                         $stay = 0;
289                         next;
290                     }
291                 }
292                 my $name;
293                 if ( defined $shelflist->{$number} ) {
294                     $name = $shelflist->{$number}->{'shelfname'};
295                     delete $shelflist->{$number};
296                 } else {
297                     $name = $privshelflist->{$number}->{'shelfname'};
298                     delete $privshelflist->{$number};
299                 }
300                 unless ( DelShelf($number) ) {
301                     push( @paramsloop, { delete_fail => $name } );
302                     last;
303                 }
304                 push( @paramsloop, { delete_ok => $name } );
305
306                 # print $query->redirect($pages{$type}->{redirect}); exit;
307                 $stay = 0;
308             }
309             $showadd = 1;
310             $stay and $template->param( shelves => 1 );
311             last SWITCH;
312         }
313     }
314
315     (@paramsloop) and $template->param( paramsloop => \@paramsloop );
316     $showadd      and $template->param( showadd    => 1 );
317     my @shelvesloop;
318     my @shelveslooppriv;
319     my $numberCanManage = 0;
320
321     # rebuild shelflist in case a shelf has been added
322     ( $shelflist, $totshelves ) = GetShelves( $category, $shelveslimit, $shelvesoffset, $loggedinuser ) unless $delflag;
323     foreach my $element ( sort { lc( $shelflist->{$a}->{'shelfname'} ) cmp lc( $shelflist->{$b}->{'shelfname'} ) } keys %$shelflist ) {
324         my %line;
325         $shelflist->{$element}->{shelf} = $element;
326         my $category  = $shelflist->{$element}->{'category'};
327         my $owner     = $shelflist->{$element}->{'owner'};
328         my $canmanage = ShelfPossibleAction( $loggedinuser, $element, 'manage' );
329         my $sortfield = $shelflist->{$element}->{'sortfield'};
330         if ( $sortfield eq 'author' ) {
331             $shelflist->{$element}->{"authorsort"} = 'author';
332         }
333         if ( $sortfield eq 'year' ) {
334             $shelflist->{$element}->{"yearsort"} = 'year';
335         }
336         $shelflist->{$element}->{"viewcategory$category"} = 1;
337         $shelflist->{$element}->{manageshelf} = $canmanage;
338         if ( $owner eq $loggedinuser or $canmanage ) {
339             $shelflist->{$element}->{'mine'} = 1;
340         }
341         my $member = GetMember( 'borrowernumber' => $owner );
342         $shelflist->{$element}->{ownername} = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
343         $numberCanManage++ if $canmanage;    # possibly outmoded
344         if ( $shelflist->{$element}->{'category'} eq '1' ) {
345             push( @shelveslooppriv, $shelflist->{$element} );
346         } else {
347             push( @shelvesloop, $shelflist->{$element} );
348         }
349     }
350
351     my $url = $type eq 'opac' ? "/cgi-bin/koha/opac-shelves.pl" : "/cgi-bin/koha/virtualshelves/shelves.pl";
352     my %qhash = ();
353     foreach (qw(display viewshelf sortfield)) {
354         $qhash{$_} = $query->param($_) if $query->param($_);
355     }
356     ( scalar keys %qhash ) and $url .= '?' . join '&', map { "$_=$qhash{$_}" } keys %qhash;
357     if ( $query->param('viewshelf') ) {
358         $template->param( { pagination_bar => pagination_bar( $url, ( int( $totitems / $shelflimit ) ) + ( ( $totitems % $shelflimit ) > 0 ? 1 : 0 ), $itemoff, "itemoff" ) } );
359     } else {
360         $template->param(
361             { pagination_bar => pagination_bar( $url, ( int( $totshelves / $shelveslimit ) ) + ( ( $totshelves % $shelveslimit ) > 0 ? 1 : 0 ), $shelfoff, "shelfoff" ) } );
362     }
363     $template->param(
364         shelveslooppriv                                                    => \@shelveslooppriv,
365         shelvesloop                                                        => \@shelvesloop,
366         shelvesloopall                                                     => [ ( @shelvesloop, @shelveslooppriv ) ],
367         numberCanManage                                                    => $numberCanManage,
368         "BiblioDefaultView" . C4::Context->preference("BiblioDefaultView") => 1,
369         csv_profiles                                                       => GetCsvProfilesLoop()
370     );
371     if (   $template->param('viewshelf')
372         or $template->param('shelves')
373         or $template->param('edit') ) {
374         $template->param( vseflag => 1 );
375     }
376     if ($template->param('shelves') or    # note: this part looks duplicative, but is intentional
377         $template->param('edit')
378       ) {
379         $template->param( seflag => 1 );
380     }
381
382     #FIXME: This refresh really only needs to happen when there is a modification of some sort
383     #       to the shelves, but the above code is so convoluted in its handling of the various
384     #       options, it is easier to do this refresh every time C4::VirtualShelves::Page.pm is
385     #       called
386
387     my ( $total, $pubshelves, $barshelves ) = RefreshShelvesSummary( $query->cookie("CGISESSID"), $loggedinuser, ( $loggedinuser == -1 ? 20 : 10 ) );
388
389     if ( defined $barshelves ) {
390         $template->param(
391             barshelves     => scalar( @{ $barshelves->[0] } ),
392             barshelvesloop => $barshelves->[0],
393         );
394         $template->param( bartotal => $total->{'bartotal'}, ) if ( $total->{'bartotal'} > scalar( @{ $barshelves->[0] } ) );
395     }
396
397     if ( defined $pubshelves ) {
398         $template->param(
399             pubshelves     => scalar( @{ $pubshelves->[0] } ),
400             pubshelvesloop => $pubshelves->[0],
401         );
402         $template->param( pubtotal => $total->{'pubtotal'}, ) if ( $total->{'pubtotal'} > scalar( @{ $pubshelves->[0] } ) );
403     }
404
405     output_html_with_http_headers $query, $cookie, $template->output;
406 }
407
408 1;
409 __END__
410
411 =head1 NAME
412
413 VirtualShelves/Page.pm
414
415 =head1 DESCRIPTION
416
417 Module used for both OPAC and intranet pages.
418
419 =head1 CGI PARAMETERS
420
421 =over 4
422
423 =item C<modifyshelfcontents>
424
425 If this script has to modify the shelf content.
426
427 =item C<shelfnumber>
428
429 To know on which shelf to work.
430
431 =item C<addbarcode>
432
433 =item C<op>
434
435  Op can be:
436     * modif: show the template allowing modification of the shelves;
437     * modifsave: save changes from modif mode.
438
439 =item C<viewshelf>
440
441 Load template with 'viewshelves param' displaying the shelf's information.
442
443 =item C<shelves>
444
445 If the param shelves == 1, then add or delete a shelf.
446
447 =item C<addshelf>
448
449 If the param shelves == 1, then addshelf is the name of the shelf to add.
450
451 =back
452
453 =cut