diff options
author | mark <mark> | 2012-02-10 01:58:08 +0000 |
---|---|---|
committer | mark <mark> | 2012-02-10 01:58:08 +0000 |
commit | c81adbf7bbaa537b650f39bb76ce0b7b8c069450 (patch) | |
tree | cf717f829697f1827b555922734e50d5e5c3ccb8 /FS | |
parent | 8e4c66cfb8aaf0aee2d1142636e53d0e17d11d4f (diff) |
allow towers without sectors, #16398 continued
Diffstat (limited to 'FS')
-rw-r--r-- | FS/FS/Record.pm | 6 | ||||
-rw-r--r-- | FS/FS/Upgrade.pm | 2 | ||||
-rw-r--r-- | FS/FS/tower.pm | 68 | ||||
-rw-r--r-- | FS/FS/tower_sector.pm | 7 |
4 files changed, 73 insertions, 10 deletions
diff --git a/FS/FS/Record.pm b/FS/FS/Record.pm index e4bc6c562..dfc2abfc4 100644 --- a/FS/FS/Record.pm +++ b/FS/FS/Record.pm @@ -900,10 +900,12 @@ sub insert { my $error = $self->check; return $error if $error; - #single-field unique keys are given a value if false + #single-field non-null unique keys are given a value if empty #(like MySQL's AUTO_INCREMENT or Pg SERIAL) foreach ( $self->dbdef_table->unique_singles) { - $self->unique($_) unless $self->getfield($_); + next if $self->getfield($_); + next if $self->dbdef_table->column($_)->null eq 'NULL'; + $self->unique($_); } #and also the primary key, if the database isn't going to diff --git a/FS/FS/Upgrade.pm b/FS/FS/Upgrade.pm index cf6158031..d00bb5cd0 100644 --- a/FS/FS/Upgrade.pm +++ b/FS/FS/Upgrade.pm @@ -263,6 +263,8 @@ sub upgrade_data { 'part_svc' => [], 'part_export' => [], + #insert default tower_sector if not present + 'tower', ; \%hash; diff --git a/FS/FS/tower.pm b/FS/FS/tower.pm index 0d9447729..227e3eb66 100644 --- a/FS/FS/tower.pm +++ b/FS/FS/tower.pm @@ -2,8 +2,9 @@ package FS::tower; use strict; use base qw( FS::o2m_Common FS::Record ); -use FS::Record qw( qsearch ); #qsearchs ); +use FS::Record qw( qsearch qsearchs ); use FS::tower_sector; +use List::Util qw( max ); =head1 NAME @@ -69,16 +70,12 @@ otherwise returns false. =cut -# the insert method can be inherited from FS::Record - =item delete Delete this record from the database. =cut -# the delete method can be inherited from FS::Record - =item replace OLD_RECORD Replaces the OLD_RECORD with this one in the database. If there is an error, @@ -112,10 +109,22 @@ sub check { $self->SUPER::check; } +=item default_sector + +Returns the default sector. + +=cut + +sub default_sector { + my $self = shift; + qsearchs('tower_sector', { towernum => $self->towernum, + sectorname => '_default' }); +} + =item tower_sector Returns the sectors of this tower, as FS::tower_sector objects (see -L<FS::tower_sector>). +L<FS::tower_sector>), except for the default sector. =cut @@ -123,11 +132,56 @@ sub tower_sector { my $self = shift; qsearch({ 'table' => 'tower_sector', - 'hashref' => { 'towernum' => $self->towernum }, + 'hashref' => { 'towernum' => $self->towernum, + 'sectorname' => { op => '!=', value => '_default' }, + }, 'order_by' => 'ORDER BY sectorname', }); } +=item process_o2m + +Wrapper for the default method (see L<FS::o2m_Common>) to manage the +default sector. + +=cut + +sub process_o2m { + my $self = shift; + my %opt = @_; + my $params = $opt{params}; + + # Adjust to make sure our default sector is in the list. + my $default_sector = $self->default_sector + or warn "creating default sector for tower ".$self->towernum."\n"; + my $idx = max(0, map { $_ =~ /^sectornum(\d+)$/ ? $1 : 0 } keys(%$params)); + $idx++; # append to the param list + my $prefix = "sectornum$idx"; + # empty sectornum will create the default sector if it doesn't exist yet + $params->{$prefix} = $default_sector ? $default_sector->sectornum : ''; + $params->{$prefix.'_sectorname'} = '_default'; + $params->{$prefix.'_ip_addr'} = $params->{'default_ip_addr'} || ''; + + $self->SUPER::process_o2m(%opt); +} + +sub _upgrade_data { + # Create default sectors for any tower that doesn't have one. + # Shouldn't do any harm if they're missing, but just for completeness. + my $class = shift; + foreach my $tower (qsearch('tower',{})) { + next if $tower->default_sector; + my $sector = FS::tower_sector->new({ + towernum => $tower->towernum, + sectorname => '_default', + ip_addr => '', + }); + my $error = $sector->insert; + die "error creating default sector: $error\n" if $error; + } + ''; +} + =back =head1 BUGS diff --git a/FS/FS/tower_sector.pm b/FS/FS/tower_sector.pm index 583b93301..80e7f51b2 100644 --- a/FS/FS/tower_sector.pm +++ b/FS/FS/tower_sector.pm @@ -137,7 +137,12 @@ Returns a description for this sector including tower name. sub description { my $self = shift; - $self->tower->towername. ' sector '. $self->sectorname; + if ( $self->sectorname eq '_default' ) { + $self->tower->towername + } + else { + $self->tower->towername. ' sector '. $self->sectorname + } } =back |