f371ec9c72dbe3e492684952451e6f9a0e8ca082
[freeside.git] / FS / FS / tower.pm
1 package FS::tower;
2 use base qw( FS::o2m_Common FS::Record );
3
4 use strict;
5 use List::Util qw( max );
6 use FS::Record qw( qsearch qsearchs );
7 use FS::tower_sector;
8
9 =head1 NAME
10
11 FS::tower - Object methods for tower records
12
13 =head1 SYNOPSIS
14
15   use FS::tower;
16
17   $record = new FS::tower \%hash;
18   $record = new FS::tower { 'column' => 'value' };
19
20   $error = $record->insert;
21
22   $error = $new_record->replace($old_record);
23
24   $error = $record->delete;
25
26   $error = $record->check;
27
28 =head1 DESCRIPTION
29
30 An FS::tower object represents a tower.  FS::tower inherits from
31 FS::Record.  The following fields are currently supported:
32
33 =over 4
34
35 =item towernum
36
37 primary key
38
39 =item towername
40
41 Tower name
42
43 =item disabled
44
45 Disabled flag, empty or 'Y'
46
47 =back
48
49 =head1 METHODS
50
51 =over 4
52
53 =item new HASHREF
54
55 Creates a new tower.  To add the tower to the database, see L<"insert">.
56
57 Note that this stores the hash reference, not a distinct copy of the hash it
58 points to.  You can ask the object for a copy with the I<hash> method.
59
60 =cut
61
62 sub table { 'tower'; }
63
64 =item insert
65
66 Adds this record to the database.  If there is an error, returns the error,
67 otherwise returns false.
68
69 =item delete
70
71 Delete this record from the database.
72
73 =item replace OLD_RECORD
74
75 Replaces the OLD_RECORD with this one in the database.  If there is an error,
76 returns the error, otherwise returns false.
77
78 =item check
79
80 Checks all fields to make sure this is a valid tower.  If there is
81 an error, returns the error, otherwise returns false.  Called by the insert
82 and replace methods.
83
84 =cut
85
86 sub check {
87   my $self = shift;
88
89   my $error = 
90     $self->ut_numbern('towernum')
91     || $self->ut_text('towername')
92     || $self->ut_enum('disabled', [ '', 'Y' ])
93     || $self->ut_coordn('latitude')
94     || $self->ut_coordn('longitude')
95     || $self->ut_enum('coord_auto', [ '', 'Y' ])
96     || $self->ut_floatn('altitude')
97     || $self->ut_floatn('height')
98     || $self->ut_floatn('veg_height')
99     || $self->ut_alphan('color')
100   ;
101   return $error if $error;
102
103   $self->SUPER::check;
104 }
105
106 =item default_sector
107
108 Returns the default sector.
109
110 =cut
111
112 sub default_sector {
113   my $self = shift;
114   qsearchs('tower_sector', { towernum => $self->towernum,
115                              sectorname => '_default' });
116 }
117
118 =item tower_sector
119
120 Returns the sectors of this tower, as FS::tower_sector objects (see
121 L<FS::tower_sector>), except for the default sector.
122
123 =cut
124
125 sub tower_sector {
126   my $self = shift;
127   qsearch({
128     'table'    => 'tower_sector',
129     'hashref'  => { 'towernum'    => $self->towernum,
130                     'sectorname'  => { op => '!=', value => '_default' },
131                   },
132     'order_by' => 'ORDER BY sectorname',
133   });
134 }
135
136 =item process_o2m
137
138 Wrapper for the default method (see L<FS::o2m_Common>) to manage the 
139 default sector.
140
141 =cut
142
143 sub process_o2m {
144   my $self = shift;
145   my %opt = @_;
146   my $params = $opt{params};
147
148   # Adjust to make sure our default sector is in the list.
149   my $default_sector = $self->default_sector
150     or warn "creating default sector for tower ".$self->towernum."\n";
151   my $idx = max(0, map { $_ =~ /^sectornum(\d+)$/ ? $1 : 0 } keys(%$params));
152   $idx++; # append to the param list
153   my $prefix = "sectornum$idx";
154   # empty sectornum will create the default sector if it doesn't exist yet
155   $params->{$prefix} = $default_sector ? $default_sector->sectornum : '';
156   $params->{$prefix.'_sectorname'} = '_default';
157   $params->{$prefix.'_ip_addr'} = $params->{'default_ip_addr'} || '';
158
159   $self->SUPER::process_o2m(%opt);
160 }
161
162 sub _upgrade_data {
163   # Create default sectors for any tower that doesn't have one.
164   # Shouldn't do any harm if they're missing, but just for completeness.
165   my $class = shift;
166   foreach my $tower (qsearch('tower',{})) {
167     next if $tower->default_sector;
168     my $sector = FS::tower_sector->new({
169         towernum => $tower->towernum,
170         sectorname => '_default',
171         ip_addr => '',
172     });
173     my $error = $sector->insert;
174     die "error creating default sector: $error\n" if $error;
175   }
176   '';
177 }
178
179 =back
180
181 =head1 BUGS
182
183 =head1 SEE ALSO
184
185 L<FS::tower_sector>, L<FS::svc_broadband>, L<FS::Record>
186
187 =cut
188
189 1;
190