fix for bug 1369: sql-datas dir, pick a naming convention, remove the rest
[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::Auth;
46 use C4::Output;
47
48
49 sub StringSearch  {
50         my ($searchstring,$type)=@_;
51         my $dbh = C4::Context->dbh;
52         $searchstring=~ s/\'/\\\'/g;
53         my @data=split(' ',$searchstring);
54         my $count=@data;
55         my $sth=$dbh->prepare("Select * from subcategorytable where (description like ?) order by subcategorycode");
56         $sth->execute("$data[0]%");
57         my @results;
58         while (my $data=$sth->fetchrow_hashref){
59         push(@results,$data);
60         }
61         #  $sth->execute;
62         $sth->finish;
63         return (scalar(@results),\@results);
64 }
65
66 my $input = new CGI;
67 my $searchfield=$input->param('description');
68 my $offset=$input->param('offset');
69 my $script_name="/cgi-bin/koha/admin/itemtypesubcategory.pl";
70 my $subcategorycode=$input->param('subcategorycode');
71 my $pagesize=20;
72 my $op = $input->param('op');
73 $searchfield=~ s/\,//g;
74 my ($template, $borrowernumber, $cookie)
75     = get_template_and_user({template_name => "admin/itemtypesubcategory.tmpl",
76                              query => $input,
77                              type => "intranet",
78                              authnotrequired => 0,
79                              flagsrequired => {parameters => 1},
80                              debug => 1,
81                              });
82
83 if ($op) {
84 $template->param(script_name => $script_name,
85                                                 $op              => 1); # we show only the TMPL_VAR names $op
86 } else {
87 $template->param(script_name => $script_name,
88                                                 else              => 1); # we show only the TMPL_VAR names $op
89 }
90 ################## ADD_FORM ##################################
91 # called by default. Used to create form to add or  modify a record
92 if ($op eq 'add_form') {
93         #start the page and read in includes
94         #---- if primkey exists, it's a modify action, so read values to modify...
95         my $data;
96     my $itemtypes;
97     my $dbh = C4::Context->dbh;
98     my @itemtypesselected;
99         if ($subcategorycode) {
100         my $sth=$dbh->prepare("select subcategorycode,description,itemtypecodes from subcategorytable where subcategorycode=?");
101                 $sth->execute($subcategorycode);
102                 $data=$sth->fetchrow_hashref;
103                 $sth->finish;
104         @itemtypesselected = split ( /\|/, $data->{'itemtypecodes'} );
105         }
106
107     my $sth=$dbh->prepare("select description,itemtype from itemtypes order by description");
108     $sth->execute;
109     while (my ($description,$itemtype) = $sth->fetchrow) {
110         $itemtypes .='<td><input type="checkbox" name="itemtypecodes" value="'.$itemtype.'"';
111         if(grep /$itemtype/,@itemtypesselected){
112             $itemtypes .=' checked';
113         }
114         $itemtypes .='>'.$description.'</td>';
115     }
116
117         $template->param(subcategorycode => $subcategorycode,
118                                                         description => $data->{'description'},
119                             itemtypes => $itemtypes
120                                                         );
121 ;
122                                                                                                         # END $OP eq ADD_FORM
123 ################## ADD_VALIDATE ##################################
124 # called by add_form, used to insert/modify data in DB
125 } elsif ($op eq 'add_validate') {
126         my $dbh = C4::Context->dbh;
127     my @itemtypecodesarray = $input->param('itemtypecodes');
128     my $itemtypecodes=join('|',@itemtypecodesarray);
129         my $sth=$dbh->prepare("replace subcategorytable (subcategorycode,description,itemtypecodes) values (?,?,?)");
130         $sth->execute(
131                 $input->param('subcategorycode'),$input->param('description'),
132                 $itemtypecodes
133         );
134         $sth->finish;
135         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=itemtypesubcategory.pl\"></html>";
136         exit;
137                                                                                                         # END $OP eq ADD_VALIDATE
138 ################## DELETE_CONFIRM ##################################
139 # called by default form, used to confirm deletion of data in DB
140 } elsif ($op eq 'delete_confirm') {
141         #start the page and read in includes
142         my $dbh = C4::Context->dbh;
143         my $sth=$dbh->prepare("select subcategorycode,description,itemtypecodes from subcategorytable where subcategorycode=?");
144         $sth->execute($subcategorycode);
145         my $data=$sth->fetchrow_hashref;
146         $sth->finish;
147
148         $template->param(subcategorycode => $subcategorycode,
149                                                         description => $data->{'description'},
150                                                         itemtypecodes => $data->{'itemtypecodes'});
151                                                                                                         # END $OP eq DELETE_CONFIRM
152 ################## DELETE_CONFIRMED ##################################
153 # called by delete_confirm, used to effectively confirm deletion of data in DB
154 } elsif ($op eq 'delete_confirmed') {
155         #start the page and read in includes
156         my $dbh = C4::Context->dbh;
157         my $subcategorycode=uc($input->param('subcategorycode'));
158         my $sth=$dbh->prepare("delete from subcategorytable where subcategorycode=?");
159         $sth->execute($subcategorycode);
160         $sth->finish;
161         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=itemtypesubcategory.pl\"></html>";
162         exit;
163                                                                                                         # END $OP eq DELETE_CONFIRMED
164 ################## DEFAULT ##################################
165 } else { # DEFAULT
166         my ($count,$results)=StringSearch($searchfield,'web');
167         my $toggle=0;
168         my @loop_data;
169         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
170                 my %row_data;
171                 if ($toggle eq 0){
172                         $toggle=1;
173                 } else {
174                         $toggle=0;
175                 }
176                 $row_data{toggle} = $toggle;
177                 $row_data{subcategorycode} = $results->[$i]{subcategorycode};
178                 $row_data{description} = $results->[$i]{description};
179                 $row_data{itemtypecodes} = $results->[$i]{itemtypecodes};
180                 push(@loop_data, \%row_data);
181         }
182         $template->param(loop => \@loop_data);
183         if ($offset>0) {
184                 my $prevpage = $offset-$pagesize;
185                 $template->param(previous => "$script_name?offset=".$prevpage);
186         }
187         if ($offset+$pagesize<$count) {
188                 my $nextpage =$offset+$pagesize;
189                 $template->param(next => "$script_name?offset=".$nextpage);
190         }
191 } #---- END $OP eq DEFAULT
192 output_html_with_http_headers $input, $cookie, $template->output;
193
194 # Local Variables:
195 # tab-width: 4
196 # End: