address standardization UI, part 1
[freeside.git] / httemplate / misc / address_standardize.cgi
1 <% encode_json($return) %>\
2 <%init>
3
4 local $SIG{__DIE__}; #disable Mason error trap
5
6 my $DEBUG = 0;
7
8 my $conf = new FS::Conf;
9
10 # figure out the prefix
11 my $pre;
12 foreach my $name ($cgi->param) {
13   if ($name =~ /^(\w*)address1$/) {
14     $pre = $1;
15     last;
16   }
17 }
18 die "no address1 field in location" if !defined($pre);
19
20 # gather relevant fields
21 my %old = ( map { $_ => scalar($cgi->param($pre . $_)) }
22   qw( company address1 address2 city state zip country )
23 );
24
25 my $cache = eval { FS::GeocodeCache->standardize(\%old) };
26 $cache->set_coord;
27 # don't do set_censustract here, though censustract may be set by now
28
29 # give the fields their prefixed names back
30 # except always name the error string 'error'
31 my $error = delete($cache->{'error'}) || '';
32 my %new = (
33   'changed' => 0,
34   'error' => $error,
35   map { $pre.$_, $cache->get($_) } keys %$cache
36 );
37
38 foreach ( qw(address1 address2 city state zip country) ) {
39   if ( $new{$pre.$_} ne $old{$pre.$_} ) {
40     $new{changed} = 1;
41     last;
42   }
43 }
44
45 # refold this to make it acceptable to jquery
46 #my $return = [ map { { name => $_, value => $new{$_} } } keys %new ];
47 my $return = \%new;
48 warn "result:\n".encode_json($return) if $DEBUG;
49
50 $r->content_type('application/json');
51 </%init>