Bug Fixing.
[koha.git] / bookshelves / 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 bookshelf 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::BookShelves;
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   => "bookshelves/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     if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' ) ) {
90         AddToShelf( $item->{'itemnumber'}, $shelfnumber );
91         foreach ( $query->param ) {
92             if (/REM-(\d*)/) {
93                 my $itemnumber = $1;
94                 DelFromShelf( $itemnumber, $shelfnumber );
95             }
96         }
97     }
98 }
99
100 # getting the Shelves list
101 my $shelflist = GetShelves( $loggedinuser, 2 );
102 $template->param( { loggedinuser => $loggedinuser } );
103 my $op = $query->param('op');
104
105 SWITCH: {
106     if ( $op && ( $op eq 'modifsave' ) ) {
107         ModShelf(
108             $query->param('shelfnumber'), $query->param('shelfname'),
109             $loggedinuser,                $query->param('category')
110         );
111         last SWITCH;
112     }
113     if ( $op && ( $op eq 'modif' ) ) {
114         my ( $shelfnumber, $shelfname, $owner, $category ) =
115           GetShelf( $query->param('shelf') );
116         $template->param(
117             edit                => 1,
118             shelfnumber         => $shelfnumber,
119             shelfname           => $shelfname,
120             "category$category" => 1
121         );
122
123         #         editshelf($query->param('shelf'));
124         last SWITCH;
125     }
126     if ( $query->param('viewshelf') ) {
127         #check that the user can view the shelf
128         my $shelfnumber = $query->param('viewshelf');
129         if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' ) ) {
130             my $items = GetShelfContents($shelfnumber);
131             $template->param(
132                 shelfname   => $shelflist->{$shelfnumber}->{'shelfname'},
133                 shelfnumber => $shelfnumber,
134                 viewshelf   => $query->param('viewshelf'),
135                 manageshelf => &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' ),
136                 itemsloop   => $items,
137             );
138         }
139         last SWITCH;
140     }
141     if ( $query->param('shelves') ) {
142         if ( my $newshelf = $query->param('addshelf') ) {
143             my $shelfnumber = AddShelf(
144                 $newshelf,
145                 $query->param('owner'),
146                 $query->param('category')
147             );
148
149             if ( $shelfnumber == -1 ) {    #shelf already exists.
150                 $template->param(
151                     {
152                         shelfnumber => $shelfnumber,
153                         already     => 1
154                     }
155                 );
156             }
157     }
158     my @paramsloop;
159     foreach ( $query->param() ) {
160         my %line;
161         if (/DEL-(\d+)/) {
162             my $delshelf = $1;
163             my ( $status, $count ) = DelShelf($delshelf);
164             if ($status) {
165                 $line{'status'} = $status;
166                 $line{'count'}  = $count;
167             }
168         }
169
170         #if the shelf is not deleted, %line points on null
171         push( @paramsloop, \%line );
172     }
173     $template->param( paramsloop => \@paramsloop );
174     my ($shelflist) = GetShelves( $loggedinuser, 2 );
175     my $color = '';
176     my @shelvesloop;
177     foreach my $element ( sort keys %$shelflist ) {
178         my %line;
179         ( $color eq 1 ) ? ( $color = 0 ) : ( $color = 1 );
180         $line{'toggle'}         = $color;
181         $line{'shelf'}          = $element;
182         $line{'shelfname'}      = $shelflist->{$element}->{'shelfname'};
183         $line{'shelfbookcount'} = $shelflist->{$element}->{'count'};
184         push( @shelvesloop, \%line );
185     }
186     $template->param(
187         shelvesloop => \@shelvesloop,
188         shelves     => 1,
189     );
190         last SWITCH;
191     }
192 }
193
194 ($shelflist) =
195   GetShelves( $loggedinuser, 2 )
196   ;    # rebuild shelflist in case a shelf has been added
197
198 my $color = '';
199 my @shelvesloop;
200 my $numberCanManage = 0;
201
202 foreach my $element ( sort keys %$shelflist ) {
203     my %line;
204     ( $color eq 1 ) ? ( $color = 0 ) : ( $color = 1 );
205     $line{'toggle'}    = $color;
206     $line{'shelf'}     = $element;
207     $line{'shelfname'} = $shelflist->{$element}->{'shelfname'};
208     $line{ "category" . $shelflist->{$element}->{'category'} } = 1;
209     $line{'mine'} = 1 if $shelflist->{$element}->{'owner'} eq $loggedinuser;
210     $line{'shelfbookcount'} = $shelflist->{$element}->{'count'};
211     $line{'canmanage'} =  ShelfPossibleAction( $loggedinuser, $element, 'manage' );
212     $line{'firstname'} = $shelflist->{$element}->{'firstname'}
213         unless $shelflist->{$element}->{'owner'} eq $loggedinuser;
214     $line{'surname'} = $shelflist->{$element}->{'surname'}
215         unless $shelflist->{$element}->{'owner'} eq $loggedinuser;
216     
217     $numberCanManage++ if $line{'canmanage'};
218     
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{'shelfbookcount'} = $shelflist->{$element}->{'count'};
273         push( @shelvesloop, \%line );
274     }
275     $innertemplate->param(
276         shelvesloop => \@shelvesloop,
277         shelves     => 1,
278     );
279 }
280
281 #
282 # $Log$
283 # Revision 1.13  2007/04/24 13:54:29  hdl
284 # functions that were in C4::Interface::CGI::Output are now in C4::Output.
285 # So this implies quite a change for files.
286 # Sorry about conflicts which will be caused.
287 # directory Interface::CGI should now be dropped.
288 # I noticed that many scripts (reports ones, but also some circ/stats.pl or opac-topissues) still use Date::Manip.
289 #
290 # Revision 1.12  2007/04/04 16:46:22  tipaul
291 # HUGE COMMIT : code cleaning circulation.
292 #
293 # some stuff to do, i'll write a mail on koha-devel NOW !
294 #
295 # Revision 1.11  2007/03/09 14:32:26  tipaul
296 # rel_3_0 moved to HEAD
297 #
298 # Revision 1.9.2.9  2007/02/05 15:54:30  toins
299 # don't display "remove selected shelves" if the user logged has no shelf.
300 #
301 # Revision 1.9.2.8  2006/12/15 17:36:57  toins
302 # - some change on the html param.
303 # - Writing directly the code of a sub called only once.
304 # - adding syspref: BiblioDefaultView.
305 #
306 # Revision 1.9.2.7  2006/12/14 17:22:55  toins
307 # bookshelves work perfectly with mod_perl and are cleaned.
308 #
309 # Revision 1.9.2.6  2006/12/13 10:06:05  toins
310 # fix a mod_perl specific bug.
311 #
312 # Revision 1.9.2.5  2006/12/11 17:10:06  toins
313 # fixing some bugs on bookshelves.
314 #
315 # Revision 1.9.2.4  2006/11/30 18:23:51  toins
316 # theses scripts don't need to use C4::Search.
317 #
318 # Revision 1.9.2.3  2006/10/30 09:50:45  tipaul
319 # better perl writting
320 #
321 # Revision 1.9.2.2  2006/10/17 07:59:35  toins
322 # ccode added.
323 #