bug 2857: fix UTF-8 conversion issues in web services
[koha.git] / virtualshelves / shelves.pl.old
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::Items;
71 use C4::Auth;
72 use C4::Output;
73
74 use vars qw($debug);
75
76 BEGIN {
77     $debug = $ENV{DEBUG} || 0;
78 }
79
80 my $query = new CGI;
81
82 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
83     {
84         template_name   => "virtualshelves/shelves.tmpl",
85         query           => $query,
86         type            => "intranet",
87         authnotrequired => 0,
88         flagsrequired   => { catalogue => 1 },
89     }
90 );
91
92 if ( $query->param('modifyshelfcontents') ) {
93     my $shelfnumber = $query->param('viewshelf');
94     my $barcode     = $query->param('addbarcode');
95     my ($item) = GetItem( 0, $barcode );
96     my ($biblio) = GetBiblioFromItemNumber($item->{'itemnumber'});
97     if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' ) ) {
98         AddToShelf( $biblio->{'biblionumber'}, $shelfnumber );
99         foreach ( $query->param ) {
100                         /REM-(\d*)/ or next;
101                         $debug and warn "SHELVES: user $loggedinuser removing item $1 from shelf $shelfnumber";
102                         DelFromShelf( $1, $shelfnumber );        # $1 is biblionumber
103         }
104     }
105 }
106
107 # getting the Shelves list
108 my $shelflist = GetShelves( $loggedinuser, 2 );
109 $template->param( { loggedinuser => $loggedinuser } );
110 my $op = $query->param('op');
111
112 SWITCH: {
113     if ( $op && ( $op eq 'modifsave' ) ) {
114         ModShelf(
115             $query->param('shelfnumber'), $query->param('shelfname'),
116             $loggedinuser,                $query->param('category'), $query->param('sortfield')
117         );
118         last SWITCH;
119     }
120     if ( $op && ( $op eq 'modif' ) ) {
121         my ( $shelfnumber, $shelfname, $owner, $category, $sortfield ) =
122           GetShelf( $query->param('shelf') );
123         $template->param(
124             edit                => 1,
125             shelfnumber         => $shelfnumber,
126             shelfname           => $shelfname,
127             "category$category" => 1,
128         "sort_$sortfield"   => 1,
129         );
130         last SWITCH;
131     }
132     if ( $query->param('viewshelf') ) {
133
134         #check that the user can view the shelf
135         my $shelfnumber = $query->param('viewshelf');
136         if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' ) ) {
137             my $items = GetShelfContents($shelfnumber);
138             $template->param(
139                 shelfname   => $shelflist->{$shelfnumber}->{'shelfname'},
140                 shelfnumber => $shelfnumber,
141                 viewshelf   => $query->param('viewshelf'),
142                 manageshelf => &ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' ),
143                 itemsloop => $items,
144             );
145         }
146         last SWITCH;
147     }
148     if ( $query->param('shelves') ) {
149         if ( my $newshelf = $query->param('addshelf') ) {
150             my $shelfnumber = AddShelf(
151                 $newshelf,
152                 $query->param('owner'),
153                 $query->param('category')
154             );
155
156             if ( $shelfnumber == -1 ) {    #shelf already exists.
157                 $template->param(
158                     {
159                         shelfnumber => $shelfnumber,
160                         already     => 1
161                     }
162                 );
163             }
164             print $query->redirect("/cgi-bin/koha/virtualshelves/shelves.pl?viewshelf=$shelfnumber");
165             exit;
166         }
167         my @paramsloop;
168         foreach ( $query->param() ) {
169             my %line;
170             if (/DEL-(\d+)/) {
171                 my $delshelf = $1;
172                 my ( $status, $count ) = DelShelf($delshelf);
173                 if ($status) {
174                     $line{'status'} = $status;
175                     $line{'count'}  = $count;
176                 }
177                 print $query->redirect("/cgi-bin/koha/virtualshelves/shelves.pl");
178                 exit;
179             }
180
181             #if the shelf is not deleted, %line points on null
182             # push( @paramsloop, \%line );
183         }
184         $template->param( paramsloop => \@paramsloop );
185         my ($shelflist) = GetShelves( $loggedinuser, 2 );
186         my $i = 0;
187         my @shelvesloop;
188         foreach my $element ( sort keys %$shelflist ) {
189             my %line;
190             (++$i % 2) and $line{'toggle'} = $i;
191             $line{'shelf'}             = $element;
192             $line{'shelfname'}         = $shelflist->{$element}->{'shelfname'};
193             $line{'shelfvirtualcount'} = $shelflist->{$element}->{'count'};
194             push( @shelvesloop, \%line );
195         }
196         $template->param(
197             shelvesloop => \@shelvesloop,
198             shelves     => 1,
199         );
200         last SWITCH;
201     }
202 }
203
204 # rebuild shelflist in case a shelf has been added
205 $shelflist = GetShelves( $loggedinuser, 2 );
206 my $i = 0;
207 my @shelvesloop;
208 my $numberCanManage = 0;
209
210 foreach my $element ( sort keys %$shelflist ) {
211         my %line;
212         (++$i % 2) and $line{'toggle'} = $i;
213         $line{'shelf'}             = $element;
214         $line{'shelfname'}         = $shelflist->{$element}->{'shelfname'};
215         $line{'shelfvirtualcount'} = $shelflist->{$element}->{'count'};
216         $line{'sortfield'}         = $shelflist->{$element}->{'sortfield'};
217         $line{"viewcategory$shelflist->{$element}->{'category'}"} = 1;
218         $line{'canmanage'} = ShelfPossibleAction( $loggedinuser, $element, 'manage' );
219         if ($shelflist->{$element}->{'owner'} eq $loggedinuser) {
220                 $line{'mine'} = 1;
221         } else {
222                 $line{'firstname'} = $shelflist->{$element}->{'firstname'};
223                 $line{'surname'}   = $shelflist->{$element}->{'surname'}  ;
224         }
225         $numberCanManage++ if $line{'canmanage'};
226         push( @shelvesloop, \%line );
227 }
228
229 $template->param(
230     shelvesloop     => \@shelvesloop,
231     numberCanManage => $numberCanManage,
232 );
233 if ($template->param('viewshelf') or
234         $template->param( 'shelves' ) or
235         $template->param(  'edit'   ) ) {
236         $template->param(vseflag => 1);
237 }
238 if ($template->param( 'shelves' ) or
239         $template->param(  'edit'   ) ) {
240         $template->param( seflag => 1);
241 }
242
243 output_html_with_http_headers $query, $cookie, $template->output;
244
245 sub shelves {
246     my $innertemplate = shift;
247     if ( my $newshelf = $query->param('addshelf') ) {
248         my $shelfnumber = AddShelf(
249             $newshelf,
250             $query->param('owner'),
251             $query->param('category')
252         );
253
254         if ( $shelfnumber == -1 ) {    #shelf already exists.
255             $template->param(
256                 {
257                     shelfnumber => $shelfnumber,
258                     already     => 1
259                 }
260             );
261         }
262     }
263     my @paramsloop;
264     foreach ( $query->param() ) {
265         my %line;
266         if (/DEL-(\d+)/) {
267             my $delshelf = $1;
268             my ( $status, $count ) = DelShelf($delshelf);
269             if ($status) {
270                 $line{'status'} = $status;
271                 $line{'count'}  = $count;
272             }
273         }
274
275         #if the shelf is not deleted, %line points on null
276         push( @paramsloop, \%line );
277     }
278     $innertemplate->param( paramsloop => \@paramsloop );
279     my ($shelflist) = GetShelves( $loggedinuser, 2 );
280     my $i = 0;
281     my @shelvesloop;
282     foreach my $element ( sort keys %$shelflist ) {
283         my %line;
284         (++$i % 2) and $line{'toggle'} = $i;
285         $line{'shelf'}             = $element;
286         $line{'shelfname'}         = $shelflist->{$element}->{'shelfname'};
287         $line{'shelfvirtualcount'} = $shelflist->{$element}->{'count'};
288         push( @shelvesloop, \%line );
289     }
290     $innertemplate->param(
291         shelvesloop => \@shelvesloop,
292         shelves     => 1,
293     );
294 }