fix to bug 1569
[koha.git] / virtualshelves / shelves.pl
1 #!/usr/bin/perl
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 =head1 NAME
22
23     shelves.pl
24
25 =head1 DESCRIPTION
26
27     this script is used to script to provide virtualshelf management
28
29 =head1 CGI PARAMETERS
30
31 =over 4
32
33 =item C<modifyshelfcontents>
34
35     if this script has to modify the shelve content.
36
37 =item C<shelfnumber>
38
39     to know on which shelve this script has to work.
40
41 =item C<addbarcode>
42
43 =item C<op>
44
45     op can be equals to:
46         * modifsave to save change on the shelves
47         * modif to change the template to allow to modify the shelves.
48
49 =item C<viewshelf>
50
51     to load the template with 'viewshelves param' which allow to read the shelves information.
52
53 =item C<shelves>
54
55     if equals to 1. then call the function shelves which add
56     or delete a shelf.
57
58 =item C<addshelf>
59
60     if the param shelves = 1 then addshelf must be equals to the name of the shelf to add.
61
62 =back
63
64 =cut
65
66 use strict;
67 use CGI;
68 use C4::VirtualShelves;
69 use C4::Biblio;
70 use C4::Auth;
71 use C4::Output;
72
73 my $query = new CGI;
74
75 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
76     {
77         template_name   => "virtualshelves/shelves.tmpl",
78         query           => $query,
79         type            => "intranet",
80         authnotrequired => 0,
81         flagsrequired   => { catalogue => 1 },
82     }
83 );
84
85 if ( $query->param('modifyshelfcontents') ) {
86     my $shelfnumber = $query->param('viewshelf');
87     my $barcode     = $query->param('addbarcode');
88     my ($item) = GetItem( 0, $barcode );
89     my ($biblio) = GetBiblioFromItemNumber($item->{'itemnumber'});
90     if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' ) ) {
91         AddToShelf( $biblio->{'biblionumber'}, $shelfnumber );
92         foreach ( $query->param ) {
93             if (/REM-(\d*)/) {
94                 my $biblionumber = $1;
95                 DelFromShelf( $biblionumber, $shelfnumber );
96             }
97         }
98     }
99 }
100
101 # getting the Shelves list
102 my $shelflist = GetShelves( $loggedinuser, 2 );
103 $template->param( { loggedinuser => $loggedinuser } );
104 my $op = $query->param('op');
105
106 SWITCH: {
107     if ( $op && ( $op eq 'modifsave' ) ) {
108         ModShelf(
109             $query->param('shelfnumber'), $query->param('shelfname'),
110             $loggedinuser,                $query->param('category'), $query->param('sortfield')
111         );
112         last SWITCH;
113     }
114     if ( $op && ( $op eq 'modif' ) ) {
115         my ( $shelfnumber, $shelfname, $owner, $category, $sortfield ) =
116           GetShelf( $query->param('shelf') );
117         $template->param(
118             edit                => 1,
119             shelfnumber         => $shelfnumber,
120             shelfname           => $shelfname,
121             "category$category" => 1,
122         "sort_$sortfield"   => 1,
123         );
124         last SWITCH;
125     }
126     if ( $query->param('viewshelf') ) {
127
128         #check that the user can view the shelf
129         my $shelfnumber = $query->param('viewshelf');
130         if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' ) ) {
131             my $items = GetShelfContents($shelfnumber);
132             $template->param(
133                 shelfname   => $shelflist->{$shelfnumber}->{'shelfname'},
134                 shelfnumber => $shelfnumber,
135                 viewshelf   => $query->param('viewshelf'),
136                 manageshelf => &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' ),
137                 itemsloop => $items,
138             );
139         }
140         last SWITCH;
141     }
142     if ( $query->param('shelves') ) {
143         if ( my $newshelf = $query->param('addshelf') ) {
144             my $shelfnumber = AddShelf(
145                 $newshelf,
146                 $query->param('owner'),
147                 $query->param('category')
148             );
149
150             if ( $shelfnumber == -1 ) {    #shelf already exists.
151                 $template->param(
152                     {
153                         shelfnumber => $shelfnumber,
154                         already     => 1
155                     }
156                 );
157             }
158             print $query->redirect("/cgi-bin/koha/virtualshelves/shelves.pl?viewshelf=$shelfnumber");
159             exit;
160         }
161         my @paramsloop;
162         foreach ( $query->param() ) {
163             my %line;
164             if (/DEL-(\d+)/) {
165                 my $delshelf = $1;
166                 my ( $status, $count ) = DelShelf($delshelf);
167                 if ($status) {
168                     $line{'status'} = $status;
169                     $line{'count'}  = $count;
170                 }
171                 print $query->redirect("/cgi-bin/koha/virtualshelves/shelves.pl");
172                 exit;
173             }
174
175             #if the shelf is not deleted, %line points on null
176             # push( @paramsloop, \%line );
177         }
178         $template->param( paramsloop => \@paramsloop );
179         my ($shelflist) = GetShelves( $loggedinuser, 2 );
180         my $color = '';
181         my @shelvesloop;
182         foreach my $element ( sort keys %$shelflist ) {
183             my %line;
184             ( $color eq 1 ) ? ( $color = 0 ) : ( $color = 1 );
185             $line{'toggle'}            = $color;
186             $line{'shelf'}             = $element;
187             $line{'shelfname'}         = $shelflist->{$element}->{'shelfname'};
188             $line{'shelfvirtualcount'} = $shelflist->{$element}->{'count'};
189             push( @shelvesloop, \%line );
190         }
191         $template->param(
192             shelvesloop => \@shelvesloop,
193             shelves     => 1,
194         );
195         last SWITCH;
196     }
197 }
198
199 # rebuild shelflist in case a shelf has been added
200 $shelflist = GetShelves( $loggedinuser, 2 );
201 my $color = '';
202 my @shelvesloop;
203 my $numberCanManage = 0;
204
205 foreach my $element ( sort keys %$shelflist ) {
206     my %line;
207     ( $color eq 1 ) ? ( $color = 0 ) : ( $color = 1 );
208     $line{'toggle'}    = $color;
209     $line{'shelf'}     = $element;
210     $line{'shelfname'} = $shelflist->{$element}->{'shelfname'};
211     $line{'sortfield'}=$shelflist->{$element}->{'sortfield'};
212     $line{"viewcategory$shelflist->{$element}->{'category'}"} = 1;
213     $line{'mine'} = 1 if $shelflist->{$element}->{'owner'} eq $loggedinuser;
214     $line{'shelfvirtualcount'} = $shelflist->{$element}->{'count'};
215     $line{'canmanage'} = ShelfPossibleAction( $loggedinuser, $element, 'manage' );
216     $line{'firstname'} = $shelflist->{$element}->{'firstname'} unless $shelflist->{$element}->{'owner'} eq $loggedinuser;
217     $line{'surname'} = $shelflist->{$element}->{'surname'} unless $shelflist->{$element}->{'owner'} eq $loggedinuser;
218     $numberCanManage++ if $line{'canmanage'};
219     push( @shelvesloop, \%line );
220 }
221
222 $template->param(
223     shelvesloop     => \@shelvesloop,
224     numberCanManage => $numberCanManage,
225 );
226
227 output_html_with_http_headers $query, $cookie, $template->output;
228
229 sub shelves {
230     my $innertemplate = shift;
231     if ( my $newshelf = $query->param('addshelf') ) {
232         my $shelfnumber = AddShelf(
233             $newshelf,
234             $query->param('owner'),
235             $query->param('category')
236         );
237
238         if ( $shelfnumber == -1 ) {    #shelf already exists.
239             $template->param(
240                 {
241                     shelfnumber => $shelfnumber,
242                     already     => 1
243                 }
244             );
245         }
246     }
247     my @paramsloop;
248     foreach ( $query->param() ) {
249         my %line;
250         if (/DEL-(\d+)/) {
251             my $delshelf = $1;
252             my ( $status, $count ) = DelShelf($delshelf);
253             if ($status) {
254                 $line{'status'} = $status;
255                 $line{'count'}  = $count;
256             }
257         }
258
259         #if the shelf is not deleted, %line points on null
260         push( @paramsloop, \%line );
261     }
262     $innertemplate->param( paramsloop => \@paramsloop );
263     my ($shelflist) = GetShelves( $loggedinuser, 2 );
264     my $color = '';
265     my @shelvesloop;
266     foreach my $element ( sort keys %$shelflist ) {
267         my %line;
268         ( $color eq 1 ) ? ( $color = 0 ) : ( $color = 1 );
269         $line{'toggle'}            = $color;
270         $line{'shelf'}             = $element;
271         $line{'shelfname'}         = $shelflist->{$element}->{'shelfname'};
272         $line{'shelfvirtualcount'} = $shelflist->{$element}->{'count'};
273         push( @shelvesloop, \%line );
274     }
275     $innertemplate->param(
276         shelvesloop => \@shelvesloop,
277         shelves     => 1,
278     );
279 }