From HDL:
[koha.git] / opac / opac-shelves.pl.old
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 shelf content.
36
37 =item C<shelfnumber>
38
39     to know on which shelf this script has to work.
40
41 =item C<addbarcode>
42
43 =item C<op>
44
45     op can equal the following values:
46         * 'modifsave' to save changes on the shelves
47         * 'modif' to change the template to allow modification of the shelves.
48
49 =item C<viewshelf>
50
51     to load the template with 'viewshelves param' which allows reading the shelves information.
52
53 =item C<shelves>
54
55     if == 1, then call the function shelves to add or delete a shelf.
56
57 =item C<addshelf>
58
59     if the param shelves == 1, then addshelf must be equals to the name of the shelf to add.
60
61 =back
62
63 =cut
64
65 use strict;
66 use warnings;
67 use CGI;
68 use C4::Output;
69 use C4::VirtualShelves;
70 use C4::Circulation;
71 use C4::Auth;
72 use C4::Output;
73 use C4::Biblio;
74 use C4::Items;
75
76 use vars qw($debug);
77
78 BEGIN { 
79         $debug = $ENV{DEBUG} || 0;
80 }
81
82 my $query = new CGI;
83
84 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
85     {
86         template_name   => "opac-shelves.tmpl",
87         query           => $query,
88         type            => "opac",
89         authnotrequired => 1,
90     }
91 );
92
93 if ( $query->param('modifyshelfcontents') ) {
94     my $shelfnumber = $query->param('viewshelf');
95     my $barcode     = $query->param('addbarcode');
96     my ($item) = GetItemnumberFromBarcode($barcode);
97     my ($biblio) = GetBiblioFromItemNumber($item->{'itemnumber'});
98     if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' ) ) {
99         AddToShelf( $biblio->{'biblionumber'}, $shelfnumber );
100         foreach ( $query->param ) {
101             /REM-(\d+)/ or next;
102                         $debug and warn "SHELVES: user $loggedinuser removing item $1 from shelf $shelfnumber";
103             DelFromShelf( $1, $shelfnumber );   # $1 is biblionumber
104         }
105     }
106 }
107
108 my $showadd = 1;
109 # set the default tab, etc.
110 my $shelf_type = $query->param('display');
111 if (defined $shelf_type) {
112         if ($shelf_type eq 'privateshelves')  {
113                 $template->param(showprivateshelves => 1);
114         } elsif ($shelf_type eq 'publicshelves') {
115                 $template->param(showpublicshelves => 1);
116                 $showadd = 0;
117         } else {
118                 $debug and warn "Invalid 'display' param ($shelf_type)";
119         }
120 } else {
121     $template->param(showprivateshelves => 1);
122 }
123
124 # getting the Shelves list
125 my $shelflist = GetShelves( $loggedinuser, 2 );
126 $template->param( { loggedinuser => $loggedinuser } );
127 my $op = $query->param('op');
128
129 SWITCH: {
130         if ( $op ) {
131                 if ( $op eq 'modifsave' ) {
132                         ModShelf(
133                                 $query->param('shelfnumber'), $query->param('shelfname'),
134                                 $loggedinuser,                $query->param('category'), $query->param('sortfield')
135                         );
136                         $shelflist = GetShelves( $loggedinuser, 2 );    # refresh after mods
137                 } elsif ( $op eq 'modif' ) {
138                         my ( $shelfnumber, $shelfname, $owner, $category, $sortfield ) =GetShelf( $query->param('shelf') );
139                         $template->param(
140                                 edit                => 1,
141                                 shelfnumber         => $shelfnumber,
142                                 shelfname           => $shelfname,
143                                 "category$category" => 1,
144                                 "sort_$sortfield"   => 1,
145                         );
146                 }
147                 last SWITCH;
148         }
149         if ( $query->param('viewshelf') ) {
150         #check that the user can view the shelf
151         my $shelfnumber = $query->param('viewshelf');
152         if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' ) ) {
153             my $items = GetShelfContents($shelfnumber);
154                         $showadd = 1;
155                         my $i = 0;
156                         foreach (grep {$i++ % 2} @$items) {             # every other item
157                                 $_->{toggle} = 1;
158                         }
159                         my $manageshelf = &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
160                         ($manageshelf) and $showadd = 1;
161             $template->param(
162                 shelfname   => $shelflist->{$shelfnumber}->{'shelfname'},
163                 shelfnumber => $shelfnumber,
164                 viewshelf   => $query->param('viewshelf'),
165                 manageshelf => $manageshelf,
166                 itemsloop   => $items,
167             );
168         } # else {;}  # FIXME - some kind of warning *may* be in order
169         last SWITCH;
170     }
171     if ( $query->param('shelves') ) {
172         if ( my $newshelf = $query->param('addshelf') ) {
173             my $shelfnumber = AddShelf(
174                 $newshelf,
175                 $query->param('owner'),
176                 $query->param('category')
177             );
178
179             if ( $shelfnumber == -1 ) {    #shelf already exists.
180                                 $showadd = 1;
181                 $template->param(
182                         shelfnumber => $shelfnumber,
183                         already     => $newshelf,
184                 );
185             } else {
186                 print $query->redirect("/cgi-bin/koha/opac-shelves.pl?viewshelf=$shelfnumber");
187                                 exit;           # can't redirect AND expect %line to DO anything!
188                         }
189         }
190         my @paramsloop;
191         foreach ( $query->param() ) {
192                         /^DEL-(\d+)/ or next;
193                         my $number = $1;
194                         my %line;
195                         if (defined $shelflist->{$number}) {
196                                 my $name = $shelflist->{$number}->{'shelfname'};
197                                 if (DelShelf($number)) {
198                                         delete $shelflist->{$number};
199                                         $line{delete_ok}   = $name;
200                                 } else {
201                                         $line{delete_fail} = $name;
202                                 }
203                         } else {
204                                 $line{unrecognized} = $number;
205                         }
206                         push(@paramsloop, \%line);
207                         # print $query->redirect("/cgi-bin/koha/opac-shelves.pl"); exit;
208                         # can't redirect and expect %line to DO anything!
209         }
210                 $showadd = 1;
211         $template->param( 
212                         paramsloop => \@paramsloop,
213             shelves    => 1,
214         );
215         last SWITCH;
216     }
217 }
218
219 # rebuilding shelflist (in case a shelf has been added) is not necessary since add redirects!
220
221 $showadd and $template->param(showadd => 1);
222 my $color = 0;
223 my @shelvesloop;
224 my @shelveslooppriv;
225
226 foreach my $element (sort { lc($shelflist->{$a}->{'shelfname'}) cmp lc($shelflist->{$b}->{'shelfname'}) } keys %$shelflist) {
227         my %line;
228         $color = ($color) ? 0 : 1;
229         $color and $line{'toggle'} = $color;
230         $line{'shelf'} = $element;
231         $line{'shelfname'} = $shelflist->{$element}->{'shelfname'};
232         $line{'sortfield'} = $shelflist->{$element}->{'sortfield'};
233         $line{"category".$shelflist->{$element}->{'category'}} = 1;
234         $line{'shelfvirtualcount'} = $shelflist->{$element}->{'count'};
235         $line{'canmanage'} = ShelfPossibleAction($loggedinuser,$element,'manage');
236         if ($shelflist->{$element}->{'owner'} eq $loggedinuser) {
237                 $line{'mine'} = 1;
238         } else {
239                 $line{'firstname'} = $shelflist->{$element}->{'firstname'};
240                 $line{ 'surname' } = $shelflist->{$element}->{ 'surname' };
241         }
242         if ($shelflist->{$element}->{'category'} eq '1') {
243         push (@shelveslooppriv, \%line);
244     } else {
245                 push (@shelvesloop,     \%line);
246         }
247 }
248
249 $template->param(
250     shelveslooppriv => \@shelveslooppriv,
251     shelvesloop     => \@shelvesloop,
252     "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
253 );
254
255 output_html_with_http_headers $query, $cookie, $template->output;