Merge remote-tracking branch 'upstream/master'
[freeside.git] / FS / FS / contact_email.pm
1 package FS::contact_email;
2 use base qw( FS::Record );
3
4 use strict;
5 use FS::Record qw( qsearch qsearchs );
6 use FS::contact;
7
8 =head1 NAME
9
10 FS::contact_email - Object methods for contact_email records
11
12 =head1 SYNOPSIS
13
14   use FS::contact_email;
15
16   $record = new FS::contact_email \%hash;
17   $record = new FS::contact_email { 'column' => 'value' };
18
19   $error = $record->insert;
20
21   $error = $new_record->replace($old_record);
22
23   $error = $record->delete;
24
25   $error = $record->check;
26
27 =head1 DESCRIPTION
28
29 An FS::contact_email object represents a contact's email address.
30 FS::contact_email inherits from FS::Record.  The following fields are currently
31 supported:
32
33 =over 4
34
35 =item contactemailnum
36
37 primary key
38
39 =item contactnum
40
41 contactnum
42
43 =item emailaddress
44
45 emailaddress
46
47
48 =back
49
50 =head1 METHODS
51
52 =over 4
53
54 =item new HASHREF
55
56 Creates a new contact email address.  To add the email address to the database,
57 see L<"insert">.
58
59 Note that this stores the hash reference, not a distinct copy of the hash it
60 points to.  You can ask the object for a copy with the I<hash> method.
61
62 =cut
63
64 sub table { 'contact_email'; }
65
66 =item insert
67
68 Adds this record to the database.  If there is an error, returns the error,
69 otherwise returns false.
70
71 =item delete
72
73 Delete this record from the database.
74
75 =item replace OLD_RECORD
76
77 Replaces the OLD_RECORD with this one in the database.  If there is an error,
78 returns the error, otherwise returns false.
79
80 =item check
81
82 Checks all fields to make sure this is a valid email address.  If there is
83 an error, returns the error, otherwise returns false.  Called by the insert
84 and replace methods.
85
86 =cut
87
88 sub check {
89   my $self = shift;
90
91   my $error = 
92     $self->ut_numbern('contactemailnum')
93     || $self->ut_number('contactnum')
94   ;
95   return $error if $error;
96
97   #technically \w and also ! # $ % & ' * + - / = ? ^ _ ` { | } ~
98   # and even more technically need to deal with i18n addreesses soon
99   #  (maybe the UI can convert them for us ala punycode.js)
100   # but for now in practice have not encountered anything outside \w . - & + '
101   #  and even & and ' are super rare and probably have scarier "pass to shell"
102   #   implications than worth being pedantic about accepting
103   #    (we always String::ShellQuote quote them, but once passed...)
104   #                              SO: \w . - +
105   if ( $self->emailaddress =~ /^\s*([\w\.\-\+]+)\@(([\w\.\-]+\.)+\w+)\s*$/ ) {
106     my($user, $domain) = ($1, $2);
107     $self->emailaddress("$1\@$2");
108   } else {
109     return gettext("illegal_email_invoice_address"). ': '. $self->emailaddress;
110   }
111
112   $self->SUPER::check;
113 }
114
115 sub contact {
116   my $self = shift;
117   qsearchs( 'contact', { 'contactnum' => $self->contactnum } );
118 }
119
120 =back
121
122 =head1 BUGS
123
124 =head1 SEE ALSO
125
126 L<FS::contact>, L<FS::Record>
127
128 =cut
129
130 1;
131