$bibid, now correctly $biblionumber.
[koha.git] / admin / itemtypes.pl
1 #!/usr/bin/perl
2 # NOTE: 4-character tabs
3
4 #script to administer the categories table
5 #written 20/02/2002 by paul.poulain@free.fr
6 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
7
8 # ALGO :
9 # this script use an $op to know what to do.
10 # if $op is empty or none of the above values,
11 #       - the default screen is build (with all records, or filtered datas).
12 #       - the   user can clic on add, modify or delete record.
13 # if $op=add_form
14 #       - if primkey exists, this is a modification,so we read the $primkey record
15 #       - builds the add/modify form
16 # if $op=add_validate
17 #       - the user has just send datas, so we create/modify the record
18 # if $op=delete_form
19 #       - we show the record having primkey=$primkey and ask for deletion validation form
20 # if $op=delete_confirm
21 #       - we delete the record having primkey=$primkey
22
23
24 # Copyright 2000-2002 Katipo Communications
25 #
26 # This file is part of Koha.
27 #
28 # Koha is free software; you can redistribute it and/or modify it under the
29 # terms of the GNU General Public License as published by the Free Software
30 # Foundation; either version 2 of the License, or (at your option) any later
31 # version.
32 #
33 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
34 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
35 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
36 #
37 # You should have received a copy of the GNU General Public License along with
38 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
39 # Suite 330, Boston, MA  02111-1307 USA
40
41 use strict;
42 use CGI;
43 use HTML::Template;
44 use List::Util qw/min/;
45
46 use C4::Koha;
47 use C4::Context;
48 use C4::Output;
49 use C4::Search;
50 use C4::Auth;
51 use C4::Interface::CGI::Output;
52
53 sub StringSearch  {
54         my ($env,$searchstring,$type)=@_;
55         my $dbh = C4::Context->dbh;
56         $searchstring=~ s/\'/\\\'/g;
57         my @data=split(' ',$searchstring);
58         my $count=@data;
59         my $sth=$dbh->prepare("Select * from itemtypes where (description like ?) order by itemtype");
60         $sth->execute("$data[0]%");
61         my @results;
62         while (my $data=$sth->fetchrow_hashref){
63         push(@results,$data);
64         }
65         #  $sth->execute;
66         $sth->finish;
67         return (scalar(@results),\@results);
68 }
69
70 my $input = new CGI;
71 my $searchfield=$input->param('description');
72 my $script_name="/cgi-bin/koha/admin/itemtypes.pl";
73 my $itemtype=$input->param('itemtype');
74 my $pagesize=5;
75 my $op = $input->param('op');
76 $searchfield=~ s/\,//g;
77 my ($template, $borrowernumber, $cookie)
78     = get_template_and_user({template_name => "admin/itemtypes.tmpl",
79                              query => $input,
80                              type => "intranet",
81                              authnotrequired => 0,
82                              flagsrequired => {parameters => 1, management => 1},
83                              debug => 1,
84                              });
85
86 if ($op) {
87 $template->param(script_name => $script_name,
88                                                 $op              => 1); # we show only the TMPL_VAR names $op
89 } else {
90 $template->param(script_name => $script_name,
91                                                 else              => 1); # we show only the TMPL_VAR names $op
92 }
93 ################## ADD_FORM ##################################
94 # called by default. Used to create form to add or  modify a record
95 if ($op eq 'add_form') {
96         #start the page and read in includes
97         #---- if primkey exists, it's a modify action, so read values to modify...
98         my $data;
99         if ($itemtype) {
100                 my $dbh = C4::Context->dbh;
101                 my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
102                 $sth->execute($itemtype);
103                 $data=$sth->fetchrow_hashref;
104                 $sth->finish;
105         }
106         # build list of images
107         my $imagedir_filesystem = getitemtypeimagedir();
108     my $imagedir_web = getitemtypeimagesrc();
109     opendir(DIR, $imagedir_filesystem)
110         or die "can't opendir ".$imagedir_filesystem.": ".$!;
111         my @imagelist;
112         while (my $line = readdir(DIR)) {
113                 if ($line =~ /\.(gif|png)$/i) {
114             push(
115                 @imagelist,
116                 {
117                     KohaImage => $line,
118                     KohaImageSrc => $imagedir_web.'/'.$line,
119                     checked => $line eq $data->{imageurl} ? 1 : 0,
120                 }
121             );
122                 }
123         }
124         closedir DIR;
125
126     my $remote_image = undef;
127     if (defined $data->{imageurl} and $data->{imageurl} =~ m/^http/) {
128         $remote_image = $data->{imageurl};
129     }
130
131         $template->param(
132         itemtype => $itemtype,
133         description => $data->{'description'},
134         renewalsallowed => $data->{'renewalsallowed'},
135         rentalcharge => sprintf("%.2f",$data->{'rentalcharge'}),
136         notforloan => $data->{'notforloan'},
137         imageurl => $data->{'imageurl'},
138         template => C4::Context->preference('template'),
139         IMAGESLOOP => \@imagelist,
140         remote_image => $remote_image,
141     );
142                                                                                                         # END $OP eq ADD_FORM
143 ################## ADD_VALIDATE ##################################
144 # called by add_form, used to insert/modify data in DB
145 } elsif ($op eq 'add_validate') {
146         my $dbh = C4::Context->dbh;
147
148     my $query = '
149 UPDATE itemtypes
150   SET description = ?
151     , renewalsallowed = ?
152     , rentalcharge = ?
153     , notforloan = ?
154     , imageurl = ?
155   WHERE itemtype = ?
156 ';
157     my $sth=$dbh->prepare($query);
158         $sth->execute(
159         $input->param('description'),
160                 $input->param('renewalsallowed'),
161         $input->param('rentalcharge'),
162                 $input->param('notforloan') ? 1 : 0,
163         $input->param('image') eq 'removeImage'
164             ? undef
165             : $input->param('image') eq 'remoteImage'
166                 ? $input->param('remoteImage')
167                 : $input->param('image'),
168                 $input->param('itemtype'),
169     );
170
171         $sth->finish;
172         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=itemtypes.pl\"></html>";
173         exit;
174                                                                                                         # END $OP eq ADD_VALIDATE
175 ################## DELETE_CONFIRM ##################################
176 # called by default form, used to confirm deletion of data in DB
177 } elsif ($op eq 'delete_confirm') {
178         #start the page and read in includes
179         my $dbh = C4::Context->dbh;
180
181         # Check both categoryitem and biblioitems, see Bug 199
182         my $total = 0;
183         for my $table ('biblioitems') {
184            my $sth=$dbh->prepare("select count(*) as total from $table where itemtype=?");
185            $sth->execute($itemtype);
186            $total += $sth->fetchrow_hashref->{total};
187            $sth->finish;
188         }
189
190         my $sth=$dbh->prepare("select itemtype,description,renewalsallowed,rentalcharge from itemtypes where itemtype=?");
191         $sth->execute($itemtype);
192         my $data=$sth->fetchrow_hashref;
193         $sth->finish;
194
195         $template->param(itemtype => $itemtype,
196                                                         description => $data->{description},
197                                                         renewalsallowed => $data->{renewalsallowed},
198                                                         rentalcharge => sprintf("%.2f",$data->{rentalcharge}),
199                                                         imageurl => $data->{imageurl},
200                                                         total => $total);
201                                                                                                         # END $OP eq DELETE_CONFIRM
202 ################## DELETE_CONFIRMED ##################################
203 # called by delete_confirm, used to effectively confirm deletion of data in DB
204 } elsif ($op eq 'delete_confirmed') {
205         #start the page and read in includes
206         my $dbh = C4::Context->dbh;
207         my $itemtype=uc($input->param('itemtype'));
208         my $sth=$dbh->prepare("delete from itemtypes where itemtype=?");
209         $sth->execute($itemtype);
210         $sth = $dbh->prepare("delete from issuingrules where itemtype=?");
211         $sth->execute($itemtype);
212         $sth->finish;
213         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=itemtypes.pl\"></html>";
214         exit;
215                                                                                                         # END $OP eq DELETE_CONFIRMED
216 ################## DEFAULT ##################################
217 } else { # DEFAULT
218     my $env;
219     my ($count,$results)=StringSearch($env,$searchfield,'web');
220
221     my $page = $input->param('page') || 1;
222     my $first = ($page - 1) * $pagesize;
223
224     # if we are on the last page, the number of the last word to display
225     # must not exceed the length of the results array
226     my $last = min(
227         $first + $pagesize - 1,
228         scalar @{$results} - 1,
229     );
230
231     my $toggle = 0;
232     my @loop;
233     foreach my $result (@{$results}[$first .. $last]) {
234         my $itemtype = $result;
235         $itemtype->{toggle} = ($toggle++%2 eq 0 ? 1 : 0);
236         $itemtype->{imageurl} =
237             getitemtypeimagesrcfromurl($itemtype->{imageurl});
238         $itemtype->{rentalcharge} = sprintf('%.2f', $itemtype->{rentalcharge});
239
240         push(@loop, $itemtype);
241     }
242
243     $template->param(
244         loop => \@loop,
245         pagination_bar => pagination_bar(
246             $script_name,
247             getnbpages(scalar @{$results}, $pagesize),
248             $page,
249             'page'
250         )
251     );
252 } #---- END $OP eq DEFAULT
253 output_html_with_http_headers $input, $cookie, $template->output;
254
255 # Local Variables:
256 # tab-width: 4
257 # End: