eliminate some false laziness in FS::Misc::send_email vs. msg_template/email.pm send_...
[freeside.git] / FS / FS / mailinglistmember.pm
1 package FS::mailinglistmember;
2 use base qw(FS::Record);
3
4 use strict;
5 use Scalar::Util qw( blessed );
6 use FS::Record qw( dbh ); # qsearch qsearchs dbh );
7
8 =head1 NAME
9
10 FS::mailinglistmember - Object methods for mailinglistmember records
11
12 =head1 SYNOPSIS
13
14   use FS::mailinglistmember;
15
16   $record = new FS::mailinglistmember \%hash;
17   $record = new FS::mailinglistmember { '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::mailinglistmember object represents a mailing list member.
30 FS::mailinglistmember inherits from FS::Record.  The following fields are
31 currently supported:
32
33 =over 4
34
35 =item membernum
36
37 primary key
38
39 =item listnum
40
41 listnum
42
43 =item svcnum
44
45 svcnum
46
47 =item contactemailnum
48
49 contactemailnum
50
51 =item email
52
53 email
54
55
56 =back
57
58 =head1 METHODS
59
60 =over 4
61
62 =item new HASHREF
63
64 Creates a new mailing list member.  To add the member to the database, see
65  L<"insert">.
66
67 Note that this stores the hash reference, not a distinct copy of the hash it
68 points to.  You can ask the object for a copy with the I<hash> method.
69
70 =cut
71
72 # the new method can be inherited from FS::Record, if a table method is defined
73
74 sub table { 'mailinglistmember'; }
75
76 =item insert
77
78 Adds this record to the database.  If there is an error, returns the error,
79 otherwise returns false.
80
81 =cut
82
83 sub insert {
84   my $self = shift;
85
86   local $SIG{HUP} = 'IGNORE';
87   local $SIG{INT} = 'IGNORE';
88   local $SIG{QUIT} = 'IGNORE';
89   local $SIG{TERM} = 'IGNORE';
90   local $SIG{TSTP} = 'IGNORE';
91   local $SIG{PIPE} = 'IGNORE';
92
93   my $oldAutoCommit = $FS::UID::AutoCommit;
94   local $FS::UID::AutoCommit = 0;
95   my $dbh = dbh;
96
97   my $error =    $self->SUPER::insert
98               || $self->export('mailinglistmember_insert');
99   if ( $error ) {
100     $dbh->rollback if $oldAutoCommit;
101     return $error;
102   }
103
104   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
105   '';
106 }
107
108 =item delete
109
110 Delete this record from the database.
111
112 =cut
113
114 sub delete {
115   my $self = shift;
116
117   local $SIG{HUP} = 'IGNORE';
118   local $SIG{INT} = 'IGNORE';
119   local $SIG{QUIT} = 'IGNORE';
120   local $SIG{TERM} = 'IGNORE';
121   local $SIG{TSTP} = 'IGNORE';
122   local $SIG{PIPE} = 'IGNORE';
123
124   my $oldAutoCommit = $FS::UID::AutoCommit;
125   local $FS::UID::AutoCommit = 0;
126   my $dbh = dbh;
127
128   my $error =    $self->SUPER::delete
129               || $self->export('mailinglistmember_delete');
130   if ( $error ) {
131     $dbh->rollback if $oldAutoCommit;
132     return $error;
133   }
134
135   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
136   '';
137 }
138
139 =item replace OLD_RECORD
140
141 Replaces the OLD_RECORD with this one in the database.  If there is an error,
142 returns the error, otherwise returns false.
143
144 =cut
145
146 sub replace {
147   my $new = shift;
148
149   my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
150               ? shift
151               : $new->replace_old;
152
153   local $SIG{HUP} = 'IGNORE';
154   local $SIG{INT} = 'IGNORE';
155   local $SIG{QUIT} = 'IGNORE';
156   local $SIG{TERM} = 'IGNORE';
157   local $SIG{TSTP} = 'IGNORE';
158   local $SIG{PIPE} = 'IGNORE';
159
160   my $oldAutoCommit = $FS::UID::AutoCommit;
161   local $FS::UID::AutoCommit = 0;
162   my $dbh = dbh;
163
164   my $error =    $new->SUPER::replace($old)
165               || $new->export('mailinglistmember_replace', $old);
166   if ( $error ) {
167     $dbh->rollback if $oldAutoCommit;
168     return $error;
169   }
170
171   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
172   '';
173 }
174
175 =item check
176
177 Checks all fields to make sure this is a valid member.  If there is
178 an error, returns the error, otherwise returns false.  Called by the insert
179 and replace methods.
180
181 =cut
182
183 # the check method should currently be supplied - FS::Record contains some
184 # data checking routines
185
186 sub check {
187   my $self = shift;
188
189   my $error = 
190     $self->ut_numbern('membernum')
191     || $self->ut_foreign_key('listnum', 'mailinglist', 'listnum')
192     || $self->ut_foreign_keyn('svcnum', 'svc_acct', 'svcnum')
193     || $self->ut_foreign_keyn('contactemailnum', 'contact_email', 'contactemailnum')
194     || $self->ut_textn('email') #XXX ut_email! from svc_forward, cust_main_invoice
195   ;
196   return $error if $error;
197
198   $self->SUPER::check;
199 }
200
201 =item mailinglist
202
203 =item email_address
204
205 =cut
206
207 sub email_address {
208   my $self = shift;
209   #XXX svcnum, contactemailnum
210   $self->email;
211 }
212
213 =item export
214
215 =cut
216
217 sub export {
218   my( $self, $method ) = ( shift, shift );
219   my $svc_mailinglist = $self->mailinglist->svc_mailinglist
220     or return '';
221   $svc_mailinglist->export($method, $self, @_);
222 }
223
224 =back
225
226 =head1 BUGS
227
228 =head1 SEE ALSO
229
230 L<FS::Record>, schema.html from the base documentation.
231
232 =cut
233
234 1;
235