enhance contacts: multiple email addresses, RT#16819
authorIvan Kohler <ivan@freeside.biz>
Sat, 24 Mar 2012 02:47:13 +0000 (19:47 -0700)
committerIvan Kohler <ivan@freeside.biz>
Sat, 24 Mar 2012 02:47:13 +0000 (19:47 -0700)
FS/FS/contact.pm
httemplate/elements/contact.html

index 774aed0..0b50dcc 100644 (file)
@@ -135,15 +135,21 @@ 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;
-    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;
+      }
+
     }
+
   }
 
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
@@ -160,7 +166,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 +248,34 @@ 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;
+      }
+
+    }
+
+  }
+
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
 
   '';
@@ -286,6 +351,22 @@ sub line {
   $data;
 }
 
+sub cust_location {
+  my $self = shift;
+  return '' unless $self->locationnum;
+  qsearchs('cust_location', { 'locationnum' => $self->locationnum } );
+}
+
+sub contact_phone {
+  my $self = shift;
+  qsearch('contact_phone', { 'contactnum' => $self->contactnum } );
+}
+
+sub contact_email {
+  my $self = shift;
+  qsearch('contact_email', { 'contactnum' => $self->contactnum } );
+}
+
 =back
 
 =head1 BUGS
index eea3694..3efa232 100644 (file)
 %             && $contact_phone->countrycode ne '1';
 %         }
 %       } elsif ( $field eq 'emailaddress' ) {
-%         #XXX multiple not yet supported
-%         my $contact_email = qsearchs('contact_email', {
-%           'contactnum' => $curr_value,
-%         });
-%         $value = $contact_email->emailaddress if $contact_email;
+%         $value = join(', ', map $_->emailaddress, $contact->contact_email);
 %       } else {
 %         $value = $contact->get($field);
 %       }