fixing virtual shelves (unfinished)
[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')
111         );
112         last SWITCH;
113     }
114     if ( $op && ( $op eq 'modif' ) ) {
115         my ( $shelfnumber, $shelfname, $owner, $category ) =
116           GetShelf( $query->param('shelf') );
117         $template->param(
118             edit                => 1,
119             shelfnumber         => $shelfnumber,
120             shelfname           => $shelfname,
121             "category$category" => 1
122         );
123
124         #         editshelf($query->param('shelf'));
125         last SWITCH;
126     }
127     if ( $query->param('viewshelf') ) {
128
129         #check that the user can view the shelf
130         my $shelfnumber = $query->param('viewshelf');
131         if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' ) ) {
132             my $items = GetShelfContents($shelfnumber);
133             $template->param(
134                 shelfname   => $shelflist->{$shelfnumber}->{'shelfname'},
135                 shelfnumber => $shelfnumber,
136                 viewshelf   => $query->param('viewshelf'),
137                 manageshelf =>
138                   &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' ),
139                 itemsloop => $items,
140             );
141         }
142         last SWITCH;
143     }
144     if ( $query->param('shelves') ) {
145         if ( my $newshelf = $query->param('addshelf') ) {
146             my $shelfnumber = AddShelf(
147                 $newshelf,
148                 $query->param('owner'),
149                 $query->param('category')
150             );
151
152             if ( $shelfnumber == -1 ) {    #shelf already exists.
153                 $template->param(
154                     {
155                         shelfnumber => $shelfnumber,
156                         already     => 1
157                     }
158                 );
159             }
160             print $query->redirect("/cgi-bin/koha/virtualshelves/shelves.pl?viewshelf=$shelfnumber");
161             exit;
162         }
163         my @paramsloop;
164         foreach ( $query->param() ) {
165             my %line;
166             if (/DEL-(\d+)/) {
167                 my $delshelf = $1;
168                 my ( $status, $count ) = DelShelf($delshelf);
169                 if ($status) {
170                     $line{'status'} = $status;
171                     $line{'count'}  = $count;
172                 }
173                 print $query->redirect("/cgi-bin/koha/virtualshelves/shelves.pl");
174                 exit;
175             }
176
177             #if the shelf is not deleted, %line points on null
178 #             push( @paramsloop, \%line );
179         }
180         $template->param( paramsloop => \@paramsloop );
181         my ($shelflist) = GetShelves( $loggedinuser, 2 );
182         my $color = '';
183         my @shelvesloop;
184         foreach my $element ( sort keys %$shelflist ) {
185             my %line;
186             ( $color eq 1 ) ? ( $color = 0 ) : ( $color = 1 );
187             $line{'toggle'}            = $color;
188             $line{'shelf'}             = $element;
189             $line{'shelfname'}         = $shelflist->{$element}->{'shelfname'};
190             $line{'shelfvirtualcount'} = $shelflist->{$element}->{'count'};
191             push( @shelvesloop, \%line );
192         }
193         $template->param(
194             shelvesloop => \@shelvesloop,
195             shelves     => 1,
196         );
197         last SWITCH;
198     }
199 }
200
201 my $shelflist = GetShelves( $loggedinuser, 2 );
202 my $color = '';
203 my @shelvesloop;
204 my $numberCanManage = 0;
205
206 foreach my $element ( sort keys %$shelflist ) {
207     my %line;
208     ( $color eq 1 ) ? ( $color = 0 ) : ( $color = 1 );
209     $line{'toggle'}    = $color;
210     $line{'shelf'}     = $element;
211     $line{'shelfname'} = $shelflist->{$element}->{'shelfname'};
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'} =
216       ShelfPossibleAction( $loggedinuser, $element, 'manage' );
217     $line{'firstname'} = $shelflist->{$element}->{'firstname'}
218       unless $shelflist->{$element}->{'owner'} eq $loggedinuser;
219     $line{'surname'} = $shelflist->{$element}->{'surname'}
220       unless $shelflist->{$element}->{'owner'} eq $loggedinuser;
221     $numberCanManage++ if $line{'canmanage'};
222     push( @shelvesloop, \%line );
223 }
224
225 $template->param(
226     shelvesloop     => \@shelvesloop,
227
228     numberCanManage => $numberCanManage,
229 );
230
231 output_html_with_http_headers $query, $cookie, $template->output;
232
233 sub shelves {
234     my $innertemplate = shift;
235     if ( my $newshelf = $query->param('addshelf') ) {
236         my $shelfnumber = AddShelf(
237             $newshelf,
238             $query->param('owner'),
239             $query->param('category')
240         );
241
242         if ( $shelfnumber == -1 ) {    #shelf already exists.
243             $template->param(
244                 {
245                     shelfnumber => $shelfnumber,
246                     already     => 1
247                 }
248             );
249         }
250     }
251     my @paramsloop;
252     foreach ( $query->param() ) {
253         my %line;
254         if (/DEL-(\d+)/) {
255             my $delshelf = $1;
256             my ( $status, $count ) = DelShelf($delshelf);
257             if ($status) {
258                 $line{'status'} = $status;
259                 $line{'count'}  = $count;
260             }
261         }
262
263         #if the shelf is not deleted, %line points on null
264         push( @paramsloop, \%line );
265     }
266     $innertemplate->param( paramsloop => \@paramsloop );
267     my ($shelflist) = GetShelves( $loggedinuser, 2 );
268     my $color = '';
269     my @shelvesloop;
270     foreach my $element ( sort keys %$shelflist ) {
271         my %line;
272         ( $color eq 1 ) ? ( $color = 0 ) : ( $color = 1 );
273         $line{'toggle'}            = $color;
274         $line{'shelf'}             = $element;
275         $line{'shelfname'}         = $shelflist->{$element}->{'shelfname'};
276         $line{'shelfvirtualcount'} = $shelflist->{$element}->{'count'};
277         push( @shelvesloop, \%line );
278     }
279     $innertemplate->param(
280         shelvesloop => \@shelvesloop,
281         shelves     => 1,
282     );
283 }
284
285 #
286 # Revision 1.13  2007/04/24 13:54:29  hdl
287 # functions that were in C4::Interface::CGI::Output are now in C4::Output.
288 # So this implies quite a change for files.
289 # Sorry about conflicts which will be caused.
290 # directory Interface::CGI should now be dropped.
291 # I noticed that many scripts (reports ones, but also some circ/stats.pl or opac-topissues) still use Date::Manip.
292 #
293 # Revision 1.12  2007/04/04 16:46:22  tipaul
294 # HUGE COMMIT : code cleaning circulation.
295 #
296 # some stuff to do, i'll write a mail on koha-devel NOW !
297 #
298 # Revision 1.11  2007/03/09 14:32:26  tipaul
299 # rel_3_0 moved to HEAD
300 #
301 # Revision 1.9.2.9  2007/02/05 15:54:30  toins
302 # don't display "remove selected shelves" if the user logged has no shelf.
303 #
304 # Revision 1.9.2.8  2006/12/15 17:36:57  toins
305 # - some change on the html param.
306 # - Writing directly the code of a sub called only once.
307 # - adding syspref: BiblioDefaultView.
308 #
309 # Revision 1.9.2.7  2006/12/14 17:22:55  toins
310 # virtualshelves work perfectly with mod_perl and are cleaned.
311 #
312 # Revision 1.9.2.6  2006/12/13 10:06:05  toins
313 # fix a mod_perl specific bug.
314 #
315 # Revision 1.9.2.5  2006/12/11 17:10:06  toins
316 # fixing some bugs on virtualshelves.
317 #
318 # Revision 1.9.2.4  2006/11/30 18:23:51  toins
319 # theses scripts don't need to use C4::Search.
320 #
321 # Revision 1.9.2.3  2006/10/30 09:50:45  tipaul
322 # better perl writting
323 #
324 # Revision 1.9.2.2  2006/10/17 07:59:35  toins
325 # ccode added.
326 #