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