]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Zips.pm
57f468b9595ac2ce09a273a08d13ee62c7ca0719
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Search / Zips.pm
1 package OpenILS::Application::Search::Zips;
2 use base qw/OpenILS::Application/;
3 use strict; use warnings;
4
5 use OpenSRF::EX qw(:try);
6 use OpenSRF::Utils::Logger qw/$logger/;
7 use OpenILS::Application::AppUtils;
8 use OpenILS::Utils::Editor;
9 use OpenSRF::Utils::SettingsClient;
10
11 use open ':utf8';
12
13 my %zips;
14
15 # -----------------------------------------------------------------
16 # Reads zip code information from a file.  File format is : 
17 # ID|StateAbb|City|Zip|IsDefault|StateID|County|AreaCode
18 # Currently, StateAbb, City, Zip, County, AreaCode are used.  
19 # IsDefault should be set to 1
20 # -----------------------------------------------------------------
21
22 sub initialize {
23     my $conf = OpenSRF::Utils::SettingsClient->new;
24     my $zfile = $conf->config_value(
25         "apps", "open-ils.search", "app_settings", "zips_file");
26     return 1 unless $zfile and -f $zfile;
27
28     $logger->info("search loaded zips file $zfile");
29     open(F,$zfile);
30     my @data = <F>;
31     close(F);
32
33     for(@data) {
34         chomp $_;
35         my @items = split(/\|/, "$_");
36         my $items = {
37             state       => $items[1],
38             city        => $items[2],
39             zip     => $items[3],
40             stateid => $items[5],
41             county  => $items[6],
42             areacode    => $items[7],
43             alert   => $items[8]
44         };
45
46         next unless $items[4] eq '1';
47         $zips{$$items{zip}} = $items;
48     }
49 }
50
51 __PACKAGE__->register_method(
52     method => 'search_zip',
53     api_name    => 'open-ils.search.zip',
54     signature   => q/
55         Given a zip code, returns address info for the zip code
56         @param auth the login session key
57         @param zip The zip code to check
58         @return On success, returns an object of the form:
59         { state=>, city=>, zip=>, stateid=>, county=>, areacode=>}
60         returns event on error
61     /
62 );
63 sub search_zip {
64     #my( $self, $conn, $auth, $zip ) = @_;
65     #my $e = OpenILS::Utils::Editor->new(authtoken=>$auth);
66     #return $e->event unless $e->checkauth;
67     #return $e->event unless $e->allowed('VIEW_ZIP_DATA');
68     my( $self, $conn, $zip ) = @_;
69     $zip =~ s/(^\d{5}).*/$1/; # we don't care about the last 4 digits if they exist 
70     return $zips{$zip};
71 }
72
73 1;