allow services with a tower but no sector to appear in search results, #33056
[freeside.git] / httemplate / elements / select-tower_sector.html
1 % if ( ! $opt{'multiple'} ) {
2 <& /elements/select-table.html,
3     table       => 'tower_sector',
4     name_col    => 'description',
5     order_by    => 'ORDER BY towernum,sectorname',
6     empty_label => ' ',
7     @_ 
8 &>
9 % }
10 % else { # currently only multiple sectors, not towers
11 <& /elements/select-table.html,
12     table       => 'tower',
13     name_col    => 'towername',
14     id          => 'towernum',
15     field       => 'towernum',
16     onchange    => 'change_towernum(this.value);',
17     element_etc => 'STYLE="vertical-align:top"',
18 &>
19 <SELECT NAME="sectornum" ID="sectornum" MULTIPLE SIZE="6"></SELECT>
20 <SCRIPT TYPE="text/javascript">
21 var sectors_of = <% encode_json(\%sectors_of) %>;
22 var select_sectornum = document.getElementById('sectornum');
23 function change_towernum(towernum) {
24   select_sectornum.options.length = 0;
25   for (var sectornum in sectors_of[towernum]) {
26     var o = document.createElement('OPTION');
27     o.value = sectornum;
28     o.text = sectors_of[towernum][sectornum];
29     o.selected = true;
30     select_sectornum.add(o, null);
31   }
32   return;
33 }
34 change_towernum('');
35 </SCRIPT>
36 <BR>
37 <INPUT TYPE="checkbox" VALUE="none" NAME="sectornum" CHECKED> <% $empty_label %>
38 % }
39 <%init>
40 my %opt = @_;
41
42 my %sectors_of;
43 my %default_of;
44 if ( $opt{'multiple'} ) {
45   foreach my $sector ( qsearch('tower_sector',{}) ) {
46     $sectors_of{$sector->towernum} ||= {};
47     $sectors_of{$sector->towernum}->{$sector->sectornum} = $sector->sectorname;
48     $sectors_of{''}->{$sector->sectornum} = $sector->description;
49     $default_of{$sector->towernum} = $sector->sectornum
50       if $sector->sectorname eq '_default';
51   }
52 }
53 foreach my $towernum (keys %sectors_of) {
54   # hide default sectors for towers that have real sectors defined
55   my $sectornum = $default_of{$towernum};
56   if ( keys %{ $sectors_of{$towernum} } > 1 ) {
57     delete $sectors_of{$towernum}->{$sectornum};
58     delete $sectors_of{''}->{$sectornum};
59   }
60   else {
61   # show default sectorname as '(all)'
62     $sectors_of{$towernum}->{$sectornum} = '(all)'
63   }
64 }
65
66 my $empty_label = $opt{'empty_label'} || 'Include services with no sector';
67 </%init>