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