Shelves - bugfix 1403 amongst others
[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;
27 use C4::Biblio;
28 use C4::Items;
29 use C4::Auth;
30 use C4::Output;
31 use Exporter;
32
33 use vars qw($debug @EXPORT @ISA $VERSION);
34
35 BEGIN {
36         $VERSION = 1.00;
37         @ISA = qw(Exporter);
38         @EXPORT = qw(&shelfpage);
39     $debug = $ENV{DEBUG} || 0;
40 }
41
42 our %pages = (
43         intranet => {
44                 redirect=>'/cgi-bin/koha/virtualshelves/shelves.pl',
45         },
46         opac => {
47                 redirect=>'/cgi-bin/koha/opac-shelves.pl',
48         },
49 );
50
51 sub shelfpage ($$$$$) {
52         my ($type, $query, $template, $loggedinuser, $cookie ) = @_;
53         ($pages{$type}) or $type = 'opac';
54         $query or die "No query";
55         $template or die "No template";
56         $template->param( { loggedinuser => $loggedinuser } );
57         my @paramsloop;
58         # getting the Shelves list
59         my $shelflist = GetShelves( $loggedinuser, 2 );
60         my $op = $query->param('op');
61
62 # the format of this is unindented for ease of diff comparison to the old script
63 # Note: do not mistake the assignment statements below for comparisons!
64
65 if ( $query->param('modifyshelfcontents') ) {
66         my ($shelfnumber,$barcode,$item,$biblio);
67     if ($shelfnumber = $query->param('viewshelf')) {
68         if (ShelfPossibleAction($loggedinuser, $shelfnumber, 'manage')) {
69                 if ($barcode = $query->param('addbarcode')) {
70                         if ($item = GetItem( 0, $barcode )) {
71                                 $biblio = GetBiblioFromItemNumber($item->{'itemnumber'});
72                                 AddToShelf($biblio->{'biblionumber'}, $shelfnumber) or 
73                                                 push @paramsloop, {duplicatebiblio=>$barcode};
74                                 } else { push @paramsloop, {failgetitem=>$barcode}; }
75                 } else { 
76                                 (grep {/REM-(\d+)/} $query->param) or push @paramsloop, {nobarcode=>1};
77                         foreach ($query->param) {
78                                         /REM-(\d+)/ or next;
79                                         $debug and warn 
80                                                 "SHELVES: user $loggedinuser removing item $1 from shelf $shelfnumber";
81                                         DelFromShelf($1, $shelfnumber);  # $1 is biblionumber
82                                 }
83                         }
84                 } else { push @paramsloop, {nopermission=>$shelfnumber}; }
85     } else { push @paramsloop, {noshelfnumber=>1}; }
86 }
87
88 my $showadd = 1;
89 # set the default tab, etc. (for OPAC)
90 my $shelf_type = $query->param('display');
91 if (defined $shelf_type) {
92         if ($shelf_type eq 'privateshelves')  {
93                 $template->param(showprivateshelves => 1);
94         } elsif ($shelf_type eq 'publicshelves') {
95                 $template->param(showpublicshelves => 1);
96                 $showadd = 0;
97         } else {
98                 $debug and warn "Invalid 'display' param ($shelf_type)";
99         }
100 } else {
101         $template->param(showprivateshelves => 1);
102 }
103
104
105 SWITCH: {
106         if ( $op ) {
107                 if ( $op eq 'modifsave' ) {
108                         ModShelf(
109                                 $query->param('shelfnumber'), $query->param('shelfname'),
110                                 $loggedinuser,                $query->param('category'), $query->param('sortfield')
111                         );
112                         $shelflist = GetShelves( $loggedinuser, 2 );    # refresh after mods
113                 } elsif ( $op eq 'modif' ) {
114                         my ( $shelfnumber, $shelfname, $owner, $category, $sortfield ) =GetShelf( $query->param('shelf') );
115                         $template->param(
116                                 edit                => 1,
117                                 shelfnumber         => $shelfnumber,
118                                 shelfname           => $shelfname,
119                                 "category$category" => 1,
120                                 "sort_$sortfield"   => 1,
121                         );
122                 }
123                 last SWITCH;
124         }
125     if (my $shelfnumber = $query->param('viewshelf') ) {
126         #check that the user can view the shelf
127         if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' ) ) {
128             my $items = GetShelfContents($shelfnumber);
129                         $showadd = 1;
130                         my $i = 0;
131                         foreach (grep {$i++ % 2} @$items) {     # every other item
132                                 $_->{toggle} = 1;
133                         }
134                         # my $manageshelf = &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
135                         # ($manageshelf) and $showadd = 1;
136             $template->param(
137                 shelfname   => $shelflist->{$shelfnumber}->{'shelfname'},
138                 shelfnumber => $shelfnumber,
139                 viewshelf   => $shelfnumber,
140                 manageshelf => &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' ),
141                 itemsloop => $items,
142             );
143         } else { push @paramsloop, {nopermission=>$shelfnumber}; }
144         last SWITCH;
145     }
146     if ( $query->param('shelves') ) {
147         if ( my $newshelf = $query->param('addshelf') ) {
148             my $shelfnumber = AddShelf(
149                 $newshelf,
150                 $query->param('owner'),
151                 $query->param('category')
152             );
153
154             if ( $shelfnumber == -1 ) {    #shelf already exists.
155                                 $showadd = 1;
156                                 push @paramsloop, { already => $newshelf };
157                 $template->param(shelfnumber => $shelfnumber);
158             } else {
159                 print $query->redirect($pages{$type}->{redirect} . "?viewshelf=$shelfnumber");
160                 exit;
161                         }
162         }
163                 my $stay = 1;
164         foreach ( $query->param() ) {
165             /DEL-(\d+)/ or next;
166                         my $number = $1;
167             my %line;
168                         if (defined $shelflist->{$number}) {
169                                 my $name = $shelflist->{$number}->{'shelfname'};
170                                 if (DelShelf($number)) {
171                                         delete $shelflist->{$number};
172                                         $line{delete_ok}   = $name;
173                                         $stay = 0;
174                                 } else {
175                                         $line{delete_fail} = $name;
176                                 }
177                         } else {
178                                 $line{unrecognized} = $number;
179                         }
180                         push(@paramsloop, \%line);
181             # print $query->redirect($pages{$type}->{redirect});
182                         # exit;
183                 }
184                 $showadd = 1;
185                 $stay and $template->param(shelves => 1);
186                 last SWITCH;
187         }
188 }
189
190 (@paramsloop) and $template->param(paramsloop => \@paramsloop);
191 # rebuild shelflist in case a shelf has been added
192 # $shelflist = GetShelves( $loggedinuser, 2 );
193 $showadd and $template->param(showadd => 1);
194 my $i = 0;
195 my @shelvesloop;
196 my @shelveslooppriv;
197 my $numberCanManage = 0;
198
199 foreach my $element (sort { lc($shelflist->{$a}->{'shelfname'}) cmp lc($shelflist->{$b}->{'shelfname'}) } keys %$shelflist) {
200         my %line;
201         (++$i % 2) and $line{'toggle'} = $i;
202         $line{'shelf'}             = $element;
203         $line{'shelfname'}         = $shelflist->{$element}->{'shelfname'};
204         $line{'shelfvirtualcount'} = $shelflist->{$element}->{'count'};
205         $line{'sortfield'}         = $shelflist->{$element}->{'sortfield'};
206         $line{"viewcategory$shelflist->{$element}->{'category'}"} = 1;
207         $line{'canmanage'} = ShelfPossibleAction( $loggedinuser, $element, 'manage' );
208         if ($shelflist->{$element}->{'owner'} eq $loggedinuser) {
209                 $line{'mine'} = 1;
210         } else {
211                 $line{'firstname'} = $shelflist->{$element}->{'firstname'};
212                 $line{'surname'}   = $shelflist->{$element}->{'surname'}  ;
213         }
214         $numberCanManage++ if $line{'canmanage'};
215         if ($shelflist->{$element}->{'category'} eq '1') {
216                 push (@shelveslooppriv, \%line);
217         } else {
218                 push (@shelvesloop, \%line);
219         }
220 }
221
222 $template->param(
223     shelveslooppriv => \@shelveslooppriv,
224     shelvesloop     => \@shelvesloop,
225     shelvesloopall  => [(@shelvesloop, @shelveslooppriv)],
226     numberCanManage => $numberCanManage,
227         "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
228 );
229 if ($template->param('viewshelf') or
230         $template->param( 'shelves' ) or
231         $template->param(  'edit'   ) ) {
232         $template->param(vseflag => 1);
233 }
234 if ($template->param( 'shelves' ) or
235         $template->param(  'edit'   ) ) {
236         $template->param( seflag => 1);
237 }
238
239 output_html_with_http_headers $query, $cookie, $template->output;
240 }       
241
242 1;
243 __END__
244
245 =head1 NAME
246
247     VirtualShelves/Page.pm
248
249 =head1 DESCRIPTION
250
251     Module used for both OPAC and intranet pages.
252
253 =head1 CGI PARAMETERS
254
255 =over 4
256
257 =item C<modifyshelfcontents>
258
259     If this script has to modify the shelf content.
260
261 =item C<shelfnumber>
262
263     To know on which shelf to work.
264
265 =item C<addbarcode>
266
267 =item C<op>
268
269     Op can be:
270         * modif: show the template allowing modification of the shelves;
271         * modifsave: save changes from modif mode.
272
273 =item C<viewshelf>
274
275     Load template with 'viewshelves param' displaying the shelf's information.
276
277 =item C<shelves>
278
279     If the param shelves == 1, then add or delete a shelf.
280
281 =item C<addshelf>
282
283     If the param shelves == 1, then addshelf is the name of the shelf to add.
284
285 =back
286
287 =cut