4 use base qw( FS::o2m_Common FS::Record );
5 use FS::Record qw( qsearch qsearchs );
7 use List::Util qw( max );
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.
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
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')
101 return $error if $error;
108 Returns the default sector.
114 qsearchs('tower_sector', { towernum => $self->towernum,
115 sectorname => '_default' });
120 Returns the sectors of this tower, as FS::tower_sector objects (see
121 L<FS::tower_sector>), except for the default sector.
128 'table' => 'tower_sector',
129 'hashref' => { 'towernum' => $self->towernum,
130 'sectorname' => { op => '!=', value => '_default' },
132 'order_by' => 'ORDER BY sectorname',
138 Wrapper for the default method (see L<FS::o2m_Common>) to manage the
146 my $params = $opt{params};
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'} || '';
159 $self->SUPER::process_o2m(%opt);
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.
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',
173 my $error = $sector->insert;
174 die "error creating default sector: $error\n" if $error;
185 L<FS::tower_sector>, L<FS::svc_broadband>, L<FS::Record>