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 GetShelvesSummary/;
30 use C4::Auth qw/get_session/;
33 use C4::Dates qw/format_date/;
36 use vars qw($debug @EXPORT @ISA $VERSION);
41 @EXPORT = qw(&shelfpage);
42 $debug = $ENV{DEBUG} || 0;
47 redirect=>'/cgi-bin/koha/virtualshelves/shelves.pl',
50 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 } );
61 # getting the Shelves list
62 my $shelflist = GetShelves( $loggedinuser, 2 );
63 my $op = $query->param('op');
64 my $imgdir = getitemtypeimagesrc();
65 my $itemtypes = GetItemTypes();
67 # the format of this is unindented for ease of diff comparison to the old script
68 # Note: do not mistake the assignment statements below for comparisons!
70 if ( $query->param('modifyshelfcontents') ) {
71 my ($shelfnumber,$barcode,$item,$biblio);
72 if ($shelfnumber = $query->param('viewshelf')) {
73 if (ShelfPossibleAction($loggedinuser, $shelfnumber, 'manage')) {
74 if ($barcode = $query->param('addbarcode')) {
75 if ($item = GetItem( 0, $barcode )) {
76 $biblio = GetBiblioFromItemNumber($item->{'itemnumber'});
77 AddToShelf($biblio->{'biblionumber'}, $shelfnumber) or
78 push @paramsloop, {duplicatebiblio=>$barcode};
79 } else { push @paramsloop, {failgetitem=>$barcode}; }
81 (grep {/REM-(\d+)/} $query->param) or push @paramsloop, {nobarcode=>1};
82 foreach ($query->param) {
85 "SHELVES: user $loggedinuser removing item $1 from shelf $shelfnumber";
86 DelFromShelf($1, $shelfnumber); # $1 is biblionumber
89 } else { push @paramsloop, {nopermission=>$shelfnumber}; }
90 } else { push @paramsloop, {noshelfnumber=>1}; }
94 # set the default tab, etc. (for OPAC)
95 my $shelf_type = $query->param('display');
96 if (defined $shelf_type) {
97 if ($shelf_type eq 'privateshelves') {
98 $template->param(showprivateshelves => 1);
99 } elsif ($shelf_type eq 'publicshelves') {
100 $template->param(showpublicshelves => 1);
103 $debug and warn "Invalid 'display' param ($shelf_type)";
106 $template->param(showprivateshelves => 1);
109 my($okmanage, $okview);
110 my $shelfnumber = $query->param('shelfnumber') || $query->param('viewshelf');
112 $okmanage = &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
113 $okview = &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' );
119 push @paramsloop, {nopermission=>$shelfnumber};
122 if ( $op eq 'modifsave' ) {
124 'shelfname' => $query->param('shelfname'),
125 'category' => $query->param('category'),
126 'sortfield' => $query->param('sortfield'),
129 ModShelf( $shelfnumber, $shelf );
130 $shelflist = GetShelves( $loggedinuser, 2 ); # refresh after mods; this also retrieves all public shelves
132 } elsif ( $op eq 'modif' ) {
133 my ( $shelfnumber2, $shelfname, $owner, $category, $sortfield ) =GetShelf( $shelfnumber );
134 my $member = GetMember($owner,'borrowernumber');
135 my $ownername = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
138 shelfnumber => $shelfnumber2,
139 shelfname => $shelfname,
141 ownername => $ownername,
142 "category$category" => 1,
143 category => $category,
144 "sort_$sortfield" => 1,
149 if ($shelfnumber = $query->param('viewshelf') ) {
150 #check that the user can view the shelf
151 if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' ) ) {
152 my $items = GetShelfContents($shelfnumber);
153 for my $this_item (@$items) {
154 $this_item->{imageurl} = $imgdir."/".$itemtypes->{ $this_item->{itemtype} }->{'imageurl'};
155 $this_item->{'description'} = $itemtypes->{ $this_item->{itemtype} }->{'description'};
156 $this_item->{'dateadded'} = format_date($this_item->{'dateadded'});
160 foreach (grep {$i++ % 2} @$items) { # every other item
163 # my $manageshelf = &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
164 # ($manageshelf) and $showadd = 1;
166 shelfname => $shelflist->{$shelfnumber}->{'shelfname'},
167 shelfnumber => $shelfnumber,
168 viewshelf => $shelfnumber,
169 manageshelf => &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' ),
172 } else { push @paramsloop, {nopermission=>$shelfnumber} };
175 if ( $query->param('shelves') ) {
177 if (my $newshelf = $query->param('addshelf')) {
178 # note: a user can always add a new shelf
179 my $shelfnumber = AddShelf(
181 $query->param('owner'),
182 $query->param('category')
185 if ( $shelfnumber == -1 ) { #shelf already exists.
187 push @paramsloop, { already => $newshelf };
188 $template->param(shelfnumber => $shelfnumber);
190 print $query->redirect($pages{$type}->{redirect} . "?viewshelf=$shelfnumber");
194 foreach ($query->param()) {
197 unless (defined $shelflist->{$number}) {
198 push(@paramsloop, {unrecognized=>$number}); last;
200 unless (ShelfPossibleAction($loggedinuser, $number, 'manage')) {
201 push(@paramsloop, {nopermission=>$shelfnumber}); last;
203 my $contents = GetShelfContents($number);
204 if (my $count = scalar @$contents){
205 unless (scalar grep {/^CONFIRM-$number$/} $query->param()) {
206 push(@paramsloop, {need_confirm=>$shelflist->{$number}->{shelfname}, count=>$count});
207 $shelflist->{$number}->{confirm} = $number;
212 my $name = $shelflist->{$number}->{'shelfname'};
213 unless (DelShelf($number)) {
214 push(@paramsloop, {delete_fail=>$name}); last;
216 delete $shelflist->{$number};
217 push(@paramsloop, {delete_ok=>$name});
218 # print $query->redirect($pages{$type}->{redirect}); exit;
222 $stay and $template->param(shelves => 1);
227 (@paramsloop) and $template->param(paramsloop => \@paramsloop);
228 # rebuild shelflist in case a shelf has been added
229 # $shelflist = GetShelves( $loggedinuser, 2 );
230 $showadd and $template->param(showadd => 1);
233 my $numberCanManage = 0;
235 foreach my $element (sort { lc($shelflist->{$a}->{'shelfname'}) cmp lc($shelflist->{$b}->{'shelfname'}) } keys %$shelflist) {
237 $shelflist->{$element}->{shelf} = $element;
238 my $category = $shelflist->{$element}->{'category'};
239 my $owner = $shelflist->{$element}->{ 'owner' };
240 my $canmanage = ShelfPossibleAction( $loggedinuser, $element, 'manage' );
241 $shelflist->{$element}->{"viewcategory$category"} = 1;
242 $shelflist->{$element}->{canmanage} = $canmanage;
243 if ($owner eq $loggedinuser or $canmanage) {
244 $shelflist->{$element}->{'mine'} = 1;
246 my $member = GetMember($owner,'borrowernumber');
247 $shelflist->{$element}->{ownername} = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
248 $numberCanManage++ if $canmanage; # possibly outmoded
249 if ($shelflist->{$element}->{'category'} eq '1') {
250 (scalar(@shelveslooppriv) % 2) and $shelflist->{$element}->{toggle} = 1;
251 push (@shelveslooppriv, $shelflist->{$element});
253 (scalar(@shelvesloop) % 2) and $shelflist->{$element}->{toggle} = 1;
254 push (@shelvesloop, $shelflist->{$element});
259 shelveslooppriv => \@shelveslooppriv,
260 shelvesloop => \@shelvesloop,
261 shelvesloopall => [(@shelvesloop, @shelveslooppriv)],
262 numberCanManage => $numberCanManage,
263 "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
265 if ($template->param('viewshelf') or
266 $template->param( 'shelves' ) or
267 $template->param( 'edit' ) ) {
268 $template->param(vseflag => 1);
270 if ($template->param( 'shelves' ) or
271 $template->param( 'edit' ) ) {
272 $template->param( seflag => 1);
275 my $sessionID = $query->cookie("CGISESSID") ;
276 my $session = get_session($sessionID);
277 my $barshelves = GetShelvesSummary($loggedinuser, 2, 10);
278 if (defined($barshelves)) {
279 $session->param('barshelves', $barshelves);
280 $template->param( barshelves => scalar (@$barshelves));
281 $template->param( barshelvesloop => $barshelves);
284 output_html_with_http_headers $query, $cookie, $template->output;
292 VirtualShelves/Page.pm
296 Module used for both OPAC and intranet pages.
298 =head1 CGI PARAMETERS
302 =item C<modifyshelfcontents>
304 If this script has to modify the shelf content.
308 To know on which shelf to work.
315 * modif: show the template allowing modification of the shelves;
316 * modifsave: save changes from modif mode.
320 Load template with 'viewshelves param' displaying the shelf's information.
324 If the param shelves == 1, then add or delete a shelf.
328 If the param shelves == 1, then addshelf is the name of the shelf to add.