POD added, Call to Bookfund.pm added, SQL code movde in Bookfund.pm.
[koha.git] / admin / roadtype.pl
1 #! /usr/bin/perl
2
3 # Copyright 2006 SAN OUEST-PROVENCE et Paul POULAIN
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 use strict;
21 use CGI;
22 use C4::Context;
23 use C4::Output;
24 use C4::Search;
25 use HTML::Template;
26 use C4::Auth;
27 use C4::Interface::CGI::Output;
28
29 sub StringSearch  {
30         my ($env,$searchstring,$type)=@_;
31         my $dbh = C4::Context->dbh;
32         $searchstring=~ s/\'/\\\'/g;
33         my @data=split(' ',$searchstring);
34         my $count=@data;
35         my $sth=$dbh->prepare("Select * from roadtype where (road_type like ?)");
36         $sth->execute("$data[0]%");
37         my @results;
38         while (my $data=$sth->fetchrow_hashref){
39         push(@results,$data);
40         }
41         #  $sth->execute;
42         $sth->finish;
43         return (scalar(@results),\@results);
44 }
45
46 my $input = new CGI;
47 my $searchfield=$input->param('road_type');
48 my $script_name="/cgi-bin/koha/admin/roadtype.pl";
49 my $roadtypeid=$input->param('roadtypeid');
50 my $op = $input->param('op');
51
52 my ($template, $loggedinuser, $cookie)
53     = get_template_and_user({template_name => "admin/roadtype.tmpl",
54                              query => $input,
55                              type => "intranet",
56                              authnotrequired => 0,
57                              flagsrequired => {parameters => 1, management => 1},
58                              debug => 1,
59                              });
60
61
62 $template->param(       script_name => $script_name,
63                         roadtypeid => $roadtypeid ,
64                         searchfield => $searchfield);
65
66
67 ################## ADD_FORM ##################################
68 # called by default. Used to create form to add or  modify a record
69 if ($op eq 'add_form') {
70         $template->param(add_form => 1);
71         
72         #---- if primkey exists, it's a modify action, so read values to modify...
73         my $data;
74         if ($roadtypeid) {
75                 my $dbh = C4::Context->dbh;
76                 my $sth=$dbh->prepare("select roadtypeid,road_type from roadtype where roadtypeid=?");
77                 $sth->execute($roadtypeid);
78                 $data=$sth->fetchrow_hashref;
79                 $sth->finish;
80         }
81
82         $template->param(       
83                                 road_type       => $data->{'road_type'},
84                         );
85 ##############ICI#####################
86 # END $OP eq ADD_FORM
87 ################## ADD_VALIDATE #################################
88 # called by add_form, used to insert/modify data in DB
89 } elsif ($op eq 'add_validate') {
90         my $dbh = C4::Context->dbh;
91         my $sth;
92         
93         if ($input->param('roadtypeid') ){
94                 $sth=$dbh->prepare("replace roadtype (roadtypeid,road_type) values (?,?) ");
95                 $sth->execute(map { $input->param($_) } ('roadtypeid','road_type'));
96         
97         }
98         else{   
99                 $sth=$dbh->prepare("replace roadtype (road_type) values (?)");
100                 $sth->execute(map { $input->param($_) } ('road_type'));
101         }
102         $sth->finish;
103         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=roadtype.pl\"></html>";
104         exit;
105
106 # END $OP eq ADD_VALIDATE
107 ################## DELETE_CONFIRM ##################################
108 # called by default form, used to confirm deletion of data in DB
109 } elsif ($op eq 'delete_confirm') {
110         $template->param(delete_confirm => 1);
111         my $dbh = C4::Context->dbh;
112         my $sth=$dbh->prepare("select count(*) as total from borrowers,roadtype where borrowers.streettype=roadtype.road_type and roadtypeid=?");
113         $sth->execute($roadtypeid);
114         my $total = $sth->fetchrow_hashref;
115         $sth->finish;
116         $template->param(total => $total->{'total'});   
117         my $sth2=$dbh->prepare("select roadtypeid,road_type from roadtype where  roadtypeid=?");
118         $sth2->execute($roadtypeid);
119         my $data=$sth2->fetchrow_hashref;
120         $sth2->finish;
121         if ($total->{'total'} >0) {
122                 $template->param(totalgtzero => 1);
123         }
124
125         $template->param(       
126                                 city_name       =>      ( $data->{'road_type'}),
127                                 );
128
129
130                                                                                                         # END $OP eq DELETE_CONFIRM
131 ################## DELETE_CONFIRMED ##################################
132 # called by delete_confirm, used to effectively confirm deletion of data in DB
133 } elsif ($op eq 'delete_confirmed') {
134         my $dbh = C4::Context->dbh;
135         my $categorycode=uc($input->param('roadtypeid'));
136         my $sth=$dbh->prepare("delete from roadtype where roadtypeid=?");
137         $sth->execute($roadtypeid);
138         $sth->finish;
139         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=roadtype.pl\"></html>";
140         exit;
141                                                                                                         # END $OP eq DELETE_CONFIRMED
142 } else { # DEFAULT
143         $template->param(else => 1);
144         my $env;
145         my @loop;
146         my ($count,$results)=StringSearch($env,$searchfield,'web');
147         my $toggle = 0;
148         for (my $i=0; $i < $count; $i++){
149                 my %row = (roadtypeid => $results->[$i]{'roadtypeid'},
150                                 road_type => $results->[$i]{'road_type'},
151                                 toggle => $toggle );    
152                 push @loop, \%row;
153                 if ( $toggle eq 0 )
154                 {
155                         $toggle = 1;
156                 }
157                 else
158                 {
159                         $toggle = 0;
160                 }
161         }
162         $template->param(loop => \@loop);
163
164
165 } #---- END $OP eq DEFAULT
166 output_html_with_http_headers $input, $cookie, $template->output;