Warning: Big Commit. Fixing Virtual Shelves
[koha.git] / opac / opac-shelves.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20
21 =head1 NAME
22
23     opac-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 warnings;
68 use CGI;
69 use C4::Output;
70 use C4::VirtualShelves;
71 use C4::Circulation;
72 use C4::Auth;
73 use C4::Output;
74 use C4::Biblio;
75
76 my $query = new CGI;
77
78 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
79     {
80         template_name   => "opac-shelves.tmpl",
81         query           => $query,
82         type            => "opac",
83         authnotrequired => 1,
84     }
85 );
86
87 if ( $query->param('modifyshelfcontents') ) {
88     my $shelfnumber = $query->param('viewshelf');
89     my $barcode     = $query->param('addbarcode');
90     my ($item) = GetItemnumberFromBarcode($barcode);
91     my ($biblio) = GetBiblioFromItemNumber($item->{'itemnumber'});
92     if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' ) ) {
93         AddToShelf( $biblio->{'biblionumber'}, $shelfnumber );
94         foreach ( $query->param ) {
95             if (/REM-(\d*)/) {
96                 my $biblionumber = $1;
97                 DelFromShelf( $biblionumber, $shelfnumber );
98             }
99         }
100     }
101 }
102
103 # set the default tab, etc.
104 my $shelf_type = $query->param('display');
105 if ((!$shelf_type) || ($shelf_type eq 'privateshelves'))  {
106     $template->param(showprivateshelves => 1);
107 } elsif ($shelf_type eq 'publicshelves') {
108     $template->param(showpublicshelves => 1);
109 }
110
111 # getting the Shelves list
112 my $shelflist = GetShelves( $loggedinuser, 2 );
113 $template->param( { loggedinuser => $loggedinuser } );
114 my $op = $query->param('op');
115
116 SWITCH: {
117     if ( $op && ( $op eq 'modifsave' ) ) {
118         ModShelf(
119             $query->param('shelfnumber'), $query->param('shelfname'),
120             $loggedinuser,                $query->param('category'), $query->param('sortfield')
121         );
122         last SWITCH;
123     }
124     if ( $op && ( $op eq 'modif' ) ) {
125         my ( $shelfnumber, $shelfname, $owner, $category, $sortfield ) =
126           GetShelf( $query->param('shelf') );
127         $template->param(
128             edit                => 1,
129             shelfnumber         => $shelfnumber,
130             shelfname           => $shelfname,
131             "category$category" => 1,
132             "sort_$sortfield"   => 1,
133         );
134         last SWITCH;
135     }
136     if ( $query->param('viewshelf') ) {
137
138         #check that the user can view the shelf
139         my $shelfnumber = $query->param('viewshelf');
140         if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' ) ) {
141             my $items = GetShelfContents($shelfnumber);
142             $template->param(
143                 shelfname   => $shelflist->{$shelfnumber}->{'shelfname'},
144                 shelfnumber => $shelfnumber,
145                 viewshelf   => $query->param('viewshelf'),
146                 manageshelf => &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' ),
147                 itemsloop   => $items,
148             );
149         }
150         last SWITCH;
151     }
152     if ( $query->param('shelves') ) {
153         if ( my $newshelf = $query->param('addshelf') ) {
154             my $shelfnumber = AddShelf(
155                 $newshelf,
156                 $query->param('owner'),
157                 $query->param('category')
158             );
159
160             if ( $shelfnumber == -1 ) {    #shelf already exists.
161                 $template->param(
162                     {
163                         shelfnumber => $shelfnumber,
164                         already     => 1
165                     }
166                 );
167             }
168             print $query->redirect("/cgi-bin/koha/opac-shelves.pl?viewshelf=$shelfnumber");
169             exit;
170         }
171         my @paramsloop;
172         foreach ( $query->param() ) {
173             my %line;
174             if (/DEL-(\d+)/) {
175                 my $delshelf = $1;
176                 my ( $status, $count ) = DelShelf($delshelf);
177                 if ($status) {
178                     $line{'status'} = $status;
179                     $line{'count'}  = $count;
180                 }
181                 print $query->redirect("/cgi-bin/koha/opac-shelves.pl");
182                 exit;
183             }
184
185             #if the shelf is not deleted, %line points on null
186             # push( @paramsloop, \%line );
187         }
188         $template->param( paramsloop => \@paramsloop );
189         my ($shelflist) = GetShelves( $loggedinuser, 2 );
190         my $color = '';
191         my @shelvesloop;
192         foreach my $element ( sort keys %$shelflist ) {
193             my %line;
194             ( $color eq 1 ) ? ( $color = 0 ) : ( $color = 1 );
195             $line{'toggle'}         = $color;
196             $line{'shelf'}          = $element;
197             $line{'shelfname'}      = $shelflist->{$element}->{'shelfname'};
198             $line{'shelfvirtualcount'} = $shelflist->{$element}->{'count'};
199             push( @shelvesloop, \%line );
200         }
201         $template->param(
202             shelvesloop => \@shelvesloop,
203             shelves     => 1,
204         );
205         last SWITCH;
206     }
207 }
208
209 # rebuild shelflist in case a shelf has been added
210 ($shelflist) = GetShelves( $loggedinuser, 2 ) ;    
211 my $color='';
212 my @shelvesloop;
213 my @shelveslooppriv;
214
215 foreach my $element (sort { lc($shelflist->{$a}->{'shelfname'}) cmp lc($shelflist->{$b}->{'shelfname'}) } keys %$shelflist) {
216     my %line;
217     my %linepriv;
218     ($color eq 0) ? ($color=1) : ($color=0);
219     if ($shelflist->{$element}->{'category'} eq 2) {
220         $line{'toggle'}= $color;
221         $line{'shelf'}=$element;
222         $line{'shelfname'}=$shelflist->{$element}->{'shelfname'};
223         $line{'sortfield'}=$shelflist->{$element}->{'sortfield'};
224         $line{"category".$shelflist->{$element}->{'category'}} = 1;
225         $line{'mine'} = 1 if $shelflist->{$element}->{'owner'} eq $loggedinuser;
226         $line{'shelfvirtualcount'}=$shelflist->{$element}->{'count'};
227         $line{'canmanage'} = ShelfPossibleAction($loggedinuser,$element,'manage');
228         $line{'firstname'}=$shelflist->{$element}->{'firstname'} unless $shelflist->{$element}->{'owner'} eq $loggedinuser;
229         $line{'surname'}=$shelflist->{$element}->{'surname'} unless $shelflist->{$element}->{'owner'} eq $loggedinuser;
230         push (@shelvesloop, \%line);
231     } elsif ($shelflist->{$element}->{'category'} eq 1) {
232         $linepriv{'toggle'}= $color;
233         $linepriv{'shelf'}=$element;
234         $linepriv{'shelfname'}=$shelflist->{$element}->{'shelfname'};
235         $linepriv{'sortfield'}=$shelflist->{$element}->{'sortfield'};
236         $linepriv{"category".$shelflist->{$element}->{'category'}} = 1;
237         $linepriv{'mine'} = 1 if $shelflist->{$element}->{'owner'} eq $loggedinuser;
238         $linepriv{'shelfvirtualcount'}=$shelflist->{$element}->{'count'};
239         $linepriv{'canmanage'} = ShelfPossibleAction($loggedinuser,$element,'manage');
240         $linepriv{'firstname'}=$shelflist->{$element}->{'firstname'} unless $shelflist->{$element}->{'owner'} eq $loggedinuser;
241         $linepriv{'surname'}=$shelflist->{$element}->{'surname'} unless $shelflist->{$element}->{'owner'} eq $loggedinuser;
242         push (@shelveslooppriv, \%linepriv);
243     }
244 }
245
246 $template->param(
247     shelveslooppriv => \@shelveslooppriv,
248     shelvesloop             => \@shelvesloop,
249     "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
250 );
251
252 output_html_with_http_headers $query, $cookie, $template->output;