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/;
38 use vars qw($debug @EXPORT @ISA $VERSION);
43 @EXPORT = qw(&shelfpage);
44 $debug = $ENV{DEBUG} || 0;
48 intranet => { redirect => '/cgi-bin/koha/virtualshelves/shelves.pl', },
49 opac => { redirect => '/cgi-bin/koha/opac-shelves.pl', },
52 sub shelfpage ($$$$$) {
53 my ( $type, $query, $template, $loggedinuser, $cookie ) = @_;
54 ( $pages{$type} ) or $type = 'opac';
55 $query or die "No query";
56 $template or die "No template";
57 $template->param( { loggedinuser => $loggedinuser } );
60 my $shelfoff = ( $query->param('shelfoff') ? $query->param('shelfoff') : 1 );
61 my $itemoff = ( $query->param('itemoff') ? $query->param('itemoff') : 1 );
62 my $displaymode = ( $query->param('display') ? $query->param('display') : 'publicshelves' );
63 my ( $shelflimit, $shelfoffset, $shelveslimit, $shelvesoffset );
65 $shelflimit = ( $type eq 'opac' ? C4::Context->preference('OPACnumSearchResults') : C4::Context->preference('numSearchResults') );
66 $shelflimit = $shelflimit || 20;
67 $shelfoffset = ( $itemoff - 1 ) * $shelflimit; # Sets the offset to begin retrieving items at
68 $shelveslimit = $shelflimit; # Limits number of shelves returned for a given query (row_count)
69 $shelvesoffset = ( $shelfoff - 1 ) * $shelflimit; # Sets the offset to begin retrieving shelves at (offset)
70 # getting the Shelves list
71 my $category = ( ( $displaymode eq 'privateshelves' ) ? 1 : 2 );
72 my ( $shelflist, $totshelves ) = GetShelves( $category, $shelveslimit, $shelvesoffset, $loggedinuser );
74 #Get a list of private shelves for possible deletion. Only do this when we've defaulted to public shelves
75 my ( $privshelflist, $privtotshelves );
76 if ( $category == 2 ) {
77 ( $privshelflist, $privtotshelves ) = GetShelves( 1, $shelveslimit, $shelvesoffset, $loggedinuser );
79 my $op = $query->param('op');
81 # my $imgdir = getitemtypeimagesrc();
82 # my $itemtypes = GetItemTypes();
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!
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 $item = GetItem( 0, $barcode );
93 if (defined $item && $item->{'itemnumber'}){
94 $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'} );
95 AddToShelf( $biblio->{'biblionumber'}, $shelfnumber )
96 or push @paramsloop, { duplicatebiblio => $barcode };
98 push @paramsloop, { failgetitem => $barcode };
101 ( grep { /REM-(\d+)/ } $query->param ) or push @paramsloop, { nobarcode => 1 };
102 foreach ( $query->param ) {
104 $debug and warn "SHELVES: user $loggedinuser removing item $1 from shelf $shelfnumber";
105 DelFromShelf( $1, $shelfnumber ); # $1 is biblionumber
109 push @paramsloop, { nopermission => $shelfnumber };
112 push @paramsloop, { noshelfnumber => 1 };
118 # set the default tab, etc. (for OPAC)
119 my $shelf_type = ( $query->param('display') ? $query->param('display') : 'publicshelves' );
120 if ( defined $shelf_type ) {
121 if ( $shelf_type eq 'privateshelves' ) {
122 $template->param( showprivateshelves => 1 );
123 } elsif ( $shelf_type eq 'publicshelves' ) {
124 $template->param( showpublicshelves => 1 );
127 $debug and warn "Invalid 'display' param ($shelf_type)";
129 } elsif ( $loggedinuser == -1 ) {
130 $template->param( showpublicshelves => 1 );
132 $template->param( showprivateshelves => 1 );
135 my ( $okmanage, $okview );
136 my $shelfnumber = $query->param('shelfnumber') || $query->param('viewshelf');
138 $okmanage = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
139 $okview = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' );
147 push @paramsloop, { nopermission => $shelfnumber };
150 if ( $op eq 'modifsave' ) {
152 'shelfname' => $query->param('shelfname'),
153 'category' => $query->param('category'),
154 'sortfield' => $query->param('sortfield'),
157 ModShelf( $shelfnumber, $shelf );
159 } elsif ( $op eq 'modif' ) {
160 my ( $shelfnumber2, $shelfname, $owner, $category, $sortfield ) = GetShelf($shelfnumber);
161 my $member = GetMember( 'borrowernumber' => $owner );
162 my $ownername = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
165 shelfnumber => $shelfnumber2,
166 shelfname => $shelfname,
168 ownername => $ownername,
169 "category$category" => 1,
170 category => $category,
171 "sort_$sortfield" => 1,
176 if ( $shelfnumber = $query->param('viewshelf') ) {
177 # explicitly fetch this shelf
178 my ($shelfnumber2,$shelfname,$owner,$category,$sorton) = GetShelf($shelfnumber);
180 #check that the user can view the shelf
181 if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' ) ) {
186 if ( $query->param('sortfield')) {
187 $sortfield = $query->param('sortfield');
189 $sortfield = $sorton;
191 $sortfield = 'title';
193 if ( $sortfield eq 'author' ) {
194 $authorsort = 'author';
196 if ( $sortfield eq 'year' ) {
199 ( $items, $totitems ) = GetShelfContents( $shelfnumber, $shelflimit, $shelfoffset );
200 for my $this_item (@$items) {
201 my $record = GetMarcBiblio( $this_item->{'biblionumber'} );
203 # the virtualshelfcontents table does not store these columns nor are they retrieved from the items
204 # and itemtypes tables, so I'm commenting them out for now to quiet the log -crn
205 #$this_item->{imageurl} = $imgdir."/".$itemtypes->{ $this_item->{itemtype} }->{'imageurl'};
206 #$this_item->{'description'} = $itemtypes->{ $this_item->{itemtype} }->{'description'};
207 $this_item->{'dateadded'} = format_date( $this_item->{'dateadded'} );
208 $this_item->{'imageurl'} = getitemtypeinfo( $this_item->{'itemtype'} )->{'imageurl'};
209 $this_item->{'coins'} = GetCOinSBiblio( $this_item->{'biblionumber'} );
210 $this_item->{'subtitle'} = GetRecordValue('subtitle', $record, GetFrameworkCode($this_item->{'biblionumber'}));
212 # Getting items infos for location display
213 my @items_infos = &GetItemsInfo( $this_item->{'biblionumber'}, $type );
214 $this_item->{'ITEM_RESULTS'} = \@items_infos;
217 push @paramsloop, { display => 'privateshelves' } if $category == 1;
220 my $manageshelf = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
222 shelfname => $shelfname,
223 shelfnumber => $shelfnumber,
224 viewshelf => $shelfnumber,
225 authorsort => $authorsort,
226 yearsort => $yearsort,
227 manageshelf => $manageshelf,
231 push @paramsloop, { nopermission => $shelfnumber };
235 if ( $query->param('shelves') ) {
237 if ( my $newshelf = $query->param('addshelf') ) {
239 # note: a user can always add a new shelf
240 my $shelfnumber = AddShelf( $newshelf, $query->param('owner'), $query->param('category'), $query->param('sortfield') );
242 if ( $shelfnumber == -1 ) { #shelf already exists.
244 push @paramsloop, { already => $newshelf };
245 $template->param( shelfnumber => $shelfnumber );
247 print $query->redirect( $pages{$type}->{redirect} . "?viewshelf=$shelfnumber" );
251 foreach ( $query->param() ) {
255 unless ( defined $shelflist->{$number} || defined $privshelflist->{$number} ) {
256 push( @paramsloop, { unrecognized => $number } );
259 unless ( ShelfPossibleAction( $loggedinuser, $number, 'manage' ) ) {
260 push( @paramsloop, { nopermission => $shelfnumber } );
264 ( $contents, $totshelves ) = GetShelfContents( $number, $shelveslimit, $shelvesoffset );
265 if ( my $count = scalar @$contents ) {
266 unless ( scalar grep { /^CONFIRM-$number$/ } $query->param() ) {
267 if ( defined $shelflist->{$number} ) {
268 push( @paramsloop, { need_confirm => $shelflist->{$number}->{shelfname}, count => $count } );
269 $shelflist->{$number}->{confirm} = $number;
271 push( @paramsloop, { need_confirm => $privshelflist->{$number}->{shelfname}, count => $count } );
272 $privshelflist->{$number}->{confirm} = $number;
279 if ( defined $shelflist->{$number} ) {
280 $name = $shelflist->{$number}->{'shelfname'};
281 delete $shelflist->{$number};
283 $name = $privshelflist->{$number}->{'shelfname'};
284 delete $privshelflist->{$number};
286 unless ( DelShelf($number) ) {
287 push( @paramsloop, { delete_fail => $name } );
290 push( @paramsloop, { delete_ok => $name } );
292 # print $query->redirect($pages{$type}->{redirect}); exit;
296 $stay and $template->param( shelves => 1 );
301 (@paramsloop) and $template->param( paramsloop => \@paramsloop );
302 $showadd and $template->param( showadd => 1 );
305 my $numberCanManage = 0;
307 # rebuild shelflist in case a shelf has been added
308 ( $shelflist, $totshelves ) = GetShelves( $category, $shelveslimit, $shelvesoffset, $loggedinuser ) unless $delflag;
309 foreach my $element ( sort { lc( $shelflist->{$a}->{'shelfname'} ) cmp lc( $shelflist->{$b}->{'shelfname'} ) } keys %$shelflist ) {
311 $shelflist->{$element}->{shelf} = $element;
312 my $category = $shelflist->{$element}->{'category'};
313 my $owner = $shelflist->{$element}->{'owner'};
314 my $canmanage = ShelfPossibleAction( $loggedinuser, $element, 'manage' );
315 my $sortfield = $shelflist->{$element}->{'sortfield'};
316 if ( $sortfield eq 'author' ) {
317 $shelflist->{$element}->{"authorsort"} = 'author';
319 if ( $sortfield eq 'year' ) {
320 $shelflist->{$element}->{"yearsort"} = 'year';
322 $shelflist->{$element}->{"viewcategory$category"} = 1;
323 $shelflist->{$element}->{manageshelf} = $canmanage;
324 if ( $owner eq $loggedinuser or $canmanage ) {
325 $shelflist->{$element}->{'mine'} = 1;
327 my $member = GetMember( 'borrowernumber' => $owner );
328 $shelflist->{$element}->{ownername} = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
329 $numberCanManage++ if $canmanage; # possibly outmoded
330 if ( $shelflist->{$element}->{'category'} eq '1' ) {
331 push( @shelveslooppriv, $shelflist->{$element} );
333 push( @shelvesloop, $shelflist->{$element} );
337 my $url = $type eq 'opac' ? "/cgi-bin/koha/opac-shelves.pl" : "/cgi-bin/koha/virtualshelves/shelves.pl";
339 foreach (qw(display viewshelf sortfield)) {
340 $qhash{$_} = $query->param($_) if $query->param($_);
342 ( scalar keys %qhash ) and $url .= '?' . join '&', map { "$_=$qhash{$_}" } keys %qhash;
343 if ( $query->param('viewshelf') ) {
344 $template->param( { pagination_bar => pagination_bar( $url, ( int( $totitems / $shelflimit ) ) + ( ( $totitems % $shelflimit ) > 0 ? 1 : 0 ), $itemoff, "itemoff" ) } );
347 { pagination_bar => pagination_bar( $url, ( int( $totshelves / $shelveslimit ) ) + ( ( $totshelves % $shelveslimit ) > 0 ? 1 : 0 ), $shelfoff, "shelfoff" ) } );
350 shelveslooppriv => \@shelveslooppriv,
351 shelvesloop => \@shelvesloop,
352 shelvesloopall => [ ( @shelvesloop, @shelveslooppriv ) ],
353 numberCanManage => $numberCanManage,
354 "BiblioDefaultView" . C4::Context->preference("BiblioDefaultView") => 1,
355 csv_profiles => GetCsvProfilesLoop()
357 if ( $template->param('viewshelf')
358 or $template->param('shelves')
359 or $template->param('edit') ) {
360 $template->param( vseflag => 1 );
362 if ($template->param('shelves') or # note: this part looks duplicative, but is intentional
363 $template->param('edit')
365 $template->param( seflag => 1 );
368 #FIXME: This refresh really only needs to happen when there is a modification of some sort
369 # to the shelves, but the above code is so convoluted in its handling of the various
370 # options, it is easier to do this refresh every time C4::VirtualShelves::Page.pm is
373 my ( $total, $pubshelves, $barshelves ) = RefreshShelvesSummary( $query->cookie("CGISESSID"), $loggedinuser, ( $loggedinuser == -1 ? 20 : 10 ) );
375 if ( defined $barshelves ) {
377 barshelves => scalar( @{ $barshelves->[0] } ),
378 barshelvesloop => $barshelves->[0],
380 $template->param( bartotal => $total->{'bartotal'}, ) if ( $total->{'bartotal'} > scalar( @{ $barshelves->[0] } ) );
383 if ( defined $pubshelves ) {
385 pubshelves => scalar( @{ $pubshelves->[0] } ),
386 pubshelvesloop => $pubshelves->[0],
388 $template->param( pubtotal => $total->{'pubtotal'}, ) if ( $total->{'pubtotal'} > scalar( @{ $pubshelves->[0] } ) );
391 output_html_with_http_headers $query, $cookie, $template->output;
399 VirtualShelves/Page.pm
403 Module used for both OPAC and intranet pages.
405 =head1 CGI PARAMETERS
409 =item C<modifyshelfcontents>
411 If this script has to modify the shelf content.
415 To know on which shelf to work.
422 * modif: show the template allowing modification of the shelves;
423 * modifsave: save changes from modif mode.
427 Load template with 'viewshelves param' displaying the shelf's information.
431 If the param shelves == 1, then add or delete a shelf.
435 If the param shelves == 1, then addshelf is the name of the shelf to add.