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