limit variable scoping
[google-map-tiles.git] / koha.pl
1 #!/usr/bin/perl
2 # Deliver click values to volcano.htm...
3 # Author. John D. Coryat 02/2010...
4 # Copyright 2010 USNaviguide LLC. All rights reserved...
5
6 use CGI qw/:standard *table/;
7 use strict ;
8 use DBI ;
9 use USNaviguide_Google_Tiles ;
10
11 my $dbh         = DBI->connect ( "dbi:Pg:dbname=koha" , "dpavlin" , "" , { AutoCommit => 1 } ) ;
12 my $point       = param('POINT') ;
13 my $zoom        = param('ZOOM') ;
14
15 my $maxpix      = 15 ;                                  # Maximum pixels between click and point
16
17 print qq{Content-type: text/xml\r\n\r\n};
18 print qq{<?xml version="1.0" encoding="UTF-8"?>\n} ;
19 print qq!<map>\n! ;
20
21 if ( !$point )
22 {
23  print qq!<info error="No point passed..."/>\n! ;
24  print qq!</map>\n! ;
25  $dbh->disconnect ;
26  exit ;
27 }
28
29 if ( $point =~ /(.*),(.*)/ )
30 {
31  my $lat        = $1 ;
32  my $lng        = $2 ;
33
34  my $value = &Google_Tile_Factors($zoom) ;                                      # Calculate Tile Factors
35
36  my ($latpix,$lngpix) = &Google_Coord_to_Pix( $value, $lat, $lng ) ;    # Convert coordinate to pixel location
37  
38  my $sql = qq{
39 select
40         city
41         ,country
42         ,count
43         ,point
44         ,point'($lat,$lng)' <-> point as distance
45 from geo_count
46 order by distance
47 limit 1
48  };
49
50  if ( my ($city,$country,$count,$volpnt,$i) = $dbh->selectrow_array($sql) )     # Got one
51  {
52   $volpnt =~ /\((.*),(.*)\)/ ; 
53   my $vlat = $1 ;
54   my $vlng = $2 ;
55
56   my ($vlatpix,$vlngpix) = &Google_Coord_to_Pix( $value, $vlat, $vlng ) ;       # Convert coordinate to pixel location
57
58   if ( sqrt(($vlatpix - $latpix)**2  + ($vlngpix - $lngpix)**2) > $maxpix )
59   {
60    # Click not within maxpix of point...
61    print qq!<info error="No publisher is within range of your click, please try again."/>\n! ;
62   } else                                                                # Good point found
63   {
64
65         my $sth = $dbh->prepare(qq{
66 select
67         author, title, bi.biblionumber
68 from geo_city c
69 join geo_biblioitems bi on bi.city = c.city_koha
70 join biblio b on b.biblionumber = bi.biblionumber
71 where c.city = ? and country = ?
72 order by timestamp
73 limit 100
74         });
75         $sth->execute( $city, $country );
76
77         $count = $sth->rows if $sth->rows > $count;
78
79         my $descript = "<b>$city</b> <em>$country</em> $count items\n<ol>";
80
81         while ( my $row = $sth->fetchrow_hashref ) {
82                 $descript .= sprintf qq|<li><a target="koha" href="http://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=%d">%s</a> %s\n|,
83                         $row->{biblionumber}, $row->{title}, $row->{author}
84                 ;
85         }
86
87         $descript .= "\n</ol>\n";
88
89    print qq!<info error=""  name="${city}_${country}" lat="$vlat" lng="$vlng">\n! ;
90    print qq! <description><\![CDATA[$descript]]></description>\n! ;
91    print qq!</info>\n! ;
92   }
93  }
94 } else
95 {
96  print qq!<info error="No valid point ($point) passed. Should be: (lat,lng) format."/>\n! ;
97 }
98 $dbh->disconnect ;
99
100 print "</map>\n\n" ;
101