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