french updated
[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
44 use List::Util qw/min/;
45
46 use C4::Koha;
47 use C4::Context;
48 use C4::Auth;
49 use C4::Output;
50
51 sub StringSearch  {
52         my ($searchstring,$type)=@_;
53         my $dbh = C4::Context->dbh;
54         $searchstring=~ s/\'/\\\'/g;
55         my @data=split(' ',$searchstring);
56         my $count=@data;
57         my $sth=$dbh->prepare("Select * from itemtypes where (description like ?) order by itemtype");
58         $sth->execute("$data[0]%");
59         my @results;
60         while (my $data=$sth->fetchrow_hashref){
61         push(@results,$data);
62         }
63         #  $sth->execute;
64         $sth->finish;
65         return (scalar(@results),\@results);
66 }
67
68 my $input = new CGI;
69 my $searchfield=$input->param('description');
70 my $script_name="/cgi-bin/koha/admin/itemtypes.pl";
71 my $itemtype=$input->param('itemtype');
72 my $pagesize=10;
73 my $op = $input->param('op');
74 $searchfield=~ s/\,//g;
75 my ($template, $borrowernumber, $cookie)
76     = get_template_and_user({template_name => "admin/itemtypes.tmpl",
77                              query => $input,
78                              type => "intranet",
79                              authnotrequired => 0,
80                              flagsrequired => {parameters => 1},
81                              debug => 1,
82                              });
83
84 if ($op) {
85 $template->param(script_name => $script_name,
86                                                 $op              => 1); # we show only the TMPL_VAR names $op
87 } else {
88 $template->param(script_name => $script_name,
89                                                 else              => 1); # we show only the TMPL_VAR names $op
90 }
91 ################## ADD_FORM ##################################
92 # called by default. Used to create form to add or  modify a record
93 if ($op eq 'add_form') {
94         #start the page and read in includes
95         #---- if primkey exists, it's a modify action, so read values to modify...
96         my $data;
97         if ($itemtype) {
98                 my $dbh = C4::Context->dbh;
99                 my $sth=$dbh->prepare("select * from itemtypes where itemtype=?");
100                 $sth->execute($itemtype);
101                 $data=$sth->fetchrow_hashref;
102                 $sth->finish;
103         }
104         # build list of images
105         my $imagedir_filesystem = getitemtypeimagedir();
106     my $imagedir_web = getitemtypeimagesrc();
107     opendir(DIR, $imagedir_filesystem)
108         or warn "can't opendir ".$imagedir_filesystem.": ".$!;
109         my @imagelist;
110         my $i=0;
111         my $image_per_line=12;
112         while (my $line = readdir(DIR)) {
113             $i++;
114                 if ($line =~ /\.(gif|png)$/i) {
115                     if($i==$image_per_line){
116                         $i=0;
117                         push @imagelist,{KohaImage => '',KohaImageSrc => ''};
118                     }
119                     else{
120                 push( @imagelist,
121                     {
122                         KohaImage => $line,
123                         KohaImageSrc => $imagedir_web.'/'.$line,
124                         checked => $line eq $data->{imageurl} ? 1 : 0,
125                     }
126                 );
127             }
128                 }
129         }
130         closedir DIR;
131
132     my $remote_image = undef;
133     if (defined $data->{imageurl} and $data->{imageurl} =~ m/^http/) {
134         $remote_image = $data->{imageurl};
135     }
136
137         $template->param(
138         itemtype => $itemtype,
139         description => $data->{'description'},
140         renewalsallowed => $data->{'renewalsallowed'},
141         rentalcharge => sprintf("%.2f",$data->{'rentalcharge'}),
142         notforloan => $data->{'notforloan'},
143         imageurl => $data->{'imageurl'},
144         template => C4::Context->preference('template'),
145         summary => $data->{summary},
146         IMAGESLOOP => \@imagelist,
147         remote_image => $remote_image,
148     );
149                                                                                                         # END $OP eq ADD_FORM
150 ################## ADD_VALIDATE ##################################
151 # called by add_form, used to insert/modify data in DB
152 } elsif ($op eq 'add_validate') {
153         my $dbh = C4::Context->dbh;
154     
155     my $modif='';
156     my $query = "
157         SELECT itemtype
158         FROM   itemtypes
159         WHERE  itemtype = ?
160     ";
161     my $sth = $dbh->prepare($query);
162     $sth->execute($itemtype);
163     if($sth->fetchrow){
164         $modif = 1;
165     }
166     
167     if($modif){  # it 's a modification
168         my $query = '
169             UPDATE itemtypes
170             SET    description = ?
171                  , renewalsallowed = ?
172                  , rentalcharge = ?
173                  , notforloan = ?
174                  , imageurl = ?
175                  , summary = ?
176             WHERE itemtype = ?
177         ';
178         my $sth=$dbh->prepare($query);
179                $sth->execute(
180                 $input->param('description'),
181                         $input->param('renewalsallowed'),
182                 $input->param('rentalcharge'),
183                         ($input->param('notforloan') ? 1 : 0),
184                 ($input->param('image') eq 'removeImage'
185                         ?''
186                         :($input->param('image') eq 'remoteImage' 
187                             ? $input->param('remoteImage')
188                             :$input->param('image')."")
189                 ),
190                 $input->param('summary'),
191                         $input->param('itemtype')
192             );
193     }
194     else { # add a new itemtype & not modif an old
195         my $query = "
196             INSERT INTO itemtypes
197                 (itemtype,description,renewalsallowed,rentalcharge, notforloan, imageurl,summary)
198             VALUES
199                 (?,?,?,?,?,?,?);
200             ";
201         my $sth=$dbh->prepare($query);
202             $sth->execute(
203                 $input->param('itemtype'),
204                 $input->param('description'),
205                 $input->param('renewalsallowed'),
206                 $input->param('rentalcharge'),
207                 $input->param('notforloan') ? 1 : 0,
208                 $input->param('image') eq 'removeImage' ? '' : $input->param('image') eq 'remoteImage' ? $input->param('remoteImage') : $input->param('image'),
209                 $input->param('summary'),
210             );
211     }
212     
213         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=itemtypes.pl\"></html>";
214         exit;
215                                                                                 # END $OP eq ADD_VALIDATE
216 ################## DELETE_CONFIRM ##################################
217 # called by default form, used to confirm deletion of data in DB
218 } elsif ($op eq 'delete_confirm') {
219         #start the page and read in includes
220         my $dbh = C4::Context->dbh;
221
222         # Check both categoryitem and biblioitems, see Bug 199
223         my $total = 0;
224         for my $table ('biblioitems') {
225            my $sth=$dbh->prepare("select count(*) as total from $table where itemtype=?");
226            $sth->execute($itemtype);
227            $total += $sth->fetchrow_hashref->{total};
228            $sth->finish;
229         }
230
231         my $sth=$dbh->prepare("select itemtype,description,renewalsallowed,rentalcharge from itemtypes where itemtype=?");
232         $sth->execute($itemtype);
233         my $data=$sth->fetchrow_hashref;
234         $sth->finish;
235
236         $template->param(itemtype => $itemtype,
237                                                         description => $data->{description},
238                                                         renewalsallowed => $data->{renewalsallowed},
239                                                         rentalcharge => sprintf("%.2f",$data->{rentalcharge}),
240                                                         imageurl => $data->{imageurl},
241                                                         total => $total);
242                                                                                                         # END $OP eq DELETE_CONFIRM
243 ################## DELETE_CONFIRMED ##################################
244 # called by delete_confirm, used to effectively confirm deletion of data in DB
245 } elsif ($op eq 'delete_confirmed') {
246         #start the page and read in includes
247         my $dbh = C4::Context->dbh;
248         my $itemtype=uc($input->param('itemtype'));
249         my $sth=$dbh->prepare("delete from itemtypes where itemtype=?");
250         $sth->execute($itemtype);
251         $sth = $dbh->prepare("delete from issuingrules where itemtype=?");
252         $sth->execute($itemtype);
253         $sth->finish;
254         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=itemtypes.pl\"></html>";
255         exit;
256                                                                                                         # END $OP eq DELETE_CONFIRMED
257 ################## DEFAULT ##################################
258 } else { # DEFAULT
259     my ($count,$results)=StringSearch($searchfield,'web');
260
261     my $page = $input->param('page') || 1;
262     my $first = ($page - 1) * $pagesize;
263
264     # if we are on the last page, the number of the last word to display
265     # must not exceed the length of the results array
266     my $last = min(
267         $first + $pagesize - 1,
268         scalar @{$results} - 1,
269     );
270
271     my $toggle = 0;
272     my @loop;
273     foreach my $result (@{$results}[$first .. $last]) {
274         my $itemtype = $result;
275         $itemtype->{toggle} = ($toggle++%2 eq 0 ? 1 : 0);
276         $itemtype->{imageurl} =
277             getitemtypeimagesrcfromurl($itemtype->{imageurl});
278         $itemtype->{rentalcharge} = sprintf('%.2f', $itemtype->{rentalcharge});
279
280         push(@loop, $itemtype);
281     }
282
283     $template->param(
284         loop => \@loop,
285         pagination_bar => pagination_bar(
286             $script_name,
287             getnbpages(scalar @{$results}, $pagesize),
288             $page,
289             'page'
290         )
291     );
292 } #---- END $OP eq DEFAULT
293 $template->param(intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
294                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
295                 IntranetNav => C4::Context->preference("IntranetNav"),
296                 );
297 output_html_with_http_headers $input, $cookie, $template->output;
298
299 # Local Variables:
300 # tab-width: 4
301 # End: