Option to ignore old CDRs, RT#81480
[freeside.git] / httemplate / search / svc_broadband-map.html
1 <& /elements/header.html, 'Broadband Search Results' &>
2   
3 <& elements/gmap.html, features => \@features, overlays => \@overlays &>
4
5 <& /elements/footer.html &>
6 <%init>
7
8 die "access denied" unless
9   $FS::CurrentUser::CurrentUser->access_right('List services');
10
11 my $conf = new FS::Conf;
12
13 $m->comp('/elements/handle_uri_query');
14
15 my @features; # geoJSON structure
16
17 # accept all the search logic from svc_broadband.cgi...
18 my %search_hash;
19 if ( $cgi->param('magic') eq 'unlinked' ) {
20   %search_hash = ( 'unlinked' => 1 );
21 } else {
22   foreach (qw( custnum agentnum svcpart cust_fields )) {
23     $search_hash{$_} = $cgi->param($_) if $cgi->param($_);
24   }
25   foreach (qw(pkgpart routernum towernum sectornum)) {
26     $search_hash{$_} = [ $cgi->param($_) ] if $cgi->param($_);
27   }
28 }
29
30 if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
31   $search_hash{'order_by'} = "ORDER BY $1";
32 }
33
34 my $sql_query = FS::svc_broadband->search(\%search_hash);
35
36 my %routerbyblock = ();
37
38 my @rows = qsearch($sql_query);
39 my %sectors;
40 my %towers;
41 my %tower_coord;
42 my %tower_bounds;
43 foreach my $svc_broadband (@rows) {
44   # don't try to show it if coords aren't set
45   next if !$svc_broadband->latitude || !$svc_broadband->longitude;
46   # coerce coordinates to numbers
47   my @coord = (
48     $svc_broadband->longitude + 0,
49     $svc_broadband->latitude + 0,
50   );
51   push @coord, $svc_broadband->altitude + 0
52     if length($svc_broadband->altitude); # it's optional
53
54   my $svcnum = $svc_broadband->svcnum;
55   my $color = $svc_broadband->addr_status_color;
56
57   push @features,
58   {
59     id        => 'svc_broadband/'.$svcnum,
60     geometry  => {
61       type        => 'Point',
62       coordinates => \@coord,
63     },
64     properties => {
65       #content => include('.svc_broadband', $svc_broadband),
66       url   => $fsurl . 'view/svc_broadband-popup.html?' . $svcnum,
67       style => {
68         icon => {
69           fillColor => $color,
70         },
71       },
72     },
73   };
74   # look up tower location and draw connecting line
75   next if !$svc_broadband->sectornum;
76   my $sector = $sectors{$svc_broadband->sectornum} ||= $svc_broadband->tower_sector;
77   my $towernum = $sector->towernum;
78   my $tower = $towers{$towernum};
79
80   if (!$tower) {
81     $tower = $towers{$towernum} = $sector->tower;
82     $tower_coord{$towernum} =
83       [ $tower->longitude + 0,
84         $tower->latitude + 0,
85         ($tower->altitude || 0) + 0,
86       ];
87
88   }
89
90   if ( $tower->latitude and $tower->longitude ) {
91     push @features,
92     {
93       geometry => {
94         type        => 'LineString',
95         coordinates => [ \@coord, $tower_coord{$towernum} ],
96       },
97       properties  => {
98         style       => {
99           strokeColor  => $color,
100           strokeWeight => 1,
101         },
102       },
103     };
104
105     # also extend tower's ROI to include this point
106     # (this is experimental; might get better results using the centroid of
107     # all connected services or something)
108     my $bounds = $tower_bounds{$towernum} ||= {
109       east => $tower->longitude,
110       west => $tower->longitude,
111       north => $tower->latitude,
112       south => $tower->latitude,
113     };
114     if ($coord[0] > $bounds->{east}) {
115       $bounds->{east} = $coord[0];
116     } elsif ($coord[0] < $bounds->{west}) {
117       $bounds->{west} = $coord[0];
118     }
119     if ($coord[1] > $bounds->{north}) {
120       $bounds->{north} = $coord[1]
121     } elsif ($coord[1] < $bounds->{south}) {
122       $bounds->{south} = $coord[1]
123     }
124
125   } # if tower has coords
126 } # foreach $svc_broadband
127
128 foreach my $tower (values(%towers)) {
129   my $towernum = $tower->towernum;
130   my $bounds = $tower_bounds{$towernum};
131   # add some padding for easier reading
132   my $dx = 0.1 * ($bounds->{east} - $bounds->{west});
133   my $dy = 0.1 * ($bounds->{north} - $bounds->{south});
134   $bounds->{east} += $dx; 
135   $bounds->{west} -= $dx;
136   $bounds->{north} += $dy;
137   $bounds->{south} -= $dy;
138   push @features,
139   {
140     id        => 'tower/'.$towernum,
141     geometry  => {
142       type        => 'Point',
143       coordinates => $tower_coord{$towernum},
144     },
145     properties => {
146       style     => {
147         icon => {
148           path        => undef,
149           url         => $fsurl.'images/antenna-square-21x51.png',
150           anchor      => { x => 10, y => 4 }
151         },
152       },
153       content   => include('.tower', $tower),
154       bounds    => $tower_bounds{$towernum},
155     },
156   };
157 }
158
159 my @overlays;
160 foreach my $sector (values %sectors) {
161   if ( length($sector->image) > 0 ) {
162     my $o = {
163       url => $fsurl.'view/sector_map-png.cgi?' . $sector->sectornum
164     };
165     foreach (qw(south north west east)) {
166       $o->{$_} = $sector->get($_) + 0;
167     }
168     push @overlays, $o;
169   };
170 };
171
172 </%init>
173 <%def .tower>
174 % my $tower = shift;
175 % my $can_edit = $FS::CurrentUser::CurrentUser->access_right('Configuration');
176 <H3>
177 % if ( $can_edit ) {
178   <a target="_blank" href="<% $fsurl %>edit/tower.html?<% $tower->towernum %>">
179 % }
180 Tower #<% $tower->towernum %> | <% $tower->towername %>
181 % if ( $can_edit ) {
182   </a>
183 % }
184 </H3>
185 </%def>