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