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.
26 use C4::VirtualShelves qw/:DEFAULT RefreshShelvesSummary/;
30 use C4::Auth qw/get_session/;
33 use C4::Dates qw/format_date/;
34 use C4::Tags qw(get_tags);
40 use vars qw($debug @EXPORT @ISA $VERSION);
45 @EXPORT = qw(&shelfpage);
46 $debug = $ENV{DEBUG} || 0;
50 intranet => { redirect => '/cgi-bin/koha/virtualshelves/shelves.pl', },
51 opac => { redirect => '/cgi-bin/koha/opac-shelves.pl', },
54 sub shelfpage ($$$$$) {
55 my ( $type, $query, $template, $loggedinuser, $cookie ) = @_;
56 ( $pages{$type} ) or $type = 'opac';
57 $query or die "No query";
58 $template or die "No template";
59 $template->param( loggedinuser => $loggedinuser );
64 my $shelfoff = ( $query->param('shelfoff') ? $query->param('shelfoff') : 1 );
65 $template->{VARS}->{'shelfoff'} = $shelfoff;
66 my $itemoff = ( $query->param('itemoff') ? $query->param('itemoff') : 1 );
67 my $displaymode = ( $query->param('display') ? $query->param('display') : 'publicshelves' );
68 my ( $shelflimit, $shelfoffset, $shelveslimit, $shelvesoffset );
69 my $marcflavour = C4::Context->preference("marcflavour");
71 $shelflimit = ( $type eq 'opac' ? C4::Context->preference('OPACnumSearchResults') : C4::Context->preference('numSearchResults') );
72 $shelflimit = $shelflimit || 20;
73 $shelfoffset = ( $itemoff - 1 ) * $shelflimit; # Sets the offset to begin retrieving items at
74 $shelveslimit = $shelflimit; # Limits number of shelves returned for a given query (row_count)
75 $shelvesoffset = ( $shelfoff - 1 ) * $shelflimit; # Sets the offset to begin retrieving shelves at (offset)
76 # getting the Shelves list
77 my $category = ( ( $displaymode eq 'privateshelves' ) ? 1 : 2 );
78 my ( $shelflist, $totshelves ) = GetShelves( $category, $shelveslimit, $shelvesoffset, $loggedinuser );
80 #Get a list of private shelves for possible deletion. Only do this when we've defaulted to public shelves
81 my ( $privshelflist, $privtotshelves );
82 if ( $category == 2 ) {
83 ( $privshelflist, $privtotshelves ) = GetShelves( 1, $shelveslimit, $shelvesoffset, $loggedinuser );
85 my $op = $query->param('op');
87 # my $imgdir = getitemtypeimagesrc();
88 # my $itemtypes = GetItemTypes();
90 # the format of this is unindented for ease of diff comparison to the old script
91 # Note: do not mistake the assignment statements below for comparisons!
92 if ( $query->param('modifyshelfcontents') ) {
93 my ( $shelfnumber, $barcode, $item, $biblio );
94 if ( $shelfnumber = $query->param('viewshelf') ) {
95 if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' ) ) {
96 if ( $barcode = $query->param('addbarcode') ) {
97 $item = GetItem( 0, $barcode );
98 if (defined $item && $item->{'itemnumber'}){
99 $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'} );
100 AddToShelf( $biblio->{'biblionumber'}, $shelfnumber )
101 or push @paramsloop, { duplicatebiblio => $barcode };
103 push @paramsloop, { failgetitem => $barcode };
106 ( grep { /REM-(\d+)/ } $query->param ) or push @paramsloop, { nobarcode => 1 };
107 foreach ( $query->param ) {
109 $debug and warn "SHELVES: user $loggedinuser removing item $1 from shelf $shelfnumber";
110 DelFromShelf( $1, $shelfnumber ); # $1 is biblionumber
114 push @paramsloop, { nopermission => $shelfnumber };
117 push @paramsloop, { noshelfnumber => 1 };
123 # set the default tab, etc. (for OPAC)
124 my $shelf_type = ( $query->param('display') ? $query->param('display') : 'publicshelves' );
125 if ( defined $shelf_type ) {
126 if ( $shelf_type eq 'privateshelves' ) {
127 $template->param( showprivateshelves => 1 );
128 } elsif ( $shelf_type eq 'publicshelves' ) {
129 $template->param( showpublicshelves => 1 );
132 $debug and warn "Invalid 'display' param ($shelf_type)";
134 } elsif ( $loggedinuser == -1 ) {
135 $template->param( showpublicshelves => 1 );
137 $template->param( showprivateshelves => 1 );
140 my ( $okmanage, $okview );
141 my $shelfnumber = $query->param('shelfnumber') || $query->param('viewshelf');
143 $okmanage = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
144 $okview = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' );
152 push @paramsloop, { nopermission => $shelfnumber };
155 if ( $op eq 'modifsave' ) {
157 'shelfname' => $query->param('shelfname'),
158 'category' => $query->param('category'),
159 'sortfield' => $query->param('sortfield'),
162 ModShelf( $shelfnumber, $shelf );
164 } elsif ( $op eq 'modif' ) {
165 my ( $shelfnumber2, $shelfname, $owner, $category, $sortfield ) = GetShelf($shelfnumber);
166 my $member = GetMember( 'borrowernumber' => $owner );
167 my $ownername = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
171 shelfnumber => $shelfnumber2,
172 shelfname => $shelfname,
174 ownername => $ownername,
175 "category$category" => 1,
176 category => $category,
177 "sort_$sortfield" => 1,
182 if ( $shelfnumber = $query->param('viewshelf') ) {
183 # explicitly fetch this shelf
184 my ($shelfnumber2,$shelfname,$owner,$category,$sorton) = GetShelf($shelfnumber);
186 $template->param( 'AllowOnShelfHolds' => C4::Context->preference('AllowOnShelfHolds') );
187 if (C4::Context->preference('TagsEnabled')) {
188 $template->param(TagsEnabled => 1);
189 foreach (qw(TagsShowOnList TagsInputOnList)) {
190 C4::Context->preference($_) and $template->param($_ => 1);
193 #check that the user can view the shelf
194 if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' ) ) {
199 my $sortfield = ( $query->param('sortfield') ? $query->param('sortfield') : 'title' );
200 if ( $sortfield eq 'author' ) {
201 $authorsort = 'author';
203 if ( $sortfield eq 'year' ) {
206 ( $items, $totitems ) = GetShelfContents( $shelfnumber, $shelflimit, $shelfoffset );
207 for my $this_item (@$items) {
208 my $biblionumber = $this_item->{'biblionumber'};
209 my $record = GetMarcBiblio($biblionumber);
210 $this_item->{XSLTBloc} =
211 XSLTParse4Display($biblionumber, $record, 'Results', 'opac')
212 if C4::Context->preference("OPACXSLTResultsDisplay") && $type eq 'opac';
214 # the virtualshelfcontents table does not store these columns nor are they retrieved from the items
215 # and itemtypes tables, so I'm commenting them out for now to quiet the log -crn
216 #$this_item->{imageurl} = $imgdir."/".$itemtypes->{ $this_item->{itemtype} }->{'imageurl'};
217 #$this_item->{'description'} = $itemtypes->{ $this_item->{itemtype} }->{'description'};
218 $this_item->{'dateadded'} = format_date( $this_item->{'dateadded'} );
219 $this_item->{'imageurl'} = getitemtypeinfo( $this_item->{'itemtype'} )->{'imageurl'};
220 $this_item->{'coins'} = GetCOinSBiblio( $this_item->{'biblionumber'} );
221 $this_item->{'subtitle'} = GetRecordValue('subtitle', $record, GetFrameworkCode($this_item->{'biblionumber'}));
222 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record,$marcflavour);
223 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record,$marcflavour);
224 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber($record,$marcflavour);
225 $this_item->{'normalized_isbn'} = GetNormalizedISBN(undef,$record,$marcflavour);
226 # Getting items infos for location display
227 my @items_infos = &GetItemsLocationInfo( $this_item->{'biblionumber'});
228 $this_item->{'itemsissued'} = CountItemsIssued( $this_item->{'biblionumber'} );
229 $this_item->{'ITEM_RESULTS'} = \@items_infos;
231 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnList')) {
232 $this_item->{'TagLoop'} = get_tags({
233 biblionumber=>$this_item->{'biblionumber'}, approved=>1, 'sort'=>'-weight',
239 push @paramsloop, { display => 'privateshelves' } if $category == 1;
242 my $manageshelf = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
244 shelfname => $shelfname,
245 shelfnumber => $shelfnumber,
246 viewshelf => $shelfnumber,
247 authorsort => $authorsort,
248 yearsort => $yearsort,
249 manageshelf => $manageshelf,
250 "category$category" => 1,
251 category => $category,
255 push @paramsloop, { nopermission => $shelfnumber };
259 if ( $query->param('shelves') ) {
261 if ( my $newshelf = $query->param('addshelf') ) {
263 # note: a user can always add a new shelf
264 my $shelfnumber = AddShelf( $newshelf, $query->param('owner'), $query->param('category'), $query->param('sortfield') );
266 if ( $shelfnumber == -1 ) { #shelf already exists.
268 push @paramsloop, { already => $newshelf };
269 $template->param( shelfnumber => $shelfnumber );
271 print $query->redirect( $pages{$type}->{redirect} . "?viewshelf=$shelfnumber" );
275 foreach ( $query->param() ) {
279 unless ( defined $shelflist->{$number} || defined $privshelflist->{$number} ) {
280 push( @paramsloop, { unrecognized => $number } );
283 unless ( ShelfPossibleAction( $loggedinuser, $number, 'manage' ) ) {
284 push( @paramsloop, { nopermission => $shelfnumber } );
288 ( $contents, $totshelves ) = GetShelfContents( $number, $shelveslimit, $shelvesoffset );
289 if ( my $count = scalar @$contents ) {
290 unless ( scalar grep { /^CONFIRM-$number$/ } $query->param() ) {
291 if ( defined $shelflist->{$number} ) {
292 push( @paramsloop, { need_confirm => $shelflist->{$number}->{shelfname}, count => $count, single => ($count eq 1 ? 1:0) } );
293 $shelflist->{$number}->{confirm} = $number;
295 push( @paramsloop, { need_confirm => $privshelflist->{$number}->{shelfname}, count => $count } );
296 $privshelflist->{$number}->{confirm} = $number;
303 if ( defined $shelflist->{$number} ) {
304 $name = $shelflist->{$number}->{'shelfname'};
305 delete $shelflist->{$number};
307 $name = $privshelflist->{$number}->{'shelfname'};
308 delete $privshelflist->{$number};
310 unless ( DelShelf($number) ) {
311 push( @paramsloop, { delete_fail => $name } );
314 push( @paramsloop, { delete_ok => $name } );
316 # print $query->redirect($pages{$type}->{redirect}); exit;
321 $template->param( shelves => 1 );
328 (@paramsloop) and $template->param( paramsloop => \@paramsloop );
329 $showadd and $template->param( showadd => 1 );
332 my $numberCanManage = 0;
334 # rebuild shelflist in case a shelf has been added
335 ( $shelflist, $totshelves ) = GetShelves( $category, $shelveslimit, $shelvesoffset, $loggedinuser ) unless $delflag;
336 foreach my $element ( sort { lc( $shelflist->{$a}->{'shelfname'} ) cmp lc( $shelflist->{$b}->{'shelfname'} ) } keys %$shelflist ) {
338 $shelflist->{$element}->{shelf} = $element;
339 my $category = $shelflist->{$element}->{'category'};
340 my $owner = $shelflist->{$element}->{'owner'};
341 my $canmanage = ShelfPossibleAction( $loggedinuser, $element, 'manage' );
342 my $sortfield = $shelflist->{$element}->{'sortfield'};
343 if ( $sortfield eq 'author' ) {
344 $shelflist->{$element}->{"authorsort"} = 'author';
346 if ( $sortfield eq 'year' ) {
347 $shelflist->{$element}->{"yearsort"} = 'year';
349 $shelflist->{$element}->{"viewcategory$category"} = 1;
350 $shelflist->{$element}->{manageshelf} = $canmanage;
351 if ( $owner eq $loggedinuser or $canmanage ) {
352 $shelflist->{$element}->{'mine'} = 1;
354 my $member = GetMember( 'borrowernumber' => $owner );
355 $shelflist->{$element}->{ownername} = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
356 $numberCanManage++ if $canmanage; # possibly outmoded
357 if ( $shelflist->{$element}->{'category'} eq '1' ) {
358 push( @shelveslooppriv, $shelflist->{$element} );
360 push( @shelvesloop, $shelflist->{$element} );
364 my $url = $type eq 'opac' ? "/cgi-bin/koha/opac-shelves.pl" : "/cgi-bin/koha/virtualshelves/shelves.pl";
366 foreach (qw(display viewshelf sortfield)) {
367 $qhash{$_} = $query->param($_) if $query->param($_);
369 ( scalar keys %qhash ) and $url .= '?' . join '&', map { "$_=$qhash{$_}" } keys %qhash;
370 if ( $shelfnumber ) {
371 $template->param( pagination_bar => pagination_bar( $url, ( int( $totitems / $shelflimit ) ) + ( ( $totitems % $shelflimit ) > 0 ? 1 : 0 ), $itemoff, "itemoff" ) );
374 pagination_bar => pagination_bar( $url, ( int( $totshelves / $shelveslimit ) ) + ( ( $totshelves % $shelveslimit ) > 0 ? 1 : 0 ), $shelfoff, "shelfoff" ) );
377 shelveslooppriv => \@shelveslooppriv,
378 shelvesloop => \@shelvesloop,
379 shelvesloopall => [ ( @shelvesloop, @shelveslooppriv ) ],
380 numberCanManage => $numberCanManage,
381 "BiblioDefaultView" . C4::Context->preference("BiblioDefaultView") => 1,
382 csv_profiles => GetCsvProfilesLoop()
387 $template->param( vseflag => 1 );
389 if ($shelves or # note: this part looks duplicative, but is intentional
392 $template->param( seflag => 1 );
395 #FIXME: This refresh really only needs to happen when there is a modification of some sort
396 # to the shelves, but the above code is so convoluted in its handling of the various
397 # options, it is easier to do this refresh every time C4::VirtualShelves::Page.pm is
400 my ( $total, $pubshelves, $barshelves ) = RefreshShelvesSummary( $query->cookie("CGISESSID"), $loggedinuser, ( $loggedinuser == -1 ? 20 : 10 ) );
402 if ( defined $barshelves ) {
404 barshelves => scalar( @{ $barshelves } ),
405 barshelvesloop => $barshelves,
407 $template->param( bartotal => $total->{'bartotal'}, ) if ( $total->{'bartotal'} > scalar( @{ $barshelves } ) );
410 if ( defined $pubshelves ) {
412 pubshelves => scalar( @{ $pubshelves } ),
413 pubshelvesloop => $pubshelves,
415 $template->param( pubtotal => $total->{'pubtotal'}, ) if ( $total->{'pubtotal'} > scalar( @{ $pubshelves } ) );
418 output_html_with_http_headers $query, $cookie, $template->output;
426 VirtualShelves/Page.pm
430 Module used for both OPAC and intranet pages.
432 =head1 CGI PARAMETERS
436 =item C<modifyshelfcontents>
438 If this script has to modify the shelf content.
442 To know on which shelf to work.
449 * modif: show the template allowing modification of the shelves;
450 * modifsave: save changes from modif mode.
454 Load template with 'viewshelves param' displaying the shelf's information.
458 If the param shelves == 1, then add or delete a shelf.
462 If the param shelves == 1, then addshelf is the name of the shelf to add.