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