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/;
37 use vars qw($debug @EXPORT @ISA $VERSION);
42 @EXPORT = qw(&shelfpage);
43 $debug = $ENV{DEBUG} || 0;
48 redirect=>'/cgi-bin/koha/virtualshelves/shelves.pl',
51 redirect=>'/cgi-bin/koha/opac-shelves.pl',
55 sub shelfpage ($$$$$) {
56 my ($type, $query, $template, $loggedinuser, $cookie ) = @_;
57 ($pages{$type}) or $type = 'opac';
58 $query or die "No query";
59 $template or die "No template";
60 $template->param( { loggedinuser => $loggedinuser } );
63 my $shelfoff = ($query->param('shelfoff') ? $query->param('shelfoff') : 1);
64 my $itemoff = ($query->param('itemoff') ? $query->param('itemoff') : 1);
65 my $displaymode = ($query->param('display') ? $query->param('display') : 'publicshelves');
66 my ($shelflimit, $shelfoffset, $shelveslimit, $shelvesoffset);
67 # FIXME: These limits should not be hardcoded...
68 $shelflimit = 20; # Limits number of items returned for a given query
69 $shelfoffset = ($itemoff - 1) * 20; # Sets the offset to begin retrieving items at
70 $shelveslimit = 20; # Limits number of shelves returned for a given query (row_count)
71 $shelvesoffset = ($shelfoff - 1) * 20; # Sets the offset to begin retrieving shelves at (offset)
72 # getting the Shelves list
73 my $category = (($displaymode eq 'privateshelves') ? 1 : 2);
74 my ($shelflist, $totshelves) = GetShelves( $category, $shelveslimit, $shelvesoffset, $loggedinuser );
75 #Get a list of private shelves for possible deletion. Only do this when we've defaulted to public shelves
76 my ($privshelflist, $privtotshelves);
78 ($privshelflist, $privtotshelves) = GetShelves( 1, $shelveslimit, $shelvesoffset, $loggedinuser );
80 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) or
95 push @paramsloop, {duplicatebiblio=>$barcode};
96 } else { push @paramsloop, {failgetitem=>$barcode}; }
98 (grep {/REM-(\d+)/} $query->param) or push @paramsloop, {nobarcode=>1};
99 foreach ($query->param) {
102 "SHELVES: user $loggedinuser removing item $1 from shelf $shelfnumber";
103 DelFromShelf($1, $shelfnumber); # $1 is biblionumber
106 } else { push @paramsloop, {nopermission=>$shelfnumber}; }
107 } else { push @paramsloop, {noshelfnumber=>1}; }
111 # set the default tab, etc. (for OPAC)
112 my $shelf_type = ($query->param('display') ? $query->param('display') : 'publicshelves');
113 if (defined $shelf_type) {
114 if ($shelf_type eq 'privateshelves') {
115 $template->param(showprivateshelves => 1);
116 } elsif ($shelf_type eq 'publicshelves') {
117 $template->param(showpublicshelves => 1);
120 $debug and warn "Invalid 'display' param ($shelf_type)";
122 } elsif ($loggedinuser == -1) {
123 $template->param(showpublicshelves => 1);
125 $template->param(showprivateshelves => 1);
128 my($okmanage, $okview);
129 my $shelfnumber = $query->param('shelfnumber') || $query->param('viewshelf');
131 $okmanage = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
132 $okview = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' );
140 push @paramsloop, {nopermission=>$shelfnumber};
143 if ( $op eq 'modifsave' ) {
145 'shelfname' => $query->param('shelfname'),
146 'category' => $query->param('category'),
147 'sortfield' => $query->param('sortfield'),
150 ModShelf( $shelfnumber, $shelf );
152 } elsif ( $op eq 'modif' ) {
153 my ( $shelfnumber2, $shelfname, $owner, $category, $sortfield ) =GetShelf( $shelfnumber );
154 my $member = GetMember($owner,'borrowernumber');
155 my $ownername = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
158 shelfnumber => $shelfnumber2,
159 shelfname => $shelfname,
161 ownername => $ownername,
162 "category$category" => 1,
163 category => $category,
164 "sort_$sortfield" => 1,
169 if ($shelfnumber = $query->param('viewshelf') ) {
170 #check that the user can view the shelf
171 if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' ) ) {
173 ($items, $totitems) = GetShelfContents($shelfnumber, $shelflimit, $shelfoffset);
174 for my $this_item (@$items) {
175 # the virtualshelfcontents table does not store these columns nor are they retrieved from the items
176 # and itemtypes tables, so I'm commenting them out for now to quiet the log -crn
177 #$this_item->{imageurl} = $imgdir."/".$itemtypes->{ $this_item->{itemtype} }->{'imageurl'};
178 #$this_item->{'description'} = $itemtypes->{ $this_item->{itemtype} }->{'description'};
179 $this_item->{'dateadded'} = format_date($this_item->{'dateadded'});
180 $this_item->{'imageurl'} = getitemtypeinfo($this_item->{'itemtype'})->{'imageurl'};
182 push @paramsloop, {display => 'privateshelves'} if $category == 1;
185 foreach (grep {$i++ % 2} @$items) { # every other item
188 my $manageshelf = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
190 shelfname => $shelflist->{$shelfnumber}->{'shelfname'} || $privshelflist->{$shelfnumber}->{'shelfname'},
191 shelfnumber => $shelfnumber,
192 viewshelf => $shelfnumber,
193 manageshelf => $manageshelf,
196 } else { push @paramsloop, {nopermission=>$shelfnumber} };
199 if ( $query->param('shelves') ) {
201 if (my $newshelf = $query->param('addshelf')) {
202 # note: a user can always add a new shelf
203 my $shelfnumber = AddShelf(
205 $query->param('owner'),
206 $query->param('category'),
207 $query->param('sortfield')
210 if ( $shelfnumber == -1 ) { #shelf already exists.
212 push @paramsloop, { already => $newshelf };
213 $template->param(shelfnumber => $shelfnumber);
215 print $query->redirect($pages{$type}->{redirect} . "?viewshelf=$shelfnumber");
219 foreach ($query->param()) {
223 unless (defined $shelflist->{$number} || defined $privshelflist->{$number}) {
224 push(@paramsloop, {unrecognized=>$number}); last;
226 unless (ShelfPossibleAction($loggedinuser, $number, 'manage')) {
227 push(@paramsloop, {nopermission=>$shelfnumber}); last;
230 ($contents, $totshelves) = GetShelfContents($number, $shelveslimit, $shelvesoffset);
231 if (my $count = scalar @$contents){
232 unless (scalar grep {/^CONFIRM-$number$/} $query->param()) {
233 if (defined $shelflist->{$number}) {
234 push(@paramsloop, {need_confirm=>$shelflist->{$number}->{shelfname}, count=>$count});
235 $shelflist->{$number}->{confirm} = $number;
237 push(@paramsloop, {need_confirm=>$privshelflist->{$number}->{shelfname}, count=>$count});
238 $privshelflist->{$number}->{confirm} = $number;
245 if (defined $shelflist->{$number}) {
246 $name = $shelflist->{$number}->{'shelfname'};
247 delete $shelflist->{$number};
249 $name = $privshelflist->{$number}->{'shelfname'};
250 delete $privshelflist->{$number};
252 unless (DelShelf($number)) {
253 push(@paramsloop, {delete_fail=>$name}); last;
255 push(@paramsloop, {delete_ok=>$name});
256 # print $query->redirect($pages{$type}->{redirect}); exit;
260 $stay and $template->param(shelves => 1);
265 (@paramsloop) and $template->param(paramsloop => \@paramsloop);
266 $showadd and $template->param(showadd => 1);
269 my $numberCanManage = 0;
271 # rebuild shelflist in case a shelf has been added
272 ($shelflist, $totshelves) = GetShelves( $category, $shelveslimit, $shelvesoffset, $loggedinuser ) unless $delflag;
273 foreach my $element (sort { lc($shelflist->{$a}->{'shelfname'}) cmp lc($shelflist->{$b}->{'shelfname'}) } keys %$shelflist) {
275 $shelflist->{$element}->{shelf} = $element;
276 my $category = $shelflist->{$element}->{'category'};
277 my $owner = $shelflist->{$element}->{ 'owner' };
278 my $canmanage = ShelfPossibleAction( $loggedinuser, $element, 'manage' );
279 $shelflist->{$element}->{"viewcategory$category"} = 1;
280 $shelflist->{$element}->{manageshelf} = $canmanage;
281 if ($owner eq $loggedinuser or $canmanage) {
282 $shelflist->{$element}->{'mine'} = 1;
284 my $member = GetMember($owner,'borrowernumber');
285 $shelflist->{$element}->{ownername} = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
286 $numberCanManage++ if $canmanage; # possibly outmoded
287 if ($shelflist->{$element}->{'category'} eq '1') {
288 (scalar(@shelveslooppriv) % 2) and $shelflist->{$element}->{toggle} = 1;
289 push (@shelveslooppriv, $shelflist->{$element});
291 (scalar(@shelvesloop) % 2) and $shelflist->{$element}->{toggle} = 1;
292 push (@shelvesloop, $shelflist->{$element});
296 my $url = $type eq 'opac' ? "/cgi-bin/koha/opac-shelves.pl" : "/cgi-bin/koha/virtualshelves/shelves.pl";
298 foreach (qw(display viewshelf)) {
299 $qhash{$_} = $query->param($_) if $query->param($_);
301 (scalar keys %qhash) and $url .= '?' . join '&', map {"$_=$qhash{$_}"} keys %qhash;
302 if ($query->param('viewshelf')) {
303 $template->param( {pagination_bar => pagination_bar($url, (int($totitems/$shelflimit)) + (($totitems % $shelflimit) > 0 ? 1 : 0), $itemoff, "itemoff")} );
305 $template->param( {pagination_bar => pagination_bar($url, (int($totshelves/$shelveslimit)) + (($totshelves % $shelveslimit) > 0 ? 1 : 0), $shelfoff, "shelfoff")} );
308 shelveslooppriv => \@shelveslooppriv,
309 shelvesloop => \@shelvesloop,
310 shelvesloopall => [(@shelvesloop, @shelveslooppriv)],
311 numberCanManage => $numberCanManage,
312 "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
314 if ($template->param('viewshelf') or
315 $template->param( 'shelves' ) or
316 $template->param( 'edit' ) ) {
317 $template->param(vseflag => 1);
319 if ($template->param( 'shelves' ) or # note: this part looks duplicative, but is intentional
320 $template->param( 'edit' ) ) {
321 $template->param( seflag => 1);
324 #FIXME: This refresh really only needs to happen when there is a modification of some sort
325 # to the shelves, but the above code is so convoluted in its handling of the various
326 # options, it is easier to do this refresh every time C4::VirtualShelves::Page.pm is
329 my ($total, $pubshelves, $barshelves) = RefreshShelvesSummary($query->cookie("CGISESSID"),$loggedinuser,($loggedinuser == -1 ? 20 : 10));
331 if (defined $barshelves) {
332 $template->param( barshelves => scalar (@{$barshelves->[0]}),
333 barshelvesloop => $barshelves->[0],
335 $template->param( bartotal => $total->{'bartotal'}, ) if ($total->{'bartotal'} > scalar (@{$barshelves->[0]}));
338 if (defined $pubshelves) {
339 $template->param( pubshelves => scalar (@{$pubshelves->[0]}),
340 pubshelvesloop => $pubshelves->[0],
342 $template->param( pubtotal => $total->{'pubtotal'}, ) if ($total->{'pubtotal'} > scalar (@{$pubshelves->[0]}));
345 output_html_with_http_headers $query, $cookie, $template->output;
353 VirtualShelves/Page.pm
357 Module used for both OPAC and intranet pages.
359 =head1 CGI PARAMETERS
363 =item C<modifyshelfcontents>
365 If this script has to modify the shelf content.
369 To know on which shelf to work.
376 * modif: show the template allowing modification of the shelves;
377 * modifsave: save changes from modif mode.
381 Load template with 'viewshelves param' displaying the shelf's information.
385 If the param shelves == 1, then add or delete a shelf.
389 If the param shelves == 1, then addshelf is the name of the shelf to add.