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