Shelves - fixup display, in particular intranet side
[koha.git] / 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::Auth qw/get_session/;
30 use C4::Members;
31 use C4::Output;
32 use Exporter;
33
34 use vars qw($debug @EXPORT @ISA $VERSION);
35
36 BEGIN {
37         $VERSION = 1.01;
38         @ISA = qw(Exporter);
39         @EXPORT = qw(&shelfpage);
40     $debug = $ENV{DEBUG} || 0;
41 }
42
43 our %pages = (
44         intranet => {
45                 redirect=>'/cgi-bin/koha/virtualshelves/shelves.pl',
46         },
47         opac => {
48                 redirect=>'/cgi-bin/koha/opac-shelves.pl',
49         },
50 );
51
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 } );
58         my @paramsloop;
59         # getting the Shelves list
60         my $shelflist = GetShelves( $loggedinuser, 2 );
61         my $op = $query->param('op');
62
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!
65
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}; }
76                 } else { 
77                                 (grep {/REM-(\d+)/} $query->param) or push @paramsloop, {nobarcode=>1};
78                         foreach ($query->param) {
79                                         /REM-(\d+)/ or next;
80                                         $debug and warn 
81                                                 "SHELVES: user $loggedinuser removing item $1 from shelf $shelfnumber";
82                                         DelFromShelf($1, $shelfnumber);  # $1 is biblionumber
83                                 }
84                         }
85                 } else { push @paramsloop, {nopermission=>$shelfnumber}; }
86     } else { push @paramsloop, {noshelfnumber=>1}; }
87 }
88
89 my $showadd = 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);
97                 $showadd = 0;
98         } else {
99                 $debug and warn "Invalid 'display' param ($shelf_type)";
100         }
101 } else {
102         $template->param(showprivateshelves => 1);
103 }
104
105 my($okmanage, $okview);
106 my $shelfnumber = $query->param('shelfnumber') || $query->param('viewshelf');
107 if ($shelfnumber) {
108         $okmanage = &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
109         $okview   = &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' );
110 }
111
112 SWITCH: {
113         if ( $op ) {
114                 unless ($okmanage) {
115                         push @paramsloop, {nopermission=>$shelfnumber};
116                         last SWITCH;
117                 }
118                 if ( $op eq 'modifsave' ) {
119                         ModShelf(
120                                 $shelfnumber, $query->param('shelfname'), $loggedinuser,
121                                 $query->param('category'), $query->param('sortfield')
122                         );
123                         $shelflist = GetShelves( $loggedinuser, 2 );    # refresh after mods
124                 } elsif ( $op eq 'modif' ) {
125                         my ( $shelfnumber2, $shelfname, $owner, $category, $sortfield ) =GetShelf( $query->param('shelfnumber') );
126                         $template->param(
127                                 edit                => 1,
128                                 shelfnumber         => $shelfnumber2,
129                                 shelfname           => $shelfname,
130                                 owner               => $owner,
131                                 "category$category" => 1,
132                                 "sort_$sortfield"   => 1,
133                         );
134                 }
135                 last SWITCH;
136         }
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);
141                         $showadd = 1;
142                         my $i = 0;
143                         foreach (grep {$i++ % 2} @$items) {     # every other item
144                                 $_->{toggle} = 1;
145                         }
146                         # my $manageshelf = &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
147                         # ($manageshelf) and $showadd = 1;
148             $template->param(
149                 shelfname   => $shelflist->{$shelfnumber}->{'shelfname'},
150                 shelfnumber => $shelfnumber,
151                 viewshelf   => $shelfnumber,
152                 manageshelf => &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' ),
153                 itemsloop => $items,
154             );
155         } else { push @paramsloop, {nopermission=>$shelfnumber}; }
156         last SWITCH;
157     }
158     if ( $query->param('shelves') ) {
159                 my $stay = 1;
160         if (my $newshelf = $query->param('addshelf')) {
161                         # note: a user can always add a new shelf
162             my $shelfnumber = AddShelf(
163                 $newshelf,
164                 $query->param('owner'),
165                 $query->param('category')
166             );
167                         $stay = 1;
168             if ( $shelfnumber == -1 ) {    #shelf already exists.
169                                 $showadd = 1;
170                                 push @paramsloop, { already => $newshelf };
171                 $template->param(shelfnumber => $shelfnumber);
172             } else {
173                 print $query->redirect($pages{$type}->{redirect} . "?viewshelf=$shelfnumber");
174                 exit;
175                         }
176         }
177                 foreach ($query->param()) {
178                         /DEL-(\d+)/ or next;
179                         my $number = $1;
180                         unless (defined $shelflist->{$number}) {
181                                 push(@paramsloop, {unrecognized=>$number}); last;
182                         }
183                         unless (ShelfPossibleAction($loggedinuser, $number, 'manage')) {
184                                 push(@paramsloop, {nopermission=>$shelfnumber}); last;
185                         }
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;
191                                         next;
192                                 }
193                         } 
194                         my $name = $shelflist->{$number}->{'shelfname'};
195                         unless (DelShelf($number)) {
196                                 push(@paramsloop, {delete_fail=>$name}); last;
197                         }
198                         delete $shelflist->{$number};
199                         push(@paramsloop, {delete_ok=>$name});
200                         # print $query->redirect($pages{$type}->{redirect}); exit;
201                         $stay = 0;
202                 }
203                 $showadd = 1;
204                 $stay and $template->param(shelves => 1);
205                 last SWITCH;
206         }
207 }
208
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);
213 my @shelvesloop;
214 my @shelveslooppriv;
215 my $numberCanManage = 0;
216
217 foreach my $element (sort { lc($shelflist->{$a}->{'shelfname'}) cmp lc($shelflist->{$b}->{'shelfname'}) } keys %$shelflist) {
218         my %line;
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;
227         } 
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});
234         } else {
235                 (scalar(@shelvesloop)     % 2) and $shelflist->{$element}->{toggle} = 1;
236                 push (@shelvesloop, $shelflist->{$element});
237         }
238 }
239
240 $template->param(
241     shelveslooppriv => \@shelveslooppriv,
242     shelvesloop     => \@shelvesloop,
243     shelvesloopall  => [(@shelvesloop, @shelveslooppriv)],
244     numberCanManage => $numberCanManage,
245         "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
246 );
247 if ($template->param('viewshelf') or
248         $template->param( 'shelves' ) or
249         $template->param(  'edit'   ) ) {
250         $template->param(vseflag => 1);
251 }
252 if ($template->param( 'shelves' ) or
253         $template->param(  'edit'   ) ) {
254         $template->param( seflag => 1);
255 }
256
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);
263
264 output_html_with_http_headers $query, $cookie, $template->output;
265 }       
266
267 1;
268 __END__
269
270 =head1 NAME
271
272     VirtualShelves/Page.pm
273
274 =head1 DESCRIPTION
275
276     Module used for both OPAC and intranet pages.
277
278 =head1 CGI PARAMETERS
279
280 =over 4
281
282 =item C<modifyshelfcontents>
283
284     If this script has to modify the shelf content.
285
286 =item C<shelfnumber>
287
288     To know on which shelf to work.
289
290 =item C<addbarcode>
291
292 =item C<op>
293
294     Op can be:
295         * modif: show the template allowing modification of the shelves;
296         * modifsave: save changes from modif mode.
297
298 =item C<viewshelf>
299
300     Load template with 'viewshelves param' displaying the shelf's information.
301
302 =item C<shelves>
303
304     If the param shelves == 1, then add or delete a shelf.
305
306 =item C<addshelf>
307
308     If the param shelves == 1, then addshelf is the name of the shelf to add.
309
310 =back
311
312 =cut