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