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