Merge commit 'biblibre/3.2_biblibre' into to-push
[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                                 # Getting items infos for location display
196                                 my @items_infos = &GetItemsInfo($this_item->{'biblionumber'}, $type);
197                                 $this_item->{'ITEM_RESULTS'} = \@items_infos;
198
199                         }
200                         push @paramsloop, {display => 'privateshelves'} if $category == 1;
201                         $showadd = 1;
202                         my $i = 0;
203                         my $manageshelf = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
204                         $template->param(
205                                 shelfname   => $shelflist->{$shelfnumber}->{'shelfname'} || $privshelflist->{$shelfnumber}->{'shelfname'},
206                                 shelfnumber => $shelfnumber,
207                                 viewshelf   => $shelfnumber,
208                                 authorsort   => $authorsort,
209                                 yearsort => $yearsort,
210                                 manageshelf => $manageshelf,
211                                 itemsloop => $items,
212                         );
213                 } else { push @paramsloop, {nopermission=>$shelfnumber} };
214         last SWITCH;
215     }
216     if ( $query->param('shelves') ) {
217                 my $stay = 1;
218         if (my $newshelf = $query->param('addshelf')) {
219                         # note: a user can always add a new shelf
220             my $shelfnumber = AddShelf(
221                 $newshelf,
222                 $query->param('owner'),
223                 $query->param('category'),
224                 $query->param('sortfield')
225             );
226                         $stay = 1;
227             if ( $shelfnumber == -1 ) {    #shelf already exists.
228                                 $showadd = 1;
229                                 push @paramsloop, { already => $newshelf };
230                 $template->param(shelfnumber => $shelfnumber);
231             } else {
232                 print $query->redirect($pages{$type}->{redirect} . "?viewshelf=$shelfnumber");
233                 exit;
234                         }
235         }
236                 foreach ($query->param()) {
237                         /DEL-(\d+)/ or next;
238                         $delflag = 1;
239                         my $number = $1;
240                         unless (defined $shelflist->{$number} || defined $privshelflist->{$number}) {
241                                 push(@paramsloop, {unrecognized=>$number}); last;
242                         }
243                         unless (ShelfPossibleAction($loggedinuser, $number, 'manage')) {
244                                 push(@paramsloop, {nopermission=>$shelfnumber}); last;
245                         }
246                         my $contents;
247                         ($contents, $totshelves) = GetShelfContents($number, $shelveslimit, $shelvesoffset);
248                         if (my $count = scalar @$contents){
249                                 unless (scalar grep {/^CONFIRM-$number$/} $query->param()) {
250                                         if (defined $shelflist->{$number}) {
251                                                 push(@paramsloop, {need_confirm=>$shelflist->{$number}->{shelfname}, count=>$count});
252                                                 $shelflist->{$number}->{confirm} = $number;
253                                         } else {
254                                                 push(@paramsloop, {need_confirm=>$privshelflist->{$number}->{shelfname}, count=>$count});
255                                                 $privshelflist->{$number}->{confirm} = $number;
256                                         }
257                                         $stay = 0;
258                                         next;
259                                 }
260                         } 
261                         my $name;
262                         if (defined $shelflist->{$number}) {
263                                 $name = $shelflist->{$number}->{'shelfname'};
264                                 delete $shelflist->{$number};
265                         } else {
266                                 $name = $privshelflist->{$number}->{'shelfname'};
267                                 delete $privshelflist->{$number};
268                         }
269                         unless (DelShelf($number)) {
270                                 push(@paramsloop, {delete_fail=>$name}); last;
271                         }
272                         push(@paramsloop, {delete_ok=>$name});
273                         # print $query->redirect($pages{$type}->{redirect}); exit;
274                         $stay = 0;
275                 }
276                 $showadd = 1;
277                 $stay and $template->param(shelves => 1);
278                 last SWITCH;
279         }
280 }
281
282 (@paramsloop) and $template->param(paramsloop => \@paramsloop);
283 $showadd and $template->param(showadd => 1);
284 my @shelvesloop;
285 my @shelveslooppriv;
286 my $numberCanManage = 0;
287
288 # rebuild shelflist in case a shelf has been added
289 ($shelflist, $totshelves) = GetShelves( $category, $shelveslimit, $shelvesoffset, $loggedinuser ) unless $delflag;
290 foreach my $element (sort { lc($shelflist->{$a}->{'shelfname'}) cmp lc($shelflist->{$b}->{'shelfname'}) } keys %$shelflist) {
291         my %line;
292         $shelflist->{$element}->{shelf} = $element;
293         my $category = $shelflist->{$element}->{'category'};
294         my $owner    = $shelflist->{$element}->{ 'owner'  };
295         my $canmanage = ShelfPossibleAction( $loggedinuser, $element, 'manage' );
296         $shelflist->{$element}->{"viewcategory$category"} = 1;
297         $shelflist->{$element}->{manageshelf} = $canmanage;
298         if ($owner eq $loggedinuser or $canmanage) {
299                 $shelflist->{$element}->{'mine'} = 1;
300         } 
301         my $member = GetMember('borrowernumber' => $owner);
302         $shelflist->{$element}->{ownername} = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
303         $numberCanManage++ if $canmanage;       # possibly outmoded
304         if ($shelflist->{$element}->{'category'} eq '1') {
305                 push (@shelveslooppriv, $shelflist->{$element});
306         } else {
307                 push (@shelvesloop, $shelflist->{$element});
308         }
309 }
310
311 my $url = $type eq 'opac' ? "/cgi-bin/koha/opac-shelves.pl" : "/cgi-bin/koha/virtualshelves/shelves.pl";
312 my %qhash = ();
313 foreach (qw(display viewshelf sortfield)) {
314     $qhash{$_} = $query->param($_) if $query->param($_);
315 }
316 (scalar keys %qhash) and $url .= '?' . join '&', map {"$_=$qhash{$_}"} keys %qhash;
317 if ($query->param('viewshelf')) {
318         $template->param( {pagination_bar => pagination_bar($url, (int($totitems/$shelflimit)) + (($totitems % $shelflimit) > 0 ? 1 : 0), $itemoff, "itemoff")} );
319 } else {
320         $template->param( {pagination_bar => pagination_bar($url, (int($totshelves/$shelveslimit)) + (($totshelves % $shelveslimit) > 0 ? 1 : 0), $shelfoff, "shelfoff")} );
321 }
322 $template->param(
323     shelveslooppriv => \@shelveslooppriv,
324     shelvesloop     => \@shelvesloop,
325     shelvesloopall  => [(@shelvesloop, @shelveslooppriv)],
326     numberCanManage => $numberCanManage,
327         "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
328     csv_profiles => GetCsvProfilesLoop()
329 );
330 if ($template->param('viewshelf') or
331         $template->param( 'shelves' ) or
332         $template->param(  'edit'   ) ) {
333         $template->param(vseflag => 1);
334 }
335 if ($template->param( 'shelves' ) or    # note: this part looks duplicative, but is intentional
336         $template->param(  'edit'   ) ) {
337         $template->param( seflag => 1);
338 }
339
340 #FIXME: This refresh really only needs to happen when there is a modification of some sort
341 #               to the shelves, but the above code is so convoluted in its handling of the various
342 #               options, it is easier to do this refresh every time C4::VirtualShelves::Page.pm is
343 #               called
344
345 my ($total, $pubshelves, $barshelves) = RefreshShelvesSummary($query->cookie("CGISESSID"),$loggedinuser,($loggedinuser == -1 ? 20 : 10));
346
347 if (defined $barshelves) {
348         $template->param(       barshelves      => scalar (@{$barshelves->[0]}),
349                                                 barshelvesloop  => $barshelves->[0],
350                                         );
351         $template->param(       bartotal                => $total->{'bartotal'}, ) if ($total->{'bartotal'} > scalar (@{$barshelves->[0]}));
352 }
353
354 if (defined $pubshelves) {
355         $template->param(       pubshelves      => scalar (@{$pubshelves->[0]}),
356                                                 pubshelvesloop  => $pubshelves->[0],
357                                         );
358         $template->param(       pubtotal                => $total->{'pubtotal'}, ) if ($total->{'pubtotal'} > scalar (@{$pubshelves->[0]}));
359 }
360
361 output_html_with_http_headers $query, $cookie, $template->output;
362 }       
363
364 1;
365 __END__
366
367 =head1 NAME
368
369     VirtualShelves/Page.pm
370
371 =head1 DESCRIPTION
372
373     Module used for both OPAC and intranet pages.
374
375 =head1 CGI PARAMETERS
376
377 =over 4
378
379 =item C<modifyshelfcontents>
380
381     If this script has to modify the shelf content.
382
383 =item C<shelfnumber>
384
385     To know on which shelf to work.
386
387 =item C<addbarcode>
388
389 =item C<op>
390
391     Op can be:
392         * modif: show the template allowing modification of the shelves;
393         * modifsave: save changes from modif mode.
394
395 =item C<viewshelf>
396
397     Load template with 'viewshelves param' displaying the shelf's information.
398
399 =item C<shelves>
400
401     If the param shelves == 1, then add or delete a shelf.
402
403 =item C<addshelf>
404
405     If the param shelves == 1, then addshelf is the name of the shelf to add.
406
407 =back
408
409 =cut