new feature : image for itemtypes.
[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 C4::Context;
44 use C4::Output;
45 use C4::Search;
46 use C4::Auth;
47 use C4::Interface::CGI::Output;
48 use HTML::Template;
49
50 sub StringSearch  {
51         my ($env,$searchstring,$type)=@_;
52         my $dbh = C4::Context->dbh;
53         $searchstring=~ s/\'/\\\'/g;
54         my @data=split(' ',$searchstring);
55         my $count=@data;
56         my $sth=$dbh->prepare("Select * from itemtypes where (description like ?) order by itemtype");
57         $sth->execute("$data[0]%");
58         my @results;
59         while (my $data=$sth->fetchrow_hashref){
60         push(@results,$data);
61         }
62         #  $sth->execute;
63         $sth->finish;
64         return (scalar(@results),\@results);
65 }
66
67 my $input = new CGI;
68 my $searchfield=$input->param('description');
69 my $offset=$input->param('offset');
70 my $script_name="/cgi-bin/koha/admin/itemtypes.pl";
71 my $itemtype=$input->param('itemtype');
72 my $pagesize=20;
73 my $op = $input->param('op');
74 $searchfield=~ s/\,//g;
75 my ($template, $borrowernumber, $cookie)
76     = get_template_and_user({template_name => "parameters/itemtypes.tmpl",
77                              query => $input,
78                              type => "intranet",
79                              authnotrequired => 0,
80                              flagsrequired => {parameters => 1, management => 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 = C4::Context->opachtdocs."/".C4::Context->preference('opacthemes');
106         warn "img : $imagedir";
107         unless (opendir(DIR, "$imagedir/itemtypeimg/")) {
108 #               my $cgidir = C4::Context->intranetdir;
109                 opendir(DIR, "$imagedir/value_builder") || die "can't opendir $imagedir/value_builder: $!";
110         } 
111         my @imagelist;
112         while (my $line = readdir(DIR)) {
113                 if ($line =~ /\.gif$/) {
114                         my %x;
115                         $x{KohaImage} = "$line";
116                         push @imagelist, \%x;
117                 }
118         }
119         closedir DIR;
120 #       my $CGIitemtypes = CGI::scrolling_list(-name=>'itemtypes',
121 #                                       -id=>"itemtypes",
122 #                                       -values=> \@imagelist,
123 #                                       -size=>1,
124 #                                       -multiple=>0,
125 #                                       );
126 #                                       
127         $template->param(itemtype => $itemtype,
128                                                         description => $data->{'description'},
129                                                         renewalsallowed => $data->{'renewalsallowed'},
130                                                         rentalcharge => sprintf("%.2f",$data->{'rentalcharge'}),
131                                                         notforloan => $data->{'notforloan'},
132                                                         imageurl => $data->{'imageurl'},
133                                                         opacthemes => C4::Context->preference('opacthemes'),
134                                                         IMAGESLOOP => \@imagelist,
135                                                         );
136 ;
137                                                                                                         # END $OP eq ADD_FORM
138 ################## ADD_VALIDATE ##################################
139 # called by add_form, used to insert/modify data in DB
140 } elsif ($op eq 'add_validate') {
141         my $dbh = C4::Context->dbh;
142         my $sth=$dbh->prepare("replace itemtypes (itemtype,description,renewalsallowed,rentalcharge,notforloan,imageurl) values (?,?,?,?,?,?)");
143         $sth->execute(
144                 $input->param('itemtype'),$input->param('description'),
145                 $input->param('renewalsallowed'),$input->param('rentalcharge'),
146                 $input->param('notforloan')?1:0,
147                 $input->param('imageurl'));
148         $sth->finish;
149         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=itemtypes.pl\"></html>";
150         exit;
151                                                                                                         # END $OP eq ADD_VALIDATE
152 ################## DELETE_CONFIRM ##################################
153 # called by default form, used to confirm deletion of data in DB
154 } elsif ($op eq 'delete_confirm') {
155         #start the page and read in includes
156         my $dbh = C4::Context->dbh;
157
158         # Check both categoryitem and biblioitems, see Bug 199
159         my $total = 0;
160         for my $table ('biblioitems') {
161            my $sth=$dbh->prepare("select count(*) as total from $table where itemtype=?");
162            $sth->execute($itemtype);
163            $total += $sth->fetchrow_hashref->{total};
164            $sth->finish;
165         }
166
167         my $sth=$dbh->prepare("select itemtype,description,renewalsallowed,rentalcharge from itemtypes where itemtype=?");
168         $sth->execute($itemtype);
169         my $data=$sth->fetchrow_hashref;
170         $sth->finish;
171
172         $template->param(itemtype => $itemtype,
173                                                         description => $data->{description},
174                                                         renewalsallowed => $data->{renewalsallowed},
175                                                         rentalcharge => sprintf("%.2f",$data->{rentalcharge}),
176                                                         imageurl => $data->{imageurl},
177                                                         total => $total);
178                                                                                                         # END $OP eq DELETE_CONFIRM
179 ################## DELETE_CONFIRMED ##################################
180 # called by delete_confirm, used to effectively confirm deletion of data in DB
181 } elsif ($op eq 'delete_confirmed') {
182         #start the page and read in includes
183         my $dbh = C4::Context->dbh;
184         my $itemtype=uc($input->param('itemtype'));
185         my $sth=$dbh->prepare("delete from itemtypes where itemtype=?");
186         $sth->execute($itemtype);
187         $sth = $dbh->prepare("delete from issuingrules where itemtype=?");
188         $sth->execute($itemtype);
189         $sth->finish;
190         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=itemtypes.pl\"></html>";
191         exit;
192                                                                                                         # END $OP eq DELETE_CONFIRMED
193 ################## DEFAULT ##################################
194 } else { # DEFAULT
195         my $env;
196         my ($count,$results)=StringSearch($env,$searchfield,'web');
197         my $toggle=0;
198         my @loop_data;
199         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
200                 my %row_data;
201                 if ($toggle eq 0){
202                         $toggle=1;
203                 } else {
204                         $toggle=0;
205                 }
206                 $row_data{toggle} = $toggle;
207                 $row_data{itemtype} = $results->[$i]{itemtype};
208                 $row_data{description} = $results->[$i]{description};
209                 $row_data{renewalsallowed} = $results->[$i]{renewalsallowed};
210                 $row_data{notforloan} = $results->[$i]{notforloan};
211                 if ($results->[$i]{imageurl} =~ /^http/) {
212                         $row_data{absoluteurl} = 1;
213                 }
214                 $row_data{imageurl} = $results->[$i]{imageurl};
215                 $row_data{rentalcharge} = sprintf("%.2f",$results->[$i]{rentalcharge});
216                 push(@loop_data, \%row_data);
217         }
218         $template->param(loop => \@loop_data,
219                                         opacthemes => C4::Context->preference('opacthemes')
220                                         );
221         if ($offset>0) {
222                 my $prevpage = $offset-$pagesize;
223                 $template->param(previous => "$script_name?offset=".$prevpage);
224         }
225         if ($offset+$pagesize<$count) {
226                 my $nextpage =$offset+$pagesize;
227                 $template->param(next => "$script_name?offset=".$nextpage);
228         }
229 } #---- END $OP eq DEFAULT
230 output_html_with_http_headers $input, $cookie, $template->output;
231
232 # Local Variables:
233 # tab-width: 4
234 # End: