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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 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 # FIXME: These limits should not be hardcoded...
66 $shelflimit = 20; # Limits number of items returned for a given query
67 $shelfoffset = ( $itemoff - 1 ) * 20; # Sets the offset to begin retrieving items at
68 $shelveslimit = 20; # Limits number of shelves returned for a given query (row_count)
69 $shelvesoffset = ( $shelfoff - 1 ) * 20; # 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 if ( $item = GetItem( 0, $barcode ) ) {
93 $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'} );
94 AddToShelf( $biblio->{'biblionumber'}, $shelfnumber )
95 or push @paramsloop, { duplicatebiblio => $barcode };
97 push @paramsloop, { failgetitem => $barcode };
100 ( grep { /REM-(\d+)/ } $query->param ) or push @paramsloop, { nobarcode => 1 };
101 foreach ( $query->param ) {
103 $debug and warn "SHELVES: user $loggedinuser removing item $1 from shelf $shelfnumber";
104 DelFromShelf( $1, $shelfnumber ); # $1 is biblionumber
108 push @paramsloop, { nopermission => $shelfnumber };
111 push @paramsloop, { noshelfnumber => 1 };
117 # set the default tab, etc. (for OPAC)
118 my $shelf_type = ( $query->param('display') ? $query->param('display') : 'publicshelves' );
119 if ( defined $shelf_type ) {
120 if ( $shelf_type eq 'privateshelves' ) {
121 $template->param( showprivateshelves => 1 );
122 } elsif ( $shelf_type eq 'publicshelves' ) {
123 $template->param( showpublicshelves => 1 );
126 $debug and warn "Invalid 'display' param ($shelf_type)";
128 } elsif ( $loggedinuser == -1 ) {
129 $template->param( showpublicshelves => 1 );
131 $template->param( showprivateshelves => 1 );
134 my ( $okmanage, $okview );
135 my $shelfnumber = $query->param('shelfnumber') || $query->param('viewshelf');
137 $okmanage = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
138 $okview = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' );
146 push @paramsloop, { nopermission => $shelfnumber };
149 if ( $op eq 'modifsave' ) {
151 'shelfname' => $query->param('shelfname'),
152 'category' => $query->param('category'),
153 'sortfield' => $query->param('sortfield'),
156 ModShelf( $shelfnumber, $shelf );
158 } elsif ( $op eq 'modif' ) {
159 my ( $shelfnumber2, $shelfname, $owner, $category, $sortfield ) = GetShelf($shelfnumber);
160 my $member = GetMember( 'borrowernumber' => $owner );
161 my $ownername = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
164 shelfnumber => $shelfnumber2,
165 shelfname => $shelfname,
167 ownername => $ownername,
168 "category$category" => 1,
169 category => $category,
170 "sort_$sortfield" => 1,
175 if ( $shelfnumber = $query->param('viewshelf') ) {
177 #check that the user can view the shelf
178 if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' ) ) {
182 my $sortfield = ( $query->param('sortfield') ? $query->param('sortfield') : 'title' );
183 if ( $sortfield eq 'author' ) {
184 $authorsort = 'author';
186 if ( $sortfield eq 'year' ) {
189 ( $items, $totitems ) = GetShelfContents( $shelfnumber, $shelflimit, $shelfoffset );
190 for my $this_item (@$items) {
191 my $record = GetMarcBiblio( $this_item->{'biblionumber'} );
193 # the virtualshelfcontents table does not store these columns nor are they retrieved from the items
194 # and itemtypes tables, so I'm commenting them out for now to quiet the log -crn
195 #$this_item->{imageurl} = $imgdir."/".$itemtypes->{ $this_item->{itemtype} }->{'imageurl'};
196 #$this_item->{'description'} = $itemtypes->{ $this_item->{itemtype} }->{'description'};
197 $this_item->{'dateadded'} = format_date( $this_item->{'dateadded'} );
198 $this_item->{'imageurl'} = getitemtypeinfo( $this_item->{'itemtype'} )->{'imageurl'};
199 $this_item->{'coins'} = GetCOinSBiblio( $this_item->{'biblionumber'} );
200 $this_item->{'subtitle'} = C4::Biblio::get_koha_field_from_marc( 'bibliosubtitle', 'subtitle', $record, '' );
202 # Getting items infos for location display
203 my @items_infos = &GetItemsInfo( $this_item->{'biblionumber'}, $type );
204 $this_item->{'ITEM_RESULTS'} = \@items_infos;
207 push @paramsloop, { display => 'privateshelves' } if $category == 1;
210 my $manageshelf = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
212 shelfname => $shelflist->{$shelfnumber}->{'shelfname'} || $privshelflist->{$shelfnumber}->{'shelfname'},
213 shelfnumber => $shelfnumber,
214 viewshelf => $shelfnumber,
215 authorsort => $authorsort,
216 yearsort => $yearsort,
217 manageshelf => $manageshelf,
221 push @paramsloop, { nopermission => $shelfnumber };
225 if ( $query->param('shelves') ) {
227 if ( my $newshelf = $query->param('addshelf') ) {
229 # note: a user can always add a new shelf
230 my $shelfnumber = AddShelf( $newshelf, $query->param('owner'), $query->param('category'), $query->param('sortfield') );
232 if ( $shelfnumber == -1 ) { #shelf already exists.
234 push @paramsloop, { already => $newshelf };
235 $template->param( shelfnumber => $shelfnumber );
237 print $query->redirect( $pages{$type}->{redirect} . "?viewshelf=$shelfnumber" );
241 foreach ( $query->param() ) {
245 unless ( defined $shelflist->{$number} || defined $privshelflist->{$number} ) {
246 push( @paramsloop, { unrecognized => $number } );
249 unless ( ShelfPossibleAction( $loggedinuser, $number, 'manage' ) ) {
250 push( @paramsloop, { nopermission => $shelfnumber } );
254 ( $contents, $totshelves ) = GetShelfContents( $number, $shelveslimit, $shelvesoffset );
255 if ( my $count = scalar @$contents ) {
256 unless ( scalar grep { /^CONFIRM-$number$/ } $query->param() ) {
257 if ( defined $shelflist->{$number} ) {
258 push( @paramsloop, { need_confirm => $shelflist->{$number}->{shelfname}, count => $count } );
259 $shelflist->{$number}->{confirm} = $number;
261 push( @paramsloop, { need_confirm => $privshelflist->{$number}->{shelfname}, count => $count } );
262 $privshelflist->{$number}->{confirm} = $number;
269 if ( defined $shelflist->{$number} ) {
270 $name = $shelflist->{$number}->{'shelfname'};
271 delete $shelflist->{$number};
273 $name = $privshelflist->{$number}->{'shelfname'};
274 delete $privshelflist->{$number};
276 unless ( DelShelf($number) ) {
277 push( @paramsloop, { delete_fail => $name } );
280 push( @paramsloop, { delete_ok => $name } );
282 # print $query->redirect($pages{$type}->{redirect}); exit;
286 $stay and $template->param( shelves => 1 );
291 (@paramsloop) and $template->param( paramsloop => \@paramsloop );
292 $showadd and $template->param( showadd => 1 );
295 my $numberCanManage = 0;
297 # rebuild shelflist in case a shelf has been added
298 ( $shelflist, $totshelves ) = GetShelves( $category, $shelveslimit, $shelvesoffset, $loggedinuser ) unless $delflag;
299 foreach my $element ( sort { lc( $shelflist->{$a}->{'shelfname'} ) cmp lc( $shelflist->{$b}->{'shelfname'} ) } keys %$shelflist ) {
301 $shelflist->{$element}->{shelf} = $element;
302 my $category = $shelflist->{$element}->{'category'};
303 my $owner = $shelflist->{$element}->{'owner'};
304 my $canmanage = ShelfPossibleAction( $loggedinuser, $element, 'manage' );
305 $shelflist->{$element}->{"viewcategory$category"} = 1;
306 $shelflist->{$element}->{manageshelf} = $canmanage;
307 if ( $owner eq $loggedinuser or $canmanage ) {
308 $shelflist->{$element}->{'mine'} = 1;
310 my $member = GetMember( 'borrowernumber' => $owner );
311 $shelflist->{$element}->{ownername} = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
312 $numberCanManage++ if $canmanage; # possibly outmoded
313 if ( $shelflist->{$element}->{'category'} eq '1' ) {
314 push( @shelveslooppriv, $shelflist->{$element} );
316 push( @shelvesloop, $shelflist->{$element} );
320 my $url = $type eq 'opac' ? "/cgi-bin/koha/opac-shelves.pl" : "/cgi-bin/koha/virtualshelves/shelves.pl";
322 foreach (qw(display viewshelf sortfield)) {
323 $qhash{$_} = $query->param($_) if $query->param($_);
325 ( scalar keys %qhash ) and $url .= '?' . join '&', map { "$_=$qhash{$_}" } keys %qhash;
326 if ( $query->param('viewshelf') ) {
327 $template->param( { pagination_bar => pagination_bar( $url, ( int( $totitems / $shelflimit ) ) + ( ( $totitems % $shelflimit ) > 0 ? 1 : 0 ), $itemoff, "itemoff" ) } );
330 { pagination_bar => pagination_bar( $url, ( int( $totshelves / $shelveslimit ) ) + ( ( $totshelves % $shelveslimit ) > 0 ? 1 : 0 ), $shelfoff, "shelfoff" ) } );
333 shelveslooppriv => \@shelveslooppriv,
334 shelvesloop => \@shelvesloop,
335 shelvesloopall => [ ( @shelvesloop, @shelveslooppriv ) ],
336 numberCanManage => $numberCanManage,
337 "BiblioDefaultView" . C4::Context->preference("BiblioDefaultView") => 1,
338 csv_profiles => GetCsvProfilesLoop()
340 if ( $template->param('viewshelf')
341 or $template->param('shelves')
342 or $template->param('edit') ) {
343 $template->param( vseflag => 1 );
345 if ($template->param('shelves') or # note: this part looks duplicative, but is intentional
346 $template->param('edit')
348 $template->param( seflag => 1 );
351 #FIXME: This refresh really only needs to happen when there is a modification of some sort
352 # to the shelves, but the above code is so convoluted in its handling of the various
353 # options, it is easier to do this refresh every time C4::VirtualShelves::Page.pm is
356 my ( $total, $pubshelves, $barshelves ) = RefreshShelvesSummary( $query->cookie("CGISESSID"), $loggedinuser, ( $loggedinuser == -1 ? 20 : 10 ) );
358 if ( defined $barshelves ) {
360 barshelves => scalar( @{ $barshelves->[0] } ),
361 barshelvesloop => $barshelves->[0],
363 $template->param( bartotal => $total->{'bartotal'}, ) if ( $total->{'bartotal'} > scalar( @{ $barshelves->[0] } ) );
366 if ( defined $pubshelves ) {
368 pubshelves => scalar( @{ $pubshelves->[0] } ),
369 pubshelvesloop => $pubshelves->[0],
371 $template->param( pubtotal => $total->{'pubtotal'}, ) if ( $total->{'pubtotal'} > scalar( @{ $pubshelves->[0] } ) );
374 output_html_with_http_headers $query, $cookie, $template->output;
382 VirtualShelves/Page.pm
386 Module used for both OPAC and intranet pages.
388 =head1 CGI PARAMETERS
392 =item C<modifyshelfcontents>
394 If this script has to modify the shelf content.
398 To know on which shelf to work.
405 * modif: show the template allowing modification of the shelves;
406 * modifsave: save changes from modif mode.
410 Load template with 'viewshelves param' displaying the shelf's information.
414 If the param shelves == 1, then add or delete a shelf.
418 If the param shelves == 1, then addshelf is the name of the shelf to add.