kohabug 1875 Public lists/virtualshelves are displayed and viewable whether a patron...
[koha_fer] / C4 / VirtualShelves / Page.pm
1 package C4::VirtualShelves::Page;
2
3 #
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
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
11 # version.
12 #
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.
16 #
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
20
21 # perldoc at the end of the file, per convention.
22
23 use strict;
24 use warnings;
25 use CGI;
26 use C4::VirtualShelves qw/:DEFAULT GetShelvesSummary/;
27 use C4::Biblio;
28 use C4::Items;
29 use C4::Koha;
30 use C4::Auth qw/get_session/;
31 use C4::Members;
32 use C4::Output;
33 use Exporter;
34
35 use vars qw($debug @EXPORT @ISA $VERSION);
36
37 BEGIN {
38         $VERSION = 1.01;
39         @ISA = qw(Exporter);
40         @EXPORT = qw(&shelfpage);
41     $debug = $ENV{DEBUG} || 0;
42 }
43
44 our %pages = (
45         intranet => {
46                 redirect=>'/cgi-bin/koha/virtualshelves/shelves.pl',
47         },
48         opac => {
49                 redirect=>'/cgi-bin/koha/opac-shelves.pl',
50         },
51 );
52
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 } );
59         my @paramsloop;
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();
65     
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!
68
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}; }
79                 } else { 
80                                 (grep {/REM-(\d+)/} $query->param) or push @paramsloop, {nobarcode=>1};
81                         foreach ($query->param) {
82                                         /REM-(\d+)/ or next;
83                                         $debug and warn 
84                                                 "SHELVES: user $loggedinuser removing item $1 from shelf $shelfnumber";
85                                         DelFromShelf($1, $shelfnumber);  # $1 is biblionumber
86                                 }
87                         }
88                 } else { push @paramsloop, {nopermission=>$shelfnumber}; }
89     } else { push @paramsloop, {noshelfnumber=>1}; }
90 }
91
92 my $showadd = 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);
100                 $showadd = 0;
101         } else {
102                 $debug and warn "Invalid 'display' param ($shelf_type)";
103         }
104 } else {
105         $template->param(showprivateshelves => 1);
106 }
107
108 my($okmanage, $okview);
109 my $shelfnumber = $query->param('shelfnumber') || $query->param('viewshelf');
110 if ($shelfnumber) {
111         $okmanage = &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
112         $okview   = &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' );
113 }
114
115 SWITCH: {
116         if ( $op ) {
117                 unless ($okmanage) {
118                         push @paramsloop, {nopermission=>$shelfnumber};
119                         last SWITCH;
120                 }
121                 if ( $op eq 'modifsave' ) {
122                         my $shelf = {
123                         'shelfname'             => $query->param('shelfname'),
124                                 'category'              => $query->param('category'),
125                                 'sortfield'             => $query->param('sortfield'),
126                         };
127                         $shelf->{'owner'} = $loggedinuser if $type eq 'intranet';       #we only overwrite the list owner if &ModShelf was called from the staff client
128
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 );
133                         $template->param(
134                                 edit                => 1,
135                                 shelfnumber         => $shelfnumber2,
136                                 shelfname           => $shelfname,
137                                 owner               => $owner,
138                                 "category$category"     => 1,
139                                 category                        => $category,
140                                 "sort_$sortfield"   => 1,
141                         );
142                 }
143                 last SWITCH;
144         }
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'};
152                         }
153                         $showadd = 1;
154                         my $i = 0;
155                         foreach (grep {$i++ % 2} @$items) {     # every other item
156                                 $_->{toggle} = 1;
157                         }
158                         # my $manageshelf = &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
159                         # ($manageshelf) and $showadd = 1;
160                         $template->param(
161                                 shelfname   => $shelflist->{$shelfnumber}->{'shelfname'},
162                                 shelfnumber => $shelfnumber,
163                                 viewshelf   => $shelfnumber,
164                                 manageshelf => &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' ),
165                                 itemsloop => $items,
166                         );
167                 } else { push @paramsloop, {nopermission=>$shelfnumber} };
168         last SWITCH;
169     }
170     if ( $query->param('shelves') ) {
171                 my $stay = 1;
172         if (my $newshelf = $query->param('addshelf')) {
173                         # note: a user can always add a new shelf
174             my $shelfnumber = AddShelf(
175                 $newshelf,
176                 $query->param('owner'),
177                 $query->param('category')
178             );
179                         $stay = 1;
180             if ( $shelfnumber == -1 ) {    #shelf already exists.
181                                 $showadd = 1;
182                                 push @paramsloop, { already => $newshelf };
183                 $template->param(shelfnumber => $shelfnumber);
184             } else {
185                 print $query->redirect($pages{$type}->{redirect} . "?viewshelf=$shelfnumber");
186                 exit;
187                         }
188         }
189                 foreach ($query->param()) {
190                         /DEL-(\d+)/ or next;
191                         my $number = $1;
192                         unless (defined $shelflist->{$number}) {
193                                 push(@paramsloop, {unrecognized=>$number}); last;
194                         }
195                         unless (ShelfPossibleAction($loggedinuser, $number, 'manage')) {
196                                 push(@paramsloop, {nopermission=>$shelfnumber}); last;
197                         }
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;
203                                         $stay = 0;
204                                         next;
205                                 }
206                         } 
207                         my $name = $shelflist->{$number}->{'shelfname'};
208                         unless (DelShelf($number)) {
209                                 push(@paramsloop, {delete_fail=>$name}); last;
210                         }
211                         delete $shelflist->{$number};
212                         push(@paramsloop, {delete_ok=>$name});
213                         # print $query->redirect($pages{$type}->{redirect}); exit;
214                         $stay = 0;
215                 }
216                 $showadd = 1;
217                 $stay and $template->param(shelves => 1);
218                 last SWITCH;
219         }
220 }
221
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);
226 my @shelvesloop;
227 my @shelveslooppriv;
228 my $numberCanManage = 0;
229
230 foreach my $element (sort { lc($shelflist->{$a}->{'shelfname'}) cmp lc($shelflist->{$b}->{'shelfname'}) } keys %$shelflist) {
231         my %line;
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;
240         } 
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});
247         } else {
248                 (scalar(@shelvesloop)     % 2) and $shelflist->{$element}->{toggle} = 1;
249                 push (@shelvesloop, $shelflist->{$element});
250         }
251 }
252
253 $template->param(
254     shelveslooppriv => \@shelveslooppriv,
255     shelvesloop     => \@shelvesloop,
256     shelvesloopall  => [(@shelvesloop, @shelveslooppriv)],
257     numberCanManage => $numberCanManage,
258         "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
259 );
260 if ($template->param('viewshelf') or
261         $template->param( 'shelves' ) or
262         $template->param(  'edit'   ) ) {
263         $template->param(vseflag => 1);
264 }
265 if ($template->param( 'shelves' ) or
266         $template->param(  'edit'   ) ) {
267         $template->param( seflag => 1);
268 }
269
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);
276
277 output_html_with_http_headers $query, $cookie, $template->output;
278 }       
279
280 1;
281 __END__
282
283 =head1 NAME
284
285     VirtualShelves/Page.pm
286
287 =head1 DESCRIPTION
288
289     Module used for both OPAC and intranet pages.
290
291 =head1 CGI PARAMETERS
292
293 =over 4
294
295 =item C<modifyshelfcontents>
296
297     If this script has to modify the shelf content.
298
299 =item C<shelfnumber>
300
301     To know on which shelf to work.
302
303 =item C<addbarcode>
304
305 =item C<op>
306
307     Op can be:
308         * modif: show the template allowing modification of the shelves;
309         * modifsave: save changes from modif mode.
310
311 =item C<viewshelf>
312
313     Load template with 'viewshelves param' displaying the shelf's information.
314
315 =item C<shelves>
316
317     If the param shelves == 1, then add or delete a shelf.
318
319 =item C<addshelf>
320
321     If the param shelves == 1, then addshelf is the name of the shelf to add.
322
323 =back
324
325 =cut