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