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