2 use base qw( FS::o2m_Common FS::Record );
5 use List::Util qw( max );
6 use FS::Record qw( qsearch qsearchs );
11 FS::tower - Object methods for tower records
17 $record = new FS::tower \%hash;
18 $record = new FS::tower { 'column' => 'value' };
20 $error = $record->insert;
22 $error = $new_record->replace($old_record);
24 $error = $record->delete;
26 $error = $record->check;
30 An FS::tower object represents a tower. FS::tower inherits from
31 FS::Record. The following fields are currently supported:
45 Disabled flag, empty or 'Y'
55 Creates a new tower. To add the tower to the database, see L<"insert">.
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.
62 sub table { 'tower'; }
66 Adds this record to the database. If there is an error, returns the error,
67 otherwise returns false.
71 Delete this record from the database.
73 =item replace OLD_RECORD
75 Replaces the OLD_RECORD with this one in the database. If there is an error,
76 returns the error, otherwise returns false.
82 my $old = shift || $self->replace_old;
83 # editing the tower location needs to regenerate coverage on its sectors
84 my $regen_coverage = 0;
85 foreach (qw(latitude longitude height)) {
86 $regen_coverage = 1 if $self->get($_) != $old->get($_);
89 my $error = $self->SUPER::replace($old);
90 return $error if $error;
92 if ($regen_coverage) {
93 foreach my $sector ($self->tower_sector) {
94 $sector->queue_generate_coverage;
101 Checks all fields to make sure this is a valid tower. If there is
102 an error, returns the error, otherwise returns false. Called by the insert
111 $self->ut_numbern('towernum')
112 || $self->ut_text('towername')
113 || $self->ut_enum('disabled', [ '', 'Y' ])
114 || $self->ut_coordn('latitude')
115 || $self->ut_coordn('longitude')
116 || $self->ut_enum('coord_auto', [ '', 'Y' ])
117 || $self->ut_floatn('altitude')
118 || $self->ut_floatn('height')
119 || $self->ut_floatn('veg_height')
120 || $self->ut_alphan('color')
122 return $error if $error;
129 Returns the default sector.
135 qsearchs('tower_sector', { towernum => $self->towernum,
136 sectorname => '_default' });
141 Returns the sectors of this tower, as FS::tower_sector objects (see
142 L<FS::tower_sector>), except for the default sector.
149 'table' => 'tower_sector',
150 'hashref' => { 'towernum' => $self->towernum,
151 'sectorname' => { op => '!=', value => '_default' },
153 'order_by' => 'ORDER BY sectorname',
159 Wrapper for the default method (see L<FS::o2m_Common>) to manage the
167 my $params = +{ %{$opt{params}} };
169 # Adjust to make sure our default sector is in the list.
170 my $default_sector = $self->default_sector
171 or warn "creating default sector for tower ".$self->towernum."\n";
172 my $idx = max(0, map { $_ =~ /^sectornum(\d+)$/ ? $1 : 0 } keys(%$params));
173 $idx++; # append to the param list
174 my $prefix = "sectornum$idx";
175 # empty sectornum will create the default sector if it doesn't exist yet
176 $params->{$prefix} = $default_sector ? $default_sector->sectornum : '';
177 $params->{$prefix.'_sectorname'} = '_default';
178 $params->{$prefix.'_ip_addr'} = $params->{'default_ip_addr'} || '';
180 $self->SUPER::process_o2m(%opt);
184 # Create default sectors for any tower that doesn't have one.
185 # Shouldn't do any harm if they're missing, but just for completeness.
187 foreach my $tower (qsearch('tower',{})) {
188 next if $tower->default_sector;
189 my $sector = FS::tower_sector->new({
190 towernum => $tower->towernum,
191 sectorname => '_default',
194 my $error = $sector->insert;
195 die "error creating default sector: $error\n" if $error;
206 L<FS::tower_sector>, L<FS::svc_broadband>, L<FS::Record>