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 # 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 $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') ) {
178 #check that the user can view the shelf
179 if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' ) ) {
183 my $sortfield = ( $query->param('sortfield') ? $query->param('sortfield') : 'title' );
184 if ( $sortfield eq 'author' ) {
185 $authorsort = 'author';
187 if ( $sortfield eq 'year' ) {
190 ( $items, $totitems ) = GetShelfContents( $shelfnumber, $shelflimit, $shelfoffset );
191 for my $this_item (@$items) {
192 my $record = GetMarcBiblio( $this_item->{'biblionumber'} );
194 # the virtualshelfcontents table does not store these columns nor are they retrieved from the items
195 # and itemtypes tables, so I'm commenting them out for now to quiet the log -crn
196 #$this_item->{imageurl} = $imgdir."/".$itemtypes->{ $this_item->{itemtype} }->{'imageurl'};
197 #$this_item->{'description'} = $itemtypes->{ $this_item->{itemtype} }->{'description'};
198 $this_item->{'dateadded'} = format_date( $this_item->{'dateadded'} );
199 $this_item->{'imageurl'} = getitemtypeinfo( $this_item->{'itemtype'} )->{'imageurl'};
200 $this_item->{'coins'} = GetCOinSBiblio( $this_item->{'biblionumber'} );
201 $this_item->{'subtitle'} = GetRecordValue('subtitle', $record, GetFrameworkCode($this_item->{'biblionumber'}));
203 # Getting items infos for location display
204 my @items_infos = &GetItemsInfo( $this_item->{'biblionumber'}, $type );
205 $this_item->{'ITEM_RESULTS'} = \@items_infos;
208 push @paramsloop, { display => 'privateshelves' } if $category == 1;
211 my $manageshelf = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
213 shelfname => $shelflist->{$shelfnumber}->{'shelfname'} || $privshelflist->{$shelfnumber}->{'shelfname'},
214 shelfnumber => $shelfnumber,
215 viewshelf => $shelfnumber,
216 authorsort => $authorsort,
217 yearsort => $yearsort,
218 manageshelf => $manageshelf,
222 push @paramsloop, { nopermission => $shelfnumber };
226 if ( $query->param('shelves') ) {
228 if ( my $newshelf = $query->param('addshelf') ) {
230 # note: a user can always add a new shelf
231 my $shelfnumber = AddShelf( $newshelf, $query->param('owner'), $query->param('category'), $query->param('sortfield') );
233 if ( $shelfnumber == -1 ) { #shelf already exists.
235 push @paramsloop, { already => $newshelf };
236 $template->param( shelfnumber => $shelfnumber );
238 print $query->redirect( $pages{$type}->{redirect} . "?viewshelf=$shelfnumber" );
242 foreach ( $query->param() ) {
246 unless ( defined $shelflist->{$number} || defined $privshelflist->{$number} ) {
247 push( @paramsloop, { unrecognized => $number } );
250 unless ( ShelfPossibleAction( $loggedinuser, $number, 'manage' ) ) {
251 push( @paramsloop, { nopermission => $shelfnumber } );
255 ( $contents, $totshelves ) = GetShelfContents( $number, $shelveslimit, $shelvesoffset );
256 if ( my $count = scalar @$contents ) {
257 unless ( scalar grep { /^CONFIRM-$number$/ } $query->param() ) {
258 if ( defined $shelflist->{$number} ) {
259 push( @paramsloop, { need_confirm => $shelflist->{$number}->{shelfname}, count => $count } );
260 $shelflist->{$number}->{confirm} = $number;
262 push( @paramsloop, { need_confirm => $privshelflist->{$number}->{shelfname}, count => $count } );
263 $privshelflist->{$number}->{confirm} = $number;
270 if ( defined $shelflist->{$number} ) {
271 $name = $shelflist->{$number}->{'shelfname'};
272 delete $shelflist->{$number};
274 $name = $privshelflist->{$number}->{'shelfname'};
275 delete $privshelflist->{$number};
277 unless ( DelShelf($number) ) {
278 push( @paramsloop, { delete_fail => $name } );
281 push( @paramsloop, { delete_ok => $name } );
283 # print $query->redirect($pages{$type}->{redirect}); exit;
287 $stay and $template->param( shelves => 1 );
292 (@paramsloop) and $template->param( paramsloop => \@paramsloop );
293 $showadd and $template->param( showadd => 1 );
296 my $numberCanManage = 0;
298 # rebuild shelflist in case a shelf has been added
299 ( $shelflist, $totshelves ) = GetShelves( $category, $shelveslimit, $shelvesoffset, $loggedinuser ) unless $delflag;
300 foreach my $element ( sort { lc( $shelflist->{$a}->{'shelfname'} ) cmp lc( $shelflist->{$b}->{'shelfname'} ) } keys %$shelflist ) {
302 $shelflist->{$element}->{shelf} = $element;
303 my $category = $shelflist->{$element}->{'category'};
304 my $owner = $shelflist->{$element}->{'owner'};
305 my $canmanage = ShelfPossibleAction( $loggedinuser, $element, 'manage' );
306 my $sortfield = $shelflist->{$element}->{'sortfield'};
307 if ( $sortfield eq 'author' ) {
308 $shelflist->{$element}->{"authorsort"} = 'author';
310 if ( $sortfield eq 'year' ) {
311 $shelflist->{$element}->{"yearsort"} = 'year';
313 $shelflist->{$element}->{"viewcategory$category"} = 1;
314 $shelflist->{$element}->{manageshelf} = $canmanage;
315 if ( $owner eq $loggedinuser or $canmanage ) {
316 $shelflist->{$element}->{'mine'} = 1;
318 my $member = GetMember( 'borrowernumber' => $owner );
319 $shelflist->{$element}->{ownername} = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
320 $numberCanManage++ if $canmanage; # possibly outmoded
321 if ( $shelflist->{$element}->{'category'} eq '1' ) {
322 push( @shelveslooppriv, $shelflist->{$element} );
324 push( @shelvesloop, $shelflist->{$element} );
328 my $url = $type eq 'opac' ? "/cgi-bin/koha/opac-shelves.pl" : "/cgi-bin/koha/virtualshelves/shelves.pl";
330 foreach (qw(display viewshelf sortfield)) {
331 $qhash{$_} = $query->param($_) if $query->param($_);
333 ( scalar keys %qhash ) and $url .= '?' . join '&', map { "$_=$qhash{$_}" } keys %qhash;
334 if ( $query->param('viewshelf') ) {
335 $template->param( { pagination_bar => pagination_bar( $url, ( int( $totitems / $shelflimit ) ) + ( ( $totitems % $shelflimit ) > 0 ? 1 : 0 ), $itemoff, "itemoff" ) } );
338 { pagination_bar => pagination_bar( $url, ( int( $totshelves / $shelveslimit ) ) + ( ( $totshelves % $shelveslimit ) > 0 ? 1 : 0 ), $shelfoff, "shelfoff" ) } );
341 shelveslooppriv => \@shelveslooppriv,
342 shelvesloop => \@shelvesloop,
343 shelvesloopall => [ ( @shelvesloop, @shelveslooppriv ) ],
344 numberCanManage => $numberCanManage,
345 "BiblioDefaultView" . C4::Context->preference("BiblioDefaultView") => 1,
346 csv_profiles => GetCsvProfilesLoop()
348 if ( $template->param('viewshelf')
349 or $template->param('shelves')
350 or $template->param('edit') ) {
351 $template->param( vseflag => 1 );
353 if ($template->param('shelves') or # note: this part looks duplicative, but is intentional
354 $template->param('edit')
356 $template->param( seflag => 1 );
359 #FIXME: This refresh really only needs to happen when there is a modification of some sort
360 # to the shelves, but the above code is so convoluted in its handling of the various
361 # options, it is easier to do this refresh every time C4::VirtualShelves::Page.pm is
364 my ( $total, $pubshelves, $barshelves ) = RefreshShelvesSummary( $query->cookie("CGISESSID"), $loggedinuser, ( $loggedinuser == -1 ? 20 : 10 ) );
366 if ( defined $barshelves ) {
368 barshelves => scalar( @{ $barshelves->[0] } ),
369 barshelvesloop => $barshelves->[0],
371 $template->param( bartotal => $total->{'bartotal'}, ) if ( $total->{'bartotal'} > scalar( @{ $barshelves->[0] } ) );
374 if ( defined $pubshelves ) {
376 pubshelves => scalar( @{ $pubshelves->[0] } ),
377 pubshelvesloop => $pubshelves->[0],
379 $template->param( pubtotal => $total->{'pubtotal'}, ) if ( $total->{'pubtotal'} > scalar( @{ $pubshelves->[0] } ) );
382 output_html_with_http_headers $query, $cookie, $template->output;
390 VirtualShelves/Page.pm
394 Module used for both OPAC and intranet pages.
396 =head1 CGI PARAMETERS
400 =item C<modifyshelfcontents>
402 If this script has to modify the shelf content.
406 To know on which shelf to work.
413 * modif: show the template allowing modification of the shelves;
414 * modifsave: save changes from modif mode.
418 Load template with 'viewshelves param' displaying the shelf's information.
422 If the param shelves == 1, then add or delete a shelf.
426 If the param shelves == 1, then addshelf is the name of the shelf to add.