1 package C4::VirtualShelves::Page;
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
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
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.
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.
21 # perldoc at the end of the file, per convention.
30 use C4::VirtualShelves qw/:DEFAULT ShelvesMax/;
34 use C4::Auth qw/get_session/;
37 use C4::Dates qw/format_date/;
38 use C4::Tags qw(get_tags);
42 use constant VIRTUALSHELVES_COUNT => 20;
44 use vars qw($debug @EXPORT @ISA $VERSION);
49 @EXPORT = qw(&shelfpage);
50 $debug = $ENV{DEBUG} || 0;
54 intranet => { redirect => '/cgi-bin/koha/virtualshelves/shelves.pl', },
55 opac => { redirect => '/cgi-bin/koha/opac-shelves.pl', },
59 my ( $type, $query, $template, $loggedinuser, $cookie ) = @_;
60 ( $pages{$type} ) or $type = 'opac';
61 $query or die "No query";
62 $template or die "No template";
64 loggedinuser => $loggedinuser,
65 OpacAllowPublicListCreation => C4::Context->preference('OpacAllowPublicListCreation'),
71 my $shelfoff = ( $query->param('shelfoff') ? $query->param('shelfoff') : 1 );
72 $template->{VARS}->{'shelfoff'} = $shelfoff;
73 my $itemoff = ( $query->param('itemoff') ? $query->param('itemoff') : 1 );
74 my $displaymode = ( $query->param('display') ? $query->param('display') : 'publicshelves' );
75 my ( $shelflimit, $shelfoffset, $shelveslimit, $shelvesoffset );
76 my $marcflavour = C4::Context->preference("marcflavour");
78 # get biblionumbers stored in the cart
80 my $cart_cookie = ( $type eq 'opac' ? "bib_list" : "intranet_bib_list" );
81 if($query->cookie($cart_cookie)){
82 my $cart_list = $query->cookie($cart_cookie);
83 @cart_list = split(/\//, $cart_list);
86 $shelflimit = ( $type eq 'opac' ? C4::Context->preference('OPACnumSearchResults') : C4::Context->preference('numSearchResults') );
87 $shelflimit = $shelflimit || ShelvesMax('MGRPAGE');
88 $shelfoffset = ( $itemoff - 1 ) * $shelflimit; # Sets the offset to begin retrieving items at
89 $shelveslimit = $shelflimit; # Limits number of shelves returned for a given query (row_count)
90 $shelvesoffset = ( $shelfoff - 1 ) * $shelflimit; # Sets the offset to begin retrieving shelves at (offset)
91 # getting the Shelves list
92 my $category = ( ( $displaymode eq 'privateshelves' ) ? 1 : 2 );
93 my ( $shelflist, $totshelves ) = GetShelves( $category, $shelveslimit, $shelvesoffset, $loggedinuser );
95 #Get a list of private shelves for possible deletion. Only do this when we've defaulted to public shelves
96 my ( $privshelflist, $privtotshelves );
97 if ( $category == 2 ) {
98 ( $privshelflist, $privtotshelves ) = GetShelves( 1, $shelveslimit, $shelvesoffset, $loggedinuser );
100 my $op = $query->param('op');
102 # the format of this is unindented for ease of diff comparison to the old script
103 # Note: do not mistake the assignment statements below for comparisons!
104 if ( $query->param('modifyshelfcontents') ) {
105 my ( $shelfnumber, $barcode, $item, $biblio );
106 if ( $shelfnumber = $query->param('viewshelf') ) {
108 if($barcode = $query->param('addbarcode') ) {
109 if(ShelfPossibleAction( $loggedinuser, $shelfnumber, 'add')) {
110 $item = GetItem( 0, $barcode);
111 if (defined $item && $item->{'itemnumber'}) {
112 $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'} );
113 AddToShelf( $biblio->{'biblionumber'}, $shelfnumber, $loggedinuser)
114 or push @paramsloop, { duplicatebiblio => $barcode };
117 push @paramsloop, { failgetitem => $barcode };
121 push @paramsloop, { nopermission => $shelfnumber };
124 elsif(grep { /REM-(\d+)/ } $query->param) {
125 #remove item(s) from shelf
126 if(ShelfPossibleAction($loggedinuser, $shelfnumber, 'delete')) {
127 #This is just a general okay; DelFromShelf checks further
129 foreach($query->param) {
131 push @bib, $1; #$1 is biblionumber
133 my $t= DelFromShelf(\@bib, $shelfnumber, $loggedinuser);
135 push @paramsloop, {nothingdeleted => $shelfnumber};
138 push @paramsloop, {somedeleted => $shelfnumber};
142 push @paramsloop, { nopermission => $shelfnumber };
147 push @paramsloop, { noshelfnumber => 1 };
153 # set the default tab, etc. (for OPAC)
154 my $shelf_type = ( $query->param('display') ? $query->param('display') : 'publicshelves' );
155 if ( defined $shelf_type ) {
156 if ( $shelf_type eq 'privateshelves' ) {
157 $template->param( showprivateshelves => 1 );
158 } elsif ( $shelf_type eq 'publicshelves' ) {
159 $template->param( showpublicshelves => 1 );
162 $debug and warn "Invalid 'display' param ($shelf_type)";
164 } elsif ( $loggedinuser == -1 ) {
165 $template->param( showpublicshelves => 1 );
167 $template->param( showprivateshelves => 1 );
170 my ( $okmanage, $okview );
171 my $shelfnumber = $query->param('shelfnumber') || $query->param('viewshelf');
173 $okmanage = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
174 $okview = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' );
181 #Saving modified shelf
182 if ( $op eq 'modifsave' ) {
184 push @paramsloop, { nopermission => $shelfnumber };
188 'shelfname' => $query->param('shelfname'),
189 'sortfield' => $query->param('sortfield'),
191 if($query->param('category')) { #optional
192 $shelf->{category}= $query->param('category');
194 unless(ModShelf($shelfnumber, $shelf )) {
195 push @paramsloop, {modifyfailure => $shelf->{shelfname}};
199 if($displaymode eq "viewshelf"){
200 print $query->redirect( $pages{$type}->{redirect} . "?viewshelf=$shelfnumber" );
201 } elsif($displaymode eq "publicshelves"){
202 print $query->redirect( $pages{$type}->{redirect} );
204 print $query->redirect( $pages{$type}->{redirect} . "?display=privateshelves" );
209 elsif ( $op eq 'modif' ) {
210 my ( $shelfnumber2, $shelfname, $owner, $category, $sortfield ) = GetShelf($shelfnumber);
211 my $member = GetMember( 'borrowernumber' => $owner );
212 my $ownername = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
214 $sortfield='' unless $sortfield;
217 display => $displaymode,
218 shelfnumber => $shelfnumber2,
219 shelfname => $shelfname,
221 ownername => $ownername,
222 "category$category" => 1,
223 category => $category,
224 "sort_$sortfield" => 1,
231 if ( $shelfnumber = $query->param('viewshelf') ) {
232 # explicitly fetch this shelf
233 my ($shelfnumber2,$shelfname,$owner,$category,$sorton) = GetShelf($shelfnumber);
235 $template->param( 'AllowOnShelfHolds' => C4::Context->preference('AllowOnShelfHolds') );
236 if (C4::Context->preference('TagsEnabled')) {
237 $template->param(TagsEnabled => 1);
238 foreach (qw(TagsShowOnList TagsInputOnList)) {
239 C4::Context->preference($_) and $template->param($_ => 1);
242 #check that the user can view the shelf
243 if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' ) ) {
248 my $sortfield = ( $query->param('sortfield') ? $query->param('sortfield') : 'title' );
249 if ( $sortfield eq 'author' ) {
250 $authorsort = 'author';
252 if ( $sortfield eq 'year' ) {
255 ( $items, $totitems ) = GetShelfContents( $shelfnumber, $shelflimit, $shelfoffset );
256 for my $this_item (@$items) {
257 my $biblionumber = $this_item->{'biblionumber'};
258 my $record = GetMarcBiblio($biblionumber);
259 $this_item->{XSLTBloc} =
260 XSLTParse4Display($biblionumber, $record, 'Results', 'opac')
261 if C4::Context->preference("OPACXSLTResultsDisplay") && $type eq 'opac';
263 # the virtualshelfcontents table does not store these columns nor are they retrieved from the items
264 # and itemtypes tables, so I'm commenting them out for now to quiet the log -crn
265 #$this_item->{imageurl} = $imgdir."/".$itemtypes->{ $this_item->{itemtype} }->{'imageurl'};
266 #$this_item->{'description'} = $itemtypes->{ $this_item->{itemtype} }->{'description'};
267 $this_item->{'dateadded'} = format_date( $this_item->{'dateadded'} );
268 $this_item->{'imageurl'} = getitemtypeinfo( $this_item->{'itemtype'} )->{'imageurl'};
269 $this_item->{'coins'} = GetCOinSBiblio( $record );
270 $this_item->{'subtitle'} = GetRecordValue('subtitle', $record, GetFrameworkCode($this_item->{'biblionumber'}));
271 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record,$marcflavour);
272 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record,$marcflavour);
273 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber($record,$marcflavour);
274 $this_item->{'normalized_isbn'} = GetNormalizedISBN(undef,$record,$marcflavour);
275 # Getting items infos for location display
276 my @items_infos = &GetItemsLocationInfo( $this_item->{'biblionumber'});
277 $this_item->{'itemsissued'} = CountItemsIssued( $this_item->{'biblionumber'} );
278 $this_item->{'ITEM_RESULTS'} = \@items_infos;
279 if ( grep {$_ eq $biblionumber} @cart_list) {
280 $this_item->{'incart'} = 1;
283 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnList')) {
284 $this_item->{'TagLoop'} = get_tags({
285 biblionumber=>$this_item->{'biblionumber'}, approved=>1, 'sort'=>'-weight',
291 push @paramsloop, { display => 'privateshelves' } if $category == 1;
294 my $manageshelf = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
296 shelfname => $shelfname,
297 shelfnumber => $shelfnumber,
298 viewshelf => $shelfnumber,
299 authorsort => $authorsort,
300 yearsort => $yearsort,
301 manageshelf => $manageshelf,
302 allowremovingitems => ShelfPossibleAction( $loggedinuser, $shelfnumber, 'delete'),
303 allowaddingitem => ShelfPossibleAction( $loggedinuser, $shelfnumber, 'add'),
304 "category$category" => 1,
305 category => $category,
307 showprivateshelves => $category==1,
310 push @paramsloop, { nopermission => $shelfnumber };
315 if ( $query->param('shelves') ) {
319 if ( my $newshelf = $query->param('addshelf') ) {
321 # note: a user can always add a new shelf
322 my $shelfnumber = AddShelf( {
323 shelfname => $newshelf,
324 sortfield => $query->param('sortfield'),
325 category => $query->param('category') },
326 $query->param('owner') );
328 if ( $shelfnumber == -1 ) { #shelf already exists.
330 push @paramsloop, { already => $newshelf };
331 $template->param( shelfnumber => $shelfnumber );
333 print $query->redirect( $pages{$type}->{redirect} . "?viewshelf=$shelfnumber" );
338 #Deleting a shelf (asking for confirmation if it has entries)
339 foreach ( $query->param() ) {
343 unless ( defined $shelflist->{$number} || defined $privshelflist->{$number} ) {
344 push( @paramsloop, { unrecognized => $number } );
347 unless ( ShelfPossibleAction( $loggedinuser, $number, 'manage' ) ) {
348 push( @paramsloop, { nopermission => $shelfnumber } );
352 ( $contents, $totshelves ) = GetShelfContents( $number, $shelveslimit, $shelvesoffset );
353 if ( my $count = scalar @$contents ) {
354 unless ( scalar grep { /^CONFIRM-$number$/ } $query->param() ) {
355 if ( defined $shelflist->{$number} ) {
356 push( @paramsloop, { need_confirm => $shelflist->{$number}->{shelfname}, count => $count, single => ($count eq 1 ? 1:0) } );
357 $shelflist->{$number}->{confirm} = $number;
359 push( @paramsloop, { need_confirm => $privshelflist->{$number}->{shelfname}, count => $count } );
360 $privshelflist->{$number}->{confirm} = $number;
367 if ( defined $shelflist->{$number} ) {
368 $name = $shelflist->{$number}->{'shelfname'};
369 delete $shelflist->{$number};
371 $name = $privshelflist->{$number}->{'shelfname'};
372 delete $privshelflist->{$number};
374 unless ( DelShelf($number) ) {
375 push( @paramsloop, { delete_fail => $name } );
378 push( @paramsloop, { delete_ok => $name } );
384 $template->param( shelves => 1 );
389 } # end of SWITCH block
391 (@paramsloop) and $template->param( paramsloop => \@paramsloop );
392 $showadd and $template->param( showadd => 1 );
395 my $numberCanManage = 0;
397 # rebuild shelflist in case a shelf has been added
398 ( $shelflist, $totshelves ) = GetShelves( $category, $shelveslimit, $shelvesoffset, $loggedinuser ) unless $delflag;
399 foreach my $element ( sort { lc( $shelflist->{$a}->{'shelfname'} ) cmp lc( $shelflist->{$b}->{'shelfname'} ) } keys %$shelflist ) {
401 $shelflist->{$element}->{shelf} = $element;
402 my $category = $shelflist->{$element}->{'category'};
403 my $owner = $shelflist->{$element}->{'owner'}||0;
404 my $canmanage = ShelfPossibleAction( $loggedinuser, $element, 'manage' );
405 my $sortfield = $shelflist->{$element}->{'sortfield'};
407 if ( $sortfield eq 'author' ) {
408 $shelflist->{$element}->{"authorsort"} = 'author';
409 } elsif ( $sortfield eq 'year' ) {
410 $shelflist->{$element}->{"yearsort"} = 'year';
413 $shelflist->{$element}->{"viewcategory$category"} = 1;
414 $shelflist->{$element}->{manageshelf} = $canmanage;
415 if($canmanage || ($loggedinuser && $owner==$loggedinuser)) {
416 $shelflist->{$element}->{'mine'} = 1;
418 my $member = GetMember( 'borrowernumber' => $owner );
419 $shelflist->{$element}->{ownername} = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
420 $numberCanManage++ if $canmanage; # possibly outmoded
421 if ( $shelflist->{$element}->{'category'} eq '1' ) {
422 push( @shelveslooppriv, $shelflist->{$element} );
424 push( @shelvesloop, $shelflist->{$element} );
428 my $url = $type eq 'opac' ? "/cgi-bin/koha/opac-shelves.pl" : "/cgi-bin/koha/virtualshelves/shelves.pl";
430 foreach (qw(display viewshelf sortfield)) {
431 $qhash{$_} = $query->param($_) if $query->param($_);
433 ( scalar keys %qhash ) and $url .= '?' . join '&', map { "$_=$qhash{$_}" } keys %qhash;
434 if ( $shelfnumber && $totitems ) {
435 $template->param( pagination_bar => pagination_bar( $url, ( int( $totitems / $shelflimit ) ) + ( ( $totitems % $shelflimit ) > 0 ? 1 : 0 ), $itemoff, "itemoff" ) );
436 } elsif ( $totshelves ) {
438 pagination_bar => pagination_bar( $url, ( int( $totshelves / $shelveslimit ) ) + ( ( $totshelves % $shelveslimit ) > 0 ? 1 : 0 ), $shelfoff, "shelfoff" ) );
441 shelveslooppriv => \@shelveslooppriv,
442 shelvesloop => \@shelvesloop,
443 shelvesloopall => [ ( @shelvesloop, @shelveslooppriv ) ],
444 numberCanManage => $numberCanManage,
445 "BiblioDefaultView" . C4::Context->preference("BiblioDefaultView") => 1,
446 csv_profiles => GetCsvProfilesLoop()
451 $template->param( vseflag => 1 );
453 if ($shelves or # note: this part looks duplicative, but is intentional
456 $template->param( seflag => 1 );
459 #Next call updates the shelves for the Lists button.
460 #May not always be needed (when nothing changed), but doesn't take much.
461 my ($total, $pubshelves, $barshelves) = C4::VirtualShelves::GetSomeShelfNames($loggedinuser, 'MASTHEAD');
463 barshelves => $total->{bartotal},
464 barshelvesloop => $barshelves,
465 pubshelves => $total->{pubtotal},
466 pubshelvesloop => $pubshelves,
469 output_html_with_http_headers $query, $cookie, $template->output;
477 VirtualShelves/Page.pm
481 Module used for both OPAC and intranet pages.
483 =head1 CGI PARAMETERS
487 =item C<modifyshelfcontents>
489 If this script has to modify the shelf content.
493 To know on which shelf to work.
500 * modif: show the template allowing modification of the shelves;
501 * modifsave: save changes from modif mode.
505 Load template with 'viewshelves param' displaying the shelf's information.
509 If the param shelves == 1, then add or delete a shelf.
513 If the param shelves == 1, then addshelf is the name of the shelf to add.