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/;
35 use vars qw($debug @EXPORT @ISA $VERSION);
40 @EXPORT = qw(&shelfpage);
41 $debug = $ENV{DEBUG} || 0;
46 redirect=>'/cgi-bin/koha/virtualshelves/shelves.pl',
49 redirect=>'/cgi-bin/koha/opac-shelves.pl',
53 sub shelfpage ($$$$$) {
54 my ($type, $query, $template, $loggedinuser, $cookie ) = @_;
55 ($pages{$type}) or $type = 'opac';
56 $query or die "No query";
57 $template or die "No template";
58 $template->param( { loggedinuser => $loggedinuser } );
60 # getting the Shelves list
61 my $shelflist = GetShelves( $loggedinuser, 2 );
62 my $op = $query->param('op');
63 my $imgdir = getitemtypeimagesrc();
64 my $itemtypes = GetItemTypes();
66 # the format of this is unindented for ease of diff comparison to the old script
67 # Note: do not mistake the assignment statements below for comparisons!
69 if ( $query->param('modifyshelfcontents') ) {
70 my ($shelfnumber,$barcode,$item,$biblio);
71 if ($shelfnumber = $query->param('viewshelf')) {
72 if (ShelfPossibleAction($loggedinuser, $shelfnumber, 'manage')) {
73 if ($barcode = $query->param('addbarcode')) {
74 if ($item = GetItem( 0, $barcode )) {
75 $biblio = GetBiblioFromItemNumber($item->{'itemnumber'});
76 AddToShelf($biblio->{'biblionumber'}, $shelfnumber) or
77 push @paramsloop, {duplicatebiblio=>$barcode};
78 } else { push @paramsloop, {failgetitem=>$barcode}; }
80 (grep {/REM-(\d+)/} $query->param) or push @paramsloop, {nobarcode=>1};
81 foreach ($query->param) {
84 "SHELVES: user $loggedinuser removing item $1 from shelf $shelfnumber";
85 DelFromShelf($1, $shelfnumber); # $1 is biblionumber
88 } else { push @paramsloop, {nopermission=>$shelfnumber}; }
89 } else { push @paramsloop, {noshelfnumber=>1}; }
93 # set the default tab, etc. (for OPAC)
94 my $shelf_type = $query->param('display');
95 if (defined $shelf_type) {
96 if ($shelf_type eq 'privateshelves') {
97 $template->param(showprivateshelves => 1);
98 } elsif ($shelf_type eq 'publicshelves') {
99 $template->param(showpublicshelves => 1);
102 $debug and warn "Invalid 'display' param ($shelf_type)";
105 $template->param(showprivateshelves => 1);
108 my($okmanage, $okview);
109 my $shelfnumber = $query->param('shelfnumber') || $query->param('viewshelf');
111 $okmanage = &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
112 $okview = &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' );
118 push @paramsloop, {nopermission=>$shelfnumber};
121 if ( $op eq 'modifsave' ) {
123 'shelfname' => $query->param('shelfname'),
124 'category' => $query->param('category'),
125 'sortfield' => $query->param('sortfield'),
127 $shelf->{'owner'} = $loggedinuser if $type eq 'intranet'; #we only overwrite the list owner if &ModShelf was called from the staff client
129 ModShelf( $shelfnumber, $shelf );
130 $shelflist = GetShelves( $loggedinuser, 2 ); # refresh after mods
131 } elsif ( $op eq 'modif' ) {
132 my ( $shelfnumber2, $shelfname, $owner, $category, $sortfield ) =GetShelf( $shelfnumber );
135 shelfnumber => $shelfnumber2,
136 shelfname => $shelfname,
138 "category$category" => 1,
139 category => $category,
140 "sort_$sortfield" => 1,
145 if ($shelfnumber = $query->param('viewshelf') ) {
146 #check that the user can view the shelf
147 if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' ) ) {
148 my $items = GetShelfContents($shelfnumber);
149 for my $this_item (@$items) {
150 $this_item->{imageurl} = $imgdir."/".$itemtypes->{ $this_item->{itemtype} }->{'imageurl'};
151 $this_item->{'description'} = $itemtypes->{ $this_item->{itemtype} }->{'description'};
155 foreach (grep {$i++ % 2} @$items) { # every other item
158 # my $manageshelf = &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
159 # ($manageshelf) and $showadd = 1;
161 shelfname => $shelflist->{$shelfnumber}->{'shelfname'},
162 shelfnumber => $shelfnumber,
163 viewshelf => $shelfnumber,
164 manageshelf => &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' ),
167 } else { push @paramsloop, {nopermission=>$shelfnumber} };
170 if ( $query->param('shelves') ) {
172 if (my $newshelf = $query->param('addshelf')) {
173 # note: a user can always add a new shelf
174 my $shelfnumber = AddShelf(
176 $query->param('owner'),
177 $query->param('category')
180 if ( $shelfnumber == -1 ) { #shelf already exists.
182 push @paramsloop, { already => $newshelf };
183 $template->param(shelfnumber => $shelfnumber);
185 print $query->redirect($pages{$type}->{redirect} . "?viewshelf=$shelfnumber");
189 foreach ($query->param()) {
192 unless (defined $shelflist->{$number}) {
193 push(@paramsloop, {unrecognized=>$number}); last;
195 unless (ShelfPossibleAction($loggedinuser, $number, 'manage')) {
196 push(@paramsloop, {nopermission=>$shelfnumber}); last;
198 my $contents = GetShelfContents($number);
199 if (my $count = scalar @$contents){
200 unless (scalar grep {/^CONFIRM-$number$/} $query->param()) {
201 push(@paramsloop, {need_confirm=>$shelflist->{$number}->{shelfname}, count=>$count});
202 $shelflist->{$number}->{confirm} = $number;
207 my $name = $shelflist->{$number}->{'shelfname'};
208 unless (DelShelf($number)) {
209 push(@paramsloop, {delete_fail=>$name}); last;
211 delete $shelflist->{$number};
212 push(@paramsloop, {delete_ok=>$name});
213 # print $query->redirect($pages{$type}->{redirect}); exit;
217 $stay and $template->param(shelves => 1);
222 (@paramsloop) and $template->param(paramsloop => \@paramsloop);
223 # rebuild shelflist in case a shelf has been added
224 # $shelflist = GetShelves( $loggedinuser, 2 );
225 $showadd and $template->param(showadd => 1);
228 my $numberCanManage = 0;
230 foreach my $element (sort { lc($shelflist->{$a}->{'shelfname'}) cmp lc($shelflist->{$b}->{'shelfname'}) } keys %$shelflist) {
232 $shelflist->{$element}->{shelf} = $element;
233 my $category = $shelflist->{$element}->{'category'};
234 my $owner = $shelflist->{$element}->{ 'owner' };
235 my $canmanage = ShelfPossibleAction( $loggedinuser, $element, 'manage' );
236 $shelflist->{$element}->{"viewcategory$category"} = 1;
237 $shelflist->{$element}->{canmanage} = $canmanage;
238 if ($owner eq $loggedinuser or $canmanage) {
239 $shelflist->{$element}->{'mine'} = 1;
241 my $member = GetMember($owner,'borrowernumber');
242 $shelflist->{$element}->{ownername} = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
243 $numberCanManage++ if $canmanage; # possibly outmoded
244 if ($shelflist->{$element}->{'category'} eq '1') {
245 (scalar(@shelveslooppriv) % 2) and $shelflist->{$element}->{toggle} = 1;
246 push (@shelveslooppriv, $shelflist->{$element});
248 (scalar(@shelvesloop) % 2) and $shelflist->{$element}->{toggle} = 1;
249 push (@shelvesloop, $shelflist->{$element});
254 shelveslooppriv => \@shelveslooppriv,
255 shelvesloop => \@shelvesloop,
256 shelvesloopall => [(@shelvesloop, @shelveslooppriv)],
257 numberCanManage => $numberCanManage,
258 "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
260 if ($template->param('viewshelf') or
261 $template->param( 'shelves' ) or
262 $template->param( 'edit' ) ) {
263 $template->param(vseflag => 1);
265 if ($template->param( 'shelves' ) or
266 $template->param( 'edit' ) ) {
267 $template->param( seflag => 1);
270 my $sessionID = $query->cookie("CGISESSID") ;
271 my $session = get_session($sessionID);
272 my $shelves = GetShelvesSummary($loggedinuser, 2, 10);
273 $session->param('shelves', $shelves);
274 $template->param( barshelves => scalar (@$shelves));
275 $template->param( barshelvesloop => $shelves);
277 output_html_with_http_headers $query, $cookie, $template->output;
285 VirtualShelves/Page.pm
289 Module used for both OPAC and intranet pages.
291 =head1 CGI PARAMETERS
295 =item C<modifyshelfcontents>
297 If this script has to modify the shelf content.
301 To know on which shelf to work.
308 * modif: show the template allowing modification of the shelves;
309 * modifsave: save changes from modif mode.
313 Load template with 'viewshelves param' displaying the shelf's information.
317 If the param shelves == 1, then add or delete a shelf.
321 If the param shelves == 1, then addshelf is the name of the shelf to add.