default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / FS / FS / tower.pm
index 0d94477..8a93d8f 100644 (file)
@@ -1,8 +1,9 @@
 package FS::tower;
 package FS::tower;
+use base qw( FS::o2m_Common FS::Record );
 
 use strict;
 
 use strict;
-use base qw( FS::o2m_Common FS::Record );
-use FS::Record qw( qsearch ); #qsearchs );
+use List::Util qw( max );
+use FS::Record qw( qsearch qsearchs );
 use FS::tower_sector;
 
 =head1 NAME
 use FS::tower_sector;
 
 =head1 NAME
@@ -43,6 +44,14 @@ Tower name
 
 Disabled flag, empty or 'Y'
 
 
 Disabled flag, empty or 'Y'
 
+=item up_rate_limit
+
+Up Rate limit for towner
+
+=item down_rate_limit
+
+Down Rate limit for tower
+
 =back
 
 =head1 METHODS
 =back
 
 =head1 METHODS
@@ -58,8 +67,6 @@ points to.  You can ask the object for a copy with the I<hash> method.
 
 =cut
 
 
 =cut
 
-# the new method can be inherited from FS::Record, if a table method is defined
-
 sub table { 'tower'; }
 
 =item insert
 sub table { 'tower'; }
 
 =item insert
@@ -67,18 +74,10 @@ sub table { 'tower'; }
 Adds this record to the database.  If there is an error, returns the error,
 otherwise returns false.
 
 Adds this record to the database.  If there is an error, returns the error,
 otherwise returns false.
 
-=cut
-
-# the insert method can be inherited from FS::Record
-
 =item delete
 
 Delete this record from the database.
 
 =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,
 =item replace OLD_RECORD
 
 Replaces the OLD_RECORD with this one in the database.  If there is an error,
@@ -86,7 +85,24 @@ returns the error, otherwise returns false.
 
 =cut
 
 
 =cut
 
-# the replace method can be inherited from FS::Record
+sub replace {
+  my $self = shift;
+  my $old = shift || $self->replace_old;
+  # editing the tower location needs to regenerate coverage on its sectors
+  my $regen_coverage = 0;
+  foreach (qw(latitude longitude height)) {
+    $regen_coverage = 1 if $self->get($_) != $old->get($_);
+  }
+
+  my $error = $self->SUPER::replace($old);
+  return $error if $error;
+
+  if ($regen_coverage) {
+    foreach my $sector ($self->tower_sector) {
+      $sector->queue_generate_coverage;
+    }
+  }
+}
 
 =item check
 
 
 =item check
 
@@ -96,9 +112,6 @@ and replace methods.
 
 =cut
 
 
 =cut
 
-# the check method should currently be supplied - FS::Record contains some
-# data checking routines
-
 sub check {
   my $self = shift;
 
 sub check {
   my $self = shift;
 
@@ -106,16 +119,37 @@ sub check {
     $self->ut_numbern('towernum')
     || $self->ut_text('towername')
     || $self->ut_enum('disabled', [ '', 'Y' ])
     $self->ut_numbern('towernum')
     || $self->ut_text('towername')
     || $self->ut_enum('disabled', [ '', 'Y' ])
+    || $self->ut_coordn('latitude')
+    || $self->ut_coordn('longitude')
+    || $self->ut_enum('coord_auto', [ '', 'Y' ])
+    || $self->ut_floatn('altitude')
+    || $self->ut_floatn('height')
+    || $self->ut_floatn('veg_height')
+    || $self->ut_alphan('color')
+    || $self->ut_numbern('up_rate_limit')
+    || $self->ut_numbern('down_rate_limit')
   ;
   return $error if $error;
 
   $self->SUPER::check;
 }
 
   ;
   return $error if $error;
 
   $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
 =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
 
 
 =cut
 
@@ -123,11 +157,56 @@ sub tower_sector {
   my $self = shift;
   qsearch({
     'table'    => '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',
   });
 }
 
     '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
 =back
 
 =head1 BUGS