Nomenclature cleanup for subscription adding page
[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 $sth = $dbh->prepare(
59         "SELECT * FROM itemtypes WHERE (description LIKE ?) ORDER BY itemtype"
60         );
61     $sth->execute("$data[0]%");
62     return $sth->fetchall_arrayref({});         # return ref-to-array of ref-to-hashes
63                                                                 # like [ fetchrow_hashref(), fetchrow_hashref() ... ]
64 }
65
66 my $input       = new CGI;
67 my $searchfield = $input->param('description');
68 my $script_name = "/cgi-bin/koha/admin/itemtypes.pl";
69 my $itemtype    = $input->param('itemtype');
70 my $pagesize    = 10;
71 my $op          = $input->param('op');
72 $searchfield =~ s/\,//g;
73 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
74     {
75         template_name   => "admin/itemtypes.tmpl",
76         query           => $input,
77         type            => "intranet",
78         authnotrequired => 0,
79         flagsrequired   => { parameters => 1 },
80         debug           => 1,
81     }
82 );
83
84 $template->param(script_name => $script_name);
85 if ($op) {
86         $template->param($op  => 1); # we show only the TMPL_VAR names $op
87 } else {
88     $template->param(else => 1);
89 }
90
91 my $dbh = C4::Context->dbh;
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     #---- if primkey exists, it's a modify action, so read values to modify...
97     my $data;
98     if ($itemtype) {
99         my $sth = $dbh->prepare("select * from itemtypes where itemtype=?");
100         $sth->execute($itemtype);
101         $data = $sth->fetchrow_hashref;
102     }
103
104     # build list of images
105     my $src = "intranet"; # so that the getitemtypeimage functions know where they were called from -fbcit
106     my $imagedir_filesystem = getitemtypeimagedir($src);
107     my $imagedir_web        = getitemtypeimagesrc($src);
108     opendir( DIR, $imagedir_filesystem )
109       or warn "cannot 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(
122                     @imagelist,
123                     {
124                         KohaImage    => $line,
125                         KohaImageSrc => $imagedir_web . '/' . $line,
126                         checked      => $line eq $data->{imageurl} ? 1 : 0,
127                     }
128                 );
129             }
130         }
131     }
132     closedir DIR;
133
134     my $remote_image = undef;
135     if ( defined $data->{imageurl} and $data->{imageurl} =~ /^http/i ) {
136         $remote_image = $data->{imageurl};
137     }
138
139     $template->param(
140         itemtype        => $itemtype,
141         description     => $data->{'description'},
142         renewalsallowed => $data->{'renewalsallowed'},
143         rentalcharge    => sprintf( "%.2f", $data->{'rentalcharge'} ),
144         notforloan      => $data->{'notforloan'},
145         imageurl        => $data->{'imageurl'},
146         template        => C4::Context->preference('template'),
147         summary         => $data->{summary},
148         IMAGESLOOP      => \@imagelist,
149         remote_image    => $remote_image,
150     );
151
152     # END $OP eq ADD_FORM
153 ################## ADD_VALIDATE ##################################
154     # called by add_form, used to insert/modify data in DB
155 }
156 elsif ( $op eq 'add_validate' ) {
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 ) {             # it's a modification
165         my $query2 = '
166             UPDATE itemtypes
167             SET    description = ?
168                  , renewalsallowed = ?
169                  , rentalcharge = ?
170                  , notforloan = ?
171                  , imageurl = ?
172                  , summary = ?
173             WHERE itemtype = ?
174         ';
175         $sth = $dbh->prepare($query2);
176         $sth->execute(
177             $input->param('description'),
178             $input->param('renewalsallowed'),
179             $input->param('rentalcharge'),
180             ( $input->param('notforloan') ? 1 : 0 ),
181             (
182                 $input->param('image') eq 'removeImage' ? '' : (
183                       $input->param('image') eq 'remoteImage'
184                     ? $input->param('remoteImage')
185                     : $input->param('image') . ""
186                 )
187             ),
188             $input->param('summary'),
189             $input->param('itemtype')
190         );
191     }
192     else {    # add a new itemtype & not modif an old
193         my $query = "
194             INSERT INTO itemtypes
195                 (itemtype,description,renewalsallowed,rentalcharge, notforloan, imageurl,summary)
196             VALUES
197                 (?,?,?,?,?,?,?);
198             ";
199         my $sth = $dbh->prepare($query);
200                 my $image = $input->param('image');
201         $sth->execute(
202             $input->param('itemtype'),
203             $input->param('description'),
204             $input->param('renewalsallowed'),
205             $input->param('rentalcharge'),
206             $input->param('notforloan') ? 1 : 0,
207             $image eq 'removeImage' ?           ''                 :
208             $image eq 'remoteImage' ? $input->param('remoteImage') :
209             $image,
210             $input->param('summary'),
211         );
212     }
213
214     print $input->redirect('itemtypes.pl');
215     exit;
216
217     # END $OP eq ADD_VALIDATE
218 ################## DELETE_CONFIRM ##################################
219     # called by default form, used to confirm deletion of data in DB
220 }
221 elsif ( $op eq 'delete_confirm' ) {
222     # Check both categoryitem and biblioitems, see Bug 199
223     my $total = 0;
224     for my $table ('biblioitems') {
225         my $sth =
226           $dbh->prepare(
227             "select count(*) as total from $table where itemtype=?");
228         $sth->execute($itemtype);
229         $total += $sth->fetchrow_hashref->{total};
230     }
231
232     my $sth =
233       $dbh->prepare(
234 "select itemtype,description,renewalsallowed,rentalcharge from itemtypes where itemtype=?"
235       );
236     $sth->execute($itemtype);
237     my $data = $sth->fetchrow_hashref;
238     $template->param(
239         itemtype        => $itemtype,
240         description     => $data->{description},
241         renewalsallowed => $data->{renewalsallowed},
242         rentalcharge    => sprintf( "%.2f", $data->{rentalcharge} ),
243         imageurl        => $data->{imageurl},
244         total           => $total
245     );
246
247     # END $OP eq DELETE_CONFIRM
248 ################## DELETE_CONFIRMED ##################################
249   # called by delete_confirm, used to effectively confirm deletion of data in DB
250 }
251 elsif ( $op eq 'delete_confirmed' ) {
252     my $itemtype = uc( $input->param('itemtype') );
253     my $sth      = $dbh->prepare("delete from itemtypes where itemtype=?");
254     $sth->execute($itemtype);
255     $sth = $dbh->prepare("delete from issuingrules where itemtype=?");
256     $sth->execute($itemtype);
257     print $input->redirect('itemtypes.pl');
258     exit;
259     # END $OP eq DELETE_CONFIRMED
260 ################## DEFAULT ##################################
261 }
262 else {    # DEFAULT
263     my ($results) = StringSearch( $searchfield, 'web' );
264     my $page = $input->param('page') || 1;
265     my $first = ( $page - 1 ) * $pagesize;
266
267     # if we are on the last page, the number of the last word to display
268     # must not exceed the length of the results array
269     my $last = min( $first + $pagesize - 1, scalar @{$results} - 1, );
270     my $toggle = 0;
271     my @loop;
272     foreach my $itemtype ( @{$results}[ $first .. $last ] ) {
273         $itemtype->{toggle} = ($toggle++ % 2) ? 0 : 1 ;
274         $itemtype->{imageurl} = getitemtypeimagesrc('intranet') . "/$itemtype->{imageurl}";
275         $itemtype->{rentalcharge} = sprintf( '%.2f', $itemtype->{rentalcharge} );
276         push( @loop, $itemtype );
277     }
278
279     $template->param(
280         loop           => \@loop,
281         pagination_bar => pagination_bar(
282             $script_name, getnbpages( scalar @{$results}, $pagesize ),
283             $page,        'page'
284         )
285     );
286 }    #---- END $OP eq DEFAULT
287
288 output_html_with_http_headers $input, $cookie, $template->output;