default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / FS / FS / nas.pm
index 873c9bc..c7f2459 100644 (file)
@@ -1,11 +1,10 @@
 package FS::nas;
 
 use strict;
-use vars qw( @ISA );
-use FS::Record qw();
-#use FS::Record qw( qsearch qsearchs );
-
-@ISA = qw(FS::Record);
+use base qw( FS::m2m_Common FS::Record );
+use FS::Record qw( qsearch qsearchs dbh );
+use FS::export_nas;
+use FS::part_export;
 
 =head1 NAME
 
@@ -16,11 +15,7 @@ FS::nas - Object methods for nas records
   use FS::nas;
 
   $record = new FS::nas \%hash;
-  $record = new FS::nas {
-    'nasnum'  => 1,
-    'nasip'   => '10.4.20.23',
-    'nasfqdn' => 'box1.brc.nv.us.example.net',
-  };
+  $record = new FS::nas { 'column' => 'value' };
 
   $error = $record->insert;
 
@@ -30,26 +25,32 @@ FS::nas - Object methods for nas records
 
   $error = $record->check;
 
-  $error = $record->heartbeat($timestamp);
-
 =head1 DESCRIPTION
 
-An FS::nas object represents an Network Access Server on your network, such as
-a terminal server or equivalent.  FS::nas inherits from FS::Record.  The
-following fields are currently supported:
+An FS::nas object represents a RADIUS client.  FS::nas inherits from
+FS::Record.  The following fields are currently supported:
 
 =over 4
 
 =item nasnum - primary key
 
-=item nas - NAS name
+=item nasname - "NAS name", i.e. IP address
+
+=item shortname - short descriptive name
+
+=item type - the equipment vendor
+
+=item ports
+
+=item secret - the authentication secret for this client
 
-=item nasip - NAS ip address
+=item server - virtual server name (optional)
 
-=item nasfqdn - NAS fully-qualified domain name
+=item community
 
-=item last - timestamp indicating the last instant the NAS was in a known
-             state (used by the session monitoring).
+=item description - a longer descriptive name
+
+=item svcnum - the L<FS::svc_broadband> record that 'owns' this device
 
 =back
 
@@ -75,30 +76,69 @@ sub table { 'nas'; }
 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.
+Delete this record from the database and remove all linked exports.
 
 =cut
 
-# the delete method can be inherited from FS::Record
+sub delete {
+  my $oldAutoCommit = $FS::UID::AutoCommit;
+  local $FS::UID::AutoCommit = 0;
+  my $dbh = dbh;
+
+  my $self = shift;
+  my $error = $self->process_m2m(
+    link_table    => 'export_nas',
+    target_table  => 'part_export',
+    params        => []
+  ) || $self->SUPER::delete;
+
+  if ( $error ) {
+    $dbh->rollback if $oldAutoCommit;
+    return $error;
+  }
+  
+  $dbh->commit if $oldAutoCommit;
+  '';
+}
 
 =item replace OLD_RECORD
 
 Replaces the OLD_RECORD with this one in the database.  If there is an error,
 returns the error, otherwise returns false.
 
+To change the list of linked exports, see the C<export_nas> method.
+
 =cut
 
-# the replace method can be inherited from FS::Record
+sub replace {
+  my $oldAutoCommit = $FS::UID::AutoCommit;
+  local $FS::UID::AutoCommit = 0;
+  my $dbh = dbh;
+
+  my ($self, $old) = @_;
+  $old ||= qsearchs('nas', { 'nasnum' => $self->nasnum });
+
+  my $error;
+  foreach my $part_export ( $self->part_export ) {
+    $error ||= $part_export->export_nas_replace($self, $old);
+  }
+
+  $error ||= $self->SUPER::replace($old);
+
+  if ( $error ) {
+    $dbh->rollback;
+    return $error;
+  }
+
+  $dbh->commit if $oldAutoCommit;
+  '';
+}
 
 =item check
 
-Checks all fields to make sure this is a valid example.  If there is
+Checks all fields to make sure this is a valid NAS.  If there is
 an error, returns the error, otherwise returns false.  Called by the insert
 and replace methods.
 
@@ -110,33 +150,39 @@ and replace methods.
 sub check {
   my $self = shift;
 
-  $self->ut_numbern('nasnum')
-    || $self->ut_text('nas')
-    || $self->ut_ip('nasip')
-    || $self->ut_domain('nasfqdn')
-    || $self->ut_numbern('last');
+  my $error = 
+    $self->ut_numbern('nasnum')
+    || $self->ut_text('nasname')
+    || $self->ut_textn('shortname')
+    || $self->ut_text('type')
+    || $self->ut_numbern('ports')
+    || $self->ut_text('secret')
+    || $self->ut_textn('server')
+    || $self->ut_textn('community')
+    || $self->ut_text('description')
+    || $self->ut_foreign_keyn('svcnum', 'svc_broadband', 'svcnum')
+  ;
+  return $error if $error;
+
+  $self->SUPER::check;
 }
 
-=sub heartbeat TIMESTAMP
+=item part_export
 
-Updates the timestamp for this nas
+Return all L<FS::part_export> objects to which this NAS is being exported.
 
 =cut
 
-sub heartbeat {
-  warn "warning: heartbeat unimplemented!"
+sub part_export {
+  my $self = shift;
+  map { qsearchs('part_export', { exportnum => $_->exportnum }) } 
+        qsearch('export_nas', { nasnum => $self->nasnum})
 }
 
 =back
 
-=head1 VERSION
-
-$Id: nas.pm,v 1.2 2000-11-07 15:00:37 ivan Exp $
-
 =head1 BUGS
 
-The author forgot to customize this manpage.
-
 =head1 SEE ALSO
 
 L<FS::Record>, schema.html from the base documentation.