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 # the new method can be inherited from FS::Record, if a table method is defined
64 sub table { 'tower'; }
68 Adds this record to the database. If there is an error, returns the error,
69 otherwise returns false.
75 Delete this record from the database.
79 =item replace OLD_RECORD
81 Replaces the OLD_RECORD with this one in the database. If there is an error,
82 returns the error, otherwise returns false.
86 # the replace method can be inherited from FS::Record
90 Checks all fields to make sure this is a valid tower. If there is
91 an error, returns the error, otherwise returns false. Called by the insert
96 # the check method should currently be supplied - FS::Record contains some
97 # data checking routines
103 $self->ut_numbern('towernum')
104 || $self->ut_text('towername')
105 || $self->ut_enum('disabled', [ '', 'Y' ])
107 return $error if $error;
114 Returns the default sector.
120 qsearchs('tower_sector', { towernum => $self->towernum,
121 sectorname => '_default' });
126 Returns the sectors of this tower, as FS::tower_sector objects (see
127 L<FS::tower_sector>), except for the default sector.
134 'table' => 'tower_sector',
135 'hashref' => { 'towernum' => $self->towernum,
136 'sectorname' => { op => '!=', value => '_default' },
138 'order_by' => 'ORDER BY sectorname',
144 Wrapper for the default method (see L<FS::o2m_Common>) to manage the
152 my $params = $opt{params};
154 # Adjust to make sure our default sector is in the list.
155 my $default_sector = $self->default_sector
156 or warn "creating default sector for tower ".$self->towernum."\n";
157 my $idx = max(0, map { $_ =~ /^sectornum(\d+)$/ ? $1 : 0 } keys(%$params));
158 $idx++; # append to the param list
159 my $prefix = "sectornum$idx";
160 # empty sectornum will create the default sector if it doesn't exist yet
161 $params->{$prefix} = $default_sector ? $default_sector->sectornum : '';
162 $params->{$prefix.'_sectorname'} = '_default';
163 $params->{$prefix.'_ip_addr'} = $params->{'default_ip_addr'} || '';
165 $self->SUPER::process_o2m(%opt);
169 # Create default sectors for any tower that doesn't have one.
170 # Shouldn't do any harm if they're missing, but just for completeness.
172 foreach my $tower (qsearch('tower',{})) {
173 next if $tower->default_sector;
174 my $sector = FS::tower_sector->new({
175 towernum => $tower->towernum,
176 sectorname => '_default',
179 my $error = $sector->insert;
180 die "error creating default sector: $error\n" if $error;
191 L<FS::tower_sector>, L<FS::svc_broadband>, L<FS::Record>