subs renamed according to coding guidelines.
[koha.git] / admin / itemtypesubcategory.pl
1 #!/usr/bin/perl
2 # NOTE: 4-character tabs
3
4 #script to administer the itemtype subcategories table
5 #modified from the itemtype script written 20/02/2002 by paul.poulain@free.fr
6 #This script written by waylon@robertson.net.nz at 2nd June, 2005
7 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
8
9 # ALGO :
10 # this script use an $op to know what to do.
11 # if $op is empty or none of the above values,
12 #       - the default screen is build (with all records, or filtered datas).
13 #       - the   user can clic on add, modify or delete record.
14 # if $op=add_form
15 #       - if primkey exists, this is a modification,so we read the $primkey record
16 #       - builds the add/modify form
17 # if $op=add_validate
18 #       - the user has just send datas, so we create/modify the record
19 # if $op=delete_form
20 #       - we show the record having primkey=$primkey and ask for deletion validation form
21 # if $op=delete_confirm
22 #       - we delete the record having primkey=$primkey
23
24
25 # Copyright 2000-2002 Katipo Communications
26 #
27 # This file is part of Koha.
28 #
29 # Koha is free software; you can redistribute it and/or modify it under the
30 # terms of the GNU General Public License as published by the Free Software
31 # Foundation; either version 2 of the License, or (at your option) any later
32 # version.
33 #
34 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
35 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
36 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
37 #
38 # You should have received a copy of the GNU General Public License along with
39 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
40 # Suite 330, Boston, MA  02111-1307 USA
41
42 use strict;
43 use CGI;
44 use C4::Context;
45 use C4::Output;
46 use C4::Search;
47 use C4::Auth;
48 use C4::Interface::CGI::Output;
49 use HTML::Template;
50
51 sub StringSearch  {
52         my ($env,$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 subcategorytable where (description like ?) order by subcategorycode");
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 $offset=$input->param('offset');
71 my $script_name="/cgi-bin/koha/admin/itemtypesubcategory.pl";
72 my $subcategorycode=$input->param('subcategorycode');
73 my $pagesize=20;
74 my $op = $input->param('op');
75 $searchfield=~ s/\,//g;
76 my ($template, $borrowernumber, $cookie)
77     = get_template_and_user({template_name => "admin/itemtypesubcategory.tmpl",
78                              query => $input,
79                              type => "intranet",
80                              authnotrequired => 0,
81                              flagsrequired => {parameters => 1, management => 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     my $itemtypes;
99     my $dbh = C4::Context->dbh;
100     my @itemtypesselected;
101         if ($subcategorycode) {
102         my $sth=$dbh->prepare("select subcategorycode,description,itemtypecodes from subcategorytable where subcategorycode=?");
103                 $sth->execute($subcategorycode);
104                 $data=$sth->fetchrow_hashref;
105                 $sth->finish;
106         @itemtypesselected = split ( /\|/, $data->{'itemtypecodes'} );
107         }
108
109     my $sth=$dbh->prepare("select description,itemtype from itemtypes order by description");
110     $sth->execute;
111     while (my ($description,$itemtype) = $sth->fetchrow) {
112         $itemtypes .='<td><input type="checkbox" name="itemtypecodes" value="'.$itemtype.'"';
113         if(grep /$itemtype/,@itemtypesselected){
114             $itemtypes .=' checked';
115         }
116         $itemtypes .='>'.$description.'</td>';
117     }
118
119         $template->param(subcategorycode => $subcategorycode,
120                                                         description => $data->{'description'},
121                             itemtypes => $itemtypes
122                                                         );
123 ;
124                                                                                                         # END $OP eq ADD_FORM
125 ################## ADD_VALIDATE ##################################
126 # called by add_form, used to insert/modify data in DB
127 } elsif ($op eq 'add_validate') {
128         my $dbh = C4::Context->dbh;
129     my @itemtypecodesarray = $input->param('itemtypecodes');
130     my $itemtypecodes=join('|',@itemtypecodesarray);
131         my $sth=$dbh->prepare("replace subcategorytable (subcategorycode,description,itemtypecodes) values (?,?,?)");
132         $sth->execute(
133                 $input->param('subcategorycode'),$input->param('description'),
134                 $itemtypecodes
135         );
136         $sth->finish;
137         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=itemtypesubcategory.pl\"></html>";
138         exit;
139                                                                                                         # END $OP eq ADD_VALIDATE
140 ################## DELETE_CONFIRM ##################################
141 # called by default form, used to confirm deletion of data in DB
142 } elsif ($op eq 'delete_confirm') {
143         #start the page and read in includes
144         my $dbh = C4::Context->dbh;
145         my $sth=$dbh->prepare("select subcategorycode,description,itemtypecodes from subcategorytable where subcategorycode=?");
146         $sth->execute($subcategorycode);
147         my $data=$sth->fetchrow_hashref;
148         $sth->finish;
149
150         $template->param(subcategorycode => $subcategorycode,
151                                                         description => $data->{'description'},
152                                                         itemtypecodes => $data->{'itemtypecodes'});
153                                                                                                         # END $OP eq DELETE_CONFIRM
154 ################## DELETE_CONFIRMED ##################################
155 # called by delete_confirm, used to effectively confirm deletion of data in DB
156 } elsif ($op eq 'delete_confirmed') {
157         #start the page and read in includes
158         my $dbh = C4::Context->dbh;
159         my $subcategorycode=uc($input->param('subcategorycode'));
160         my $sth=$dbh->prepare("delete from subcategorytable where subcategorycode=?");
161         $sth->execute($subcategorycode);
162         $sth->finish;
163         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=itemtypesubcategory.pl\"></html>";
164         exit;
165                                                                                                         # END $OP eq DELETE_CONFIRMED
166 ################## DEFAULT ##################################
167 } else { # DEFAULT
168         my $env;
169         my ($count,$results)=StringSearch($env,$searchfield,'web');
170         my $toggle=0;
171         my @loop_data;
172         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
173                 my %row_data;
174                 if ($toggle eq 0){
175                         $toggle=1;
176                 } else {
177                         $toggle=0;
178                 }
179                 $row_data{toggle} = $toggle;
180                 $row_data{subcategorycode} = $results->[$i]{subcategorycode};
181                 $row_data{description} = $results->[$i]{description};
182                 $row_data{itemtypecodes} = $results->[$i]{itemtypecodes};
183                 push(@loop_data, \%row_data);
184         }
185         $template->param(loop => \@loop_data);
186         if ($offset>0) {
187                 my $prevpage = $offset-$pagesize;
188                 $template->param(previous => "$script_name?offset=".$prevpage);
189         }
190         if ($offset+$pagesize<$count) {
191                 my $nextpage =$offset+$pagesize;
192                 $template->param(next => "$script_name?offset=".$nextpage);
193         }
194 } #---- END $OP eq DEFAULT
195 output_html_with_http_headers $input, $cookie, $template->output;
196
197 # Local Variables:
198 # tab-width: 4
199 # End: