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