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