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