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