backup the schema for tables we don't need the data from. RT#85959
[freeside.git] / FS / FS / svc_mailinglist.pm
1 package FS::svc_mailinglist;
2 use base qw( FS::svc_Domain_Mixin FS::svc_Common );
3
4 use strict;
5 use Scalar::Util qw( blessed );
6 use FS::Record qw( dbh ); # qsearch qsearchs dbh );
7 use FS::mailinglist;
8
9 =head1 NAME
10
11 FS::svc_mailinglist - Object methods for svc_mailinglist records
12
13 =head1 SYNOPSIS
14
15   use FS::svc_mailinglist;
16
17   $record = new FS::svc_mailinglist \%hash;
18   $record = new FS::svc_mailinglist { '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::svc_mailinglist object represents a mailing list customer service.
31 FS::svc_mailinglist inherits from FS::Record.  The following fields are
32 currently supported:
33
34 =over 4
35
36 =item svcnum
37
38 primary key
39
40 =item username
41
42 username
43
44 =item domsvc
45
46 domsvc
47
48 =item listnum
49
50 listnum
51
52 =item reply_to_group
53
54 reply_to_group
55
56 =item remove_author
57
58 remove_author
59
60 =item reject_auto
61
62 reject_auto
63
64 =item remove_to_and_cc
65
66 remove_to_and_cc
67
68 =back
69
70 =head1 METHODS
71
72 =over 4
73
74 =item new HASHREF
75
76 Creates a new record.  To add the record to the database, see L<"insert">.
77
78 Note that this stores the hash reference, not a distinct copy of the hash it
79 points to.  You can ask the object for a copy with the I<hash> method.
80
81 =cut
82
83 # the new method can be inherited from FS::Record, if a table method is defined
84
85 sub table { 'svc_mailinglist'; }
86
87 sub table_info {
88   {
89     'name' => 'Mailing list',
90     'display_weight' => 31,
91     'cancel_weight'  => 55,
92     'fields' => {
93       'username' => { 'label' => 'List address',
94                       'disable_default'   => 1,
95                       'disable_fixed'     => 1,
96                       'disable_inventory' => 1,
97                     },
98       'domsvc' => { 'label' => 'List address domain',
99                     'disable_inventory' => 1,
100                     },
101       'domain' => 'List address domain',
102       'listnum' => { 'label' => 'List name',
103                      'disable_inventory' => 1,
104                    },
105       'listname' => 'List name', #actually mailinglist.listname
106       'reply_to' => { 'label' => 'Reply-To list',
107                       'type'  => 'checkbox',
108                       'disable_inventory' => 1,
109                       'disable_select'    => 1,
110                     },
111       'remove_from' => { 'label' => 'Remove From: from messages',
112                           'type'  => 'checkbox',
113                           'disable_inventory' => 1,
114                           'disable_select'    => 1,
115                         },
116       'reject_auto' => { 'label' => 'Reject automatic messages',
117                          'type'  => 'checkbox',
118                          'disable_inventory' => 1,
119                          'disable_select'    => 1,
120                        },
121       'remove_to_and_cc' => { 'label' => 'Remove To: and Cc: from messages',
122                               'type'  => 'checkbox',
123                               'disable_inventory' => 1,
124                               'disable_select'    => 1,
125                             },
126     },
127   };
128 }
129
130 =item insert
131
132 Adds this record to the database.  If there is an error, returns the error,
133 otherwise returns false.
134
135 =cut
136
137 sub insert {
138   my $self = shift;
139
140   local $SIG{HUP} = 'IGNORE';
141   local $SIG{INT} = 'IGNORE';
142   local $SIG{QUIT} = 'IGNORE';
143   local $SIG{TERM} = 'IGNORE';
144   local $SIG{TSTP} = 'IGNORE';
145   local $SIG{PIPE} = 'IGNORE';
146
147   my $oldAutoCommit = $FS::UID::AutoCommit;
148   local $FS::UID::AutoCommit = 0;
149   my $dbh = dbh;
150
151   my $error;
152
153   #attach to existing lists?  sound scary 
154   #unless ( $self->listnum ) {
155     my $mailinglist = new FS::mailinglist {
156       'listname' => $self->get('listname'),
157     };
158     $error = $mailinglist->insert;
159     if ( $error ) {
160       $dbh->rollback if $oldAutoCommit;
161       return $error;
162     }
163     $self->listnum($mailinglist->listnum);
164   #}
165
166   $error = $self->SUPER::insert(@_);
167   if ( $error ) {
168     $dbh->rollback if $oldAutoCommit;
169     return $error;
170   }
171
172   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
173   '';
174 }
175
176 =item delete
177
178 Delete this record from the database.
179
180 =cut
181
182 sub delete {
183   my $self = shift;
184
185   local $SIG{HUP} = 'IGNORE';
186   local $SIG{INT} = 'IGNORE';
187   local $SIG{QUIT} = 'IGNORE';
188   local $SIG{TERM} = 'IGNORE';
189   local $SIG{TSTP} = 'IGNORE';
190   local $SIG{PIPE} = 'IGNORE';
191
192   my $oldAutoCommit = $FS::UID::AutoCommit;
193   local $FS::UID::AutoCommit = 0;
194   my $dbh = dbh;
195
196   my $error = $self->mailinglist->delete || $self->SUPER::delete;
197   if ( $error ) {
198     $dbh->rollback if $oldAutoCommit;
199     return $error;
200   }
201
202   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
203   '';
204
205 }
206
207 =item replace OLD_RECORD
208
209 Replaces the OLD_RECORD with this one in the database.  If there is an error,
210 returns the error, otherwise returns false.
211
212 =cut
213
214 sub replace {
215   my $new = shift;
216
217   my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
218               ? shift
219               : $new->replace_old;
220
221   return "can't change listnum" if $old->listnum != $new->listnum; #?
222
223   my %options = @_;
224
225   local $SIG{HUP} = 'IGNORE';
226   local $SIG{INT} = 'IGNORE';
227   local $SIG{QUIT} = 'IGNORE';
228   local $SIG{TERM} = 'IGNORE';
229   local $SIG{TSTP} = 'IGNORE';
230   local $SIG{PIPE} = 'IGNORE';
231
232   my $oldAutoCommit = $FS::UID::AutoCommit;
233   local $FS::UID::AutoCommit = 0;
234   my $dbh = dbh;
235
236   if ( $new->get('listname') && $new->get('listname') ne $old->listname ) {
237     my $mailinglist = $old->mailinglist;
238     $mailinglist->listname($new->get('listname'));
239     my $error = $mailinglist->replace;
240     if ( $error ) {
241       $dbh->rollback if $oldAutoCommit;
242       return $error if $error;
243     }
244   }
245
246   my $error = $new->SUPER::replace($old, %options);
247   if ( $error ) {
248     $dbh->rollback if $oldAutoCommit;
249     return $error if $error;
250   }
251
252   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
253   ''; #no error
254   
255
256 }
257
258 =item check
259
260 Checks all fields to make sure this is a valid record.  If there is
261 an error, returns the error, otherwise returns false.  Called by the insert
262 and replace methods.
263
264 =cut
265
266 # the check method should currently be supplied - FS::Record contains some
267 # data checking routines
268
269 sub check {
270   my $self = shift;
271
272   my $error = 
273     $self->ut_numbern('svcnum')
274     || $self->ut_text('username')
275     || $self->ut_foreign_key('domsvc', 'svc_domain', 'svcnum')
276     #|| $self->ut_foreign_key('listnum', 'mailinglist', 'listnum')
277     || $self->ut_foreign_keyn('listnum', 'mailinglist', 'listnum')
278     || $self->ut_enum('reply_to_group', [ '', 'Y' ] )
279     || $self->ut_enum('remove_author', [ '', 'Y' ] )
280     || $self->ut_enum('reject_auto', [ '', 'Y' ] )
281     || $self->ut_enum('remove_to_and_cc', [ '', 'Y' ] )
282   ;
283   return $error if $error;
284
285   return "Can't remove listnum" if $self->svcnum && ! $self->listnum;
286
287   $self->SUPER::check;
288 }
289
290 =item mailinglist
291
292 =item listname
293
294 =cut
295
296 sub listname {
297   my $self = shift;
298   my $mailinglist = $self->mailinglist;
299   $mailinglist ? $mailinglist->listname : '';
300 }
301
302 =item label
303
304 =cut
305
306 sub label {
307   my $self = shift;
308   $self->listname. ' <'. $self->username. '@'. $self->domain. '>';
309 }
310
311 =back
312
313 =head1 BUGS
314
315 =head1 SEE ALSO
316
317 L<FS::Record>, schema.html from the base documentation.
318
319 =cut
320
321 1;
322