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/;
29 use C4::Auth qw/get_session/;
34 use vars qw($debug @EXPORT @ISA $VERSION);
39 @EXPORT = qw(&shelfpage);
40 $debug = $ENV{DEBUG} || 0;
45 redirect=>'/cgi-bin/koha/virtualshelves/shelves.pl',
48 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 } );
59 # getting the Shelves list
60 my $shelflist = GetShelves( $loggedinuser, 2 );
61 my $op = $query->param('op');
63 # the format of this is unindented for ease of diff comparison to the old script
64 # Note: do not mistake the assignment statements below for comparisons!
66 if ( $query->param('modifyshelfcontents') ) {
67 my ($shelfnumber,$barcode,$item,$biblio);
68 if ($shelfnumber = $query->param('viewshelf')) {
69 if (ShelfPossibleAction($loggedinuser, $shelfnumber, 'manage')) {
70 if ($barcode = $query->param('addbarcode')) {
71 if ($item = GetItem( 0, $barcode )) {
72 $biblio = GetBiblioFromItemNumber($item->{'itemnumber'});
73 AddToShelf($biblio->{'biblionumber'}, $shelfnumber) or
74 push @paramsloop, {duplicatebiblio=>$barcode};
75 } else { push @paramsloop, {failgetitem=>$barcode}; }
77 (grep {/REM-(\d+)/} $query->param) or push @paramsloop, {nobarcode=>1};
78 foreach ($query->param) {
81 "SHELVES: user $loggedinuser removing item $1 from shelf $shelfnumber";
82 DelFromShelf($1, $shelfnumber); # $1 is biblionumber
85 } else { push @paramsloop, {nopermission=>$shelfnumber}; }
86 } else { push @paramsloop, {noshelfnumber=>1}; }
90 # set the default tab, etc. (for OPAC)
91 my $shelf_type = $query->param('display');
92 if (defined $shelf_type) {
93 if ($shelf_type eq 'privateshelves') {
94 $template->param(showprivateshelves => 1);
95 } elsif ($shelf_type eq 'publicshelves') {
96 $template->param(showpublicshelves => 1);
99 $debug and warn "Invalid 'display' param ($shelf_type)";
102 $template->param(showprivateshelves => 1);
105 my($okmanage, $okview);
106 my $shelfnumber = $query->param('shelfnumber') || $query->param('viewshelf');
108 $okmanage = &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
109 $okview = &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' );
115 push @paramsloop, {nopermission=>$shelfnumber};
118 if ( $op eq 'modifsave' ) {
120 $shelfnumber, $query->param('shelfname'), $loggedinuser,
121 $query->param('category'), $query->param('sortfield')
123 $shelflist = GetShelves( $loggedinuser, 2 ); # refresh after mods
124 } elsif ( $op eq 'modif' ) {
125 my ( $shelfnumber2, $shelfname, $owner, $category, $sortfield ) =GetShelf( $query->param('shelfnumber') );
128 shelfnumber => $shelfnumber2,
129 shelfname => $shelfname,
131 "category$category" => 1,
132 "sort_$sortfield" => 1,
137 if ($shelfnumber = $query->param('viewshelf') ) {
138 #check that the user can view the shelf
139 if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' ) ) {
140 my $items = GetShelfContents($shelfnumber);
143 foreach (grep {$i++ % 2} @$items) { # every other item
146 # my $manageshelf = &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
147 # ($manageshelf) and $showadd = 1;
149 shelfname => $shelflist->{$shelfnumber}->{'shelfname'},
150 shelfnumber => $shelfnumber,
151 viewshelf => $shelfnumber,
152 manageshelf => &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' ),
155 } else { push @paramsloop, {nopermission=>$shelfnumber}; }
158 if ( $query->param('shelves') ) {
160 if (my $newshelf = $query->param('addshelf')) {
161 # note: a user can always add a new shelf
162 my $shelfnumber = AddShelf(
164 $query->param('owner'),
165 $query->param('category')
168 if ( $shelfnumber == -1 ) { #shelf already exists.
170 push @paramsloop, { already => $newshelf };
171 $template->param(shelfnumber => $shelfnumber);
173 print $query->redirect($pages{$type}->{redirect} . "?viewshelf=$shelfnumber");
177 foreach ($query->param()) {
180 unless (defined $shelflist->{$number}) {
181 push(@paramsloop, {unrecognized=>$number}); last;
183 unless (ShelfPossibleAction($loggedinuser, $number, 'manage')) {
184 push(@paramsloop, {nopermission=>$shelfnumber}); last;
186 my $contents = GetShelfContents($number);
187 if (my $count = scalar @$contents){
188 unless (scalar grep {/^CONFIRM-$number$/} $query->param()) {
189 push(@paramsloop, {need_confirm=>$shelflist->{$number}->{shelfname}, count=>$count});
190 $shelflist->{$number}->{confirm} = $number;
194 my $name = $shelflist->{$number}->{'shelfname'};
195 unless (DelShelf($number)) {
196 push(@paramsloop, {delete_fail=>$name}); last;
198 delete $shelflist->{$number};
199 push(@paramsloop, {delete_ok=>$name});
200 # print $query->redirect($pages{$type}->{redirect}); exit;
204 $stay and $template->param(shelves => 1);
209 (@paramsloop) and $template->param(paramsloop => \@paramsloop);
210 # rebuild shelflist in case a shelf has been added
211 # $shelflist = GetShelves( $loggedinuser, 2 );
212 $showadd and $template->param(showadd => 1);
215 my $numberCanManage = 0;
217 foreach my $element (sort { lc($shelflist->{$a}->{'shelfname'}) cmp lc($shelflist->{$b}->{'shelfname'}) } keys %$shelflist) {
219 $shelflist->{$element}->{shelf} = $element;
220 my $category = $shelflist->{$element}->{'category'};
221 my $owner = $shelflist->{$element}->{ 'owner' };
222 my $canmanage = ShelfPossibleAction( $loggedinuser, $element, 'manage' );
223 $shelflist->{$element}->{"viewcategory$category"} = 1;
224 $shelflist->{$element}->{canmanage} = $canmanage;
225 if ($owner eq $loggedinuser or $canmanage) {
226 $shelflist->{$element}->{'mine'} = 1;
228 my $member = GetMember($owner,'borrowernumber');
229 $shelflist->{$element}->{ownername} = $member->{firstname} . " " . $member->{surname};
230 $numberCanManage++ if $canmanage; # possibly outmoded
231 if ($shelflist->{$element}->{'category'} eq '1') {
232 (scalar(@shelveslooppriv) % 2) and $shelflist->{$element}->{toggle} = 1;
233 push (@shelveslooppriv, $shelflist->{$element});
235 (scalar(@shelvesloop) % 2) and $shelflist->{$element}->{toggle} = 1;
236 push (@shelvesloop, $shelflist->{$element});
241 shelveslooppriv => \@shelveslooppriv,
242 shelvesloop => \@shelvesloop,
243 shelvesloopall => [(@shelvesloop, @shelveslooppriv)],
244 numberCanManage => $numberCanManage,
245 "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
247 if ($template->param('viewshelf') or
248 $template->param( 'shelves' ) or
249 $template->param( 'edit' ) ) {
250 $template->param(vseflag => 1);
252 if ($template->param( 'shelves' ) or
253 $template->param( 'edit' ) ) {
254 $template->param( seflag => 1);
257 my $sessionID = $query->cookie("CGISESSID") ;
258 my $session = get_session($sessionID);
259 my $shelves = GetShelvesSummary($loggedinuser, 2, 10);
260 $session->param('shelves', $shelves);
261 $template->param( barshelves => scalar (@$shelves));
262 $template->param( barshelvesloop => $shelves);
264 output_html_with_http_headers $query, $cookie, $template->output;
272 VirtualShelves/Page.pm
276 Module used for both OPAC and intranet pages.
278 =head1 CGI PARAMETERS
282 =item C<modifyshelfcontents>
284 If this script has to modify the shelf content.
288 To know on which shelf to work.
295 * modif: show the template allowing modification of the shelves;
296 * modifsave: save changes from modif mode.
300 Load template with 'viewshelves param' displaying the shelf's information.
304 If the param shelves == 1, then add or delete a shelf.
308 If the param shelves == 1, then addshelf is the name of the shelf to add.