From f2e15ed1a7e7b5938f32e1bd961082bed83de662 Mon Sep 17 00:00:00 2001 From: Ivan Kohler Date: Fri, 23 Mar 2012 19:47:13 -0700 Subject: [PATCH] enhance contacts: multiple email addresses, RT#16819 --- FS/FS/contact.pm | 99 ++++++++++++++++++++++++++++++++++++---- httemplate/elements/contact.html | 6 +-- 2 files changed, 91 insertions(+), 14 deletions(-) diff --git a/FS/FS/contact.pm b/FS/FS/contact.pm index 774aed088..0b50dcc98 100644 --- a/FS/FS/contact.pm +++ b/FS/FS/contact.pm @@ -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 diff --git a/httemplate/elements/contact.html b/httemplate/elements/contact.html index eea3694e3..3efa23246 100644 --- a/httemplate/elements/contact.html +++ b/httemplate/elements/contact.html @@ -21,11 +21,7 @@ % && $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); % } -- 2.11.0