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