self-service access for contacts, RT#25533
[freeside.git] / FS / FS / contact.pm
index 774aed0..735fe13 100644 (file)
@@ -1,13 +1,8 @@
 package FS::contact;
+use base qw( FS::Record );
 
 use strict;
-use base qw( FS::Record );
-use FS::Record qw( qsearch qsearchs dbh );
-use FS::prospect_main;
-use FS::cust_main;
-use FS::cust_location;
-use FS::contact_phone;
-use FS::contact_email;
+use FS::Record qw( qsearchs dbh ); # qw( qsearch qsearchs dbh );
 
 =head1 NAME
 
@@ -67,6 +62,16 @@ title
 
 comment
 
+=item selfservice_access
+
+empty or Y
+
+=item _password
+
+=item _password_encoding
+
+empty or bcrypt
+
 =item disabled
 
 disabled
@@ -135,16 +140,32 @@ sub insert {
   }
 
   if ( $self->get('emailaddress') =~ /\S/ ) {
-    my $contact_email = new FS::contact_email {
-      'contactnum'   => $self->contactnum,
-      'emailaddress' => $self->get('emailaddress'),
-    };
-    $error = $contact_email->insert;
+
+    foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
+      my $contact_email = new FS::contact_email {
+        'contactnum'   => $self->contactnum,
+        'emailaddress' => $email,
+      };
+      $error = $contact_email->insert;
+      if ( $error ) {
+        $dbh->rollback if $oldAutoCommit;
+        return $error;
+      }
+
+    }
+
+  }
+
+  #unless ( $import || $skip_fuzzyfiles ) {
+    #warn "  queueing fuzzyfiles update\n"
+    #  if $DEBUG > 1;
+    $error = $self->queue_fuzzyfiles_update;
     if ( $error ) {
       $dbh->rollback if $oldAutoCommit;
-      return $error;
+      return "updating fuzzy search cache: $error";
     }
-  }
+  #}
 
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
 
@@ -160,7 +181,38 @@ Delete this record from the database.
 
 # the delete method can be inherited from FS::Record
 
-# XXX delete contact_phone, contact_email
+sub delete {
+  my $self = shift;
+
+  local $SIG{HUP} = 'IGNORE';
+  local $SIG{INT} = 'IGNORE';
+  local $SIG{QUIT} = 'IGNORE';
+  local $SIG{TERM} = 'IGNORE';
+  local $SIG{TSTP} = 'IGNORE';
+  local $SIG{PIPE} = 'IGNORE';
+
+  my $oldAutoCommit = $FS::UID::AutoCommit;
+  local $FS::UID::AutoCommit = 0;
+  my $dbh = dbh;
+
+  foreach my $object ( $self->contact_phone, $self->contact_email ) {
+    my $error = $object->delete;
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return $error;
+    }
+  }
+
+  my $error = $self->SUPER::delete;
+  if ( $error ) {
+    $dbh->rollback if $oldAutoCommit;
+    return $error;
+  }
+
+  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+  '';
+
+}
 
 =item replace OLD_RECORD
 
@@ -211,6 +263,44 @@ sub replace {
     }
   }
 
+  if ( defined($self->get('emailaddress')) ) {
+
+    #ineffecient but whatever, how many email addresses can there be?
+
+    foreach my $contact_email ( $self->contact_email ) {
+      my $error = $contact_email->delete;
+      if ( $error ) {
+        $dbh->rollback if $oldAutoCommit;
+        return $error;
+      }
+    }
+
+    foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
+      my $contact_email = new FS::contact_email {
+        'contactnum'   => $self->contactnum,
+        'emailaddress' => $email,
+      };
+      $error = $contact_email->insert;
+      if ( $error ) {
+        $dbh->rollback if $oldAutoCommit;
+        return $error;
+      }
+
+    }
+
+  }
+
+  #unless ( $import || $skip_fuzzyfiles ) {
+    #warn "  queueing fuzzyfiles update\n"
+    #  if $DEBUG > 1;
+    $error = $self->queue_fuzzyfiles_update;
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return "updating fuzzy search cache: $error";
+    }
+  #}
+
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
 
   '';
@@ -240,6 +330,44 @@ sub _parse_phonestring {
   );
 }
 
+=item queue_fuzzyfiles_update
+
+Used by insert & replace to update the fuzzy search cache
+
+=cut
+
+use FS::cust_main::Search;
+sub queue_fuzzyfiles_update {
+  my $self = shift;
+
+  local $SIG{HUP} = 'IGNORE';
+  local $SIG{INT} = 'IGNORE';
+  local $SIG{QUIT} = 'IGNORE';
+  local $SIG{TERM} = 'IGNORE';
+  local $SIG{TSTP} = 'IGNORE';
+  local $SIG{PIPE} = 'IGNORE';
+
+  my $oldAutoCommit = $FS::UID::AutoCommit;
+  local $FS::UID::AutoCommit = 0;
+  my $dbh = dbh;
+
+  foreach my $field ( 'first', 'last' ) {
+    my $queue = new FS::queue { 
+      'job' => 'FS::cust_main::Search::append_fuzzyfiles_fuzzyfield'
+    };
+    my @args = "contact.$field", $self->get($field);
+    my $error = $queue->insert( @args );
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return "queueing job (transaction rolled back): $error";
+    }
+  }
+
+  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+  '';
+
+}
+
 =item check
 
 Checks all fields to make sure this is a valid example.  If there is
@@ -257,12 +385,16 @@ sub check {
   my $error = 
     $self->ut_numbern('contactnum')
     || $self->ut_foreign_keyn('prospectnum', 'prospect_main', 'prospectnum')
-    || $self->ut_foreign_keyn('custnum', 'cust_main', 'custnum')
+    || $self->ut_foreign_keyn('custnum',     'cust_main',     'custnum')
     || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
-    || $self->ut_textn('last')
-    || $self->ut_textn('first')
+    || $self->ut_foreign_keyn('classnum',    'contact_class', 'classnum')
+    || $self->ut_namen('last')
+    || $self->ut_namen('first')
     || $self->ut_textn('title')
     || $self->ut_textn('comment')
+    || $self->ut_enum('selfservice_access', [ '', 'Y' ])
+    || $self->ut_textn('_password')
+    || $self->ut_enum('_password_encoding', [ '', 'bcrypt'])
     || $self->ut_enum('disabled', [ '', 'Y' ])
   ;
   return $error if $error;
@@ -286,6 +418,12 @@ sub line {
   $data;
 }
 
+sub contact_classname {
+  my $self = shift;
+  my $contact_class = $self->contact_class or return '';
+  $contact_class->classname;
+}
+
 =back
 
 =head1 BUGS