default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / httemplate / search / svc_broadband-json.cgi
1 <% encode_json({
2   type => 'FeatureCollection',
3   features => \@features
4 }) %>
5 <%init>
6
7 die "access denied" unless
8   $FS::CurrentUser::CurrentUser->access_right('List services');
9
10 my $conf = new FS::Conf;
11
12 my @features; # geoJSON structure
13
14 # accept all the search logic from svc_broadband.cgi...
15 my %search_hash;
16 if ( $cgi->param('magic') eq 'unlinked' ) {
17   %search_hash = ( 'unlinked' => 1 );
18 } else {
19   foreach (qw( custnum agentnum svcpart cust_fields )) {
20     $search_hash{$_} = $cgi->param($_) if $cgi->param($_);
21   }
22   foreach (qw(pkgpart routernum towernum sectornum)) {
23     $search_hash{$_} = [ $cgi->param($_) ] if $cgi->param($_);
24   }
25 }
26
27 if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
28   $search_hash{'order_by'} = "ORDER BY $1";
29 }
30
31 my $sql_query = FS::svc_broadband->search(\%search_hash);
32
33 my %routerbyblock = ();
34
35 my @rows = qsearch($sql_query);
36 my %sectors;
37 my %towers;
38 my %tower_coord;
39
40 foreach my $svc_broadband (@rows) {
41   # don't try to show it if coords aren't set
42   next if !$svc_broadband->latitude || !$svc_broadband->longitude;
43   # coerce coordinates to numbers
44   my @coord = (
45     $svc_broadband->longitude + 0,
46     $svc_broadband->latitude + 0,
47   );
48   push @coord, $svc_broadband->altitude + 0
49     if length($svc_broadband->altitude); # it's optional
50
51   my $svcnum = $svc_broadband->svcnum;
52   my $color = $svc_broadband->addr_status_color;
53
54   push @features,
55   {
56     type      => 'Feature',
57     id        => 'svc_broadband/'.$svcnum,
58     geometry  => {
59       type        => 'Point',
60       coordinates => \@coord,
61     },
62     properties => {
63       #content => include('.svc_broadband', $svc_broadband),
64       url   => $fsurl . 'view/svc_broadband-popup.html?' . $svcnum,
65       style => {
66         icon => {
67           fillColor => $color,
68         },
69       },
70     },
71   };
72   # look up tower location and draw connecting line
73   next if !$svc_broadband->sectornum;
74   my $sector = $sectors{$svc_broadband->sectornum} ||= $svc_broadband->tower_sector;
75   my $towernum = $sector->towernum;
76   my $tower = $towers{$towernum};
77
78   if (!$tower) {
79     $tower = $towers{$towernum} = $sector->tower;
80     $tower_coord{$towernum} =
81       [ $tower->longitude + 0,
82         $tower->latitude + 0,
83         ($tower->altitude || 0) + 0,
84       ];
85
86   }
87
88   if ( $tower->latitude and $tower->longitude ) {
89     push @features,
90     {
91       type => 'Feature',
92       id   => 'svc_broadband/'.$svcnum.'/line',
93       geometry => {
94         type        => 'LineString',
95         coordinates => [ \@coord, $tower_coord{$towernum} ],
96       },
97       properties  => {
98         style       => {
99           visible      => 0,
100           strokeColor  => $color,
101           strokeWeight => 2,
102         },
103       },
104     };
105
106   } # if tower has coords
107 } # foreach $svc_broadband
108 </%init>