Patch from Joe Atzberger to remove $Id$ and $Log$ from scripts
[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
91     if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' ) ) {
92         AddToShelf( $biblio->{'biblionumber'}, $shelfnumber );
93         foreach ( $query->param ) {
94             if (/REM-(\d*)/) {
95                 my $biblionumber = $1;
96                 DelFromShelf( $biblionumber, $shelfnumber );
97             }
98         }
99     }
100 }
101
102 # getting the Shelves list
103 my $shelflist = GetShelves( $loggedinuser, 2 );
104 $template->param( { loggedinuser => $loggedinuser } );
105 my $op = $query->param('op');
106
107 SWITCH: {
108     if ( $op && ( $op eq 'modifsave' ) ) {
109         ModShelf(
110             $query->param('shelfnumber'), $query->param('shelfname'),
111             $loggedinuser,                $query->param('category')
112         );
113         last SWITCH;
114     }
115     if ( $op && ( $op eq 'modif' ) ) {
116         my ( $shelfnumber, $shelfname, $owner, $category ) =
117           GetShelf( $query->param('shelf') );
118         $template->param(
119             edit                => 1,
120             shelfnumber         => $shelfnumber,
121             shelfname           => $shelfname,
122             "category$category" => 1
123         );
124
125         #         editshelf($query->param('shelf'));
126         last SWITCH;
127     }
128     if ( $query->param('viewshelf') ) {
129
130         #check that the user can view the shelf
131         my $shelfnumber = $query->param('viewshelf');
132         if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' ) ) {
133             my $items = GetShelfContents($shelfnumber);
134             $template->param(
135                 shelfname   => $shelflist->{$shelfnumber}->{'shelfname'},
136                 shelfnumber => $shelfnumber,
137                 viewshelf   => $query->param('viewshelf'),
138                 manageshelf =>
139                   &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' ),
140                 itemsloop => $items,
141             );
142         }
143         last SWITCH;
144     }
145     if ( $query->param('shelves') ) {
146         if ( my $newshelf = $query->param('addshelf') ) {
147             my $shelfnumber = AddShelf(
148                 $newshelf,
149                 $query->param('owner'),
150                 $query->param('category')
151             );
152
153             if ( $shelfnumber == -1 ) {    #shelf already exists.
154                 $template->param(
155                     {
156                         shelfnumber => $shelfnumber,
157                         already     => 1
158                     }
159                 );
160             }
161         }
162         my @paramsloop;
163         foreach ( $query->param() ) {
164             my %line;
165             if (/DEL-(\d+)/) {
166                 my $delshelf = $1;
167                 my ( $status, $count ) = DelShelf($delshelf);
168                 if ($status) {
169                     $line{'status'} = $status;
170                     $line{'count'}  = $count;
171                 }
172             }
173
174             #if the shelf is not deleted, %line points on null
175             push( @paramsloop, \%line );
176         }
177         $template->param( paramsloop => \@paramsloop );
178         my ($shelflist) = GetShelves( $loggedinuser, 2 );
179         my $color = '';
180         my @shelvesloop;
181         foreach my $element ( sort keys %$shelflist ) {
182             my %line;
183             ( $color eq 1 ) ? ( $color = 0 ) : ( $color = 1 );
184             $line{'toggle'}            = $color;
185             $line{'shelf'}             = $element;
186             $line{'shelfname'}         = $shelflist->{$element}->{'shelfname'};
187             $line{'shelfvirtualcount'} = $shelflist->{$element}->{'count'};
188             push( @shelvesloop, \%line );
189         }
190         $template->param(
191             shelvesloop => \@shelvesloop,
192             shelves     => 1,
193         );
194         last SWITCH;
195     }
196 }
197
198 my $shelflist = GetShelves( $loggedinuser, 2 );
199 my $color = '';
200 my @shelvesloop;
201 my $numberCanManage = 0;
202
203 foreach my $element ( sort keys %$shelflist ) {
204     my %line;
205     ( $color eq 1 ) ? ( $color = 0 ) : ( $color = 1 );
206     $line{'toggle'}    = $color;
207     $line{'shelf'}     = $element;
208     $line{'shelfname'} = $shelflist->{$element}->{'shelfname'};
209     $line{"viewcategory$shelflist->{$element}->{'category'}"} = 1;
210     $line{'mine'} = 1 if $shelflist->{$element}->{'owner'} eq $loggedinuser;
211     $line{'shelfvirtualcount'} = $shelflist->{$element}->{'count'};
212     $line{'canmanage'} =
213       ShelfPossibleAction( $loggedinuser, $element, 'manage' );
214     $line{'firstname'} = $shelflist->{$element}->{'firstname'}
215       unless $shelflist->{$element}->{'owner'} eq $loggedinuser;
216     $line{'surname'} = $shelflist->{$element}->{'surname'}
217       unless $shelflist->{$element}->{'owner'} eq $loggedinuser;
218     $numberCanManage++ if $line{'canmanage'};
219     push( @shelvesloop, \%line );
220 }
221
222 $template->param(
223     shelvesloop     => \@shelvesloop,
224
225     numberCanManage => $numberCanManage,
226 );
227
228 output_html_with_http_headers $query, $cookie, $template->output;
229
230 sub shelves {
231     my $innertemplate = shift;
232     if ( my $newshelf = $query->param('addshelf') ) {
233         my $shelfnumber = AddShelf(
234             $newshelf,
235             $query->param('owner'),
236             $query->param('category')
237         );
238
239         if ( $shelfnumber == -1 ) {    #shelf already exists.
240             $template->param(
241                 {
242                     shelfnumber => $shelfnumber,
243                     already     => 1
244                 }
245             );
246         }
247     }
248     my @paramsloop;
249     foreach ( $query->param() ) {
250         my %line;
251         if (/DEL-(\d+)/) {
252             my $delshelf = $1;
253             my ( $status, $count ) = DelShelf($delshelf);
254             if ($status) {
255                 $line{'status'} = $status;
256                 $line{'count'}  = $count;
257             }
258         }
259
260         #if the shelf is not deleted, %line points on null
261         push( @paramsloop, \%line );
262     }
263     $innertemplate->param( paramsloop => \@paramsloop );
264     my ($shelflist) = GetShelves( $loggedinuser, 2 );
265     my $color = '';
266     my @shelvesloop;
267     foreach my $element ( sort keys %$shelflist ) {
268         my %line;
269         ( $color eq 1 ) ? ( $color = 0 ) : ( $color = 1 );
270         $line{'toggle'}            = $color;
271         $line{'shelf'}             = $element;
272         $line{'shelfname'}         = $shelflist->{$element}->{'shelfname'};
273         $line{'shelfvirtualcount'} = $shelflist->{$element}->{'count'};
274         push( @shelvesloop, \%line );
275     }
276     $innertemplate->param(
277         shelvesloop => \@shelvesloop,
278         shelves     => 1,
279     );
280 }
281
282 #
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 # virtualshelves 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 virtualshelves.
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 #