fixing display of itemtypes and itemtype icons in virtual shelves
[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::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                         ModShelf(
123                                 $shelfnumber, $query->param('shelfname'), $loggedinuser,
124                                 $query->param('category'), $query->param('sortfield')
125                         );
126                         $shelflist = GetShelves( $loggedinuser, 2 );    # refresh after mods
127                 } elsif ( $op eq 'modif' ) {
128                         my ( $shelfnumber2, $shelfname, $owner, $category, $sortfield ) =GetShelf( $query->param('shelfnumber') );
129                         $template->param(
130                                 edit                => 1,
131                                 shelfnumber         => $shelfnumber2,
132                                 shelfname           => $shelfname,
133                                 owner               => $owner,
134                                 "category$category" => 1,
135                                 "sort_$sortfield"   => 1,
136                         );
137                 }
138                 last SWITCH;
139         }
140     if ($shelfnumber = $query->param('viewshelf') ) {
141         #check that the user can view the shelf
142         if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' ) ) {
143             my $items = GetShelfContents($shelfnumber);
144             for my $this_item (@$items) {
145                 $this_item->{imageurl} = $imgdir."/".$itemtypes->{ $this_item->{itemtype}  }->{'imageurl'};
146                 $this_item->{'description'} = $itemtypes->{ $this_item->{itemtype} }->{'description'};
147             }
148                         $showadd = 1;
149                         my $i = 0;
150                         foreach (grep {$i++ % 2} @$items) {     # every other item
151                                 $_->{toggle} = 1;
152                         }
153                         # my $manageshelf = &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
154                         # ($manageshelf) and $showadd = 1;
155             $template->param(
156                 shelfname   => $shelflist->{$shelfnumber}->{'shelfname'},
157                 shelfnumber => $shelfnumber,
158                 viewshelf   => $shelfnumber,
159                 manageshelf => &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' ),
160                 itemsloop => $items,
161             );
162         } else { push @paramsloop, {nopermission=>$shelfnumber}; }
163         last SWITCH;
164     }
165     if ( $query->param('shelves') ) {
166                 my $stay = 1;
167         if (my $newshelf = $query->param('addshelf')) {
168                         # note: a user can always add a new shelf
169             my $shelfnumber = AddShelf(
170                 $newshelf,
171                 $query->param('owner'),
172                 $query->param('category')
173             );
174                         $stay = 1;
175             if ( $shelfnumber == -1 ) {    #shelf already exists.
176                                 $showadd = 1;
177                                 push @paramsloop, { already => $newshelf };
178                 $template->param(shelfnumber => $shelfnumber);
179             } else {
180                 print $query->redirect($pages{$type}->{redirect} . "?viewshelf=$shelfnumber");
181                 exit;
182                         }
183         }
184                 foreach ($query->param()) {
185                         /DEL-(\d+)/ or next;
186                         my $number = $1;
187                         unless (defined $shelflist->{$number}) {
188                                 push(@paramsloop, {unrecognized=>$number}); last;
189                         }
190                         unless (ShelfPossibleAction($loggedinuser, $number, 'manage')) {
191                                 push(@paramsloop, {nopermission=>$shelfnumber}); last;
192                         }
193                         my $contents = GetShelfContents($number);
194                         if (my $count = scalar @$contents){
195                                 unless (scalar grep {/^CONFIRM-$number$/} $query->param()) {
196                                         push(@paramsloop, {need_confirm=>$shelflist->{$number}->{shelfname}, count=>$count});
197                                         $shelflist->{$number}->{confirm} = $number;
198                                         next;
199                                 }
200                         } 
201                         my $name = $shelflist->{$number}->{'shelfname'};
202                         unless (DelShelf($number)) {
203                                 push(@paramsloop, {delete_fail=>$name}); last;
204                         }
205                         delete $shelflist->{$number};
206                         push(@paramsloop, {delete_ok=>$name});
207                         # print $query->redirect($pages{$type}->{redirect}); exit;
208                         $stay = 0;
209                 }
210                 $showadd = 1;
211                 $stay and $template->param(shelves => 1);
212                 last SWITCH;
213         }
214 }
215
216 (@paramsloop) and $template->param(paramsloop => \@paramsloop);
217 # rebuild shelflist in case a shelf has been added
218 # $shelflist = GetShelves( $loggedinuser, 2 );
219 $showadd and $template->param(showadd => 1);
220 my @shelvesloop;
221 my @shelveslooppriv;
222 my $numberCanManage = 0;
223
224 foreach my $element (sort { lc($shelflist->{$a}->{'shelfname'}) cmp lc($shelflist->{$b}->{'shelfname'}) } keys %$shelflist) {
225         my %line;
226         $shelflist->{$element}->{shelf} = $element;
227         my $category = $shelflist->{$element}->{'category'};
228         my $owner    = $shelflist->{$element}->{ 'owner'  };
229         my $canmanage = ShelfPossibleAction( $loggedinuser, $element, 'manage' );
230         $shelflist->{$element}->{"viewcategory$category"} = 1;
231         $shelflist->{$element}->{canmanage} = $canmanage;
232         if ($owner eq $loggedinuser or $canmanage) {
233                 $shelflist->{$element}->{'mine'} = 1;
234         } 
235         my $member = GetMember($owner,'borrowernumber');
236         $shelflist->{$element}->{ownername} = $member->{firstname} . " " . $member->{surname};
237         $numberCanManage++ if $canmanage;       # possibly outmoded
238         if ($shelflist->{$element}->{'category'} eq '1') {
239                 (scalar(@shelveslooppriv) % 2) and $shelflist->{$element}->{toggle} = 1;
240                 push (@shelveslooppriv, $shelflist->{$element});
241         } else {
242                 (scalar(@shelvesloop)     % 2) and $shelflist->{$element}->{toggle} = 1;
243                 push (@shelvesloop, $shelflist->{$element});
244         }
245 }
246
247 $template->param(
248     shelveslooppriv => \@shelveslooppriv,
249     shelvesloop     => \@shelvesloop,
250     shelvesloopall  => [(@shelvesloop, @shelveslooppriv)],
251     numberCanManage => $numberCanManage,
252         "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
253 );
254 if ($template->param('viewshelf') or
255         $template->param( 'shelves' ) or
256         $template->param(  'edit'   ) ) {
257         $template->param(vseflag => 1);
258 }
259 if ($template->param( 'shelves' ) or
260         $template->param(  'edit'   ) ) {
261         $template->param( seflag => 1);
262 }
263
264 my $sessionID = $query->cookie("CGISESSID") ;
265 my $session = get_session($sessionID);
266 my $shelves = GetShelvesSummary($loggedinuser, 2, 10);
267 $session->param('shelves', $shelves);
268 $template->param( barshelves     => scalar (@$shelves));
269 $template->param( barshelvesloop => $shelves);
270
271 output_html_with_http_headers $query, $cookie, $template->output;
272 }       
273
274 1;
275 __END__
276
277 =head1 NAME
278
279     VirtualShelves/Page.pm
280
281 =head1 DESCRIPTION
282
283     Module used for both OPAC and intranet pages.
284
285 =head1 CGI PARAMETERS
286
287 =over 4
288
289 =item C<modifyshelfcontents>
290
291     If this script has to modify the shelf content.
292
293 =item C<shelfnumber>
294
295     To know on which shelf to work.
296
297 =item C<addbarcode>
298
299 =item C<op>
300
301     Op can be:
302         * modif: show the template allowing modification of the shelves;
303         * modifsave: save changes from modif mode.
304
305 =item C<viewshelf>
306
307     Load template with 'viewshelves param' displaying the shelf's information.
308
309 =item C<shelves>
310
311     If the param shelves == 1, then add or delete a shelf.
312
313 =item C<addshelf>
314
315     If the param shelves == 1, then addshelf is the name of the shelf to add.
316
317 =back
318
319 =cut