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