summaryrefslogtreecommitdiff
path: root/httemplate/search/svc_broadband-map.html
blob: 95a31d3524272f6de0532d0e01fc41348637e33e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<& /elements/header.html, 'Broadband Search Results' &>
  
<& elements/gmap.html, features => \@features, overlays => \@overlays &>

<& /elements/footer.html &>
<%init>

die "access denied" unless
  $FS::CurrentUser::CurrentUser->access_right('List services');

my $conf = new FS::Conf;

my @features; # geoJSON structure

# accept all the search logic from svc_broadband.cgi...
my %search_hash;
if ( $cgi->param('magic') eq 'unlinked' ) {
  %search_hash = ( 'unlinked' => 1 );
} else {
  foreach (qw( custnum agentnum svcpart cust_fields )) {
    $search_hash{$_} = $cgi->param($_) if $cgi->param($_);
  }
  foreach (qw(pkgpart routernum towernum sectornum)) {
    $search_hash{$_} = [ $cgi->param($_) ] if $cgi->param($_);
  }
}

if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
  $search_hash{'order_by'} = "ORDER BY $1";
}

my $sql_query = FS::svc_broadband->search(\%search_hash);

my %routerbyblock = ();

my @rows = qsearch($sql_query);
my %sectors;
my %towers;
my %tower_coord;
my %tower_bounds;
foreach my $svc_broadband (@rows) {
  # don't try to show it if coords aren't set
  next if !$svc_broadband->latitude || !$svc_broadband->longitude;
  # coerce coordinates to numbers
  my @coord = (
    $svc_broadband->longitude + 0,
    $svc_broadband->latitude + 0,
  );
  push @coord, $svc_broadband->altitude + 0
    if length($svc_broadband->altitude); # it's optional

  push @features,
  {
    id        => 'svc_broadband/'.$svc_broadband->svcnum,
    geometry  => {
      type        => 'Point',
      coordinates => \@coord,
    },
    properties => {
      content => include('.svc_broadband', $svc_broadband),
    },
  };
  # look up tower location and draw connecting line
  next if !$svc_broadband->sectornum;
  my $sector = $sectors{$svc_broadband->sectornum} ||= $svc_broadband->tower_sector;
  my $towernum = $sector->towernum;
  my $tower = $towers{$towernum};

  if (!$tower) {
    $tower = $towers{$towernum} = $sector->tower;
    $tower_coord{$towernum} =
      [ $tower->longitude + 0,
        $tower->latitude + 0,
        ($tower->altitude || 0) + 0,
      ];

  }

  if ( $tower->latitude and $tower->longitude ) {
    push @features,
    {
      geometry => {
        type        => 'LineString',
        coordinates => [ \@coord, $tower_coord{$towernum} ],
      },
      properties  => {
        style       => {
          strokeColor  => ($tower->color || 'green'),
          strokeWeight => 2,
        },
      },
    };

    # also extend tower's ROI to include this point
    # (this is experimental; might get better results using the centroid of
    # all connected services or something)
    my $bounds = $tower_bounds{$towernum} ||= {
      east => $tower->longitude,
      west => $tower->longitude,
      north => $tower->latitude,
      south => $tower->latitude,
    };
    if ($coord[0] > $bounds->{east}) {
      $bounds->{east} = $coord[0];
    } elsif ($coord[0] < $bounds->{west}) {
      $bounds->{west} = $coord[0];
    }
    if ($coord[1] > $bounds->{north}) {
      $bounds->{north} = $coord[1]
    } elsif ($coord[1] < $bounds->{south}) {
      $bounds->{south} = $coord[1]
    }

  } # if tower has coords
} # foreach $svc_broadband

# if we were given a towernum or sectornum, ensure that the map includes
# that tower/sector, even if there are no services

foreach my $towernum ($cgi->param('towernum')) {
  next if $towernum !~ /^\d+$/;
  next if exists($towers{$towernum});
  $towers{$towernum} = FS::tower->by_key($towernum);
}

foreach my $sectornum ($cgi->param('sectornum')) {
  next if $sectornum !~ /^\d+$/;
  next if exists($sectors{$sectornum});
  $sectors{$sectornum} = FS::tower_sector->by_key($sectornum)
    or die "bad sectornum $sectornum";
  # and put it on the tower list also
  my $towernum = $sectors{$sectornum}->towernum;
  if (!exists($towers{$towernum})) {
    $towers{$towernum} = FS::tower->by_key($towernum);
  }
}

# if the tower/sector was included in the search, but has no services, set
# default bounds around it of 1 minute in each direction
my $default_bounds = 0.017;

foreach my $tower (values(%towers)) {
  my $towernum = $tower->towernum;
  $tower_coord{$towernum} ||= [ $tower->longitude + 0,
                                $tower->latitude + 0,
                                ($tower->altitude || 0) + 0
                              ];
warn encode_json($tower_coord{$towernum});
  my $bounds = $tower_bounds{$towernum}
           ||= { 'north' => $tower->latitude + $default_bounds,
                 'south' => $tower->latitude - $default_bounds,
                 'east'  => $tower->longitude + $default_bounds,
                 'west'  => $tower->longitude - $default_bounds,
               };
  # add some padding for easier reading
  my $dx = 0.1 * ($bounds->{east} - $bounds->{west});
  my $dy = 0.1 * ($bounds->{north} - $bounds->{south});
  $bounds->{east} += $dx; 
  $bounds->{west} -= $dx;
  $bounds->{north} += $dy;
  $bounds->{south} -= $dy;
  push @features,
  {
    id        => 'tower/'.$towernum,
    geometry  => {
      type        => 'Point',
      coordinates => $tower_coord{$towernum},
    },
    properties => {
      style     => {
        icon => {
          path        => undef,
          url         => $fsurl.'images/antenna-square-21x51.png',
          anchor      => { x => 10, y => 4 }
        },
      },
      content   => include('.tower', $tower),
      bounds    => $tower_bounds{$towernum},
    },
  };
}

my @overlays;
foreach my $sector (values %sectors) {
  if ( length($sector->image) > 0 ) {
    my $o = {
      url => $fsurl.'view/sector_map-png.cgi?' . $sector->sectornum
    };
    foreach (qw(south north west east)) {
      $o->{$_} = $sector->get($_) + 0;
    }
    push @overlays, $o;
  };
};
</%init>
<%def .svc_broadband>
% my $svc = shift;
% my @label = $svc->cust_svc->label;
<H3>
  <a target="_blank" href="<% $fsurl %>view/svc_broadband.cgi?<% $svc->svcnum %>">
    <% $label[0] |h %> #<% $svc->svcnum %> | <% $label[1] %>
  </a>
</H3>
% my $cust_main = $svc->cust_main;
<a target="_blank" href="<% $fsurl %>view/cust_main.cgi?<% $cust_main->custnum %>">
<& /elements/small_custview.html, {
  cust_main => $svc->cust_main,
  #url => $fsurl.'view/cust_main.cgi',
} &>
</a>
</%def>
<%def .tower>
% my $tower = shift;
% my $can_edit = $FS::CurrentUser::CurrentUser->access_right('Configuration');
<H3>
% if ( $can_edit ) {
  <a target="_blank" href="<% $fsurl %>edit/tower.html?<% $tower->towernum %>">
% }
Tower #<% $tower->towernum %> | <% $tower->towername %>
% if ( $can_edit ) {
  </a>
% }
</H3>
</%def>