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